|
18 | 18 | import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; |
19 | 19 | import static org.junit.Assert.assertArrayEquals; |
20 | 20 | import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertTrue; |
21 | 22 | import static org.mockito.ArgumentMatchers.any; |
22 | 23 | import static org.mockito.ArgumentMatchers.anyInt; |
23 | 24 | import static org.mockito.ArgumentMatchers.eq; |
24 | 25 | import static org.mockito.Mockito.mock; |
25 | 26 | import static org.mockito.Mockito.when; |
| 27 | +import static org.mockito.Mockito.withSettings; |
26 | 28 |
|
27 | 29 | import java.io.File; |
28 | 30 | import java.io.IOException; |
|
33 | 35 | import java.util.HashMap; |
34 | 36 | import java.util.List; |
35 | 37 | import java.util.Map; |
| 38 | +import java.util.Objects; |
36 | 39 | import org.apache.commons.httpclient.HttpStatus; |
37 | 40 | import org.apache.commons.io.FileUtils; |
38 | 41 | import org.custommonkey.xmlunit.SimpleNamespaceContext; |
39 | 42 | import org.custommonkey.xmlunit.XMLUnit; |
| 43 | +import org.geowebcache.GeoWebCacheException; |
40 | 44 | import org.geowebcache.config.DefaultGridsets; |
41 | 45 | import org.geowebcache.config.ServerConfiguration; |
42 | 46 | import org.geowebcache.config.XMLGridSubset; |
|
53 | 57 | import org.geowebcache.grid.SRS; |
54 | 58 | import org.geowebcache.io.ByteArrayResource; |
55 | 59 | import org.geowebcache.io.Resource; |
| 60 | +import org.geowebcache.layer.TileJSONProvider; |
56 | 61 | import org.geowebcache.layer.TileLayer; |
57 | 62 | import org.geowebcache.layer.TileLayerDispatcher; |
| 63 | +import org.geowebcache.layer.meta.TileJSON; |
58 | 64 | import org.geowebcache.mime.MimeType; |
59 | 65 | import org.geowebcache.service.OWSException; |
60 | 66 | import org.geowebcache.stats.RuntimeStats; |
@@ -153,12 +159,89 @@ public void testGetTileWithStyle() throws Exception { |
153 | 159 | assertArrayEquals(getSampleTileContent().getContents(), resp.getContentAsByteArray()); |
154 | 160 | } |
155 | 161 |
|
| 162 | + @Test |
| 163 | + public void testGetTileJSONWithStyle() throws Exception { |
| 164 | + addTileLayerJsonMock(); |
| 165 | + |
| 166 | + MockHttpServletRequest req = new MockHttpServletRequest(); |
| 167 | + req.setPathInfo("geowebcache/service/wmts/rest/mockLayerTileJSON/style-a/tilejson"); |
| 168 | + req.addParameter("format", "json"); |
| 169 | + MockHttpServletResponse resp = dispatch(req); |
| 170 | + |
| 171 | + assertEquals(200, resp.getStatus()); |
| 172 | + String content = resp.getContentAsString(); |
| 173 | + |
| 174 | + // Checking the response contains a tileUrl with the style |
| 175 | + assertTrue( |
| 176 | + content.contains( |
| 177 | + "\"tiles\":[\"http://localhost/service/wmts/rest/mockLayerTileJSON/style-a/EPSG:900913/EPSG:900913:{z}/{y}/{x}?format=image/png\"]")); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testGetTileJSONWithoutStyle() throws Exception { |
| 182 | + addTileLayerJsonMock(); |
| 183 | + |
| 184 | + MockHttpServletRequest req = new MockHttpServletRequest(); |
| 185 | + req.setPathInfo("geowebcache/service/wmts/rest/mockLayerTileJSON/tilejson"); |
| 186 | + req.addParameter("format", "json"); |
| 187 | + MockHttpServletResponse resp = dispatch(req); |
| 188 | + |
| 189 | + assertEquals(200, resp.getStatus()); |
| 190 | + String content = resp.getContentAsString(); |
| 191 | + |
| 192 | + // Checking the response contains a tileUrl without the style |
| 193 | + assertTrue( |
| 194 | + content.contains( |
| 195 | + "\"tiles\":[\"http://localhost/service/wmts/rest/mockLayerTileJSON/EPSG:900913/EPSG:900913:{z}/{y}/{x}?format=image/png\"]")); |
| 196 | + } |
| 197 | + |
| 198 | + private void addTileLayerJsonMock() throws GeoWebCacheException { |
| 199 | + |
| 200 | + final MimeType mimeType1 = MimeType.createFromFormat("image/png"); |
| 201 | + |
| 202 | + String layerNameJson = "mockLayerTileJSON"; |
| 203 | + TileLayer tileLayerJson = |
| 204 | + mock(TileLayer.class, withSettings().extraInterfaces(TileJSONProvider.class)); |
| 205 | + when(tileLayerDispatcher.getTileLayer(eq(layerNameJson))).thenReturn(tileLayerJson); |
| 206 | + when(tileLayerJson.getName()).thenReturn(layerNameJson); |
| 207 | + when(tileLayerJson.isEnabled()).thenReturn(true); |
| 208 | + when(tileLayerJson.isAdvertised()).thenReturn(true); |
| 209 | + when(tileLayerJson.getMimeTypes()).thenReturn(Arrays.asList(mimeType1)); |
| 210 | + TileJSONProvider tileJSONProvider = (TileJSONProvider) tileLayerJson; |
| 211 | + when(tileJSONProvider.supportsTileJSON()).thenReturn(true); |
| 212 | + when(tileJSONProvider.getTileJSON()).thenReturn(new TileJSON()); |
| 213 | + |
| 214 | + String googleMercator = "EPSG:900913"; |
| 215 | + when(tileLayerJson.getGridSubsets()).thenReturn(Collections.singleton(googleMercator)); |
| 216 | + GridSubset subset = mock(GridSubset.class); |
| 217 | + when(subset.getGridNames()).thenReturn(new String[] {googleMercator + ":1"}); |
| 218 | + when(tileLayerJson.getGridSubset(eq(googleMercator))).thenReturn(subset); |
| 219 | + |
| 220 | + when(tileLayerDispatcher.getLayerList()).thenReturn(Arrays.asList(tileLayerJson)); |
| 221 | + } |
| 222 | + |
156 | 223 | public MockHttpServletResponse dispatch(MockHttpServletRequest req) throws Exception { |
157 | 224 | MockHttpServletResponse resp = new MockHttpServletResponse(); |
158 | 225 | try { |
159 | 226 | final Conveyor conveyor = wmtsService.getConveyor(req, resp); |
160 | 227 |
|
161 | 228 | if (conveyor.reqHandler == Conveyor.RequestHandler.SERVICE) { |
| 229 | + final String layerName = conveyor.getLayerId(); |
| 230 | + |
| 231 | + final TileLayer layer; |
| 232 | + if (Objects.nonNull(layerName)) { |
| 233 | + layer = tileLayerDispatcher.getTileLayer(layerName); |
| 234 | + if (layer != null && !layer.isEnabled()) { |
| 235 | + throw new OWSException( |
| 236 | + 400, |
| 237 | + "InvalidParameterValue", |
| 238 | + "LAYERS", |
| 239 | + "Layer '" + layerName + "' is disabled"); |
| 240 | + } |
| 241 | + if (conveyor instanceof ConveyorTile) { |
| 242 | + ((ConveyorTile) conveyor).setTileLayer(layer); |
| 243 | + } |
| 244 | + } |
162 | 245 | wmtsService.handleRequest(conveyor); |
163 | 246 | } else { |
164 | 247 | ResponseUtils.writeTile( |
|
0 commit comments