TaskVariablesPayloadValidator { public UpdateTaskVariablePayload handleUpdateTaskVariablePayload(UpdateTaskVariablePayload updateTaskVariablePayload) { checkNotValidCharactersInVariableName(updateTaskVariablePayload.getName(), "You cannot update a variable with not a valid name: "); Object value = updateTaskVariablePayload.getValue(); if (value instanceof String) { handleAsDate((String) value).ifPresent(updateTaskVariablePayload::setValue); } return updateTaskVariablePayload; } 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_stringRepresentsADateInUpdateTaskPayload() { UpdateTaskVariablePayload updateTaskVariablePayload = TaskPayloadBuilder.updateVariable().withVariable("date", "1970-01-01").build(); UpdateTaskVariablePayload handledDatePayload = taskVariablesPayloadValidator.handleUpdateTaskVariablePayload(updateTaskVariablePayload); assertThat(handledDatePayload.getValue()).isEqualTo(new Date(0)); }
@Test public void should_convertStringToDate_when_stringRepresentsADateWithTimeInUpdateTaskPayload() { UpdateTaskVariablePayload updateTaskVariablePayload = TaskPayloadBuilder .updateVariable() .withVariable("date", "1970-01-01T01:01:01.001Z") .build(); long time = Duration.ofHours(1).toMillis() + Duration.ofMinutes(1).toMillis() + Duration.ofSeconds(1).toMillis() + 1; UpdateTaskVariablePayload handledDatePayload = taskVariablesPayloadValidator.handleUpdateTaskVariablePayload(updateTaskVariablePayload); assertThat(handledDatePayload.getValue()).isEqualTo(new Date(time)); }
@Test public void should_doNothing_when_stringIsNotADateInUpdateTaskPayload() { UpdateTaskVariablePayload updateTaskVariablePayload = TaskPayloadBuilder .updateVariable() .withVariable("notADate", "this is not a date") .build(); UpdateTaskVariablePayload handledDatePayload = taskVariablesPayloadValidator.handleUpdateTaskVariablePayload(updateTaskVariablePayload); assertThat(handledDatePayload.getValue()).isEqualTo("this is not a date"); }
@Test public void should_doNothing_when_itIsNotAStringInUpdateTaskPayload() { UpdateTaskVariablePayload updateTaskVariablePayload = TaskPayloadBuilder .updateVariable() .withVariable("notAString", true) .build(); UpdateTaskVariablePayload handledDatePayload = taskVariablesPayloadValidator.handleUpdateTaskVariablePayload(updateTaskVariablePayload); assertThat(handledDatePayload.getValue()).isEqualTo(true); }