Skip to content

Commit 3c78b9d

Browse files
author
admitrov
committed
test: refactor DateUtilsTest to use parameterized tests for relative time assertions
1 parent 2ca1d7e commit 3c78b9d

File tree

1 file changed

+71
-55
lines changed
  • testcontainers-common/src/test/java/com/playtika/testcontainer/common/utils

1 file changed

+71
-55
lines changed
Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
package com.playtika.testcontainer.common.utils;
22

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
47

58
import java.time.Instant;
69
import java.time.OffsetDateTime;
710
import java.time.ZoneId;
811
import java.time.temporal.ChronoField;
9-
import java.util.LinkedHashMap;
10-
import java.util.Map;
12+
import java.util.stream.Stream;
1113

1214
import static org.assertj.core.api.Assertions.assertThat;
1315

1416
class DateUtilsTest {
1517

16-
private static Map<Long, String> relativeTimeToStrings = createRelativeTimeToStringExamples();
17-
18-
private static Map<Long, String> createRelativeTimeToStringExamples() {
19-
Map<Long, String> map = new LinkedHashMap<>();
20-
map.put(1000L * 0, " 0 sec = a minute ago");
21-
map.put(1000L * 89, "89 sec = a minute ago");
22-
map.put(1000L * 90, "90 sec = 2 minutes ago");
23-
map.put(1000L * 60 * 50, "50 min = 50 minutes ago");
24-
map.put(1000L * 60 * 51, "51 min = an hour ago");
25-
map.put(1000L * 60 * 89, "89 min = an hour ago");
26-
map.put(1000L * 60 * 90, "90 min = 2 hours ago");
27-
map.put(1000L * 60 * 60 * 20, "20 hrs = 20 hours ago");
28-
map.put(1000L * 60 * 60 * 21, "21 hrs = yesterday");
29-
map.put(1000L * 60 * 60 * 35, "35 hrs = yesterday");
30-
map.put(1000L * 60 * 60 * 36, "36 hrs = 2 days ago");
31-
map.put(1000L * 60 * 60 * 24 * 5, " 5 day = 5 days ago");
32-
map.put(1000L * 60 * 60 * 24 * 6, " 6 day = a week ago");
33-
map.put(1000L * 60 * 60 * 24 * 10, "10 day = a week ago");
34-
map.put(1000L * 60 * 60 * 24 * 11, "11 day = 2 weeks ago");
35-
map.put(1000L * 60 * 60 * 24 * 17, "17 day = 2 weeks ago");
36-
map.put(1000L * 60 * 60 * 24 * 18, "18 day = 3 weeks ago");
37-
map.put(1000L * 60 * 60 * 24 * 24, "24 day = 3 weeks ago");
38-
map.put(1000L * 60 * 60 * 24 * 25, "25 day = a month ago");
39-
map.put(1000L * 60 * 60 * 24 * 45, "45 day = a month ago");
40-
map.put(1000L * 60 * 60 * 24 * 46, "46 day = 2 months ago");
41-
map.put(1000L * 60 * 60 * 24 * 30 * 10, "10 mon = 10 months ago");
42-
map.put(1000L * 60 * 60 * 24 * 30 * 11, "11 mon = a year ago");
43-
map.put(1000L * 60 * 60 * 24 * 30 * 13, "13 mon = a year ago");
44-
map.put(1000L * 60 * 60 * 24 * 30 * 14, "14 mon = 1 year 2 months ago");
45-
map.put(1000L * 60 * 60 * 24 * 30 * 23, "23 mon = 1 year 11 months ago");
46-
map.put(1000L * 60 * 60 * 24 * 30 * 25, "25 mon = 2 years ago");
47-
map.put(1000L * 60 * 60 * 24 * 30 * 26, "26 mon = 2 years 2 months ago");
48-
return map;
18+
static Stream<Arguments> relativeTimeTestData() {
19+
return Stream.of(
20+
Arguments.of(1000L * 0, " 0 sec = a minute ago"),
21+
Arguments.of(1000L * 89, "89 sec = a minute ago"),
22+
Arguments.of(1000L * 90, "90 sec = 2 minutes ago"),
23+
Arguments.of(1000L * 60 * 50, "50 min = 50 minutes ago"),
24+
Arguments.of(1000L * 60 * 51, "51 min = an hour ago"),
25+
Arguments.of(1000L * 60 * 89, "89 min = an hour ago"),
26+
Arguments.of(1000L * 60 * 90, "90 min = 2 hours ago"),
27+
Arguments.of(1000L * 60 * 60 * 20, "20 hrs = 20 hours ago"),
28+
Arguments.of(1000L * 60 * 60 * 21, "21 hrs = yesterday"),
29+
Arguments.of(1000L * 60 * 60 * 35, "35 hrs = yesterday"),
30+
Arguments.of(1000L * 60 * 60 * 36, "36 hrs = 2 days ago"),
31+
Arguments.of(1000L * 60 * 60 * 24 * 5, " 5 day = 5 days ago"),
32+
Arguments.of(1000L * 60 * 60 * 24 * 6, " 6 day = a week ago"),
33+
Arguments.of(1000L * 60 * 60 * 24 * 10, "10 day = a week ago"),
34+
Arguments.of(1000L * 60 * 60 * 24 * 11, "11 day = 2 weeks ago"),
35+
Arguments.of(1000L * 60 * 60 * 24 * 17, "17 day = 2 weeks ago"),
36+
Arguments.of(1000L * 60 * 60 * 24 * 18, "18 day = 3 weeks ago"),
37+
Arguments.of(1000L * 60 * 60 * 24 * 24, "24 day = 3 weeks ago"),
38+
Arguments.of(1000L * 60 * 60 * 24 * 25, "25 day = a month ago"),
39+
Arguments.of(1000L * 60 * 60 * 24 * 45, "45 day = a month ago"),
40+
Arguments.of(1000L * 60 * 60 * 24 * 46, "46 day = 2 months ago"),
41+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 10, "10 mon = 10 months ago"),
42+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 11, "11 mon = a year ago"),
43+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 13, "13 mon = a year ago"),
44+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 14, "14 mon = 1 year 2 months ago"),
45+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 23, "23 mon = 1 year 11 months ago"),
46+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 25, "25 mon = 2 years ago"),
47+
Arguments.of(1000L * 60 * 60 * 24 * 30 * 26, "26 mon = 2 years 2 months ago")
48+
);
4949
}
5050

