forked from Gaia3D/geoserver
/
GetFeatureTest.java
294 lines (241 loc) · 12.6 KB
/
GetFeatureTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wfs;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.Collections;
import javax.xml.namespace.QName;
import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.geoserver.data.test.SystemTestData;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class GetFeatureTest extends WFSTestSupport {
public static QName NULL_GEOMETRIES = new QName(SystemTestData.CITE_URI, "NullGeometries", SystemTestData.CITE_PREFIX);
public static QName FIFTEEN_DUPLICATE = new QName(SystemTestData.CITE_URI, "Fifteen", SystemTestData.CITE_PREFIX);
@Override
protected void setUpInternal(SystemTestData data) throws Exception {
WFSInfo wfs = getWFS();
wfs.setFeatureBounding(true);
getGeoServer().save(wfs);
data.addVectorLayer (NULL_GEOMETRIES, Collections.EMPTY_MAP, getClass(), getCatalog());
data.addVectorLayer (FIFTEEN_DUPLICATE, Collections.EMPTY_MAP, getClass(), getCatalog());
}
@Test
public void testGet() throws Exception {
testGetFifteenAll("wfs?request=GetFeature&typename=cdf:Fifteen&version=1.0.0&service=wfs");
}
@Test
public void testGetPropertyNameEmpty() throws Exception {
testGetFifteenAll("wfs?request=GetFeature&typename=cdf:Fifteen&version=1.0.0&service=wfs&propertyname=");
}
@Test
public void testGetPropertyNameStar() throws Exception {
testGetFifteenAll("wfs?request=GetFeature&typename=cdf:Fifteen&version=1.0.0&service=wfs&propertyname=*");
}
private void testGetFifteenAll(String request) throws Exception {
Document doc = getAsDOM(request);
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
NodeList featureMembers = doc.getElementsByTagName("gml:featureMember");
assertEquals(15, featureMembers.getLength());
}
// see GEOS-1893
@Test
public void testGetMissingParams() throws Exception {
Document doc = getAsDOM("wfs?request=GetFeature&typeNameWrongParam=cdf:Fifteen&version=1.0.0&service=wfs");
// trick: the document specifies a namespace with schema reference, as a result xpath expressions
// do work only if fully qualified
XMLAssert.assertXpathEvaluatesTo("1", "count(//ogc:ServiceException)", doc);
XMLAssert.assertXpathEvaluatesTo("MissingParameterValue", "//ogc:ServiceException/@code", doc);
}
@Test
public void testAlienNamespace() throws Exception {
// if the namespace is not known, complain with a service exception
Document doc = getAsDOM("wfs?request=GetFeature&typename=youdontknowme:Fifteen&version=1.0.0&service=wfs");
assertEquals("ServiceExceptionReport", doc.getDocumentElement()
.getNodeName());
}
@Test
public void testGetNullGeometies() throws Exception {
Document doc;
doc = getAsDOM("wfs?request=GetFeature&typeName=" + getLayerId(NULL_GEOMETRIES) + "&version=1.0.0&service=wfs");
// print(doc);
XMLAssert.assertXpathEvaluatesTo("1", "count(//cite:NullGeometries[@fid=\"NullGeometries.1107531701010\"]/gml:boundedBy)", doc);
XMLAssert.assertXpathEvaluatesTo("0", "count(//cite:NullGeometries[@fid=\"NullGeometries.1107531701011\"]/boundedBy)", doc);
}
// see GEOS-1287
@Test
public void testGetWithFeatureId() throws Exception {
Document doc;
doc = getAsDOM("wfs?request=GetFeature&typeName=cdf:Fifteen&version=1.0.0&service=wfs&featureid=Fifteen.2");
// super.print(doc);
assertEquals("wfs:FeatureCollection", doc.getDocumentElement().getNodeName());
XMLAssert.assertXpathEvaluatesTo("1", "count(//wfs:FeatureCollection/gml:featureMember)",
doc);
XMLAssert.assertXpathEvaluatesTo("Fifteen.2",
"//wfs:FeatureCollection/gml:featureMember/cdf:Fifteen/@fid", doc);
doc = getAsDOM("wfs?request=GetFeature&typeName=cite:NamedPlaces&version=1.0.0&service=wfs&featureId=NamedPlaces.1107531895891");
//super.print(doc);
assertEquals("wfs:FeatureCollection", doc.getDocumentElement().getNodeName());
XMLAssert.assertXpathEvaluatesTo("1", "count(//wfs:FeatureCollection/gml:featureMember)",
doc);
XMLAssert.assertXpathEvaluatesTo("NamedPlaces.1107531895891",
"//wfs:FeatureCollection/gml:featureMember/cite:NamedPlaces/@fid", doc);
}
@Test
public void testPost() throws Exception {
String xml = "<wfs:GetFeature " + "service=\"WFS\" "
+ "version=\"1.0.0\" "
+ "xmlns:cdf=\"http://www.opengis.net/cite/data\" "
+ "xmlns:ogc=\"http://www.opengis.net/ogc\" "
+ "xmlns:wfs=\"http://www.opengis.net/wfs\" " + "> "
+ "<wfs:Query typeName=\"cdf:Other\"> "
+ "<ogc:PropertyName>cdf:string2</ogc:PropertyName> "
+ "</wfs:Query> " + "</wfs:GetFeature>";
Document doc = postAsDOM("wfs", xml);
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
NodeList featureMembers = doc.getElementsByTagName("gml:featureMember");
assertFalse(featureMembers.getLength() == 0);
}
@Test
public void testPostWithFilter() throws Exception {
String xml = "<wfs:GetFeature " + "service=\"WFS\" "
+ "version=\"1.0.0\" " + "outputFormat=\"GML2\" "
+ "xmlns:cdf=\"http://www.opengis.net/cite/data\" "
+ "xmlns:wfs=\"http://www.opengis.net/wfs\" "
+ "xmlns:ogc=\"http://www.opengis.net/ogc\" > "
+ "<wfs:Query typeName=\"cdf:Other\"> "
+ "<ogc:PropertyName>cdf:string2</ogc:PropertyName> "
+ "<ogc:Filter> " + "<ogc:PropertyIsEqualTo> "
+ "<ogc:PropertyName>cdf:integers</ogc:PropertyName> "
+ "<ogc:Add> " + "<ogc:Literal>4</ogc:Literal> "
+ "<ogc:Literal>3</ogc:Literal> " + "</ogc:Add> "
+ "</ogc:PropertyIsEqualTo> " + "</ogc:Filter> "
+ "</wfs:Query> " + "</wfs:GetFeature>";
Document doc = postAsDOM("wfs", xml);
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
NodeList featureMembers = doc.getElementsByTagName("gml:featureMember");
assertFalse(featureMembers.getLength() == 0);
}
@Test
public void testLax() throws Exception {
String xml =
"<GetFeature version='1.1.0' xmlns:gml=\"http://www.opengis.net/gml\">" +
" <Query typeName=\"" + SystemTestData.BUILDINGS.getLocalPart() + "\">" +
" <PropertyName>ADDRESS</PropertyName>" +
" <Filter>" +
" <PropertyIsEqualTo>" +
" <PropertyName>ADDRESS</PropertyName>" +
" <Literal>123 Main Street</Literal>" +
" </PropertyIsEqualTo>" +
" </Filter>" +
" </Query>" +
"</GetFeature>";
Document doc = postAsDOM( "wfs", xml );
//print( doc );
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
NodeList featureMembers = doc.getElementsByTagName("cite:Buildings");
assertEquals(1,featureMembers.getLength());
}
@Test
public void testMixed() throws Exception {
String xml = "<wfs:GetFeature " + "service=\"WFS\" "
+ "version=\"1.0.0\" "
+ "xmlns:cdf=\"http://www.opengis.net/cite/data\" "
+ "xmlns:ogc=\"http://www.opengis.net/ogc\" "
+ "xmlns:wfs=\"http://www.opengis.net/wfs\" " + "> "
+ "<wfs:Query typeName=\"cdf:Other\"> "
+ "<ogc:PropertyName>cdf:string2</ogc:PropertyName> "
+ "</wfs:Query> " + "</wfs:GetFeature>";
Document doc = postAsDOM("wfs?request=GetFeature", xml);
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
NodeList featureMembers = doc.getElementsByTagName("gml:featureMember");
assertFalse(featureMembers.getLength() == 0);
}
@Test
public void testLikeMatchCase() throws Exception {
// first run, without matching case, should match both buildings
String xml =
"<GetFeature version='1.1.0' xmlns:gml=\"http://www.opengis.net/gml\">" +
" <Query typeName=\"" + SystemTestData.BUILDINGS.getLocalPart() + "\">" +
" <PropertyName>ADDRESS</PropertyName>" +
" <Filter>" +
" <PropertyIsLike wildCard=\"*\" singleChar=\".\" escapeChar=\"\\\" matchCase=\"false\">" +
" <PropertyName>ADDRESS</PropertyName>" +
" <Literal>* MAIN STREET</Literal>" +
" </PropertyIsLike>" +
" </Filter>" +
" </Query>" +
"</GetFeature>";
Document doc = postAsDOM( "wfs", xml );
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
NodeList featureMembers = doc.getElementsByTagName("cite:Buildings");
assertEquals(2,featureMembers.getLength());
// second run, with match case, should match none
xml =
"<GetFeature version='1.1.0' xmlns:gml=\"http://www.opengis.net/gml\">" +
" <Query typeName=\"" + SystemTestData.BUILDINGS.getLocalPart() + "\">" +
" <PropertyName>ADDRESS</PropertyName>" +
" <Filter>" +
" <PropertyIsLike wildCard=\"*\" singleChar=\".\" escapeChar=\"\\\" matchCase=\"true\">" +
" <PropertyName>ADDRESS</PropertyName>" +
" <Literal>* MAIN STREET</Literal>" +
" </PropertyIsLike>" +
" </Filter>" +
" </Query>" +
"</GetFeature>";
doc = postAsDOM( "wfs", xml );
assertEquals("wfs:FeatureCollection", doc.getDocumentElement()
.getNodeName());
featureMembers = doc.getElementsByTagName("cite:Buildings");
assertEquals(0,featureMembers.getLength());
}
@Test
public void testWorkspaceQualified() throws Exception {
testGetFifteenAll("cdf/wfs?request=GetFeature&typename=cdf:Fifteen&version=1.0.0&service=wfs");
testGetFifteenAll("cdf/wfs?request=GetFeature&typename=Fifteen&version=1.0.0&service=wfs");
Document doc = getAsDOM("sf/wfs?request=GetFeature&typename=cdf:Fifteen&version=1.0.0&service=wfs");
XMLAssert.assertXpathEvaluatesTo("1", "count(//ogc:ServiceException)", doc);
}
@Test
public void testLayerQualified() throws Exception {
testGetFifteenAll("cdf/Fifteen/wfs?request=GetFeature&typename=cdf:Fifteen&version=1.0.0&service=wfs");
testGetFifteenAll("cdf/Fifteen/wfs?request=GetFeature&typename=Fifteen&version=1.0.0&service=wfs");
Document doc = getAsDOM("sf/Fifteen/wfs?request=GetFeature&typename=cdf:Seven&version=1.0.0&service=wfs");
XMLAssert.assertXpathEvaluatesTo("1", "count(//ogc:ServiceException)", doc);
}
@Test
public void testMultiLayer() throws Exception {
Document doc = getAsDOM("/wfs?request=GetFeature&typename=" + getLayerId(SystemTestData.BASIC_POLYGONS)
+ "," + getLayerId(SystemTestData.BRIDGES) + "&version=1.0.0&service=wfs");
// print(doc);
XpathEngine engine = XMLUnit.newXpathEngine();
String schemaLocation = engine.evaluate("wfs:FeatureCollection/@xsi:schemaLocation", doc);
assertNotNull(schemaLocation);
String[] parsedLocations = schemaLocation.split("\\s+");
// System.out.println(Arrays.toString(parsedLocations));
int i = 0;
for (; i < parsedLocations.length; i+=2) {
if(parsedLocations[i].equals("http://www.opengis.net/cite")) {
assertEquals("http://localhost:8080/geoserver/wfs?service=WFS&version=1.0.0&request=DescribeFeatureType&typeName=cite%3ABasicPolygons,cite%3ABridges", parsedLocations[i+1]);
break;
}
}
if(i >= parsedLocations.length) {
fail("Could not find the http://www.opengis.net/cite schema location!");
}
}
}