ConfigTransformer { public static void transformConfigFile(InputStream sourceStream, String destPath, Properties bootstrapProperties) throws Exception { ConvertableSchema<ConfigSchema> convertableSchema = throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(sourceStream)); ConfigSchema configSchema = throwIfInvalid(convertableSchema.convert()); SecurityPropertiesSchema securityProperties = BootstrapTransformer.buildSecurityPropertiesFromBootstrap(bootstrapProperties).orElse(null); ProvenanceReportingSchema provenanceReportingProperties = BootstrapTransformer.buildProvenanceReportingPropertiesFromBootstrap(bootstrapProperties).orElse(null); if (securityProperties != null) { configSchema.setSecurityProperties(securityProperties); logger.info("Bootstrap flow override: Replaced security properties"); } if (provenanceReportingProperties != null) { configSchema.setProvenanceReportingProperties(provenanceReportingProperties); logger.info("Bootstrap flow override: Replaced provenance reporting properties"); } if (BootstrapTransformer.processorSSLOverride(bootstrapProperties)) { for (ProcessorSchema processorConfig : configSchema.getProcessGroupSchema().getProcessors()) { processorConfig.getProperties().replace("SSL Context Service", processorConfig.getProperties().get("SSL Context Service"), "SSL-Context-Service"); logger.info("Bootstrap flow override: Replaced {} SSL Context Service with parent MiNiFi SSL", processorConfig.getName()); } } ByteArrayOutputStream nifiPropertiesOutputStream = new ByteArrayOutputStream(); writeNiFiProperties(configSchema, nifiPropertiesOutputStream); writeFlowXmlFile(configSchema, destPath); writeNiFiPropertiesFile(nifiPropertiesOutputStream, destPath); } private  ConfigTransformer(); static void transformConfigFile(InputStream sourceStream, String destPath, Properties bootstrapProperties); static final String NIFI_VERSION; static final String ROOT_GROUP; static final String NIFI_VERSION_KEY; static final Logger logger; static final String PROPERTIES_FILE_APACHE_2_0_LICENSE; }
@Test public void doesTransformOnMultipleProcessors() throws Exception { File inputFile = new File("./src/test/resources/config-multiple-processors.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformOnMultipleRemoteProcessGroups() throws Exception { File inputFile = new File("./src/test/resources/config-multiple-RPGs.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformOnMultipleInputPorts() throws Exception { File inputFile = new File("./src/test/resources/config-multiple-input-ports.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformOnMinimal() throws Exception { File inputFile = new File("./src/test/resources/config-minimal.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformOnProvenanceRepository() throws Exception { File inputFile = new File("./src/test/resources/config-provenance-repository.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); String nifi = FileUtils.readFileToString(nifiPropertiesFile, Charset.defaultCharset()); assertTrue(nifi.contains("nifi.provenance.repository.implementation=org.apache.nifi.provenance.MiNiFiPersistentProvenanceRepository")); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformOnCustomProvenanceRepository() throws Exception { File inputFile = new File("./src/test/resources/config-provenance-custom-repository.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); String nifi = FileUtils.readFileToString(nifiPropertiesFile, Charset.defaultCharset()); assertTrue(nifi.contains("nifi.provenance.repository.implementation=org.apache.nifi.provenance.CustomProvenanceRepository")); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void handleTransformInvalidFile() throws Exception { try { File inputFile = new File("./src/test/resources/config-invalid.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); fail("Invalid configuration file was not detected."); } catch (SchemaLoaderException e){ assertEquals("Provided YAML configuration is not a Map", e.getMessage()); } }
@Test public void handleTransformMalformedField() throws Exception { try { File inputFile = new File("./src/test/resources/config-malformed-field.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); fail("Invalid configuration file was not detected."); } catch (InvalidConfigurationException e){ assertEquals("Failed to transform config file due to:['threshold' in section 'Swap' because it is found but could not be parsed as a Number]", e.getMessage()); } }
@Test public void handleTransformEmptyFile() throws Exception { try { File inputFile = new File("./src/test/resources/config-empty.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); fail("Invalid configuration file was not detected."); } catch (SchemaLoaderException e){ assertEquals("Provided YAML configuration is not a Map", e.getMessage()); } }
@Test public void handleTransformFileMissingRequiredField() throws Exception { try { File inputFile = new File("./src/test/resources/config-missing-required-field.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); fail("Invalid configuration file was not detected."); } catch (InvalidConfigurationException e){ assertEquals("Failed to transform config file due to:['class' in section 'Processors' because it was not found and it is required]", e.getMessage()); } }
@Test public void handleTransformFileMultipleProblems() throws Exception { try { File inputFile = new File("./src/test/resources/config-multiple-problems.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); fail("Invalid configuration file was not detected."); } catch (InvalidConfigurationException e){ assertEquals("Failed to transform config file due to:['class' in section 'Processors' because it was not found and it is required], " + "['scheduling strategy' in section 'Provenance Reporting' because it is not a valid scheduling strategy], " + "['source name' in section 'Connections' because it was not found and it is required]", e.getMessage()); } }
@Test public void checkSSLOverrides() throws Exception { File inputFile = new File("./src/test/resources/MINIFI-516/config.yml"); final Properties bootstrapProperties = getTestBootstrapProperties("MINIFI-516/bootstrap.conf"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", bootstrapProperties); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); String flow = loadFlowXML(new FileInputStream(flowXml)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document xml = db.parse(new StringBufferInputStream(flow)); XPath xPath = XPathFactory.newInstance().newXPath(); String result = xPath.evaluate("/flowController/rootGroup/processor/property[name = \"SSL Context Service\"]/value/text()", xml); assertEquals(result, "SSL-Context-Service"); flowXml.deleteOnExit(); }
@Test public void doesTransformFile() throws Exception { File inputFile = new File("./src/test/resources/config.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformV1File() throws Exception { File inputFile = new File("./src/test/resources/config-v1.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformInputStream() throws Exception { File inputFile = new File("./src/test/resources/config.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }
@Test public void doesTransformOnDefaultFile() throws Exception { File inputFile = new File("./src/test/resources/default.yml"); ConfigTransformer.transformConfigFile(new FileInputStream(inputFile), "./target/", null); File nifiPropertiesFile = new File("./target/nifi.properties"); assertTrue(nifiPropertiesFile.exists()); assertTrue(nifiPropertiesFile.canRead()); nifiPropertiesFile.deleteOnExit(); File flowXml = new File("./target/flow.xml.gz"); assertTrue(flowXml.exists()); assertTrue(flowXml.canRead()); flowXml.deleteOnExit(); }