2727import java .io .File ;
2828import java .io .IOException ;
2929import java .io .OutputStream ;
30+ import java .io .UnsupportedEncodingException ;
3031import java .net .URL ;
3132import java .util .ArrayList ;
3233import java .util .Arrays ;
7374import org .geowebcache .layer .TileLayer ;
7475import org .geowebcache .layer .TileLayerDispatcher ;
7576import org .geowebcache .layer .meta .MetadataURL ;
77+ import org .geowebcache .layer .meta .TileJSON ;
78+ import org .geowebcache .layer .meta .VectorLayerMetadata ;
79+ import org .geowebcache .mime .ApplicationMime ;
80+ import org .geowebcache .mime .ImageMime ;
7681import org .geowebcache .mime .MimeType ;
7782import org .geowebcache .mime .XMLMime ;
7883import org .geowebcache .service .OWSException ;
@@ -236,15 +241,11 @@ private TileLayer mockTileLayerWithJSONSupport(String layerName, List<String> gr
236241 when (tileLayer .isEnabled ()).thenReturn (true );
237242 when (tileLayer .isAdvertised ()).thenReturn (true );
238243
239- final MimeType mimeType1 = MimeType .createFromFormat ("image/png" );
240- final MimeType mimeType2 = MimeType .createFromFormat ("image/jpeg" );
241- when (tileLayer .getMimeTypes ()).thenReturn (Arrays .asList (mimeType1 , mimeType2 ));
244+ final MimeType mimeType1 = ImageMime .png ;
245+ final MimeType mimeType2 = ApplicationMime .mapboxVector ;
242246
243- final MimeType infoMimeType1 = MimeType .createFromFormat ("text/plain" );
244- final MimeType infoMimeType2 = MimeType .createFromFormat ("text/html" );
245- final MimeType infoMimeType3 = MimeType .createFromFormat ("application/vnd.ogc.gml" );
246- when (tileLayer .getInfoMimeTypes ())
247- .thenReturn (Arrays .asList (infoMimeType1 , infoMimeType2 , infoMimeType3 ));
247+ // Mocking a tile layer supporting both png (raster) and mvt (vector)
248+ when (tileLayer .getMimeTypes ()).thenReturn (Arrays .asList (mimeType1 , mimeType2 ));
248249
249250 Map <String , GridSubset > subsets = new HashMap <>();
250251 Map <SRS , List <GridSubset >> bySrs = new HashMap <>();
@@ -276,6 +277,11 @@ private TileLayer mockTileLayerWithJSONSupport(String layerName, List<String> gr
276277 when (tileLayer .getGridSubsets ()).thenReturn (subsets .keySet ());
277278 TileJSONProvider tileJSONProvider = (TileJSONProvider ) tileLayer ;
278279 when (tileJSONProvider .supportsTileJSON ()).thenReturn (true );
280+ TileJSON json = new TileJSON ();
281+ VectorLayerMetadata metadata = new VectorLayerMetadata ();
282+ metadata .setFields (Collections .singletonMap ("FIELD" , "TYPE" ));
283+ json .setLayers (Collections .singletonList (metadata ));
284+ when (tileJSONProvider .getTileJSON ()).thenReturn (json );
279285
280286 // sanity check
281287 for (String gsetName : gridSetNames ) {
@@ -1489,7 +1495,7 @@ public void testGetFeatureSecure() throws Exception {
14891495 }
14901496
14911497 @ Test
1492- public void testGetCapWithTileJSON () throws Exception {
1498+ public void testGetCapWithTileJSONDifferentUrls () throws Exception {
14931499
14941500 GeoWebCacheDispatcher gwcd = mock (GeoWebCacheDispatcher .class );
14951501 when (gwcd .getServletPrefix ()).thenReturn (null );
@@ -1507,12 +1513,9 @@ public void testGetCapWithTileJSON() throws Exception {
15071513 when (req .getCharacterEncoding ()).thenReturn ("UTF-8" );
15081514 when (req .getParameterMap ()).thenReturn (kvp );
15091515
1510- {
1511- List <String > gridSetNames = Arrays .asList ("EPSG:900913" );
1512-
1513- TileLayer tileLayer = mockTileLayerWithJSONSupport ("mockLayer" , gridSetNames );
1514- when (tld .getLayerList ()).thenReturn (Collections .singletonList (tileLayer ));
1515- }
1516+ List <String > gridSetNames = Arrays .asList ("EPSG:900913" );
1517+ TileLayer tileLayer = mockTileLayerWithJSONSupport ("mockLayer" , gridSetNames );
1518+ when (tld .getLayerList ()).thenReturn (Collections .singletonList (tileLayer ));
15161519
15171520 Conveyor conv = service .getConveyor (req , resp );
15181521 assertNotNull (conv );
@@ -1535,7 +1538,6 @@ public void testGetCapWithTileJSON() throws Exception {
15351538 "inline;filename=wmts-getcapabilities.xml" , resp .getHeader ("content-disposition" ));
15361539 String result = resp .getContentAsString ();
15371540
1538- // Ensure the advertised Layer is contained and the unadvertised not
15391541 assertTrue (result .contains ("mockLayer" ));
15401542
15411543 Document doc = XMLUnit .buildTestDocument (result );
@@ -1557,10 +1559,49 @@ public void testGetCapWithTileJSON() throws Exception {
15571559 "1" ,
15581560 xpath .evaluate (
15591561 "count(//wmts:Contents/wmts:Layer/wmts:ResourceURL[@resourceType='TileJSON']"
1560- + "[@format='json']"
1562+ + "[@format='application/ json']"
15611563 + "[@template='http://localhost:8080/geowebcache"
15621564 + WMTSService .REST_PATH
1563- + "/mockLayer/{style}/tilejson?format=json'])" ,
1565+ + "/mockLayer/{style}/tilejson/png ?format=application/ json'])" ,
15641566 doc ));
1567+
1568+ assertEquals (
1569+ "1" ,
1570+ xpath .evaluate (
1571+ "count(//wmts:Contents/wmts:Layer/wmts:ResourceURL[@resourceType='TileJSON']"
1572+ + "[@format='application/json']"
1573+ + "[@template='http://localhost:8080/geowebcache"
1574+ + WMTSService .REST_PATH
1575+ + "/mockLayer/{style}/tilejson/pbf?format=application/json'])" ,
1576+ doc ));
1577+
1578+ // Sending the above 2 getTileJson urls
1579+ // Vector tilejson
1580+ kvp .put ("request" , new String [] {"GetTileJSON" });
1581+ kvp .put ("tileformat" , new String [] {"pbf" });
1582+ when (req .getParameterMap ()).thenReturn (kvp );
1583+ conv = service .getConveyor (req , resp );
1584+ result = writeTileJsonResponse ((ConveyorTile ) conv , tileLayer , resp );
1585+ assertTrue (
1586+ result .contains (
1587+ "\" tiles\" :[\" http://localhost/service/wmts/rest/mockLayer/EPSG:900913/EPSG:900913:{z}/{y}/{x}?format=application/vnd.mapbox-vector-tile\" ]" ));
1588+
1589+ // Raster tilejson
1590+ kvp .put ("tileformat" , new String [] {"png" });
1591+ when (req .getParameterMap ()).thenReturn (kvp );
1592+ conv = service .getConveyor (req , resp );
1593+ result = writeTileJsonResponse ((ConveyorTile ) conv , tileLayer , resp );
1594+ assertTrue (
1595+ result .contains (
1596+ "\" tiles\" :[\" http://localhost/service/wmts/rest/mockLayer/EPSG:900913/EPSG:900913:{z}/{y}/{x}?format=image/png\" ]" ));
1597+ }
1598+
1599+ private String writeTileJsonResponse (
1600+ ConveyorTile conv , TileLayer tileLayer , MockHttpServletResponse resp )
1601+ throws UnsupportedEncodingException {
1602+ WMTSTileJSON tileJSON =
1603+ new WMTSTileJSON (conv , "http://localhost" , "" , null , new NullURLMangler ());
1604+ tileJSON .writeResponse (tileLayer );
1605+ return resp .getContentAsString ();
15651606 }
15661607}
0 commit comments