LayerFactory { protected WmsLayerConfig getLayerConfigFromWmsCapabilities(List<WmsLayer> wmsLayers) { WmsLayerConfig wmsLayerConfig = new WmsLayerConfig(); WmsCapabilities wmsCapabilities = wmsLayers.get(0).getCapability().getCapabilities(); String version = wmsCapabilities.getVersion(); if (version.equals("1.3.0")) { wmsLayerConfig.wmsVersion = version; } else if (version.equals("1.1.1")) { wmsLayerConfig.wmsVersion = version; } else { throw new RuntimeException( Logger.makeMessage("LayerFactory", "getLayerConfigFromWmsCapabilities", "Version not compatible")); } String requestUrl = wmsCapabilities.getCapability().getRequest().getGetMap().getGetUrl(); if (requestUrl == null) { throw new RuntimeException( Logger.makeMessage("LayerFactory", "getLayerConfigFromWmsCapabilities", "Unable to resolve GetMap URL")); } else { wmsLayerConfig.serviceAddress = requestUrl; } StringBuilder sb = null; Set<String> matchingCoordinateSystems = null; for (WmsLayer wmsLayer : wmsLayers) { String layerName = wmsLayer.getName(); if (sb == null) { sb = new StringBuilder(layerName); } else { sb.append(",").append(layerName); } List<String> wmsLayerCoordinateSystems = wmsLayer.getReferenceSystems(); if (matchingCoordinateSystems == null) { matchingCoordinateSystems = new HashSet<>(); matchingCoordinateSystems.addAll(wmsLayerCoordinateSystems); } else { matchingCoordinateSystems.retainAll(wmsLayerCoordinateSystems); } } wmsLayerConfig.layerNames = sb.toString(); if (matchingCoordinateSystems.contains("EPSG:4326")) { wmsLayerConfig.coordinateSystem = "EPSG:4326"; } else if (matchingCoordinateSystems.contains("CRS:84")) { wmsLayerConfig.coordinateSystem = "CRS:84"; } else { throw new RuntimeException( Logger.makeMessage("LayerFactory", "getLayerConfigFromWmsCapabilities", "Coordinate systems not compatible")); } List<String> imageFormats = wmsCapabilities.getCapability().getRequest().getGetMap().getFormats(); for (String compatibleImageFormat : this.compatibleImageFormats) { if (imageFormats.contains(compatibleImageFormat)) { wmsLayerConfig.imageFormat = compatibleImageFormat; break; } } if (wmsLayerConfig.imageFormat == null) { throw new RuntimeException( Logger.makeMessage("LayerFactory", "getLayerConfigFromWmsCapabilities", "Image Formats Not Compatible")); } return wmsLayerConfig; } LayerFactory(); Layer createFromGeoPackage(String pathName, Callback callback); Layer createFromWms(String serviceAddress, String layerName, Callback callback); Layer createFromWms(String serviceAddress, List<String> layerNames, Callback callback); Layer createFromWmsLayerCapabilities(WmsLayer layerCapabilities, Callback callback); Layer createFromWmsLayerCapabilities(List<WmsLayer> layerCapabilities, Callback callback); Layer createFromWmts(String serviceAddress, String layerIdentifier, Callback callback); Layer createFromWmtsLayer(WmtsLayer wmtsLayer, Callback callback);  }
@Test public void testGetLayerConfigFromWmsCapabilities_Nominal() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); assertEquals("Version", DEFAULT_VERSION, wmsLayerConfig.wmsVersion); assertEquals("Layer Name", DEFAULT_LAYER_NAME, wmsLayerConfig.layerNames); assertEquals("Request URL", DEFAULT_REQUEST_URL, wmsLayerConfig.serviceAddress); assertEquals("Reference Systems", "EPSG:4326", wmsLayerConfig.coordinateSystem); assertEquals("Image Format", "image/png", wmsLayerConfig.imageFormat); }
@Test public void testGetLayerConfigFromWmsCapabilities_InvalidVersion() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); PowerMockito.when(wmsCapabilities.getVersion()).thenReturn("1.2.1"); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); try { WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); fail("Invalid Version"); } catch (RuntimeException e) { assertNotNull("Invalid Version", e); } }
@Test public void testGetLayerConfigFromWmsCapabilities_InvalidRequestUrl() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); PowerMockito.when(wmsCapabilities.getCapability().getRequest().getGetMap().getGetUrl()).thenReturn(null); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); try { WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); fail("Invalid Request URL"); } catch (RuntimeException e) { assertNotNull("Invalid Request URL", e); } }
@Test public void testGetLayerConfigFromWmsCapabilities_OtherCoordinateSystem() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<String> modifiedReferenceSystems = Arrays.asList("CRS:84"); PowerMockito.when(wmsLayer.getReferenceSystems()).thenReturn(modifiedReferenceSystems); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); assertEquals("Alternate Coordinate System", "CRS:84", wmsLayerConfig.coordinateSystem); }
@Test public void testGetLayerConfigFromWmsCapabilities_InvalidCoordinateSystem() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<String> modifiedReferenceSystems = Arrays.asList("EPSG:1234"); PowerMockito.when(wmsLayer.getReferenceSystems()).thenReturn(modifiedReferenceSystems); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); try { WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); fail("Invalid Coordinate System"); } catch (RuntimeException e) { assertNotNull("Invalid Coordinate System", e); } }
@Test public void testGetLayerConfigFromWmsCapabilities_OtherImageFormat() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<String> modifiedImageFormats = Arrays.asList("image/dds", "image/jpg"); PowerMockito.when(wmsCapabilities.getCapability().getRequest().getGetMap().getFormats()).thenReturn(modifiedImageFormats); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); assertEquals("Alternate Image Format", "image/jpg", wmsLayerConfig.imageFormat); }
@Test public void testGetLayerConfigFromWmsCapabilities_InvalidImageFormat() throws Exception { WmsCapabilities wmsCapabilities = getBoilerPlateWmsCapabilities(); WmsLayer wmsLayer = wmsCapabilities.getNamedLayer(DEFAULT_LAYER_NAME); List<String> modifiedImageFormats = Arrays.asList("image/dds", "image/never"); PowerMockito.when(wmsCapabilities.getCapability().getRequest().getGetMap().getFormats()).thenReturn(modifiedImageFormats); List<WmsLayer> layerCapabilities = Arrays.asList(wmsLayer); LayerFactory layerFactory = new LayerFactory(); try { WmsLayerConfig wmsLayerConfig = layerFactory.getLayerConfigFromWmsCapabilities(layerCapabilities); fail("Invalid Image Format"); } catch (RuntimeException e) { assertNotNull("Invalid Image Format"); } }