TemplateRenderer { public String renderTemplateFile(File templateFile, Map<String, Object> contextMap) { for (Map.Entry<String, Object> entry : contextMap.entrySet()) { renderContext.put(entry.getKey(), entry.getValue()); } return renderTemplateFile(templateFile); } TemplateRenderer();  TemplateRenderer(Map<String, Object> initMap); String renderTemplateFile(File templateFile, Map<String, Object> contextMap); String renderTemplateFile(File templateFile); Optional<String> renderClassPathWorkflow(); static final String CLASSPATH_WORKFLOW_XML; }
@Test public void testRenderTemplateFileDoesNotModifyWithoutProperties() throws IOException { File file = composeTemplateXmlToFile(); String result = tr.renderTemplateFile(file); assertThat(result).isEqualTo(composeTemplateXml()); }
@Test public void testRenderTemplateFileDoesNotModifyWithEmptyPropertiesFromInstantiate() throws IOException { TemplateRenderer trWithCustomProperties = mkTemplateRendererWithPropertiesMap(properties0); File file = composeTemplateXmlToFile(); String result = trWithCustomProperties.renderTemplateFile(file); assertThat(result).isEqualTo(composeTemplateXml()); }
@Test public void testRenderTemplateFileDoesNotModifyWithEmptyPropertiesFromWithinTemplate() throws IOException { File file = composeTemplateXmlToFile(properties0); String result = tr.renderTemplateFile(file); assertThat(result).isEqualTo(composeTemplateXml(properties0)); }
@Test public void testRenderTemplateFileSubstitutedPropertiesFromInstantiate() throws IOException { TemplateRenderer trWithCustomProperties = mkTemplateRendererWithPropertiesMap(properties1); File file = composeTemplateXmlToFile(); String result = trWithCustomProperties.renderTemplateFile(file); assertThat(result).doesNotContain("$" + property1key); assertThat(result).doesNotContain("${" + property1key + "}"); assertThat(result).contains("$" + property2key); assertThat(result).contains("<test" + property1key + ">" + property1value + "</test" + property1key + ">"); }
@Test public void testRenderTemplateFileSubstitutedPropertiesFromWithinTemplate() throws IOException { File file = composeTemplateXmlToFile(properties1); String result = tr.renderTemplateFile(file); assertThat(result).doesNotContain("$" + property1key); assertThat(result).doesNotContain("${" + property1key + "}"); assertThat(result).contains("$" + property2key); assertThat(result).contains("<test" + property1key + ">" + property1value + "</test" + property1key + ">"); }
@Test public void testRenderTemplateFileSubstitutedPropertiesFromBothDirections() throws IOException { TemplateRenderer trWithCustomProperties = mkTemplateRendererWithPropertiesMap(properties2); File file = composeTemplateXmlToFile(properties1); String result = trWithCustomProperties.renderTemplateFile(file); assertThat(result).doesNotContain("$" + property1key); assertThat(result).doesNotContain("${" + property1key + "}"); assertThat(result).doesNotContain("$" + property2key); assertThat(result).contains("<test" + property1key + ">" + property1value + "</test" + property1key + ">"); assertThat(result).contains("<test" + property2key + ">" + property2value + "</test" + property2key + ">"); }
@Test public void testRenderTemplateFileSubstitutedPropertiesFromBothDirectionsAndUnusedProperties() throws IOException { TemplateRenderer trWithCustomProperties = mkTemplateRendererWithPropertiesMap(properties2and3); File file = composeTemplateXmlToFile(properties1and3); String result = trWithCustomProperties.renderTemplateFile(file); assertThat(result).doesNotContain("$" + property1key); assertThat(result).doesNotContain("${" + property1key + "}"); assertThat(result).doesNotContain("$" + property2key); assertThat(result).contains("<test" + property1key + ">" + property1value + "</test" + property1key + ">"); assertThat(result).contains("<test" + property2key + ">" + property2value + "</test" + property2key + ">"); }
@Test public void testRenderTemplateFileSubstitutesPropertiesWithDotsFromContext() throws IOException { TemplateRenderer trWithCustomProperties = mkTemplateRendererWithPropertiesMap(new HashMap<>()); File file = writeTemplateFromString(wrapTemplateXml(addPropertiesToTemplate(exampleDotTemplate, new HashMap<>()))); HashMap<String, Object> contextMap = new HashMap<>(); contextMap.put("property", new Property()); String result = trWithCustomProperties.renderTemplateFile(file, contextMap); assertThat(result).doesNotContain("$" + propertyWithDotKey); assertThat(result).doesNotContain("${" + propertyWithDotKey + "}"); assertThat(result).doesNotContain("$" + propertyWithDotKey); assertThat(result).contains("<test" + propertyWithDotValue + ">" + propertyWithDotValue + "</test" + propertyWithDotValue + ">"); }
@Test(expected = ExecutionException.class) public void testRenderTemplateFileShouldNotReturnInvalidXmlIfTemplateWasInvalid() throws IOException { String invalidXmlTemplate = "<invalid xml"; File file = writeTemplateFromString(invalidXmlTemplate); tr.renderTemplateFile(file); }
@Test public void testRenderWorkflowFileWithSystemEnvironmentVariables() throws IOException, ParserConfigurationException, SAXException{ environmentVariables.set("ANTENNATESTVARIABLE", "WARN"); Map<String, String> systemEnvs = System.getenv(); assertThat(systemEnvs.get("ANTENNATESTVARIABLE")).isEqualTo("WARN"); HashMap<String, Object> contextMap = new HashMap<>(); File workflowDefFile = new File(TemplateRendererTest.class.getResource("/workflow.xml").getFile()); TemplateRenderer tr = new TemplateRenderer(contextMap); String renderedWorkflow = tr.renderTemplateFile(workflowDefFile); Workflow workflow; XmlSettingsReader workflowReader = new XmlSettingsReader(renderedWorkflow); workflow = workflowReader.getComplexType("workflow", Workflow.class); assertThat(workflow.getProcessors().getStep().stream().findFirst().get().getConfiguration().getEntry() .stream() .filter(entry -> entry.getKey().equals("incompleteSourcesSeverity")) .findFirst() .get() .getValue()) .isEqualTo("WARN"); }