Skip to content

Commit

Permalink
AAE-21057 Support Java8 dates in DateFormatterProvider (#4630)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni007 committed Apr 8, 2024
1 parent 47053bb commit d3338fe
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public Date toDate(Object value) {
return new Date((long)value);
}

if (value instanceof LocalDate) {
return Date.from(((LocalDate)value).atStartOfDay(getZoneId()).toInstant());
}

if (value instanceof LocalDateTime) {
return Date.from(((LocalDateTime)value).atZone(getZoneId()).toInstant());
}

throw new DateTimeException(MessageFormat.format("Error while parsing date. Type: {0}, value: {1}", value.getClass().getName(), value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.Date;

Expand Down Expand Up @@ -80,6 +82,26 @@ public void should_returnDate_when_dateIsProvided() {
assertThat(date).isEqualTo(initialDate);
}

@Test
public void should_returnDate_when_localDateIsProvided() {

LocalDate localDate = LocalDate.now();

Date date = provider.toDate(localDate);

assertThat(date).isEqualTo(Date.from(localDate.atStartOfDay(provider.getZoneId()).toInstant()));
}

@Test
public void should_returnDate_when_localDateTimeIsProvided() {

LocalDateTime localDateTime = LocalDateTime.now();

Date date = provider.toDate(localDateTime);

assertThat(date).isEqualTo(Date.from(localDateTime.atZone(provider.getZoneId()).toInstant()));
}

@Test
public void should_throwException_when_isNotAStringADateOrALong() {
double value = 1.2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.activiti.spring.process.model.ProcessVariablesMapping;
import org.activiti.spring.process.model.VariableDefinition;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.assertj.core.api.InstanceOfAssertFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -44,7 +43,11 @@
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -342,6 +345,81 @@ public void calculateOutputVariablesShouldConvertValueFromIntegerToBigDecimal()
assertThat(outPutVariables.get(processVariableName)).asInstanceOf(InstanceOfAssertFactories.BIG_DECIMAL).isEqualByComparingTo(bigDecimalValue);
}

@Test
public void calculateOutputVariablesShouldConvertLocalDateToDate() {
//given
String taskId = "task-id";
String processVariableId = "process-variable-id";
String processVariableName = "localdate-process-variable";
String integerOutputName = "date-output";

Extension extension = new Extension();
DelegateExecution execution = buildExecution(extension, taskId);

VariableDefinition localDateProcessVariable = new VariableDefinition();
localDateProcessVariable.setType("date");
localDateProcessVariable.setName(processVariableName);
localDateProcessVariable.setId(processVariableId);
extension.setProperties(Map.of(processVariableId, localDateProcessVariable));

ProcessVariablesMapping mappings = new ProcessVariablesMapping();
Mapping mapping = new Mapping();
mapping.setType(Mapping.SourceMappingType.VARIABLE);
mapping.setValue(integerOutputName);
mappings.setOutputs(Map.of(processVariableName, mapping));
extension.setMappings(Map.of(taskId, mappings));

LocalDate localDateValue = LocalDate.now();
Date dateValue = Date.from(localDateValue.atStartOfDay(ZoneOffset.UTC).toInstant());

Map<String, Object> availableVariables = singletonMap(integerOutputName, localDateValue);

//when
Map<String, Object> outPutVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution),
availableVariables);

//then
assertThat(outPutVariables.get(processVariableName)).asInstanceOf(InstanceOfAssertFactories.DATE).isEqualTo(dateValue);
}

@Test
public void calculateOutputVariablesShouldConvertLocalDateTimeToDate() {
//given
String taskId = "task-id";
String processVariableId = "process-variable-id";
String processVariableName = "localdate-process-variable";
String integerOutputName = "date-output";

Extension extension = new Extension();
DelegateExecution execution = buildExecution(extension, taskId);

VariableDefinition localDateProcessVariable = new VariableDefinition();
localDateProcessVariable.setType("date");
localDateProcessVariable.setName(processVariableName);
localDateProcessVariable.setId(processVariableId);
extension.setProperties(Map.of(processVariableId, localDateProcessVariable));

ProcessVariablesMapping mappings = new ProcessVariablesMapping();
Mapping mapping = new Mapping();
mapping.setType(Mapping.SourceMappingType.VARIABLE);
mapping.setValue(integerOutputName);
mappings.setOutputs(Map.of(processVariableName, mapping));
extension.setMappings(Map.of(taskId, mappings));

LocalDateTime localDateTimeValue = LocalDateTime.now();
Date dateValue = Date.from(localDateTimeValue.atZone(ZoneOffset.UTC).toInstant());

Map<String, Object> availableVariables = singletonMap(integerOutputName, localDateTimeValue);

//when
Map<String, Object> outPutVariables = variablesMappingProvider.calculateOutPutVariables(buildMappingExecutionContext(execution),
availableVariables);

//then
assertThat(outPutVariables.get(processVariableName)).asInstanceOf(InstanceOfAssertFactories.DATE).isEqualTo(dateValue);
}


@Test
public void calculateOutputVariablesShouldConvertValueFromStringToBigDecimal() {

Expand Down

0 comments on commit d3338fe

Please sign in to comment.