FlowParser { public Document parse(final File flowConfigurationFile) { if (flowConfigurationFile == null) { logger.debug("Flow Configuration file was null"); return null; } final Path flowPath = flowConfigurationFile.toPath(); try { if (!Files.exists(flowPath) || Files.size(flowPath) == 0) { logger.warn("Flow Configuration does not exist or was empty"); return null; } } catch (IOException e) { logger.error("An error occurred determining the size of the Flow Configuration file"); return null; } try (final InputStream in = Files.newInputStream(flowPath, StandardOpenOption.READ); final InputStream gzipIn = new GZIPInputStream(in)) { final byte[] flowBytes = IOUtils.toByteArray(gzipIn); if (flowBytes == null || flowBytes.length == 0) { logger.warn("Could not extract root group id because Flow Configuration File was empty"); return null; } final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true); docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); docBuilder.setErrorHandler(new LoggingXmlParserErrorHandler("Flow Configuration", logger)); final Document document = docBuilder.parse(new ByteArrayInputStream(flowBytes)); return document; } catch (final SAXException | ParserConfigurationException | IOException ex) { logger.error("Unable to parse flow {} due to {}", new Object[]{flowPath.toAbsolutePath(), ex}); return null; } }  Document parse(final File flowConfigurationFile); void writeFlow(final Document flowDocument, final Path flowXmlPath); static List<Element> getChildrenByTagName(final Element element, final String tagName);  }
@Test public void testCanParseFlow() { FlowParser flowParser = new FlowParser(); Document parsedFlow = flowParser.parse(new File(TEST_FLOW_XML_PATH)); Assert.assertNotNull(parsedFlow); NodeList processors = parsedFlow.getElementsByTagName(FlowEnricher.PROCESSOR_TAG_NAME); NodeList controllerServices = parsedFlow.getElementsByTagName(FlowEnricher.CONTROLLER_SERVICE_TAG_NAME); NodeList reportingTasks = parsedFlow.getElementsByTagName(FlowEnricher.REPORTING_TASK_TAG_NAME); Assert.assertEquals("Verify correct number of processors", 3, processors.getLength()); Assert.assertEquals("Verify correct number of controller services", 1, controllerServices.getLength()); Assert.assertEquals("Verify correct number of reporting tasks", 0, reportingTasks.getLength()); }