### Question:
SetVariablesRequestHandler implements OcppRequestHandler<SetVariablesRequest> { @Override public void handle(String callId, SetVariablesRequest request) { List<SetVariableDatum> setVariableData = request.getSetVariableData(); List<SetVariableValidationResult> results = validate(setVariableData); List<SetVariableResult> setVariableResults = results.stream() .map(SetVariableValidationResult::getResult) .collect(toList()); sendResponse(callId, new SetVariablesResponse().withSetVariableResult(setVariableResults)); results.stream() .filter(SetVariableValidationResult::isAccepted) .map(SetVariableValidationResult::getSetVariableDatum) .forEach(data -> { CiString.CiString50 componentName = data.getComponent().getName(); Optional<StationComponent> optionalComponent = stationComponentsHolder.getComponent(data.getComponent().getName()); StationComponent component = optionalComponent.orElseThrow(() -> new UnknownComponentException(componentName.toString())); component.setVariable(data); }); } SetVariablesRequestHandler(StationComponentsHolder stationComponentsHolder, StationMessageSender stationMessageSender); @Override void handle(String callId, SetVariablesRequest request);  }### Answer:
@Test void shouldHandleVariable() throws JsonProcessingException { initOCPPCommCtrlComponentMock(SetVariableResult.AttributeStatus.ACCEPTED); SetVariablesRequest setVariablesRequest = createSetVariablesRequest() .withComponent(OCPPCommCtrlrComponent.NAME) .withVariable(HeartbeatIntervalVariableAccessor.NAME) .withAttributeValue(String.valueOf(DEFAULT_HEARTBEAT_INTERVAL)) .build(); setVariablesRequestHandler.handle(DEFAULT_MESSAGE_ID, setVariablesRequest); verify(stationMessageSender).sendMessage(messageCaptor.capture()); SetVariablesResponse setVariablesResponse = createSetVariablesResponse() .withComponent(OCPPCommCtrlrComponent.NAME) .withVariable(HeartbeatIntervalVariableAccessor.NAME) .withAttributeStatus(SetVariableResult.AttributeStatus.ACCEPTED) .build(); String expectedCallResultJson = createCallResult() .withMessageId(DEFAULT_MESSAGE_ID) .withPayload(setVariablesResponse) .toJson(); assertThat(messageCaptor.getValue().getData().get()).isEqualTo(expectedCallResultJson); }
@Test void shouldFailIfComponentNotExists() throws Exception { given(componentsHolder.getComponent(new CiString.CiString50(UNKNOWN_COMPONENT_NAME))).willReturn(Optional.empty()); SetVariablesRequest setVariablesRequest = createSetVariablesRequest() .withComponent(UNKNOWN_COMPONENT_NAME) .withVariable(HeartbeatIntervalVariableAccessor.NAME) .build(); setVariablesRequestHandler.handle(DEFAULT_MESSAGE_ID, setVariablesRequest); verify(stationMessageSender).sendMessage(messageCaptor.capture()); SetVariablesResponse setVariablesResponse = createSetVariablesResponse() .withComponent(UNKNOWN_COMPONENT_NAME) .withVariable(HeartbeatIntervalVariableAccessor.NAME) .withAttributeStatus(SetVariableResult.AttributeStatus.UNKNOWN_COMPONENT) .build(); String expectedCallResultJson = createCallResult() .withMessageId(DEFAULT_MESSAGE_ID) .withPayload(setVariablesResponse) .toJson(); assertThat(messageCaptor.getValue().getData().get()).isEqualTo(expectedCallResultJson); }
@Test void shouldFailIfValidationFailed() throws Exception { initOCPPCommCtrlComponentMock(SetVariableResult.AttributeStatus.REJECTED); SetVariablesRequest setVariablesRequest = createSetVariablesRequest() .withComponent(OCPPCommCtrlrComponent.NAME) .withVariable(HeartbeatIntervalVariableAccessor.NAME) .build(); setVariablesRequestHandler.handle(DEFAULT_MESSAGE_ID, setVariablesRequest); verify(stationMessageSender).sendMessage(messageCaptor.capture()); SetVariablesResponse setVariablesResponse = createSetVariablesResponse() .withComponent(OCPPCommCtrlrComponent.NAME) .withVariable(HeartbeatIntervalVariableAccessor.NAME) .withAttributeStatus(SetVariableResult.AttributeStatus.REJECTED) .build(); String expectedCallResultJson = createCallResult() .withMessageId(DEFAULT_MESSAGE_ID) .withPayload(setVariablesResponse) .toJson(); assertThat(messageCaptor.getValue().getData().get()).isEqualTo(expectedCallResultJson); }