### Question:
XmlFieldExtractor { public Map<String, String> extract(InputStream xml, String[] fields) { Map<String, String> parsedFields = Maps.newHashMap(); try { DocumentBuilder documentBuilder = documentBuilderSupplier.get(); if (documentBuilder == null) { logger.warn("Could not create DocumentBuilder"); return parsedFields; } Document doc = documentBuilder.parse(xml); for (String field : fields) { try { String value = extract(doc, field); if (value != null) { parsedFields.put(field, value); } } catch (XPathExpressionException e) { logger.warn("Invalid XPath: " + field, e); } } } catch (SAXException e) { logger.warn("Couldn't process XML into a Document", e); } catch (IOException e) { logger.warn("Problem reading input stream", e); } return parsedFields; } @Inject  XmlFieldExtractor(
      @Named("soapXmlLogger") Logger soapXmlLogger,
      Supplier<DocumentBuilder> documentBuilderSupplier,
      Supplier<XPath> xpathSupplier); Map<String, String> extract(InputStream xml, String[] fields);  }### Answer:
@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 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")); }
@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_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_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")); }
@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)); }