Skip to content

Commit 525259e

Browse files
Merge pull request #48 from rico-projects/time-conversion-tests
Additional unit tests
2 parents 95345bc + c70a97e commit 525259e

File tree

4 files changed

+378
-1
lines changed

4 files changed

+378
-1
lines changed

remoting/rico-remoting-common/src/main/java/dev/rico/internal/remoting/RemotingConstants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public interface RemotingConstants {
3333

3434
String INTERNAL_ATTRIBUTES_BEAN_NAME = "@@@ HIGHLANDER_BEAN @@@";
3535

36-
String REMOTING_DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
36+
String REMOTING_DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
3737

3838
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2018-2019 Karakun AG.
3+
* Copyright 2015-2018 Canoo Engineering AG.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package dev.rico.internal.remoting.converters;
18+
19+
import dev.rico.remoting.converter.Converter;
20+
import dev.rico.remoting.converter.ValueConverterException;
21+
import org.testng.Assert;
22+
import org.testng.annotations.Test;
23+
24+
import java.text.ParseException;
25+
import java.time.LocalDate;
26+
import java.time.LocalDateTime;
27+
import java.time.LocalTime;
28+
import java.time.ZonedDateTime;
29+
import java.util.Arrays;
30+
import java.util.Objects;
31+
import java.util.TimeZone;
32+
33+
public class LocalDateConverterTest {
34+
35+
@Test
36+
public void testSupportedType() {
37+
//given
38+
final LocalDateConverterFactory factory = new LocalDateConverterFactory();
39+
40+
//then
41+
Assert.assertTrue(factory.supportsType(LocalDate.class));
42+
Assert.assertFalse(factory.supportsType(ZonedDateTime.class));
43+
Assert.assertFalse(factory.supportsType(LocalDateTime.class));
44+
Assert.assertFalse(factory.supportsType(LocalTime.class));
45+
}
46+
47+
@Test
48+
public void testNullValue() throws ValueConverterException {
49+
//given
50+
final LocalDateConverterFactory factory = new LocalDateConverterFactory();
51+
final Converter converter = factory.getConverterForType(LocalDate.class);
52+
53+
//when
54+
final Object rawObject = converter.convertToRemoting(null);
55+
final Object reConverted = converter.convertFromRemoting(rawObject);
56+
57+
//then
58+
Assert.assertNull(reConverted);
59+
}
60+
61+
@Test
62+
public void testSameTimeZone() throws ValueConverterException {
63+
//given
64+
final LocalDateConverterFactory factory = new LocalDateConverterFactory();
65+
final LocalDate time = LocalDate.now();
66+
final Converter converter = factory.getConverterForType(LocalDate.class);
67+
68+
//when
69+
final Object rawObject = converter.convertToRemoting(time);
70+
System.out.println(rawObject);
71+
final Object reConverted = converter.convertFromRemoting(rawObject);
72+
73+
//then
74+
Assert.assertNotNull(rawObject);
75+
Assert.assertNotNull(reConverted);
76+
Assert.assertTrue(LocalDate.class.isAssignableFrom(reConverted.getClass()));
77+
final LocalDate reconvertedTime = (LocalDate) reConverted;
78+
Assert.assertEquals(reconvertedTime, time);
79+
}
80+
81+
@Test(enabled = false)
82+
public void testDifferentTimeZone() throws ValueConverterException {
83+
final TimeZone defaultZone = TimeZone.getDefault();
84+
try {
85+
86+
//given
87+
final LocalDateConverterFactory factory = new LocalDateConverterFactory();
88+
final LocalDate time = LocalDate.now();
89+
final Converter converter = factory.getConverterForType(LocalDate.class);
90+
final TimeZone differentZone = Arrays.asList(TimeZone.getAvailableIDs()).stream()
91+
.map(id -> TimeZone.getTimeZone(id))
92+
.filter(zone -> !Objects.equals(defaultZone, zone))
93+
.findAny()
94+
.orElseThrow(() -> new RuntimeException("No time zone found"));
95+
96+
//when
97+
final Object rawObject = converter.convertToRemoting(time);
98+
TimeZone.setDefault(differentZone);
99+
final Object reConverted = converter.convertFromRemoting(rawObject);
100+
101+
//then
102+
Assert.assertNotNull(rawObject);
103+
Assert.assertNotNull(reConverted);
104+
Assert.assertTrue(LocalDate.class.isAssignableFrom(reConverted.getClass()));
105+
final LocalDate reconvertedTime = (LocalDate) reConverted;
106+
Assert.assertEquals(reconvertedTime, time);
107+
} finally {
108+
TimeZone.setDefault(defaultZone);
109+
}
110+
}
111+
112+
@Test
113+
public void testRawSameTimeZone() throws ValueConverterException, ParseException {
114+
final TimeZone defaultZone = TimeZone.getDefault();
115+
try {
116+
117+
//given
118+
final LocalDateConverterFactory localDateFactory = new LocalDateConverterFactory();
119+
final Converter localDateConverter = localDateFactory.getConverterForType(LocalDate.class);
120+
final ZonedDateTimeConverterFactory zonedDateTimeFactory = new ZonedDateTimeConverterFactory();
121+
final Converter zonedDateTimeConverter = zonedDateTimeFactory.getConverterForType(LocalDate.class);
122+
final String rawObject = "2019-01-30T11:25:07.341+03:00";
123+
final ZonedDateTime zonedDateTime = (ZonedDateTime) zonedDateTimeConverter.convertFromRemoting(rawObject);
124+
final LocalDate fromZonedTime = LocalDate.from(zonedDateTime);
125+
126+
//when
127+
final Object reconvertedLocalDate = localDateConverter.convertFromRemoting(rawObject);
128+
129+
//then
130+
Assert.assertNotNull(reconvertedLocalDate);
131+
Assert.assertTrue(LocalDate.class.isAssignableFrom(reconvertedLocalDate.getClass()));
132+
final LocalDate reconvertedTime = (LocalDate) reconvertedLocalDate;
133+
Assert.assertEquals(reconvertedTime, fromZonedTime);
134+
135+
} finally {
136+
TimeZone.setDefault(defaultZone);
137+
}
138+
}
139+
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2018-2019 Karakun AG.
3+
* Copyright 2015-2018 Canoo Engineering AG.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package dev.rico.internal.remoting.converters;
18+
19+
import dev.rico.remoting.converter.Converter;
20+
import dev.rico.remoting.converter.ValueConverterException;
21+
import org.testng.Assert;
22+
import org.testng.annotations.Test;
23+
24+
import java.time.LocalDate;
25+
import java.time.LocalDateTime;
26+
import java.time.LocalTime;
27+
import java.time.ZonedDateTime;
28+
import java.util.Arrays;
29+
import java.util.Objects;
30+
import java.util.TimeZone;
31+
32+
public class LocalDateTimeConverterTest {
33+
34+
@Test
35+
public void testSupportedType() {
36+
//given
37+
final LocalDateTimeConverterFactory factory = new LocalDateTimeConverterFactory();
38+
39+
//then
40+
Assert.assertTrue(factory.supportsType(LocalDateTime.class));
41+
Assert.assertFalse(factory.supportsType(ZonedDateTime.class));
42+
Assert.assertFalse(factory.supportsType(LocalDate.class));
43+
Assert.assertFalse(factory.supportsType(LocalTime.class));
44+
}
45+
46+
@Test
47+
public void testNullValue() throws ValueConverterException {
48+
//given
49+
final LocalDateTimeConverterFactory factory = new LocalDateTimeConverterFactory();
50+
final Converter converter = factory.getConverterForType(LocalDateTime.class);
51+
52+
//when
53+
final Object rawObject = converter.convertToRemoting(null);
54+
final Object reConverted = converter.convertFromRemoting(rawObject);
55+
56+
//then
57+
Assert.assertNull(reConverted);
58+
}
59+
60+
@Test
61+
public void testSameTimeZone() throws ValueConverterException {
62+
//given
63+
final LocalDateTimeConverterFactory factory = new LocalDateTimeConverterFactory();
64+
final LocalDateTime time = LocalDateTime.now();
65+
final Converter converter = factory.getConverterForType(LocalDateTime.class);
66+
67+
//when
68+
final Object rawObject = converter.convertToRemoting(time);
69+
final Object reConverted = converter.convertFromRemoting(rawObject);
70+
71+
//then
72+
Assert.assertNotNull(rawObject);
73+
Assert.assertNotNull(reConverted);
74+
Assert.assertTrue(LocalDateTime.class.isAssignableFrom(reConverted.getClass()));
75+
final LocalDateTime reconvertedTime = (LocalDateTime) reConverted;
76+
Assert.assertEquals(reconvertedTime, time);
77+
}
78+
79+
@Test
80+
public void testDifferentTimeZone() throws ValueConverterException {
81+
final TimeZone defaultZone = TimeZone.getDefault();
82+
try {
83+
84+
//given
85+
final LocalDateTimeConverterFactory factory = new LocalDateTimeConverterFactory();
86+
final LocalDateTime time = LocalDateTime.now();
87+
final Converter converter = factory.getConverterForType(LocalDateTime.class);
88+
final TimeZone differentZone = Arrays.asList(TimeZone.getAvailableIDs()).stream()
89+
.map(id -> TimeZone.getTimeZone(id))
90+
.filter(zone -> !Objects.equals(defaultZone, zone))
91+
.findAny()
92+
.orElseThrow(() -> new RuntimeException("No time zone found"));
93+
94+
//when
95+
final Object rawObject = converter.convertToRemoting(time);
96+
TimeZone.setDefault(differentZone);
97+
final Object reConverted = converter.convertFromRemoting(rawObject);
98+
99+
//then
100+
Assert.assertNotNull(rawObject);
101+
Assert.assertNotNull(reConverted);
102+
Assert.assertTrue(LocalDateTime.class.isAssignableFrom(reConverted.getClass()));
103+
final LocalDateTime reconvertedTime = (LocalDateTime) reConverted;
104+
Assert.assertEquals(reconvertedTime, time);
105+
} finally {
106+
TimeZone.setDefault(defaultZone);
107+
}
108+
}
109+
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2018-2019 Karakun AG.
3+
* Copyright 2015-2018 Canoo Engineering AG.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package dev.rico.internal.remoting.converters;
18+
19+
import dev.rico.remoting.converter.Converter;
20+
import dev.rico.remoting.converter.ValueConverterException;
21+
import org.testng.Assert;
22+
import org.testng.annotations.Test;
23+
24+
import java.text.ParseException;
25+
import java.time.LocalDate;
26+
import java.time.LocalDateTime;
27+
import java.time.ZoneId;
28+
import java.time.ZonedDateTime;
29+
import java.util.Objects;
30+
import java.util.TimeZone;
31+
32+
public class ZonedDateTimeConverterTest {
33+
34+
@Test
35+
public void testSupportedType() {
36+
//given
37+
final ZonedDateTimeConverterFactory factory = new ZonedDateTimeConverterFactory();
38+
39+
//then
40+
Assert.assertTrue(factory.supportsType(ZonedDateTime.class));
41+
Assert.assertFalse(factory.supportsType(LocalDateTime.class));
42+
}
43+
44+
@Test
45+
public void testNullValue() throws ValueConverterException {
46+
//given
47+
final ZonedDateTimeConverterFactory factory = new ZonedDateTimeConverterFactory();
48+
final Converter converter = factory.getConverterForType(ZonedDateTime.class);
49+
50+
//when
51+
final Object rawObject = converter.convertToRemoting(null);
52+
final Object reConverted = converter.convertFromRemoting(rawObject);
53+
54+
//then
55+
Assert.assertNull(reConverted);
56+
}
57+
58+
@Test
59+
public void testSameTimeZone() throws ValueConverterException {
60+
//given
61+
final ZonedDateTimeConverterFactory factory = new ZonedDateTimeConverterFactory();
62+
final ZonedDateTime time = ZonedDateTime.now();
63+
final Converter converter = factory.getConverterForType(ZonedDateTime.class);
64+
65+
//when
66+
final Object rawObject = converter.convertToRemoting(time);
67+
final Object reConverted = converter.convertFromRemoting(rawObject);
68+
69+
//then
70+
Assert.assertNotNull(rawObject);
71+
Assert.assertNotNull(reConverted);
72+
Assert.assertTrue(ZonedDateTime.class.isAssignableFrom(reConverted.getClass()));
73+
final ZonedDateTime reconvertedTime = (ZonedDateTime) reConverted;
74+
Assert.assertEquals(reconvertedTime, time);
75+
}
76+
77+
@Test
78+
public void testDifferentTimeZone() throws ValueConverterException {
79+
//given
80+
final ZonedDateTimeConverterFactory factory = new ZonedDateTimeConverterFactory();
81+
final ZoneId currentZoneId = ZoneId.systemDefault();
82+
final ZoneId differentZoneId = ZoneId.getAvailableZoneIds().stream()
83+
.map(i -> ZoneId.of(i))
84+
.filter(zoneId -> !Objects.equals(zoneId, currentZoneId))
85+
.findAny()
86+
.orElseThrow(() -> new RuntimeException("No Zone ID found!"));
87+
final ZonedDateTime time = ZonedDateTime.now(differentZoneId);
88+
final Converter converter = factory.getConverterForType(ZonedDateTime.class);
89+
90+
//when
91+
final Object rawObject = converter.convertToRemoting(time);
92+
System.out.println(rawObject);
93+
final Object reConverted = converter.convertFromRemoting(rawObject);
94+
95+
//then
96+
Assert.assertNotNull(rawObject);
97+
Assert.assertNotNull(reConverted);
98+
Assert.assertTrue(ZonedDateTime.class.isAssignableFrom(reConverted.getClass()));
99+
final ZonedDateTime reconvertedTime = (ZonedDateTime) reConverted;
100+
Assert.assertEquals(reconvertedTime, time);
101+
}
102+
103+
@Test
104+
public void testRawSameTimeZone() throws ValueConverterException, ParseException {
105+
final TimeZone defaultZone = TimeZone.getDefault();
106+
try {
107+
108+
//given
109+
final ZonedDateTimeConverterFactory zonedDateTimeFactory = new ZonedDateTimeConverterFactory();
110+
final Converter zonedDateTimeConverter = zonedDateTimeFactory.getConverterForType(LocalDate.class);
111+
final String rawObject = "2019-01-30T11:25:07.341+08:00";
112+
113+
//when
114+
final Object reconverted = zonedDateTimeConverter.convertFromRemoting(rawObject);
115+
116+
//then
117+
Assert.assertNotNull(reconverted);
118+
Assert.assertTrue(ZonedDateTime.class.isAssignableFrom(reconverted.getClass()));
119+
final ZonedDateTime reconvertedTime = (ZonedDateTime) reconverted;
120+
Assert.assertEquals(ZoneId.of("Australia/Sydney"), reconvertedTime.getZone());
121+
122+
} finally {
123+
TimeZone.setDefault(defaultZone);
124+
}
125+
}
126+
127+
}

0 commit comments

Comments
 (0)