Skip to content

Commit

Permalink
Fix #23: Decode Base64-encoded schema in SOAP response
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmartell committed Nov 3, 2016
1 parent cf50285 commit e2fd5ab
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.opengis.cite.iso19142.simple;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Base64;
import java.util.logging.Level;

import javax.xml.XMLConstants;
Expand Down Expand Up @@ -28,6 +31,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.jersey.api.client.ClientResponse;

Expand All @@ -41,6 +45,8 @@
*/
public class DescribeFeatureTypeTests extends BaseFixture {

DocumentBuilder docBuilder;

/**
* Builds a DOM Document node representing the request entity
* (/wfs:DescribeFeatureType).
Expand All @@ -50,8 +56,8 @@ public void buildRequestEntity() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
this.reqEntity = builder.parse(getClass().getResourceAsStream("DescribeFeatureType.xml"));
this.docBuilder = factory.newDocumentBuilder();
this.reqEntity = docBuilder.parse(getClass().getResourceAsStream("DescribeFeatureType.xml"));
} catch (Exception e) {
TestSuiteLogger.log(Level.WARNING, "Failed to parse request entity from classpath", e);
}
Expand Down Expand Up @@ -118,6 +124,12 @@ public void describeAllFeatureTypes(ProtocolBinding binding) {
ErrorMessage.get(ErrorMessageKeys.UNEXPECTED_STATUS));
Assert.assertTrue(rsp.hasEntity(), ErrorMessage.get(ErrorMessageKeys.MISSING_XML_ENTITY));
Element docElem = this.rspEntity.getDocumentElement();
if (docElem.getLocalName().equals("DescribeFeatureTypeResponse")) {
// special case for SOAP response
Document appSchema = decodeSchema(this.rspEntity);
Assert.assertNotNull(appSchema, "Base64-encoded schema could not be read.");
docElem = appSchema.getDocumentElement();
}
Assert.assertEquals(docElem.getLocalName(), "schema", "Document element has unexpected [local name].");
// TODO: compile schema
}
Expand Down Expand Up @@ -150,4 +162,28 @@ public void describeUnknownFeatureType(ProtocolBinding binding) {
Assert.assertFalse(validator.ruleViolationsDetected(), ErrorMessage.format(ErrorMessageKeys.NOT_SCHEMA_VALID,
validator.getRuleViolationCount(), XMLUtils.resultToString(result)));
}

/**
* The body of a SOAP response contains a DescribeFeatureTypeResponse
* element. Its content is a Base64-encoded application schema.
*
* @param doc
* A Document containing a wfs:DescribeFeatureTypeResponse
* element.
* @return A Document representing an XML Schema.
*
* @see "ISO 19142, D.4.5: Encoding XML Schema in a SOAP Body"
*/
Document decodeSchema(Document doc) {
String base64Schema = doc.getDocumentElement().getTextContent().trim();
byte[] schema = Base64.getDecoder().decode(base64Schema);
Document appSchema = null;
try {
appSchema = this.docBuilder.parse(new ByteArrayInputStream(schema), doc.getDocumentURI());
} catch (SAXException | IOException e) {
TestSuiteLogger.log(Level.WARNING, String.format("Failed to parse decoded schema from %s.\n%s ",
doc.getDocumentURI(), e.getMessage()));
}
return appSchema;
}
}
2 changes: 2 additions & 0 deletions src/site/xhtml/changelog.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
VersioningTests::replacementVersionHasNoSuccessor test alters gml:id value</li>
<li style="list-style:square">Fix <a target="_blank" href="https://github.com/opengeospatial/ets-wfs20/issues/51">#51</a>:
VersioningTests::deletedFeatureIsRetired test does not specify "LAST" version</li>
<li style="list-style:square">Fix <a target="_blank" href="https://github.com/opengeospatial/ets-wfs20/issues/23">#23</a>:
SOAP response is not evaluated correctly in describeAllFeatureTypes</li>
</ul>

<h2>1.25 (2016-10-04)</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.After;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -26,43 +26,50 @@
*/
public class VerifyDescribeFeatureTypeTests {

private static ITestContext testContext;
private static ISuite suite;
private static DocumentBuilder docBuilder;
private static ITestContext testContext;
private static ISuite suite;
private static DocumentBuilder docBuilder;

public VerifyDescribeFeatureTypeTests() {
}
public VerifyDescribeFeatureTypeTests() {
}

@BeforeClass
public static void setUpClass() throws Exception {
testContext = mock(ITestContext.class);
suite = mock(ISuite.class);
when(testContext.getSuite()).thenReturn(suite);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
docBuilder = dbf.newDocumentBuilder();
}
@BeforeClass
public static void setUpClass() throws Exception {
testContext = mock(ITestContext.class);
suite = mock(ISuite.class);
when(testContext.getSuite()).thenReturn(suite);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
docBuilder = dbf.newDocumentBuilder();
}

@Before
public void setUp() {
}
@Before
public void setUp() {
}

@After
public void tearDown() {
}
@After
public void tearDown() {
}

@Test
public void addOneFeatureType() throws SAXException, IOException {
Document doc = docBuilder.parse(this.getClass().getResourceAsStream(
"/DescribeFeatureType-Empty.xml"));
DescribeFeatureTypeTests iut = new DescribeFeatureTypeTests();
iut.addFeatureType(doc,
new QName("http://example.org", "Unknown1.Type"));
Element typeName = (Element) doc.getElementsByTagNameNS(Namespaces.WFS,
WFS2.TYPENAME_ELEM).item(0);
String[] qName = typeName.getTextContent().split(":");
Assert.assertEquals("Qualified name should be 'prefix:localPart'.", 2,
qName.length);
Assert.assertEquals("Unexpected type name.", "Unknown1.Type", qName[1]);
}
@Test
public void addOneFeatureType() throws SAXException, IOException {
Document doc = docBuilder.parse(this.getClass().getResourceAsStream("/DescribeFeatureType-Empty.xml"));
DescribeFeatureTypeTests iut = new DescribeFeatureTypeTests();
iut.addFeatureType(doc, new QName("http://example.org", "Unknown1.Type"));
Element typeName = (Element) doc.getElementsByTagNameNS(Namespaces.WFS, WFS2.TYPENAME_ELEM).item(0);
String[] qName = typeName.getTextContent().split(":");
assertEquals("Qualified name should be 'prefix:localPart'.", 2, qName.length);
assertEquals("Unexpected type name.", "Unknown1.Type", qName[1]);
}

@Test
public void decodeAppSchema() throws SAXException, IOException {
Document doc = docBuilder.parse(this.getClass().getResourceAsStream("/DescribeFeatureTypeResponse-Base64.xml"));
DescribeFeatureTypeTests iut = new DescribeFeatureTypeTests();
iut.buildRequestEntity();
Document schema = iut.decodeSchema(doc);
assertNotNull("Failed to parse decoded schema.", schema);
assertEquals("Unexpected namespace name", "http://www.w3.org/2001/XMLSchema",
schema.getDocumentElement().getNamespaceURI());
}
}

0 comments on commit e2fd5ab

Please sign in to comment.