TaskVariablesPayloadValidator { public CreateTaskVariablePayload handleCreateTaskVariablePayload(CreateTaskVariablePayload createTaskVariablePayload) { checkNotValidCharactersInVariableName(createTaskVariablePayload.getName(), "Variable has not a valid name: "); Object value = createTaskVariablePayload.getValue(); if (value instanceof String) { handleAsDate((String)value).ifPresent(createTaskVariablePayload::setValue); } return createTaskVariablePayload; } TaskVariablesPayloadValidator(DateFormatterProvider dateFormatterProvider,
                                         VariableNameValidator variableNameValidator); CreateTaskVariablePayload handleCreateTaskVariablePayload(CreateTaskVariablePayload createTaskVariablePayload); UpdateTaskVariablePayload handleUpdateTaskVariablePayload(UpdateTaskVariablePayload updateTaskVariablePayload); Map<String, Object> handlePayloadVariables(Map<String, Object> variables);  }
@Test public void should_convertStringToDate_when_stringRepresentsADateInCreateTaskVariablePayload() { CreateTaskVariablePayload payload = TaskPayloadBuilder.createVariable() .withVariable("date", "1970-01-01") .build(); CreateTaskVariablePayload handledVariablePayload = taskVariablesPayloadValidator.handleCreateTaskVariablePayload(payload); assertThat(handledVariablePayload.getValue()).isEqualTo(new Date(0)); }
@Test public void should_convertStringToDate_when_stringRepresentsADateWithTimeInCreateTaskVariablePayload() { CreateTaskVariablePayload payload = TaskPayloadBuilder.createVariable() .withVariable("date", "1970-01-01T01:01:01.001Z") .build(); long time = Duration.ofHours(1).toMillis() + Duration.ofMinutes(1).toMillis() + Duration.ofSeconds(1).toMillis() + 1; CreateTaskVariablePayload handledVariablePayload = taskVariablesPayloadValidator.handleCreateTaskVariablePayload(payload); assertThat(handledVariablePayload.getValue()).isEqualTo(new Date(time)); }
@Test public void should_doNothing_when_stringIsNotADateInCreateTaskVariablePayload() { CreateTaskVariablePayload payload = TaskPayloadBuilder.createVariable() .withVariable("notADate", "this is not a date") .build(); CreateTaskVariablePayload handledVariablePayload = taskVariablesPayloadValidator.handleCreateTaskVariablePayload(payload); assertThat(handledVariablePayload.getValue()).isEqualTo("this is not a date"); }
@Test public void should_doNothing_when_itIsNotAStringInCreateTaskVariablePayload() { CreateTaskVariablePayload payload = TaskPayloadBuilder.createVariable() .withVariable("notAString", 10) .build(); CreateTaskVariablePayload handledVariablePayload = taskVariablesPayloadValidator.handleCreateTaskVariablePayload(payload); assertThat(handledVariablePayload.getValue()).isEqualTo(10); }
@Test public void should_returnErrorList_when_setVariableWithWrongCharactersInName() throws Exception { CreateTaskVariablePayload payload = TaskPayloadBuilder.createVariable() .withVariable("wrong-name", 10) .build(); String expectedTypeErrorMessage = "wrong-name"; Throwable throwable = catchThrowable(() -> taskVariablesPayloadValidator.handleCreateTaskVariablePayload(payload)); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(expectedTypeErrorMessage); }