@Test public void xmlResponseCanBeParsed() throws Exception { String xmlResponse = "<xml>" + "<test>" + OK_RESPONSE + "</test>" + "</xml>"; respond(SC_OK, xmlResponse); String xPath = " List<String> xmls = httpHelper.queryXml(baseUrl, A_USERNAME, A_PASSWORD, xPath); assertEquals(1, xmls.size()); assertEquals(OK_RESPONSE, xmls.get(0)); }
public List<String> queryXml(String url, String username, String password, String... xpathQueries) throws IOException { byte[] xmlContent = doGet(url, username, password); List<String> results = new ArrayList<>(); try { InputSource is = new InputSource(new ByteArrayInputStream(xmlContent)); Document xmlDocument = documentBuilder.parse(is); xmlDocument.getDocumentElement().normalize(); for (String xpathQuery : xpathQueries) { String result = xPath.compile(xpathQuery).evaluate(xmlDocument); if (result == null || result.isEmpty()) { LOG.warn("XPath query {} produced no results on content: \n{}", xpathQuery, new String(xmlContent)); result = ""; } results.add(result); } } catch (XPathExpressionException | SAXException | IOException ex) { throw new IOException("Cannot perform the given xpath query", ex); } return results; }
HttpHelper { public List<String> queryXml(String url, String username, String password, String... xpathQueries) throws IOException { byte[] xmlContent = doGet(url, username, password); List<String> results = new ArrayList<>(); try { InputSource is = new InputSource(new ByteArrayInputStream(xmlContent)); Document xmlDocument = documentBuilder.parse(is); xmlDocument.getDocumentElement().normalize(); for (String xpathQuery : xpathQueries) { String result = xPath.compile(xpathQuery).evaluate(xmlDocument); if (result == null || result.isEmpty()) { LOG.warn("XPath query {} produced no results on content: \n{}", xpathQuery, new String(xmlContent)); result = ""; } results.add(result); } } catch (XPathExpressionException | SAXException | IOException ex) { throw new IOException("Cannot perform the given xpath query", ex); } return results; } }
HttpHelper { public List<String> queryXml(String url, String username, String password, String... xpathQueries) throws IOException { byte[] xmlContent = doGet(url, username, password); List<String> results = new ArrayList<>(); try { InputSource is = new InputSource(new ByteArrayInputStream(xmlContent)); Document xmlDocument = documentBuilder.parse(is); xmlDocument.getDocumentElement().normalize(); for (String xpathQuery : xpathQueries) { String result = xPath.compile(xpathQuery).evaluate(xmlDocument); if (result == null || result.isEmpty()) { LOG.warn("XPath query {} produced no results on content: \n{}", xpathQuery, new String(xmlContent)); result = ""; } results.add(result); } } catch (XPathExpressionException | SAXException | IOException ex) { throw new IOException("Cannot perform the given xpath query", ex); } return results; } HttpHelper(); }
HttpHelper { public List<String> queryXml(String url, String username, String password, String... xpathQueries) throws IOException { byte[] xmlContent = doGet(url, username, password); List<String> results = new ArrayList<>(); try { InputSource is = new InputSource(new ByteArrayInputStream(xmlContent)); Document xmlDocument = documentBuilder.parse(is); xmlDocument.getDocumentElement().normalize(); for (String xpathQuery : xpathQueries) { String result = xPath.compile(xpathQuery).evaluate(xmlDocument); if (result == null || result.isEmpty()) { LOG.warn("XPath query {} produced no results on content: \n{}", xpathQuery, new String(xmlContent)); result = ""; } results.add(result); } } catch (XPathExpressionException | SAXException | IOException ex) { throw new IOException("Cannot perform the given xpath query", ex); } return results; } HttpHelper(); String retrieveContent(String url); byte[] post(String url, byte[] content, String username, String password); byte[] post(String url, byte[] content, String username, String password,
            Map<String, String> headers); String retrieveContent(String url, String username, String password); List<String> queryXml(String url, String username, String password, String... xpathQueries); void setConnectionTimeout(int connectionTimeout); }
HttpHelper { public List<String> queryXml(String url, String username, String password, String... xpathQueries) throws IOException { byte[] xmlContent = doGet(url, username, password); List<String> results = new ArrayList<>(); try { InputSource is = new InputSource(new ByteArrayInputStream(xmlContent)); Document xmlDocument = documentBuilder.parse(is); xmlDocument.getDocumentElement().normalize(); for (String xpathQuery : xpathQueries) { String result = xPath.compile(xpathQuery).evaluate(xmlDocument); if (result == null || result.isEmpty()) { LOG.warn("XPath query {} produced no results on content: \n{}", xpathQuery, new String(xmlContent)); result = ""; } results.add(result); } } catch (XPathExpressionException | SAXException | IOException ex) { throw new IOException("Cannot perform the given xpath query", ex); } return results; } HttpHelper(); String retrieveContent(String url); byte[] post(String url, byte[] content, String username, String password); byte[] post(String url, byte[] content, String username, String password,
            Map<String, String> headers); String retrieveContent(String url, String username, String password); List<String> queryXml(String url, String username, String password, String... xpathQueries); void setConnectionTimeout(int connectionTimeout);  }