1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.geojson;
18
19 import static org.assertj.core.api.Assertions.assertThat;
20
21 import java.util.List;
22 import java.util.Map;
23 import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.locationtech.jts.geom.Geometry;
27 import org.locationtech.jts.geom.GeometryCollection;
28 import org.locationtech.jts.geom.LineString;
29 import org.locationtech.jts.geom.MultiLineString;
30 import org.locationtech.jts.geom.MultiPoint;
31 import org.locationtech.jts.geom.MultiPolygon;
32 import org.locationtech.jts.geom.Point;
33 import org.locationtech.jts.geom.Polygon;
34 import tools.jackson.core.type.TypeReference;
35 import tools.jackson.databind.json.JsonMapper;
36
37
38
39
40
41
42 @ExtendWith(SoftAssertionsExtension.class)
43 class GeoJsonObjectMapperModuleTest {
44
45 private static final GeoJsonGeometryFactory geometryFactory = new GeoJsonGeometryFactory();
46
47 private static final JsonMapper target = JsonMapper.builder()
48 .addModules(new GeoJsonObjectMapperModule(geometryFactory, true, true))
49 .build();
50
51
52
53
54 @Test
55 void mapPoint() {
56 var expected = geometryFactory.createPoint(1.234, 5.6789);
57 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
58 System.out.println(json);
59
60 var actual = target.readValue(json, Point.class);
61
62 assertThat(actual).isEqualTo(expected);
63 }
64
65
66
67
68 @Test
69 void mapLineString() {
70 var expected = geometryFactory.createGeometryFromWellKnownText(
71 "LINESTRING (0 0, 0 1, 1 1, 1 0, 0 0)");
72 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
73 System.out.println(json);
74
75 var actual = target.readValue(json, LineString.class);
76
77 assertThat(actual).isEqualTo(expected);
78 }
79
80
81
82
83 @Test
84 void mapPolygon() {
85 var expected = geometryFactory.createGeometryFromWellKnownText(
86 "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))");
87 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
88 System.out.println(json);
89
90 var actual = target.readValue(json, Polygon.class);
91
92 assertThat(actual).isEqualTo(expected);
93 }
94
95
96
97
98 @Test
99 void mapMultiPoint() {
100 var expected = geometryFactory.createGeometryFromWellKnownText(
101 "MULTIPOINT ((1 2), (6 7))");
102 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
103 System.out.println(json);
104
105 var actual = target.readValue(json, MultiPoint.class);
106
107 assertThat(actual).isEqualTo(expected);
108 }
109
110
111
112
113 @Test
114 void mapMultiLineString() {
115 var expected = geometryFactory.createGeometryFromWellKnownText(
116 "MULTILINESTRING ((0 0, 0 1, 1 1, 1 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2))");
117 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
118 System.out.println(json);
119
120 var actual = target.readValue(json, MultiLineString.class);
121
122 assertThat(actual).isEqualTo(expected);
123 }
124
125
126
127
128 @Test
129 void mapMultiPolygon() {
130 var expected = geometryFactory.createGeometryFromWellKnownText(
131 "MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)), ((0 0, 0 1, 1 1, 1 0, 0 0), "
132 + "(0.1 0.1, 0.1 0.2, 0.2 0.2, 0.2 0.1, 0.1 0.1), "
133 + "(0.8 0.8, 0.8 0.9, 0.9 0.9, 0.9 0.8, 0.8 0.8)))");
134 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
135 System.out.println(json);
136
137 var actual = target.readValue(json, MultiPolygon.class);
138
139 assertThat(actual).isEqualTo(expected);
140 }
141
142
143
144
145 @Test
146 void mapGeometryCollection() {
147 var expected = geometryFactory.createGeometryFromWellKnownText(
148 "GEOMETRYCOLLECTION (POINT (-17.8 -6.001), LINESTRING (0 0, 0 1, 1 1, 1 0, 0 0), "
149 + "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0)), MULTIPOINT ((5.5 -3.3)), "
150 + "MULTILINESTRING ((0 0, 0 1, 1 1, 1 0, 0 0), (2 2, 2 4, 4 4, 4 2, 2 2)), "
151 + "MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)), ((0 0, 0 1, 1 1, 1 0, 0 0), "
152 + "(0.1 0.1, 0.1 0.2, 0.2 0.2, 0.2 0.1, 0.1 0.1), "
153 + "(0.8 0.8, 0.8 0.9, 0.9 0.9, 0.9 0.8, 0.8 0.8))), "
154 + "GEOMETRYCOLLECTION (POINT (17.8 6.001)))");
155 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
156 System.out.println(json);
157
158 var actual = target.readValue(json, GeometryCollection.class);
159 assertThat(GeoJsonGeometryFactory.equals(actual, expected))
160 .isTrue();
161 }
162
163
164
165
166 @Test
167 void mapFeature() {
168 var lineString = geometryFactory.createGeometryFromWellKnownText(
169 "LINESTRING (0 0, 0 1, 1 1, 1 0, 0 0)");
170 var expected = new GeoJsonFeature<Geometry, Map<String, Object>>(
171 "987",
172 lineString,
173 true,
174 Map.of("foo", "bar"));
175 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
176 System.out.println(json);
177 GeoJsonFeature<Geometry, Map<String, Object>> actual = target
178 .readValue(json, new TypeReference<>() {
179 });
180 assertThat(actual)
181 .isEqualTo(expected);
182 }
183
184
185
186
187 @Test
188 void mapFeatureCollection() {
189 var lineString = geometryFactory.createGeometryFromWellKnownText(
190 "LINESTRING (0 0, 0 1, 1 1, 1 0, 0 0)");
191 var f0 = new GeoJsonFeature<Geometry, Object>(
192 "986",
193 lineString,
194 false,
195 Map.of("foo", "bar"));
196 var f1 = new GeoJsonFeature<Geometry, Object>(
197 "987",
198 lineString,
199 false,
200 Map.of("foo", "bar"));
201 var expected = new GeoJsonFeatureCollection<>(List.of(f0, f1), true);
202 var json = target.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
203 System.out.println(json);
204 GeoJsonFeatureCollection<Geometry, Object> actual = target
205 .readValue(json, new TypeReference<>() {
206 });
207 assertThat(actual)
208 .isEqualTo(expected);
209 }
210
211 }