Skip to content

Commit 05de2ea

Browse files
author
Ramachandran Nellaiyappan
committed
[feat] #7 geojson data modeling
- Point.java added with tests - Position updated with new method to get coordinates as array -
1 parent 9dc5497 commit 05de2ea

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.nramc.dev.journey.api.geojson;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.github.nramc.dev.journey.api.geojson.types.GeoJsonType;
5+
import com.github.nramc.dev.journey.api.geojson.types.Position;
6+
import lombok.Getter;
7+
8+
@Getter
9+
public final class Point extends Geometry {
10+
private final Position coordinates;
11+
12+
public Point(Position coordinates) {
13+
super(GeoJsonType.POINT);
14+
this.coordinates = coordinates;
15+
}
16+
17+
@JsonCreator
18+
public static Point of(GeoJsonType type, Position coordinates) {
19+
if (type != GeoJsonType.POINT) {
20+
throw new IllegalArgumentException("Invalid type. 'Point' expected");
21+
}
22+
if (coordinates == null) {
23+
throw new IllegalArgumentException("Invalid coordinates. 'coordinates' can not be empty or null");
24+
}
25+
return new Point(coordinates);
26+
}
27+
28+
public static Point of(Position position) {
29+
return new Point(position);
30+
}
31+
32+
}

src/main/java/com/github/nramc/dev/journey/api/geojson/types/Position.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ public Position(double longitude, double latitude, double altitude) {
2626
this.values = new double[]{longitude, latitude, altitude};
2727
}
2828

29+
public static Position of(double longitude, double latitude) {
30+
return new Position(longitude, latitude);
31+
}
32+
33+
public static Position of(double longitude, double latitude, double altitude) {
34+
return new Position(longitude, latitude, altitude);
35+
}
36+
37+
public double[] getCoordinates() {
38+
return this.values;
39+
}
40+
2941
public double getLongitude() {
3042
return values.length > 0 ? values[0] : Double.NaN;
3143
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)