1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery.vectortile.mapbox.style;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
9 |
|
---|
10 |
|
---|
11 | import java.util.Locale;
|
---|
12 | import java.util.stream.Collectors;
|
---|
13 | import java.util.stream.Stream;
|
---|
14 |
|
---|
15 | import javax.json.Json;
|
---|
16 | import javax.json.JsonObject;
|
---|
17 | import javax.json.JsonValue;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.Bounds;
|
---|
20 |
|
---|
21 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
22 | import org.junit.jupiter.api.Test;
|
---|
23 | import org.openstreetmap.josm.data.imagery.vectortile.mapbox.InvalidMapboxVectorTileException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Test class for {@link Source}
|
---|
27 | * @author Taylor Smock
|
---|
28 | * @since xxx
|
---|
29 | */
|
---|
30 | public class SourceTest {
|
---|
31 | @Test
|
---|
32 | void testEquals() {
|
---|
33 | EqualsVerifier.forClass(Source.class).usingGetClass().verify();
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Test
|
---|
37 | void testSimpleSources() {
|
---|
38 | final JsonObject emptyObject = Json.createObjectBuilder().build();
|
---|
39 | assertThrows(NullPointerException.class, () -> new Source("Test source", emptyObject));
|
---|
40 |
|
---|
41 | final JsonObject badTypeValue = Json.createObjectBuilder().add("type", "bad type value").build();
|
---|
42 | assertThrows(IllegalArgumentException.class, () -> new Source("Test source", badTypeValue));
|
---|
43 |
|
---|
44 | // Only SourceType.{VECTOR,RASTER} are supported
|
---|
45 | final SourceType[] supported = new SourceType[] {SourceType.VECTOR, SourceType.RASTER};
|
---|
46 | for (SourceType type : supported) {
|
---|
47 | final JsonObject goodSourceType = Json.createObjectBuilder().add("type", type.toString().toLowerCase(Locale.ROOT)).build();
|
---|
48 | Source source = assertDoesNotThrow(() -> new Source(type.name(), goodSourceType));
|
---|
49 | // Check defaults
|
---|
50 | assertEquals(0, source.getMinZoom());
|
---|
51 | assertEquals(22, source.getMaxZoom());
|
---|
52 | assertEquals(type.name(), source.getName());
|
---|
53 | assertNull(source.getAttributionText());
|
---|
54 | assertTrue(source.getUrls().isEmpty());
|
---|
55 | assertEquals(new Bounds(-85.051129, -180, 85.051129, 180), source.getBounds());
|
---|
56 | }
|
---|
57 |
|
---|
58 | // Check that unsupported types throw
|
---|
59 | for (SourceType type : Stream.of(SourceType.values()).filter(t -> Stream.of(supported).noneMatch(t::equals)).collect(
|
---|
60 | Collectors.toList())) {
|
---|
61 | final JsonObject goodSourceType = Json.createObjectBuilder().add("type", type.toString().toLowerCase(Locale.ROOT)).build();
|
---|
62 | assertThrows(UnsupportedOperationException.class, () -> new Source(type.name(), goodSourceType));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Test
|
---|
67 | void testTileJsonSpec() {
|
---|
68 | // This isn't currently implemented, so it should throw. Mostly here to remind implementor to add tests...
|
---|
69 | final JsonObject tileJsonSpec = Json.createObjectBuilder()
|
---|
70 | .add("type", SourceType.VECTOR.name()).add("url", "some-random-url.com")
|
---|
71 | .build();
|
---|
72 | assertThrows(InvalidMapboxVectorTileException.class, () -> new Source("Test TileJson", tileJsonSpec));
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Test
|
---|
76 | void testBounds() {
|
---|
77 | // Check a "good" bounds
|
---|
78 | final JsonObject tileJsonSpec = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("bounds",
|
---|
79 | Json.createArrayBuilder().add(-1).add(-2).add(3).add(4)).build();
|
---|
80 | Source source = new Source("Test Bounds[-1, -2, 3, 4]", tileJsonSpec);
|
---|
81 | assertEquals(new Bounds(-2, -1, 4, 3), source.getBounds());
|
---|
82 |
|
---|
83 | // Check "bad" bounds
|
---|
84 | final JsonObject tileJsonSpecShort = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("bounds",
|
---|
85 | Json.createArrayBuilder().add(-1).add(-2).add(3)).build();
|
---|
86 | IllegalArgumentException badLengthException = assertThrows(IllegalArgumentException.class,
|
---|
87 | () -> new Source("Test Bounds[-1, -2, 3]", tileJsonSpecShort));
|
---|
88 | assertEquals("bounds must have four values, but has 3", badLengthException.getMessage());
|
---|
89 |
|
---|
90 | final JsonObject tileJsonSpecLong = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("bounds",
|
---|
91 | Json.createArrayBuilder().add(-1).add(-2).add(3).add(4).add(5)).build();
|
---|
92 | badLengthException = assertThrows(IllegalArgumentException.class, () -> new Source("Test Bounds[-1, -2, 3, 4, 5]", tileJsonSpecLong));
|
---|
93 | assertEquals("bounds must have four values, but has 5", badLengthException.getMessage());
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Test
|
---|
97 | void testTiles() {
|
---|
98 | // No url
|
---|
99 | final JsonObject tileJsonSpecEmpty = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("tiles",
|
---|
100 | JsonValue.NULL).build();
|
---|
101 | Source source = new Source("Test Tile[]", tileJsonSpecEmpty);
|
---|
102 | assertTrue(source.getUrls().isEmpty());
|
---|
103 |
|
---|
104 | // Create a tile URL
|
---|
105 | final JsonObject tileJsonSpec = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("tiles",
|
---|
106 | Json.createArrayBuilder().add("https://example.org/{bbox-epsg-3857}")).build();
|
---|
107 | source = new Source("Test Tile[https://example.org/{bbox-epsg-3857}]", tileJsonSpec);
|
---|
108 | assertEquals(1, source.getUrls().size());
|
---|
109 | // Make certain that {bbox-epsg-3857} is replaced with the JOSM equivalent
|
---|
110 | assertEquals("https://example.org/{bbox}", source.getUrls().get(0));
|
---|
111 |
|
---|
112 | // Check with invalid data
|
---|
113 | final JsonObject tileJsonSpecBad = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("tiles",
|
---|
114 | Json.createArrayBuilder().add(1).add("https://example.org/{bbox-epsg-3857}").add(false).add(Json.createArrayBuilder().add("hello"))
|
---|
115 | .add(Json.createObjectBuilder().add("bad", "array"))).build();
|
---|
116 | source = new Source("Test Tile[1, https://example.org/{bbox-epsg-3857}, false, [\"hello\"], {\"bad\": \"array\"}]", tileJsonSpecBad);
|
---|
117 | assertEquals(1, source.getUrls().size());
|
---|
118 | // Make certain that {bbox-epsg-3857} is replaced with the JOSM equivalent
|
---|
119 | assertEquals("https://example.org/{bbox}", source.getUrls().get(0));
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Test
|
---|
123 | void testZoom() {
|
---|
124 | // Min zoom
|
---|
125 | final JsonObject minZoom5 = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("minzoom",
|
---|
126 | 5).build();
|
---|
127 | Source source = new Source("Test Zoom[minzoom=5]", minZoom5);
|
---|
128 | assertEquals(5, source.getMinZoom());
|
---|
129 | assertEquals(22, source.getMaxZoom());
|
---|
130 |
|
---|
131 | // Negative min zoom
|
---|
132 | final JsonObject minZoomNeg1 = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("minzoom",
|
---|
133 | -1).build();
|
---|
134 | source = new Source("Test Zoom[minzoom=-1]", minZoomNeg1);
|
---|
135 | assertEquals(0, source.getMinZoom());
|
---|
136 | assertEquals(22, source.getMaxZoom());
|
---|
137 |
|
---|
138 | // Max zoom
|
---|
139 | final JsonObject maxZoom5 = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("maxzoom",
|
---|
140 | 5).build();
|
---|
141 | source = new Source("Test Zoom[maxzoom=5]", maxZoom5);
|
---|
142 | assertEquals(0, source.getMinZoom());
|
---|
143 | assertEquals(5, source.getMaxZoom());
|
---|
144 |
|
---|
145 | // Big Max zoom
|
---|
146 | final JsonObject maxZoom31 = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("maxzoom",
|
---|
147 | 31).build();
|
---|
148 | source = new Source("Test Zoom[maxzoom=31]", maxZoom31);
|
---|
149 | assertEquals(0, source.getMinZoom());
|
---|
150 | assertEquals(30, source.getMaxZoom());
|
---|
151 |
|
---|
152 | // Negative max zoom
|
---|
153 | final JsonObject maxZoomNeg5 = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("maxzoom",
|
---|
154 | -5).build();
|
---|
155 | source = new Source("Test Zoom[maxzoom=-5]", maxZoomNeg5);
|
---|
156 | assertEquals(0, source.getMinZoom());
|
---|
157 | assertEquals(0, source.getMaxZoom());
|
---|
158 |
|
---|
159 | // Min max zoom
|
---|
160 | final JsonObject minZoom1MaxZoom5 = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("maxzoom",
|
---|
161 | 5).add("minzoom", 1).build();
|
---|
162 | source = new Source("Test Zoom[minzoom=1,maxzoom=5]", minZoom1MaxZoom5);
|
---|
163 | assertEquals(1, source.getMinZoom());
|
---|
164 | assertEquals(5, source.getMaxZoom());
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Test
|
---|
168 | void testToString() {
|
---|
169 | // Simple (no urls)
|
---|
170 | final JsonObject noTileJsonSpec = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).build();
|
---|
171 | Source source = new Source("Test String[]", noTileJsonSpec);
|
---|
172 | assertEquals("Test String[]", source.toString());
|
---|
173 |
|
---|
174 | // With one url
|
---|
175 | final JsonObject tileJsonSpec = Json.createObjectBuilder().add("type", SourceType.VECTOR.name()).add("tiles",
|
---|
176 | Json.createArrayBuilder().add("https://example.org/{bbox-epsg-3857}")).build();
|
---|
177 | source = new Source("Test String[https://example.org/{bbox-epsg-3857}]", tileJsonSpec);
|
---|
178 | assertEquals("Test String[https://example.org/{bbox-epsg-3857}] https://example.org/{bbox}", source.toString());
|
---|
179 |
|
---|
180 | // With two URLs
|
---|
181 | final JsonObject tileJsonSpecMultiple = Json.createObjectBuilder().add("type", SourceType.VECTOR.name())
|
---|
182 | .add("tiles", Json.createArrayBuilder()
|
---|
183 | .add("https://example.org/{bbox-epsg-3857}")
|
---|
184 | .add("https://example.com/{bbox-epsg-3857}")).build();
|
---|
185 | source = new Source("Test String[https://example.org/{bbox-epsg-3857},https://example.com/{bbox-epsg-3857}]", tileJsonSpecMultiple);
|
---|
186 | assertEquals("Test String[https://example.org/{bbox-epsg-3857},https://example.com/{bbox-epsg-3857}] https://example.org/{bbox} "
|
---|
187 | + "https://example.com/{bbox}", source.toString());
|
---|
188 | }
|
---|
189 | }
|
---|