1+ package com .github .nramc .dev .journey .api .geojson ;
2+
3+ import com .github .nramc .dev .journey .api .geojson .types .GeoJsonType ;
4+ import com .github .nramc .dev .journey .api .geojson .types .Position ;
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+ import org .springframework .boot .test .json .JsonContent ;
12+
13+ import java .io .IOException ;
14+ import java .util .List ;
15+
16+ import static org .assertj .core .api .Assertions .assertThat ;
17+ import static org .junit .jupiter .api .Assertions .assertThrows ;
18+
19+ @ JsonTest
20+ class LineStringTest {
21+ @ Autowired
22+ JacksonTester <LineString > jacksonTester ;
23+
24+ @ Test
25+ void deserialization_withLongitudeAndLatitude () throws IOException {
26+ String json = """
27+ {
28+ "type": "LineString",
29+ "coordinates": [
30+ [100.0, 0.0],
31+ [101.0, 1.0]
32+ ]
33+ }
34+ """ ;
35+ LineString object = jacksonTester .parseObject (json );
36+ assertThat (object ).isNotNull ()
37+ .satisfies (obj -> assertThat (obj .getType ()).isEqualTo (GeoJsonType .LINE_STRING .getType ()))
38+ .extracting (LineString ::getCoordinates ).asList ().isNotEmpty ()
39+ .hasSize (2 )
40+ .containsExactly (Position .of (100.0 , 0.0 ), Position .of (101.0 , 1.0 ));
41+ }
42+
43+ @ Test
44+ void deserialization_withLongitudeAndLatitudeAndAltitude () throws IOException {
45+ String json = """
46+ {
47+ "type": "LineString",
48+ "coordinates": [
49+ [100.0, 0.0, 45.0],
50+ [101.0, 1.0, 45.0]
51+ ]
52+ }
53+ """ ;
54+ LineString object = jacksonTester .parseObject (json );
55+ assertThat (object ).isNotNull ()
56+ .satisfies (obj -> assertThat (obj .getType ()).isEqualTo (GeoJsonType .LINE_STRING .getType ()))
57+ .extracting (LineString ::getCoordinates ).asList ().isNotEmpty ()
58+ .hasSize (2 )
59+ .containsExactly (Position .of (100.0 , 0.0 , 45.0 ), Position .of (101.0 , 1.0 , 45.0 ));
60+ }
61+
62+ @ Test
63+ void deserialization_withLongitudeAndLatitudeAndAltitude_andWithManyCoordinates () throws IOException {
64+ String json = """
65+ {
66+ "type": "LineString",
67+ "coordinates": [
68+ [100.0, 51.0, 45.0],
69+ [101.0, 52.0, 45.0],
70+ [102.0, 53.0, 45.0],
71+ [103.0, 54.0, 45.0],
72+ [104.0, 55.0, 45.0],
73+ [105.0, 56.0, 45.0],
74+ [106.0, 57.0, 45.0],
75+ [107.0, 58.0, 45.0],
76+ [108.0, 59.0, 45.0],
77+ [109.0, 60.0, 45.0],
78+ [110.0, 61.0, 45.0]
79+ ]
80+ }
81+ """ ;
82+ LineString object = jacksonTester .parseObject (json );
83+ assertThat (object ).isNotNull ()
84+ .satisfies (obj -> assertThat (obj .getType ()).isEqualTo (GeoJsonType .LINE_STRING .getType ()))
85+ .extracting (LineString ::getCoordinates ).asList ().isNotEmpty ()
86+ .hasSize (11 )
87+ .containsExactly (
88+ Position .of (100.0 , 51.0 , 45.0 ),
89+ Position .of (101.0 , 52.0 , 45.0 ),
90+ Position .of (102.0 , 53.0 , 45.0 ),
91+ Position .of (103.0 , 54.0 , 45.0 ),
92+ Position .of (104.0 , 55.0 , 45.0 ),
93+ Position .of (105.0 , 56.0 , 45.0 ),
94+ Position .of (106.0 , 57.0 , 45.0 ),
95+ Position .of (107.0 , 58.0 , 45.0 ),
96+ Position .of (108.0 , 59.0 , 45.0 ),
97+ Position .of (109.0 , 60.0 , 45.0 ),
98+ Position .of (110.0 , 61.0 , 45.0 )
99+ );
100+ }
101+
102+ @ ParameterizedTest
103+ @ ValueSource (strings = {
104+ """
105+ {
106+ "type": "invalidType",
107+ "coordinates": [
108+ [100.0, 0.0, 45.0],
109+ [101.0, 1.0, 45.0]
110+ ]
111+ }
112+ """ , """
113+ {
114+ "type": "LineString",
115+ "coordinates": [
116+ [190.0, 0.0, 45.0],
117+ [101.0, 1.0, 45.0]
118+ ]
119+ }
120+ """ , """
121+ {
122+ "type": "LineString",
123+ "coordinates": [
124+ [90.0, 91.0, 45.0],
125+ [101.0, 1.0, 45.0]
126+ ]
127+ }
128+ """ , """
129+ {
130+ "type": "LineString",
131+ "coordinates": [
132+ [90.0, 45.0, 45.0]
133+ ]
134+ }
135+ """ , """
136+ {
137+ "type": "LineString",
138+ "coordinates": [
139+ [190.0, 90.0, 45.0],
140+ [101.0, 1.0, 45.0]
141+ ]
142+ }
143+ """ , """
144+ {
145+ "type": "LineString",
146+ "coordinates": [
147+ [90.0, 90.0, 45.0],
148+ [101.0, -91.0, 45.0]
149+ ]
150+ }
151+ """
152+ })
153+ void deserialization_withInvalidContent (String json ) {
154+ assertThrows (Exception .class , () -> jacksonTester .parseObject (json ));
155+ }
156+
157+ @ Test
158+ void serialisation_withCoordinates_shouldThrowError () {
159+ assertThrows (Exception .class , () -> new LineString (List .of ()));
160+ }
161+
162+ @ Test
163+ void serialisation_withOneCoordinates_shouldThrowError () {
164+ assertThrows (Exception .class , () -> new LineString (List .of (Position .of (180 , 90 ))));
165+ }
166+
167+ @ Test
168+ void serialisation_withValidCoordinates_shouldBeSuccess () throws IOException {
169+ LineString lineString = new LineString (List .of (Position .of (180 , 90 ), Position .of (-180 , -90 )));
170+ JsonContent <LineString > jsonContent = jacksonTester .write (lineString );
171+ assertThat (jsonContent ).isEqualToJson ("""
172+ {
173+ "type": "LineString",
174+ "coordinates": [
175+ [180.0, 90.0],
176+ [-180.0, -90.0]
177+ ]
178+ }
179+ """ );
180+ }
181+
182+ @ Test
183+ void serialisation_withValidCoordinates_andAltitude_shouldBeSuccess () throws IOException {
184+ LineString lineString = new LineString (List .of (Position .of (180 , 90 , 45 ), Position .of (-180 , -90 , 45 )));
185+ JsonContent <LineString > jsonContent = jacksonTester .write (lineString );
186+ assertThat (jsonContent ).isEqualToJson ("""
187+ {
188+ "type": "LineString",
189+ "coordinates": [
190+ [180.0, 90.0, 45.0],
191+ [-180.0, -90.0, 45.0]
192+ ]
193+ }
194+ """ );
195+ }
196+
197+ }
0 commit comments