Skip to content

Commit

Permalink
added getAsMapML to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
turingtestfail committed Feb 20, 2024
1 parent 0481c84 commit ca95fc4
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 227 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* (c) 2024 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.mapml;

import static org.junit.Assert.fail;

import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Map;
import javax.xml.bind.DataBindingException;
import javax.xml.bind.JAXBException;
import org.geoserver.mapml.xml.Mapml;
import org.geoserver.wms.WMSTestSupport;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

/** MapML test support class */
public class MapMLTestSupport extends WMSTestSupport {
/**
* Get the WMS response as a MapML object
*
* @param name the name of the layer
* @param kvp the key value pairs
* @param locale the locale
* @param srs the SRS
* @param styles the styles
* @return the MapML object
* @throws Exception if an error occurs
*/
protected Mapml getWMSAsMapML(
String name,
Map kvp,
Locale locale,
String srs,
String styles,
boolean isFeatureRepresentation)
throws Exception {
MockHttpServletRequest request =
getMapMLWMSRequest(name, kvp, locale, srs, styles, isFeatureRepresentation);
MockHttpServletResponse response = dispatch(request);
return mapml(response);
}

protected String getWMSAsMapMLString(
String name,
Map kvp,
Locale locale,
String srs,
String styles,
boolean isFeatureRepresentation)
throws Exception {
MockHttpServletRequest request =
getMapMLWMSRequest(name, kvp, locale, srs, styles, isFeatureRepresentation);
MockHttpServletResponse response = dispatch(request);
return response.getContentAsString();
}

/**
* Get the response as a MapML object
*
* @param path the path to the resource
* @return the MapML object
* @throws Exception if an error occurs
*/
protected Mapml getAsMapML(final String path) throws Exception {
MockHttpServletResponse response = getAsServletResponse(path);
return mapml(response);
}

/**
* Convert the response to a MapML object
*
* @param response the response
* @return the MapML object
* @throws JAXBException if an error occurs
* @throws UnsupportedEncodingException if an error occurs
*/
protected Mapml mapml(MockHttpServletResponse response)
throws JAXBException, UnsupportedEncodingException {
MapMLEncoder encoder = new MapMLEncoder();
StringReader reader = new StringReader(response.getContentAsString());
Mapml mapml = null;
try {
mapml = encoder.decode(reader);
} catch (DataBindingException e) {
fail("MapML response is not valid XML");
}
return mapml;
}

/**
* Get a MapML request
*
* @param name the name of the layer
* @param kvp the key value pairs
* @param locale the locale
* @param srs the SRS
* @param styles the styles
* @return the request
* @throws Exception if an error occurs
*/
protected MockHttpServletRequest getMapMLWMSRequest(
String name,
Map kvp,
Locale locale,
String srs,
String styles,
boolean isFeatureRepresentation)
throws Exception {
String path = null;
MockHttpServletRequest request = null;
String formatOptions =
isFeatureRepresentation
? MapMLConstants.MAPML_FEATURE_FORMAT_OPTIONS
: MapMLConstants.MAPML_WMS_MIME_TYPE_OPTION + ":image/png";
if (kvp != null) {
path = "wms";
request = createRequest(path, kvp);
} else {
path =
"wms?LAYERS="
+ name
+ "&STYLES="
+ (styles != null ? styles : "")
+ "&FORMAT="
+ MapMLConstants.MAPML_MIME_TYPE
+ "&SERVICE=WMS&VERSION=1.3.0"
+ "&REQUEST=GetMap"
+ "&SRS="
+ srs
+ "&BBOX=0,0,1,1"
+ "&WIDTH=150"
+ "&HEIGHT=150"
+ "&format_options="
+ formatOptions;
request = createRequest(path);
}

return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
import static org.geoserver.mapml.MapMLConstants.MAPML_USE_TILES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.StringReader;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.xml.bind.DataBindingException;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.LayerGroupInfo;
Expand All @@ -23,14 +18,11 @@
import org.geoserver.data.test.SystemTestData;
import org.geoserver.mapml.xml.Mapml;
import org.geoserver.mapml.xml.Polygon;
import org.geoserver.wms.WMSTestSupport;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.After;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class MapMLWMSFeatureTest extends WMSTestSupport {
public class MapMLWMSFeatureTest extends MapMLTestSupport {
@Override
protected void onSetUp(SystemTestData testData) throws Exception {
super.onSetUp(testData);
Expand Down Expand Up @@ -73,18 +65,10 @@ public void testMapMLUseFeatures() throws Exception {
li.getMetadata().put(MAPML_USE_TILES, false);
cat.save(li);

MockHttpServletResponse response =
getMockRequestResponse(
MockData.POLYGONS.getLocalPart(), null, null, "EPSG:3857", null);
Mapml mapmlFeatures =
getWMSAsMapML(
MockData.POLYGONS.getLocalPart(), null, null, "EPSG:3857", null, true);

MapMLEncoder encoder = new MapMLEncoder();
StringReader reader = new StringReader(response.getContentAsString());
Mapml mapmlFeatures = null;
try {
mapmlFeatures = encoder.decode(reader);
} catch (DataBindingException e) {
fail("MapML response is not valid XML");
}
assertEquals(
"Polygons layer has one feature, so one should show up in the conversion",
1,
Expand Down Expand Up @@ -115,19 +99,19 @@ public void testExceptionBecauseMoreThanOneFeatureType() throws Exception {
lgi.getMetadata().put(MAPML_USE_FEATURES, true);
lgi.getMetadata().put(MAPML_USE_TILES, false);
cat.save(lgi);
MockHttpServletResponse response =
getMockRequestResponse(
String response =
getWMSAsMapMLString(
"layerGroup" + "," + MockData.POLYGONS.getLocalPart(),
null,
null,
"EPSG:3857",
null);
null,
true);

assertTrue(
"MapML response contains an exception due to multiple feature types",
response.getContentAsString()
.contains(
"MapML WMS Feature format does not currently support Multiple Feature Type output."));
response.contains(
"MapML WMS Feature format does not currently support Multiple Feature Type output."));
}

@Test
Expand All @@ -137,50 +121,13 @@ public void testExceptionBecauseBecauseRaster() throws Exception {
liRaster.getMetadata().put(MAPML_USE_FEATURES, true);
liRaster.getMetadata().put(MAPML_USE_TILES, false);
cat.save(liRaster);
MockHttpServletResponse response =
getMockRequestResponse(
MockData.WORLD.getLocalPart(), null, null, "EPSG:3857", null);
String response =
getWMSAsMapMLString(
MockData.WORLD.getLocalPart(), null, null, "EPSG:3857", null, true);

assertTrue(
"MapML response contains an exception due to non-vector type",
response.getContentAsString()
.contains(
"MapML WMS Feature format does not currently support non-vector layers."));
}

private MockHttpServletResponse getMockRequestResponse(
String name, Map kvp, Locale locale, String srs, String styles) throws Exception {
String path = null;
MockHttpServletRequest request = null;
if (kvp != null) {
path = "wms";
request = createRequest(path, kvp);
} else {
path =
"wms?LAYERS="
+ name
+ "&STYLES="
+ (styles != null ? styles : "")
+ "&FORMAT="
+ MapMLConstants.MAPML_MIME_TYPE
+ "&SERVICE=WMS&VERSION=1.3.0"
+ "&REQUEST=GetMap"
+ "&SRS="
+ srs
+ "&BBOX=0,0,1,1"
+ "&WIDTH=150"
+ "&HEIGHT=150"
+ "&format_options="
+ MapMLConstants.MAPML_FEATURE_FORMAT_OPTIONS
+ ":image/png";
request = createRequest(path);
}

if (locale != null) {
request.addPreferredLocale(locale);
}
request.setMethod("GET");
request.setContent(new byte[] {});
return dispatch(request, "UTF-8");
response.contains(
"MapML WMS Feature format does not currently support non-vector layers."));
}
}
Loading

0 comments on commit ca95fc4

Please sign in to comment.