Skip to content

Commit e79e69d

Browse files
ISSUE-268 # Support timezone-aware datetime assertions
1 parent df85b94 commit e79e69d

5 files changed

Lines changed: 80 additions & 16 deletions

File tree

core/src/main/java/org/jsmart/zerocode/core/engine/assertion/field/FieldHasDateAfterValueAsserter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
package org.jsmart.zerocode.core.engine.assertion.field;
33

44
import java.time.LocalDateTime;
5-
import java.time.format.DateTimeFormatter;
65
import java.time.format.DateTimeParseException;
76
import org.jsmart.zerocode.core.engine.assertion.JsonAsserter;
87
import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher;
98

109
import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aMatchingMessage;
1110
import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aNotMatchingMessage;
11+
import static org.jsmart.zerocode.core.utils.DateTimeUtils.parseLocalDateTime;
1212

1313
public class FieldHasDateAfterValueAsserter implements JsonAsserter {
1414
private final String path;
@@ -40,8 +40,7 @@ public FieldAssertionMatcher actualEqualsToExpected(Object result) {
4040
} else {
4141
LocalDateTime resultDT = null;
4242
try {
43-
resultDT = LocalDateTime.parse((String) result,
44-
DateTimeFormatter.ISO_DATE_TIME);
43+
resultDT = parseLocalDateTime((String) result);
4544
areEqual = resultDT.isAfter(expected);
4645
} catch (DateTimeParseException ex) {
4746
areEqual = false;

core/src/main/java/org/jsmart/zerocode/core/engine/assertion/field/FieldHasDateBeforeValueAsserter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
package org.jsmart.zerocode.core.engine.assertion.field;
33

44
import java.time.LocalDateTime;
5-
import java.time.format.DateTimeFormatter;
65
import java.time.format.DateTimeParseException;
76
import org.jsmart.zerocode.core.engine.assertion.JsonAsserter;
87
import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher;
98

109
import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aMatchingMessage;
1110
import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aNotMatchingMessage;
11+
import static org.jsmart.zerocode.core.utils.DateTimeUtils.parseLocalDateTime;
1212

1313
public class FieldHasDateBeforeValueAsserter implements JsonAsserter {
1414
private final String path;
@@ -40,8 +40,7 @@ public FieldAssertionMatcher actualEqualsToExpected(Object result) {
4040
} else {
4141
LocalDateTime resultDT = null;
4242
try {
43-
resultDT = LocalDateTime.parse((String) result,
44-
DateTimeFormatter.ISO_DATE_TIME);
43+
resultDT = parseLocalDateTime((String) result);
4544
areEqual = resultDT.isBefore(expected);
4645
} catch (DateTimeParseException ex) {
4746
areEqual = false;

core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
import java.io.IOException;
3333
import java.math.BigDecimal;
34-
import java.time.LocalDateTime;
35-
import java.time.format.DateTimeFormatter;
3634
import java.util.ArrayList;
3735
import java.util.HashMap;
3836
import java.util.Iterator;
@@ -71,6 +69,7 @@
7169
import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded;
7270
import static org.jsmart.zerocode.core.utils.SmartUtils.getJsonFilePhToken;
7371
import static org.jsmart.zerocode.core.utils.SmartUtils.isValidAbsolutePath;
72+
import static org.jsmart.zerocode.core.utils.DateTimeUtils.parseLocalDateTime;
7473
import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksRemoved;
7574
import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksReplaced;
7675
import static org.jsmart.zerocode.core.utils.TokenUtils.getTestCaseTokens;
@@ -437,10 +436,6 @@ private boolean isPropertyKey(String runTimeToken) {
437436
return propertyKeys.contains(runTimeToken);
438437
}
439438

440-
private LocalDateTime parseLocalDateTime(String value) {
441-
return LocalDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME);
442-
}
443-
444439
boolean isPathValueJson(Object jsonPathValue) {
445440
return jsonPathValue instanceof LinkedHashMap || jsonPathValue instanceof List<?>;
446441
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.jsmart.zerocode.core.utils;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.OffsetDateTime;
5+
import java.time.ZoneId;
6+
import java.time.ZonedDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.time.format.DateTimeParseException;
9+
10+
public class DateTimeUtils {
11+
12+
public static LocalDateTime parseLocalDateTime(String value) {
13+
try {
14+
return ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME)
15+
.withZoneSameInstant(ZoneId.systemDefault())
16+
.toLocalDateTime();
17+
} catch (DateTimeParseException ignored) {
18+
}
19+
20+
try {
21+
return OffsetDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME)
22+
.atZoneSameInstant(ZoneId.systemDefault())
23+
.toLocalDateTime();
24+
} catch (DateTimeParseException ignored) {
25+
}
26+
27+
return LocalDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME);
28+
}
29+
}

core/src/test/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImplTest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
import java.util.HashMap;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.TimeZone;
3334

3435
import static com.jayway.jsonpath.JsonPath.read;
3536
import static org.hamcrest.CoreMatchers.containsString;
3637
import static org.hamcrest.CoreMatchers.is;
3738
import static org.hamcrest.CoreMatchers.not;
3839
import static org.jsmart.zerocode.core.utils.HelperJsonUtils.readJsonPath;
40+
import static org.jsmart.zerocode.core.utils.DateTimeUtils.parseLocalDateTime;
3941
import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded;
4042
import static org.jsmart.zerocode.core.utils.SmartUtils.readJsonAsString;
4143
import static org.jsmart.zerocode.core.utils.TokenUtils.getTestCaseTokens;
@@ -977,6 +979,46 @@ public void testDateAfterBefore_both() throws Exception {
977979
assertThat(failedReports.size(), is(0));
978980
}
979981

982+
@Test
983+
public void testDateAfterBefore_withTimeZoneOffsetsAndHttpDate() throws Exception {
984+
TimeZone defaultTimeZone = TimeZone.getDefault();
985+
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
986+
try {
987+
String resolvedAssertions =
988+
"{\n"
989+
+ " \"status\": 200,\n"
990+
+ " \"body\": {\n"
991+
+ " \"projectDetails\": {\n"
992+
+ " \"startDateTime\": \"$LOCAL.DATETIME.BEFORE:2019-06-15T10:00:00.000Z\",\n"
993+
+ " \"endDateTime\": \"$LOCAL.DATETIME.AFTER:2019-06-15T10:00:00.000Z\",\n"
994+
+ " \"lastModified\": \"$LOCAL.DATETIME.BEFORE:Sat, 06 Jul 2019 17:07:15 GMT\"\n"
995+
+ " }\n"
996+
+ " }\n"
997+
+ "}";
998+
999+
List<JsonAsserter> asserters = jsonPreProcessor.createJsonAsserters(resolvedAssertions);
1000+
assertThat(asserters.size(), is(4));
1001+
1002+
String mockTestResponse =
1003+
"{\n"
1004+
+ " \"status\": 200,\n"
1005+
+ " \"body\": {\n"
1006+
+ " \"projectDetails\": {\n"
1007+
+ " \"startDateTime\": \"2019-06-15T11:30:00.000+02:00\",\n"
1008+
+ " \"endDateTime\": \"2019-06-15T12:30:00.000+02:00\",\n"
1009+
+ " \"lastModified\": \"Sat, 06 Jul 2019 16:07:15 GMT\"\n"
1010+
+ " }\n"
1011+
+ " }\n"
1012+
+ "}";
1013+
1014+
List<FieldAssertionMatcher> failedReports =
1015+
jsonPreProcessor.assertAllAndReturnFailed(asserters, mockTestResponse);
1016+
assertThat(failedReports.size(), is(0));
1017+
} finally {
1018+
TimeZone.setDefault(defaultTimeZone);
1019+
}
1020+
}
1021+
9801022
@Test
9811023
public void testDateAfterBefore_fail_both() throws Exception {
9821024
ScenarioSpec scenarioSpec =
@@ -1018,12 +1060,12 @@ public void testDateAfterBefore_fail_both() throws Exception {
10181060
failedReports.get(0).toString(),
10191061
is(
10201062
"Assertion jsonPath '$.body.projectDetails.startDateTime' with actual value '2017-04-14T11:49:56.000Z' "
1021-
+ "did not match the expected value 'Date Before:2016-09-14T09:49:34'"));
1063+
+ "did not match the expected value 'Date Before:" + parseLocalDateTime("2016-09-14T09:49:34.000Z") + "'"));
10221064
assertThat(
10231065
failedReports.get(1).toString(),
10241066
is(
10251067
"Assertion jsonPath '$.body.projectDetails.endDateTime' with actual value '2018-11-12T09:39:34.000Z' "
1026-
+ "did not match the expected value 'Date After:2019-09-14T09:49:34'"));
1068+
+ "did not match the expected value 'Date After:" + parseLocalDateTime("2019-09-14T09:49:34.000Z") + "'"));
10271069
}
10281070

10291071
@Test
@@ -1063,7 +1105,7 @@ public void testDateAfterBefore_fail_afterSameDate() throws Exception {
10631105
failedReports.get(0).toString(),
10641106
is(
10651107
"Assertion jsonPath '$.body.projectDetails.startDateTime' with actual value '2015-09-14T09:49:34.000Z' "
1066-
+ "did not match the expected value 'Date After:2015-09-14T09:49:34'"));
1108+
+ "did not match the expected value 'Date After:" + parseLocalDateTime("2015-09-14T09:49:34.000Z") + "'"));
10671109
}
10681110

10691111
@Test
@@ -1103,7 +1145,7 @@ public void testDateAfterBefore_fail_beforeSameDate() throws Exception {
11031145
failedReports.get(0).toString(),
11041146
is(
11051147
"Assertion jsonPath '$.body.projectDetails.startDateTime' with actual value '2015-09-14T09:49:34.000Z' "
1106-
+ "did not match the expected value 'Date Before:2015-09-14T09:49:34'"));
1148+
+ "did not match the expected value 'Date Before:" + parseLocalDateTime("2015-09-14T09:49:34.000Z") + "'"));
11071149
}
11081150

11091151
@Test

0 commit comments

Comments
 (0)