Changeset 18372 in josm for trunk/test/unit/org


Ignore:
Timestamp:
2022-02-09T17:45:20+01:00 (2 years ago)
Author:
taylor.smock
Message:

see #21850: WMS and WMTS don't support {apikey}

Add tests for r18371.

Location:
trunk/test/unit/org/openstreetmap/josm/data/imagery
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSourceTest.java

    r17275 r18372  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertThrows;
    56import static org.junit.jupiter.api.Assertions.assertTrue;
     7import static org.openstreetmap.josm.tools.I18n.tr;
    68
    79import org.junit.jupiter.api.Test;
    810import org.junit.jupiter.api.extension.RegisterExtension;
     11import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    912import org.openstreetmap.gui.jmapviewer.TileXY;
    1013import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
     
    201204    }
    202205
     206    @Test
     207    void testApiKeyValid() {
     208        try {
     209            Projection projection = Projections.getProjectionByCode("EPSG:4326");
     210            ProjectionRegistry.setProjection(projection);
     211            FeatureAdapter.registerApiKeyAdapter(id -> "test_api_key");
     212            ImageryInfo testImageryWMS = new ImageryInfo("test imagery",
     213                    "https://maps.six.nsw.gov.au/arcgis/services/{apikey}/NSW_Imagery_Dates/MapServer/WMSServer?SERVICE=WMS&VERSION=1.3.0&"
     214                            + "REQUEST=GetMap&CRS={proj}&BBOX={bbox}&WIDTH={width}&HEIGHT={height}&LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&"
     215                            + "MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE", "wms", null, null);
     216            testImageryWMS.setId("TemplatedWMSTileSourceTest#testApiKeyValid");
     217            TemplatedWMSTileSource ts = new TemplatedWMSTileSource(testImageryWMS, projection);
     218            assertEquals("https://maps.six.nsw.gov.au/arcgis/services/test_api_key/NSW_Imagery_Dates/MapServer/WMSServer?SERVICE=WMS&"
     219                            + "VERSION=1.3.0&REQUEST=GetMap&CRS=EPSG:4326&BBOX=-1350.0000000,540.0000000,-990.0000000,900.0000000&WIDTH=512&"
     220                            + "HEIGHT=512&LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE",
     221                    ts.getTileUrl(1, 2, 3));
     222        } finally {
     223            FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
     224        }
     225    }
     226
     227    @Test
     228    void testApiKeyInvalid() {
     229        try {
     230            FeatureAdapter.registerApiKeyAdapter(id -> null);
     231            ImageryInfo testImageryWMS = new ImageryInfo("test imagery",
     232                    "https://maps.six.nsw.gov.au/arcgis/services/{apikey}/NSW_Imagery_Dates/MapServer/WMSServer?SERVICE=WMS&VERSION=1.3.0&"
     233                            + "REQUEST=GetMap&CRS={proj}&BBOX={bbox}&WIDTH={width}&HEIGHT={height}&LAYERS=0&STYLES=&FORMAT=image/png32&DPI=96&"
     234                            + "MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE", "wms", null, null);
     235            testImageryWMS.setId("TemplatedWMSTileSourceTest#testApiKeyInvalid");
     236            Projection projection = Projections.getProjectionByCode("EPSG:4326");
     237            IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new TemplatedWMSTileSource(testImageryWMS, projection));
     238            assertEquals(tr("Could not retrieve API key for imagery with id={0}. Cannot add layer.", testImageryWMS.getId()),
     239                    exception.getMessage());
     240        } finally {
     241            FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
     242        }
     243    }
     244
    203245    private void verifyMercatorTile(TemplatedWMSTileSource source, int x, int y, int z) {
    204246        TemplatedTMSTileSource verifier = new TemplatedTMSTileSource(testImageryTMS);
  • trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java

    r18106 r18372  
    22package org.openstreetmap.josm.data.imagery;
    33
     4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    45import static org.junit.jupiter.api.Assertions.assertEquals;
     6import static org.junit.jupiter.api.Assertions.assertThrows;
    57import static org.junit.jupiter.api.Assertions.assertTrue;
     8import static org.junit.jupiter.api.Assumptions.assumeFalse;
     9import static org.openstreetmap.josm.tools.I18n.tr;
    610
    711import java.io.File;
     
    1822import org.junit.jupiter.api.Test;
    1923import org.junit.jupiter.api.extension.RegisterExtension;
     24import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    2025import org.openstreetmap.gui.jmapviewer.TileXY;
    2126import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
     
    439444                source.getTileUrl(11, tile.getXIndex(), tile.getYIndex()));
    440445    }
     446
     447    @Test
     448    void testApiKeyValid() {
     449        assumeFalse(testImagery12168 == null);
     450        try {
     451            ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:4326"));
     452            FeatureAdapter.registerApiKeyAdapter(id -> TestUtils.getTestDataRoot());
     453            ImageryInfo testImageryWMTS = new ImageryInfo(testImagery12168);
     454            testImageryWMTS.setUrl(testImageryWMTS.getUrl().replace(TestUtils.getTestDataRoot(), "{apikey}"));
     455            assertTrue(testImageryWMTS.getUrl().contains("{apikey}"));
     456            testImageryWMTS.setId("WMTSTileSourceTest#testApiKeyValid");
     457            WMTSTileSource ts = assertDoesNotThrow(() -> new WMTSTileSource(testImageryWMTS, ProjectionRegistry.getProjection()));
     458            assertEquals("http://www.ngi.be/cartoweb/1.0.0/topo/default/3812/1/3/2.png",
     459                    ts.getTileUrl(1, 2, 3));
     460        } finally {
     461            FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
     462        }
     463    }
     464
     465    @Test
     466    void testApiKeyInvalid() {
     467        assumeFalse(testImagery12168 == null);
     468        try {
     469            ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:4326"));
     470            FeatureAdapter.registerApiKeyAdapter(id -> null);
     471            ImageryInfo testImageryWMTS = new ImageryInfo(testImagery12168);
     472            testImageryWMTS.setUrl(testImageryWMTS.getUrl().replace(TestUtils.getTestDataRoot(), "{apikey}"));
     473            assertTrue(testImageryWMTS.getUrl().contains("{apikey}"));
     474            testImageryWMTS.setId("TemplatedWMSTileSourceTest#testApiKeyInvalid");
     475            org.openstreetmap.josm.data.projection.Projection projection = Projections.getProjectionByCode("EPSG:4326");
     476            IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new WMTSTileSource(testImageryWMTS, projection));
     477            assertEquals(tr("Could not retrieve API key for imagery with id={0}. Cannot add layer.", testImageryWMTS.getId()),
     478                    exception.getMessage());
     479        } finally {
     480            FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
     481        }
     482    }
    441483}
Note: See TracChangeset for help on using the changeset viewer.