5151
private static OffsetDateTime toOffsetDateTime(Instant now, Long time) {
@@ -57,32 +57,43 @@ private static OffsetDateTime toOffsetDateTimePlusNanoseconds(Instant now, Long
5757
return now.minusMillis(time).atZone(ZoneId.systemDefault()).toOffsetDateTime();
5858
}
5959

60-
@Test
61-
void toDateAndTimeAgo() {
60+
@ParameterizedTest(name = "{index}: {1}")
61+
@MethodSource("relativeTimeTestData")
62+
void toDateAndTimeAgo(Long time, String description) {
6263
Instant now = Instant.now();
63-
relativeTimeToStrings.forEach((time, text) -> assertThat(DateUtils
64-
.toDateAndTimeAgo(toOffsetDateTimePlusNanoseconds(now, time).toString()))
65-
.as(toOffsetDateTime(now, time) + " = " + text).isEqualTo(String
66-
.format("%s (%s)", toOffsetDateTime(now, time), text.split(" = ")[1])));
64+
String expectedTimeAgo = description.split(" = ")[1];
65+
OffsetDateTime dateTime = toOffsetDateTime(now, time);
66+
String expected = String.format("%s (%s)", dateTime, expectedTimeAgo);
67+
68+
assertThat(DateUtils.toDateAndTimeAgo(toOffsetDateTimePlusNanoseconds(now, time).toString()))
69+
.as(dateTime + " = " + description)
70+
.isEqualTo(expected);
6771
}
6872

69-
@Test
70-
void toTimeAgoLong() {
73+
@ParameterizedTest(name = "{index}: {1}")
74+
@MethodSource("relativeTimeTestData")
75+
void toTimeAgoLong(Long time, String description) {
7176
Instant now = Instant.now();
72-
relativeTimeToStrings.forEach((time, text) -> assertThat(DateUtils.toTimeAgo(now.toEpochMilli() - time))
73-
.as(toOffsetDateTime(now, time) + " = " + text).isEqualTo(text.split(" = ")[1]));
77+
String expectedTimeAgo = description.split(" = ")[1];
78+
79+
assertThat(DateUtils.toTimeAgo(now.toEpochMilli() - time))
80+
.as(toOffsetDateTime(now, time) + " = " + description)
81+
.isEqualTo(expectedTimeAgo);
7482
}
7583

76-
@Test
77-
void toTimeAgoString() {
84+
@ParameterizedTest(name = "{index}: {1}")
85+
@MethodSource("relativeTimeTestData")
86+
void toTimeAgoString(Long time, String description) {
7887
Instant now = Instant.now();
79-
relativeTimeToStrings.forEach((time, text) -> assertThat(DateUtils
80-
.toTimeAgo(toOffsetDateTimePlusNanoseconds(now, time).toString()))
81-
.as(toOffsetDateTime(now, time) + " = " + text).isEqualTo(text.split(" = ")[1]));
88+
String expectedTimeAgo = description.split(" = ")[1];
89+
90+
assertThat(DateUtils.toTimeAgo(toOffsetDateTimePlusNanoseconds(now, time).toString()))
91+
.as(toOffsetDateTime(now, time) + " = " + description)
92+
.isEqualTo(expectedTimeAgo);
8293
}
8394

8495
@Test
85-
void parseToInstantOrString() {
96+
void parseToInstantOrString_edgeCases() {
8697
assertThat(DateUtils.parseToInstantOrString(null)).isNull();
8798
assertThat(DateUtils.parseToInstantOrString("")).isEqualTo("");
8899
assertThat(DateUtils.parseToInstantOrString(" ")).isEqualTo("");
@@ -93,12 +104,17 @@ void parseToInstantOrString() {
93104
assertThat(DateUtils.parseToInstantOrString("2021-12-31T23:59:59"))
94105
.as("Zone offset like +05:00 is required")
95106
.isEqualTo("Text '2021-12-31T23:59:59' could not be parsed at index 19");
107+
}
96108

109+
@ParameterizedTest(name = "{index}: {1}")
110+
@MethodSource("relativeTimeTestData")
111+
void parseToInstantOrString(Long time, String description) {
97112
Instant now = Instant.now();
98-
relativeTimeToStrings.forEach((time, text) -> assertThat(DateUtils
99-
.parseToInstantOrString(toOffsetDateTimePlusNanoseconds(now, time).toString()))
100-
.as(toOffsetDateTime(now, time) + " = " + text)
101-
.isEqualTo(now.minusMillis(time).with(ChronoField.NANO_OF_SECOND, 0)));
113+
Instant expected = now.minusMillis(time).with(ChronoField.NANO_OF_SECOND, 0);
114+
115+
assertThat(DateUtils.parseToInstantOrString(toOffsetDateTimePlusNanoseconds(now, time).toString()))
116+
.as(toOffsetDateTime(now, time) + " = " + description)
117+
.isEqualTo(expected);
102118
}
103119

104120
}

0 commit comments

Comments
 (0)