1
+ package com .github .nramc .dev .journey .api .geojson ;
2
+
3
+ import com .github .nramc .dev .journey .api .geojson .types .Position ;
4
+ import org .junit .jupiter .api .Assertions ;
5
+ import org .junit .jupiter .api .Test ;
6
+ import org .junit .jupiter .params .ParameterizedTest ;
7
+ import org .junit .jupiter .params .provider .ValueSource ;
8
+ import org .springframework .beans .factory .annotation .Autowired ;
9
+ import org .springframework .boot .test .autoconfigure .json .JsonTest ;
10
+ import org .springframework .boot .test .json .JacksonTester ;
11
+
12
+ import java .io .IOException ;
13
+
14
+ import static org .assertj .core .api .Assertions .assertThat ;
15
+
16
+ @ JsonTest
17
+ class PointTest {
18
+ @ Autowired
19
+ private JacksonTester <Point > jacksonTester ;
20
+
21
+
22
+ @ Test
23
+ void testJsonSerialisation_withMandatoryCoordinates () throws IOException {
24
+ assertThat (jacksonTester .write (Point .of (Position .of (60.8 , 20.5 ))))
25
+ .isEqualToJson ("""
26
+ { "type": "Point", "coordinates": [60.8, 20.5] }""" );
27
+
28
+ }
29
+
30
+ @ Test
31
+ void testJsonSerialisation_withMandatoryAndOptionalCoordinates () throws IOException {
32
+ assertThat (jacksonTester .write (Point .of (Position .of (60.8 , 20.5 , 43.2 ))))
33
+ .isEqualToJson ("""
34
+ { "type": "Point", "coordinates": [60.8, 20.5, 43.2] }""" );
35
+
36
+ }
37
+
38
+ @ Test
39
+ void testDeserialization_withMandatoryCoordinates () throws IOException {
40
+ String jsonString = """
41
+ { "type": "Point", "coordinates": [60.8, 20.5] }""" ;
42
+ Point point = Point .of (Position .of (60.8 , 20.5 ));
43
+ assertThat (jacksonTester .parseObject (jsonString ))
44
+ .satisfies (p -> assertThat (p .getType ()).isEqualTo (point .getType ()))
45
+ .satisfies (p -> assertThat (p .getCoordinates ().getCoordinates ()).isEqualTo (point .getCoordinates ().getCoordinates ()));
46
+ }
47
+
48
+ @ Test
49
+ void testDeserialization_withOptionalCoordinates () throws IOException {
50
+ String jsonString = """
51
+ { "type": "Point", "coordinates": [60.8, 20.5, 54.7] }""" ;
52
+ Point point = Point .of (Position .of (60.8 , 20.5 , 54.7 ));
53
+ assertThat (jacksonTester .parseObject (jsonString ))
54
+ .satisfies (p -> assertThat (p .getType ()).isEqualTo (point .getType ()))
55
+ .satisfies (p -> assertThat (p .getCoordinates ().getCoordinates ()).isEqualTo (point .getCoordinates ().getCoordinates ()));
56
+ }
57
+
58
+ @ ParameterizedTest
59
+ @ ValueSource (strings = {"""
60
+ { "type": "Point", "coordinates": [60.8, 20.5, 54.7, 11.2] }""" , """
61
+ { "type": "Point", "coordinates": [] }""" , """
62
+ { "type": "Point", "coordinates": [60.8, 20.5, 54.7, 10.4, 15.7] }""" , """
63
+ { "type": "Point" }"""
64
+
65
+ })
66
+ void testDeserialization_withInvalidCoordinates (String jsonString ) throws IOException {
67
+ Assertions .assertThrows (Exception .class , () -> jacksonTester .parseObject (jsonString ));
68
+ }
69
+
70
+ }
0 commit comments