Skip to content

Commit

Permalink
[GEOS-8675] WFS 2.0.2 allows stored query returnFeatureTypes to be an…
Browse files Browse the repository at this point in the history
… empty string
  • Loading branch information
aaime committed Apr 5, 2018
1 parent c9e8e99 commit c741177
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/wfs/src/main/java/org/geoserver/wfs/StoredQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ public void validate() throws WFSException, IOException {
}

Set<QName> returnTypes = new HashSet(qe.getReturnFeatureTypes());
boolean allowAnyReturnType = returnTypes.equals(Collections.singleton(new QName("")));
for (Iterator<QName> it = queryTypes.iterator(); it.hasNext();) {
QName qName = it.next();
if (!returnTypes.contains(qName) && !isParameter(qName.getLocalPart(), queryDef.getParameter())) {
if (!returnTypes.contains(qName) && !allowAnyReturnType &&
!isParameter(qName.getLocalPart(), queryDef.getParameter())) {
throw new WFSException(String.format("StoredQuery references typeName %s:%s " +
"not listed in returnFeatureTypes: %s", qName.getPrefix(),
qName.getLocalPart(), toString(qe.getReturnFeatureTypes())));
Expand All @@ -179,8 +181,8 @@ public void validate() throws WFSException, IOException {
returnTypes.remove(qName);
}
}

if (!returnTypes.isEmpty() && !returnTypes.equals(Collections.singleton(new QName("")))) {
if (!returnTypes.isEmpty() && !allowAnyReturnType) {
throw new WFSException(String.format("StoredQuery declares return feature type(s) not " +
"not referenced in query definition: %s", toString(returnTypes)));
}
Expand Down
69 changes: 64 additions & 5 deletions src/wfs/src/test/java/org/geoserver/wfs/v2_0/StoredQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.geoserver.wfs.WFSException;
import org.geoserver.wfs.xml.FeatureTypeSchemaBuilder;
import org.geoserver.wfs.xml.v1_1_0.WFSConfiguration;
import org.geotools.filter.v2_0.FES;
import org.geotools.gml3.v3_2.GML;
import org.geotools.wfs.v2_0.WFS;
import org.geotools.xml.Parser;
import org.junit.Before;
Expand Down Expand Up @@ -92,7 +94,7 @@ public void testCreateStoredQuery() throws Exception {
assertEquals("wfs:ListStoredQueriesResponse", dom.getDocumentElement().getNodeName());
XMLAssert.assertXpathEvaluatesTo("1", "count(//wfs:StoredQuery)", dom);

xml = getCreatePrimitiveWithinQuery();
xml = getCreatePrimitiveWithinQuery(false);

dom = postAsDOM("wfs", xml);
assertEquals("wfs:CreateStoredQueryResponse", dom.getDocumentElement().getNodeName());
Expand All @@ -106,7 +108,7 @@ public void testCreateStoredQuery() throws Exception {

@Test
public void testDuplicateStoredQuery() throws Exception {
String xml = getCreatePrimitiveWithinQuery();
String xml = getCreatePrimitiveWithinQuery(false);
Document dom = postAsDOM("wfs", xml);
assertEquals("wfs:CreateStoredQueryResponse", dom.getDocumentElement().getNodeName());
assertEquals("OK", dom.getDocumentElement().getAttribute("status"));
Expand All @@ -118,7 +120,9 @@ public void testDuplicateStoredQuery() throws Exception {
checkOws11Exception(dom, "2.0.0", WFSException.DUPLICATE_STORED_QUERY_ID_VALUE, "myStoredQuery");
}

public String getCreatePrimitiveWithinQuery() {
public String getCreatePrimitiveWithinQuery(boolean emptyReturnFeatureTypes) {
String returnFeatureTypes = emptyReturnFeatureTypes ? "" : "sf:PrimitiveGeoFeature";

return "<wfs:CreateStoredQuery service='WFS' version='2.0.0' " +
" xmlns:wfs='http://www.opengis.net/wfs/2.0' " +
" xmlns:fes='http://www.opengis.net/fes/2.0' " +
Expand All @@ -127,8 +131,8 @@ public String getCreatePrimitiveWithinQuery() {
" xmlns:sf='" + MockData.SF_URI + "'>" +
" <wfs:StoredQueryDefinition id='myStoredQuery'> " +
" <wfs:Parameter name='AreaOfInterest' type='gml:Polygon'/> " +
" <wfs:QueryExpressionText " +
" returnFeatureTypes='sf:PrimitiveGeoFeature' " +
" <wfs:QueryExpressionText " +
" returnFeatureTypes='" + returnFeatureTypes + "' " +
" language='urn:ogc:def:queryLanguage:OGC-WFS::WFS_QueryExpression' " +
" isPrivate='false'> " +
" <wfs:Query typeNames='sf:PrimitiveGeoFeature'> " +
Expand Down Expand Up @@ -447,4 +451,59 @@ public void testCreateWithLocalNamespaceDeclaration() throws Exception {
XMLAssert.assertXpathEvaluatesTo("2", "count(//wfs:StoredQuery)", dom);
XMLAssert.assertXpathExists("//wfs:StoredQuery[@id = 'urn:example:wfs2-query:GetFeatureByName']", dom);
}

@Test
public void testCreateStoredQueryWithEmptyReturnFeatureTypes() throws Exception {
String xml =
"<wfs:ListStoredQueries service='WFS' version='2.0.0' " +
" xmlns:wfs='" + WFS.NAMESPACE + "'/>";
Document dom = postAsDOM("wfs", xml);
// print(dom);
assertEquals("wfs:ListStoredQueriesResponse", dom.getDocumentElement().getNodeName());
XMLAssert.assertXpathEvaluatesTo("1", "count(//wfs:StoredQuery)", dom);

xml = getCreatePrimitiveWithinQuery(true);

dom = postAsDOM("wfs", xml);
// print(dom);
assertEquals("wfs:CreateStoredQueryResponse", dom.getDocumentElement().getNodeName());
assertEquals("OK", dom.getDocumentElement().getAttribute("status"));

// verify it can be listed
dom = getAsDOM("wfs?request=ListStoredQueries");
// print(dom);
XMLAssert.assertXpathEvaluatesTo("2", "count(//wfs:StoredQuery)", dom);
XMLAssert.assertXpathExists("//wfs:StoredQuery[@id = 'myStoredQuery']", dom);
XMLAssert.assertXpathExists("//wfs:ReturnFeatureType[text() = 'sf:PrimitiveGeoFeature']", dom);

// verify it describes correctly
xml = "<wfs:DescribeStoredQueries xmlns:wfs='" + WFS.NAMESPACE + "' service='WFS'>" +
"<wfs:StoredQueryId>myStoredQuery</wfs:StoredQueryId>" +
"</wfs:DescribeStoredQueries>";
dom = postAsDOM("wfs", xml);
// print(dom);
assertEquals("wfs:DescribeStoredQueriesResponse", dom.getDocumentElement().getNodeName());
XMLAssert.assertXpathExists("//wfs:StoredQueryDescription[@id='myStoredQuery']", dom);
XMLAssert.assertXpathExists("//wfs:QueryExpressionText/@returnFeatureTypes", dom);
XMLAssert.assertXpathEvaluatesTo("", "//wfs:QueryExpressionText/@returnFeatureTypes", dom);

// verify it can be run
xml = "<wfs:GetFeature service='WFS' version='2.0.0' xmlns:gml='" + GML.NAMESPACE + "'" +
" xmlns:wfs='" + WFS.NAMESPACE + "' xmlns:fes='" + FES.NAMESPACE + "'>" +
" <wfs:StoredQuery id='myStoredQuery'> " +
" <wfs:Parameter name='AreaOfInterest'>"
+ " <gml:Envelope srsName='EPSG:4326'>"
+ " <gml:lowerCorner>57.0 -4.5</gml:lowerCorner>"
+ " <gml:upperCorner>62.0 1.0</gml:upperCorner>"
+ " </gml:Envelope>" +
" </wfs:Parameter> " +
" </wfs:StoredQuery> " +
"</wfs:GetFeature>";
dom = postAsDOM("wfs", xml);
// print(dom);
assertGML32(dom);
XMLAssert.assertXpathEvaluatesTo("1", "count(//sf:PrimitiveGeoFeature)", dom);
XMLAssert.assertXpathExists("//sf:PrimitiveGeoFeature/gml:name[text() = 'name-f002']", dom);

}
}

0 comments on commit c741177

Please sign in to comment.