Search in sources :

Example 1 with SimpleNamespaceResolver

use of org.glassfish.jersey.internal.util.SimpleNamespaceResolver in project jersey by jersey.

the class ExtendedWadlWebappOsgiTest method checkWadl.

private void checkWadl(String wadl, URI baseUri) throws Exception {
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    DocumentBuilder b = bf.newDocumentBuilder();
    Document document = b.parse(new ByteArrayInputStream(wadl.getBytes(Charset.forName("UTF-8"))));
    XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("/wadl:application/wadl:resources/@base", document, XPathConstants.STRING);
    assertEquals(baseUri.toString(), val.endsWith("/") ? val.substring(0, val.length() - 1) : val);
    val = (String) xp.evaluate("count(//wadl:resource)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements.", val, "4");
    val = (String) xp.evaluate("count(//wadl:resource[@path='items'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'items' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with '{id}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with '{id}' path.", "2", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Typically returns the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at '{id}' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Tries hard to return the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at 'try-hard' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='items']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'items' path.", "4", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:application/wadl:doc)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of doc elements in application element.", "3", val);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document)

Example 2 with SimpleNamespaceResolver

use of org.glassfish.jersey.internal.util.SimpleNamespaceResolver in project jersey by jersey.

the class ExtendedWadlWebappTest method checkWadl.

private void checkWadl(String wadl, URI baseUri) throws Exception {
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    //        if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
    //            bf.setXIncludeAware(false);
    //        }
    DocumentBuilder b = bf.newDocumentBuilder();
    Document document = b.parse(new ByteArrayInputStream(wadl.getBytes(Charset.forName("UTF-8"))));
    XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("/wadl:application/wadl:resources/@base", document, XPathConstants.STRING);
    assertEquals(baseUri.toString(), val.endsWith("/") ? val.substring(0, val.length() - 1) : val);
    val = (String) xp.evaluate("count(//wadl:resource)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements.", val, "4");
    val = (String) xp.evaluate("count(//wadl:resource[@path='items'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'items' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with '{id}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with '{id}' path.", "2", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Typically returns the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at '{id}' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Tries hard to return the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at 'try-hard' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='items']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'items' path.", "4", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:application/wadl:doc)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of doc elements in application element.", "3", val);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document)

Example 3 with SimpleNamespaceResolver

use of org.glassfish.jersey.internal.util.SimpleNamespaceResolver in project jersey by jersey.

the class MultiPartWebAppTest method testApplicationWadl.

@Test
public void testApplicationWadl() throws Exception {
    final WebTarget target = target().path("application.wadl");
    final Response response = target.request().get();
    assertEquals(200, response.getStatus());
    final File tmpFile = response.readEntity(File.class);
    final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
    }
    final DocumentBuilder b = bf.newDocumentBuilder();
    final Document d = b.parse(tmpFile);
    final XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("//wadl:resource[@path='part']/wadl:method[@name='POST']/wadl:request/wadl:representation/@mediaType", d, XPathConstants.STRING);
    assertEquals("multipart/form-data", val);
}
Also used : Response(javax.ws.rs.core.Response) XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) WebTarget(javax.ws.rs.client.WebTarget) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document) File(java.io.File) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 4 with SimpleNamespaceResolver

use of org.glassfish.jersey.internal.util.SimpleNamespaceResolver in project jersey by jersey.

the class WadlBeanParamTest method testBeanParamConstructorInitializedBean.

private void testBeanParamConstructorInitializedBean(String resource) throws Exception {
    final Response response = target("/application.wadl").request().get();
    final Document d = WadlResourceTest.extractWadlAsDocument(response);
    final XPath xp = XPathFactory.newInstance().newXPath();
    final SimpleNamespaceResolver nsContext = new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02");
    xp.setNamespaceContext(nsContext);
    final Diff diff = XMLUnit.compareXML(nodeAsString(xp.evaluate("//wadl:resource[@path='wadlBeanParamReference']/wadl:resource", d, XPathConstants.NODE)), nodeAsString(xp.evaluate("//wadl:resource[@path='" + resource + "']/wadl:resource", d, XPathConstants.NODE)));
    XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(ImmutableMap.of("wadl", "http://wadl.dev.java.net/2009/02")));
    diff.overrideElementQualifier(elementQualifier);
    XMLAssert.assertXMLEqual(diff, true);
}
Also used : Response(javax.ws.rs.core.Response) XPath(javax.xml.xpath.XPath) Diff(org.custommonkey.xmlunit.Diff) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) SimpleNamespaceContext(org.custommonkey.xmlunit.SimpleNamespaceContext) Document(org.w3c.dom.Document)

Example 5 with SimpleNamespaceResolver

use of org.glassfish.jersey.internal.util.SimpleNamespaceResolver in project jersey by jersey.

the class ResourceExtendedFlagTest method testLimitedWadl.

/**
     * Tests limited wadl with only user resources.
     *
     * @throws ParserConfigurationException
     * @throws XPathExpressionException
     * @throws IOException
     * @throws SAXException
     */
@Test
public void testLimitedWadl() throws ParserConfigurationException, XPathExpressionException, IOException, SAXException {
    Response response = target("/application.wadl").request(MediaTypes.WADL_TYPE).get();
    assertEquals(200, response.getStatus());
    File tmpFile = response.readEntity(File.class);
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
    }
    DocumentBuilder b = bf.newDocumentBuilder();
    Document d = b.parse(tmpFile);
    printSource(new DOMSource(d));
    XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    // check base URI
    String val = (String) xp.evaluate("/wadl:application/wadl:resources/@base", d, XPathConstants.STRING);
    assertEquals(val, getBaseUri().toString());
    // check total number of resources is 8
    val = (String) xp.evaluate("count(//wadl:resource)", d, XPathConstants.STRING);
    assertEquals("2", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='all-extended'])", d, XPathConstants.STRING);
    assertEquals("0", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='resource'])", d, XPathConstants.STRING);
    assertEquals("1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='application.wadl'])", d, XPathConstants.STRING);
    assertEquals("0", val);
    xp.setNamespaceContext(new SimpleNamespaceResolver("jersey", "http://jersey.java.net/"));
    val = (String) xp.evaluate("count(//jersey:extended)", d, XPathConstants.STRING);
    assertEquals("0", val);
}
Also used : Response(javax.ws.rs.core.Response) XPath(javax.xml.xpath.XPath) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document) File(java.io.File) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

XPath (javax.xml.xpath.XPath)6 SimpleNamespaceResolver (org.glassfish.jersey.internal.util.SimpleNamespaceResolver)6 Document (org.w3c.dom.Document)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 Response (javax.ws.rs.core.Response)4 File (java.io.File)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DOMSource (javax.xml.transform.dom.DOMSource)2 WebTarget (javax.ws.rs.client.WebTarget)1 Diff (org.custommonkey.xmlunit.Diff)1 SimpleNamespaceContext (org.custommonkey.xmlunit.SimpleNamespaceContext)1