public void testErrorThrowingResponse() throws Exception { URL url = getClass().getResource("applicationContext-errorResponse.xml"); try (FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString())) { Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher"); MockHttpServletRequest request = setupRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); dispatcher.handleRequest(request, response); // the output is not there final String outputContent = response.getContentAsString(); assertThat(outputContent, not(containsString("Hello world!"))); // only the exception Document dom = XMLUnit.buildTestDocument(outputContent); assertEquals("ows:ExceptionReport", dom.getDocumentElement().getNodeName()); } } }
/** * Assert that the node lists of two Xpaths in two documents are equal * @param xpathOne * @param xpathTwo * @param controlDocument * @param testDocument * @see XpathEngine */ public static void assertXpathsEqual(String controlXpath, InputSource controlDocument, String testXpath, InputSource testDocument) throws SAXException, IOException, XpathException { assertXpathsEqual(controlXpath, XMLUnit.buildControlDocument(controlDocument), testXpath, XMLUnit.buildTestDocument(testDocument)); }
/** * Assert that the node lists of two Xpaths in two documents are equal * @param controlXpath * @param testXpath * @param controlDocument * @param testDocument * @see XpathEngine */ public static void assertXpathsEqual(String controlXpath, InputSource controlDocument, String testXpath, InputSource testDocument) throws SAXException, IOException, XpathException { assertXpathsEqual(controlXpath, XMLUnit.buildControlDocument(controlDocument), testXpath, XMLUnit.buildTestDocument(testDocument)); }
/** * Assert that the evaluation of two Xpaths in two XML strings are equal * @param xpathOne * @param control * @param xpathTwo * @param test * @throws SAXException * @throws IOException */ public static void assertXpathValuesEqual(String controlXpath, InputSource control, String testXpath, InputSource test) throws SAXException, IOException, XpathException { assertXpathValuesEqual(controlXpath, XMLUnit.buildControlDocument(control), testXpath, XMLUnit.buildTestDocument(test)); }
/** * Assert that the node lists of two Xpaths in two XML strings are equal * @param controlXpath * @param inControlXMLString * @param testXpath * @param inTestXMLString * @throws SAXException * @throws IOException */ public static void assertXpathsEqual(String controlXpath, String inControlXMLString, String testXpath, String inTestXMLString) throws SAXException, IOException, XpathException { assertXpathsEqual( controlXpath, XMLUnit.buildControlDocument(inControlXMLString), testXpath, XMLUnit.buildTestDocument(inTestXMLString)); }
/** * Assert that the evaluation of two Xpaths in two XML strings are equal * @param controlXpath * @param control * @param testXpath * @param test * @throws SAXException * @throws IOException */ public static void assertXpathValuesEqual(String controlXpath, InputSource control, String testXpath, InputSource test) throws SAXException, IOException, XpathException { assertXpathValuesEqual(controlXpath, XMLUnit.buildControlDocument(control), testXpath, XMLUnit.buildTestDocument(test)); }
/** * Assert that the node lists of two Xpaths in two XML strings are NOT equal * @param controlXpath * @param inControlXMLString * @param testXpath * @param inTestXMLString * @throws SAXException * @throws IOException */ public static void assertXpathsNotEqual(String controlXpath, String inControlXMLString, String testXpath, String inTestXMLString) throws SAXException, IOException, XpathException { assertXpathsNotEqual( controlXpath, XMLUnit.buildControlDocument(inControlXMLString), testXpath, XMLUnit.buildTestDocument(inTestXMLString)); }
/** * Assert that the node lists of two Xpaths in two XML strings are * NOT equal * @param controlXpath * @param controlDocument * @param testXpath * @param testDocument * @throws SAXException * @throws IOException */ public static void assertXpathsNotEqual(String controlXpath, InputSource controlDocument, String testXpath, InputSource testDocument) throws SAXException, IOException, XpathException { assertXpathsNotEqual(controlXpath, XMLUnit.buildControlDocument(controlDocument), testXpath, XMLUnit.buildTestDocument(testDocument)); }
/** * Assert that the node lists of two Xpaths in two XML strings are equal * @param xpathOne * @param inControlXMLString * @param xpathTwo * @param inTestXMLString * @throws SAXException * @throws IOException */ public static void assertXpathsEqual(String controlXpath, String inControlXMLString, String testXpath, String inTestXMLString) throws SAXException, IOException, XpathException { assertXpathsEqual( controlXpath, XMLUnit.buildControlDocument(inControlXMLString), testXpath, XMLUnit.buildTestDocument(inTestXMLString)); }
/** * Assert that the node lists of two Xpaths in two XML strings are NOT equal * @param xpathOne * @param inControlXMLString * @param xpathTwo * @param inTestXMLString * @throws SAXException * @throws IOException */ public static void assertXpathsNotEqual(String controlXpath, String inControlXMLString, String testXpath, String inTestXMLString) throws SAXException, IOException, XpathException { assertXpathsNotEqual( controlXpath, XMLUnit.buildControlDocument(inControlXMLString), testXpath, XMLUnit.buildTestDocument(inTestXMLString)); }
/** * Assert that the evaluation of two Xpaths in two XML strings are equal * @param controlXpath * @param inControlXMLString * @param testXpath * @param inTestXMLString * @throws SAXException * @throws IOException */ public static void assertXpathValuesEqual(String controlXpath, String inControlXMLString, String testXpath, String inTestXMLString) throws SAXException, IOException, XpathException { assertXpathValuesEqual(controlXpath, XMLUnit.buildControlDocument(inControlXMLString), testXpath, XMLUnit.buildTestDocument(inTestXMLString)); }
/** * Assert that the evaluation of two Xpaths in two XML strings are equal * @param xpathOne * @param inControlXMLString * @param xpathTwo * @param inTestXMLString * @throws SAXException * @throws IOException */ public static void assertXpathValuesEqual(String controlXpath, String inControlXMLString, String testXpath, String inTestXMLString) throws SAXException, IOException, XpathException { assertXpathValuesEqual(controlXpath, XMLUnit.buildControlDocument(inControlXMLString), testXpath, XMLUnit.buildTestDocument(inTestXMLString)); }
private Document getResponseEntityAsXML(CloseableHttpResponse response) throws Exception { Document doc; doc = XMLUnit.buildTestDocument(new InputSource(response.getEntity().getContent())); doc.normalizeDocument(); return doc; }
void doGetXML( URI uri, CloseableHttpClient client, Matcher<Integer> statusMatcher, Assertions<Document> body) throws Exception { final HttpGet request = new HttpGet(jetty.getUri().resolve(uri)); final Document doc; try (CloseableHttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent()) { if (response.getStatusLine().getStatusCode() != 401) { doc = XMLUnit.buildTestDocument(new InputSource(in)); body.accept(doc); } assertThat(response.getStatusLine(), hasProperty("statusCode", statusMatcher)); } }
private void assertCount(String expectedCount) throws Exception { String resp = getResponse("/appdata/john.doe/@self/app", "GET", "xml", "application/xml"); XSDValidator.validateOpenSocial(resp); NodeList result = xp.getMatchingNodes("/:appdata/:entry", XMLUnit.buildTestDocument(resp)); Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result); assertEquals(1, v.size()); assertTrue(v.containsKey("john.doe")); if (expectedCount != null) { assertEquals(1, v.get("john.doe").size()); assertEquals(String.valueOf(expectedCount), v.get("john.doe") .get("count").get(0)); } else { assertEquals(0, v.get("john.doe").size()); } }
private void assertCount(String expectedCount) throws Exception { String resp = getResponse("/appdata/john.doe/@self/app", "GET", "xml", "application/xml"); XSDValidator.validateOpenSocial(resp); NodeList result = xp.getMatchingNodes("/:appdata/:entry", XMLUnit.buildTestDocument(resp)); Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result); assertEquals(1, v.size()); assertTrue(v.containsKey("john.doe")); if (expectedCount != null) { assertEquals(1, v.get("john.doe").size()); assertEquals(String.valueOf(expectedCount), v.get("john.doe") .get("count").get(0)); } else { assertEquals(0, v.get("john.doe").size()); } }
private void assertCount(String expectedCount) throws Exception { String resp = getResponse("/appdata/john.doe/@self/app", "GET", "xml", "application/xml"); XSDValidator.validateOpenSocial(resp); NodeList result = xp.getMatchingNodes("/:appdata/:entry", XMLUnit.buildTestDocument(resp)); Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result); assertEquals(1, v.size()); assertTrue(v.containsKey("john.doe")); if (expectedCount != null) { assertEquals(1, v.get("john.doe").size()); assertEquals(String.valueOf(expectedCount), v.get("john.doe") .get("count").get(0)); } else { assertEquals(0, v.get("john.doe").size()); } }
@Test public void testCreateActivity() throws Exception { String postData = XSDValidator.XMLDEC+"<activity><body>and dad.</body><title>hi mom!</title></activity>"; String createResponse = getResponse("/activities/john.doe/@self", "POST", postData, "xml", "application/xml"); XSDValidator.validateOpenSocial(createResponse); String resp = getResponse("/activities/john.doe/@self", "GET", "xml", "application/xml"); XSDValidator.validateOpenSocial(resp); assertEquals("0", xp.evaluate("/:response/:startIndex", XMLUnit.buildTestDocument(resp))); assertEquals("2", xp.evaluate("/:response/:totalResults", XMLUnit.buildTestDocument(resp))); NodeList nl = xp.getMatchingNodes("/:response/:list/:entry/:activity", XMLUnit.buildTestDocument(resp)); assertEquals(2, nl.getLength()); Map<String, List<String>> v = childNodesToMap(nl.item(0)); if (v.containsKey("id")) { v = childNodesToMap(nl.item(1)); } assertEquals("hi mom!", v.get("title").get(0)); assertEquals("and dad.", v.get("body").get(0)); }
@Test public void testCreateActivity() throws Exception { String postData = XSDValidator.XMLDEC+"<activity><body>and dad.</body><title>hi mom!</title></activity>"; String createResponse = getResponse("/activities/john.doe/@self", "POST", postData, "xml", "application/xml"); XSDValidator.validateOpenSocial(createResponse); String resp = getResponse("/activities/john.doe/@self", "GET", "xml", "application/xml"); XSDValidator.validateOpenSocial(resp); assertEquals("0", xp.evaluate("/:response/:startIndex", XMLUnit.buildTestDocument(resp))); assertEquals("2", xp.evaluate("/:response/:totalResults", XMLUnit.buildTestDocument(resp))); NodeList nl = xp.getMatchingNodes("/:response/:entry/:activity", XMLUnit.buildTestDocument(resp)); assertEquals(2, nl.getLength()); Map<String, List<String>> v = childNodesToMap(nl.item(0)); if (v.containsKey("id")) { v = childNodesToMap(nl.item(1)); } assertEquals("hi mom!", v.get("title").get(0)); assertEquals("and dad.", v.get("body").get(0)); }
@Test public void testOWSException() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/EPSG:4326/EPSG:4326:0/0/0/0/0"); req.addParameter("format", "text/none"); MockHttpServletResponse resp = dispatch(req); assertEquals(HttpStatus.SC_BAD_REQUEST, resp.getStatus()); assertEquals("text/xml", resp.getContentType()); Document doc = XMLUnit.buildTestDocument(resp.getContentAsString()); assertXpathExists( "//ows:ExceptionReport/ows:Exception[@exceptionCode='InvalidParameterValue']", doc); }