for (String field : fields) { try { String value = extract(doc, field); if (value != null) { parsedFields.put(field, value);
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); documentBuilderSupplier = Suppliers.ofInstance(DocumentBuilderFactory.newInstance().newDocumentBuilder()); xpathSupplier = Suppliers.ofInstance(XPathFactory.newInstance().newXPath()); xmlFieldExtractor = new XmlFieldExtractor(logger, documentBuilderSupplier, xpathSupplier); }
/** * Verifies that if the DocumentBuilder supplier returns null, the {@code extract} method will * return an empty map. */ @Test public void testExtract_nullDocumentBuilder_returnsEmptyMap() { xmlFieldExtractor = new XmlFieldExtractor(logger, Suppliers.<DocumentBuilder>ofInstance(null), xpathSupplier); Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream(REPORT_DOWNLOAD_ERROR.getBytes(UTF_8)), new String[] {"trigger"}); assertEquals(0, ret.size()); verify(logger, times(1)).warn(contains("DocumentBuilder")); }
for (String field : fields) { try { String value = extract(doc, field); if (value != null) { parsedFields.put(field, value);
@Test public void testExtract_singleField() { Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream(REPORT_DOWNLOAD_ERROR.getBytes(UTF_8)), new String[] {"trigger"}); assertEquals("AdFormatt", ret.get("trigger")); }
@Test public void testExtract_fieldNotInXml() { Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream(REPORT_DOWNLOAD_ERROR.getBytes(UTF_8)), new String[] {"foo"}); assertNull(ret.get("foo")); }
@Test public void testConvert_fieldNotInXml_returnsEmptyMap() { Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream(REPORT_DOWNLOAD_ERROR.getBytes(UTF_8)), new String[] {"foo"}); assertEquals(0, ret.size()); }
@Test public void testExtract_IOExceptionThrown_returnsEmptyMap() throws IOException { InputStream mockInputStream = mock(InputStream.class); String message = "Intentionally throwing IOException"; IOException exception = new IOException(message); when(mockInputStream.read()).thenThrow(exception); Map<String, String> ret = xmlFieldExtractor.extract(mockInputStream, new String[] {"trigger"}); assertEquals(0, ret.size()); verify(logger, times(1)).warn(contains("input"), same(exception)); }
@Test public void testExtract_invalidXml_returnsEmptyMap() { Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream("this is not XML".getBytes(UTF_8)), new String[] {"trigger"}); assertEquals(0, ret.size()); verify(logger, times(1)).warn(contains("XML"), isA(SAXException.class)); } }
@Test public void testExtract_invalidXPath_returnsEmptyMap() { Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream(REPORT_DOWNLOAD_ERROR.getBytes(UTF_8)), new String[] {"not valid XPath"}); assertEquals(0, ret.size()); verify(logger, times(1)).warn(contains("not valid XPath"), isA(XPathExpressionException.class)); }
@Test public void testExtract_multipleFields() { Map<String, String> ret = xmlFieldExtractor.extract( new ByteArrayInputStream(REPORT_DOWNLOAD_ERROR.getBytes(UTF_8)), new String[] {"type", "trigger", "fieldPath"}); assertEquals("ReportDefinitionError.INVALID_FIELD_NAME_FOR_REPORT", ret.get("type")); assertEquals("AdFormatt", ret.get("trigger")); assertEquals("foobar", ret.get("fieldPath")); }
exceptionBuilder.build(rawResponse.getHttpStatus(), responseText); Map<String, String> fields = xmlFieldExtractor.extract( new ByteArrayInputStream(responseText.getBytes(rawResponse.getCharset())), new String[] {"fieldPath", "trigger", "type"});
exceptionBuilder.build(rawResponse.getHttpStatus(), responseText); Map<String, String> fields = xmlFieldExtractor.extract( new ByteArrayInputStream(responseText.getBytes(rawResponse.getCharset())), new String[] {"fieldPath", "trigger", "type"});