|
| 1 | +/* |
| 2 | + * Copyright 2015-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.geo; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.data.geo.Point; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Bjorn Harvold |
| 31 | + */ |
| 32 | +public class GeoJsonModuleRoundTripUnitTests { |
| 33 | + |
| 34 | + ObjectMapper mapper; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp() { |
| 38 | + |
| 39 | + mapper = new ObjectMapper(); |
| 40 | + mapper.registerModule(new GeoJsonModule()); |
| 41 | + mapper.registerModule(new GeoJsonSerializersModule()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldMarshalJsonPointCorrectly() throws IOException { |
| 46 | + |
| 47 | + String json = "{\"type\":\"Point\",\"coordinates\":[10.0,20.0]}"; |
| 48 | + GeoJsonPoint point = new GeoJsonPoint(10D, 20D); |
| 49 | + |
| 50 | + assertThat(mapper.readValue(json, GeoJsonPoint.class)).isEqualTo(point); |
| 51 | + |
| 52 | + String backToJSON = mapper.writeValueAsString(point); |
| 53 | + |
| 54 | + assertThat(backToJSON).isEqualTo(json); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void shouldMarshalGeoJsonLineStringCorrectly() |
| 59 | + throws IOException { |
| 60 | + |
| 61 | + String json = "{\"type\":\"LineString\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}"; |
| 62 | + |
| 63 | + GeoJsonLineString lineString = new GeoJsonLineString(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60))); |
| 64 | + |
| 65 | + assertThat(mapper.readValue(json, GeoJsonLineString.class)).isEqualTo(lineString); |
| 66 | + |
| 67 | + String backToJSON = mapper.writeValueAsString(lineString); |
| 68 | + |
| 69 | + assertThat(backToJSON).isEqualTo(json); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void shouldMarshalGeoJsonMultiPointCorrectly() throws IOException { |
| 74 | + |
| 75 | + String json = "{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}"; |
| 76 | + GeoJsonMultiPoint multiPoint = new GeoJsonMultiPoint(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60))); |
| 77 | + |
| 78 | + assertThat(mapper.readValue(json, GeoJsonMultiPoint.class)).isEqualTo(multiPoint); |
| 79 | + |
| 80 | + String backToJSON = mapper.writeValueAsString(multiPoint); |
| 81 | + |
| 82 | + assertThat(backToJSON).isEqualTo(json); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + public void shouldMarshalGeoJsonMultiLineStringCorrectly() throws IOException { |
| 88 | + |
| 89 | + String json = "{\"type\":\"MultiLineString\",\"coordinates\":[[[10.0,20.0],[30.0,40.0]],[[50.0,60.0],[70.0,80.0]]]}"; |
| 90 | + GeoJsonMultiLineString multiLineString = new GeoJsonMultiLineString(Arrays.asList(new Point(10, 20), new Point(30, 40)), Arrays.asList(new Point(50, 60), new Point(70, 80))); |
| 91 | + |
| 92 | + assertThat(mapper.readValue(json, GeoJsonMultiLineString.class)).isEqualTo(multiLineString); |
| 93 | + |
| 94 | + String backToJSON = mapper.writeValueAsString(multiLineString); |
| 95 | + |
| 96 | + assertThat(backToJSON).isEqualTo(json); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void shouldMarshalGeoJsonPolygonCorrectly() throws IOException { |
| 101 | + String json = "{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}"; |
| 102 | + |
| 103 | + List<Point> points = Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0)); |
| 104 | + GeoJsonPolygon polygon = new GeoJsonPolygon(points); |
| 105 | + |
| 106 | + assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(polygon); |
| 107 | + |
| 108 | + String backToJSON = mapper.writeValueAsString(polygon); |
| 109 | + |
| 110 | + assertThat(backToJSON).isEqualTo(json); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void shouldMarshalGeoJsonMultiPolygonCorrectly() |
| 115 | + throws IOException { |
| 116 | + |
| 117 | + String json = "{\"type\":\"MultiPolygon\",\"coordinates\":[" |
| 118 | + + "[" |
| 119 | + + "[" |
| 120 | + + "[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]" |
| 121 | + + "]" |
| 122 | + + "]," |
| 123 | + + "[" |
| 124 | + + "[" |
| 125 | + + "[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]" |
| 126 | + + "]" |
| 127 | + + "]," |
| 128 | + + "[" |
| 129 | + + "[" |
| 130 | + + "[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]" |
| 131 | + + "]" |
| 132 | + + "]" |
| 133 | + + "]" |
| 134 | + + "}"; |
| 135 | + |
| 136 | + GeoJsonMultiPolygon multiPolygon = new GeoJsonMultiPolygon(Arrays.asList( |
| 137 | + new GeoJsonPolygon(Arrays.asList(new Point(102, 2), new Point(103, 2), new Point(103, 3), new Point(102, 3), |
| 138 | + new Point(102, 2))), |
| 139 | + new GeoJsonPolygon(Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), |
| 140 | + new Point(100, 0))), |
| 141 | + new GeoJsonPolygon(Arrays.asList(new Point(100.2, 0.2), new Point(100.8, 0.2), new Point(100.8, 0.8), |
| 142 | + new Point(100.2, 0.8), new Point(100.2, 0.2))))); |
| 143 | + |
| 144 | + assertThat(mapper.readValue(json, GeoJsonMultiPolygon.class)).isEqualTo(multiPolygon); |
| 145 | + |
| 146 | + String backToJSON = mapper.writeValueAsString(multiPolygon); |
| 147 | + |
| 148 | + assertThat(backToJSON).isEqualTo(json); |
| 149 | + |
| 150 | + } |
| 151 | +} |
0 commit comments