### Question:
WorkflowConfigurationParser { Object evaluateReader(Reader r, String fileName, StateDatabaseConnection connection) throws Exception { Global scope = jsConfigParser.createGlobalScope(); Object wrappedThis = Context.javaToJS(this, scope); Map jsProperties = Maps.newHashMap(additionalJsVariables); jsProperties.put("celosWorkflowConfigurationParser", wrappedThis); jsProperties.put("celosConnection", connection); jsConfigParser.putPropertiesInScope(jsProperties, scope); InputStream scripts = WorkflowConfigurationParser.class.getResourceAsStream(CELOS_SCRIPTS_FILENAME); jsConfigParser.evaluateReader(scope, new InputStreamReader(scripts), CELOS_SCRIPTS_FILENAME); return jsConfigParser.evaluateReader(scope, r, fileName); } WorkflowConfigurationParser(File defaultsDir, Map<String, String> additionalJsVariables); WorkflowConfigurationParser parseConfiguration(File workflowsDir, StateDatabaseConnection connection); WorkflowConfiguration getWorkflowConfiguration(); void importDefaultsIntoScope(String label, Global scope); void addWorkflow(Workflow wf); static final String WORKFLOW_FILE_EXTENSION; }### Answer:
@Test public void evaluatesAdditionalVar() throws Exception { WorkflowConfigurationParser parser = new WorkflowConfigurationParser(new File("unused"), ImmutableMap.of("var1", "val1")); Object jsResult = parser.evaluateReader(new StringReader("var1"), "string", new MemoryStateDatabase().openConnection()); Assert.assertEquals(jsResult, "val1"); }
@Test public void testTakesDefaultUsername() throws Exception { URL resource = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/defaults-oozie-props"); File defaults = new File(resource.toURI()); WorkflowConfigurationParser parser = new WorkflowConfigurationParser(defaults, ImmutableMap.of("var1", "val1")); String func = "function (slotId) {" + " return {" + " \"oozie.wf.application.path\": \"/workflow.xml\"," + " \"inputDir\": \"/input\"," + " \"outputDir\": \"/output\"" + " }" + " }"; String str = "importDefaults(\"test\"); celos.makePropertiesGen(" + func + "); "; NativeJavaObject jsResult = (NativeJavaObject) parser.evaluateReader(new StringReader(str), "string", new MemoryStateDatabase().openConnection()); PropertiesGenerator generator = (PropertiesGenerator) jsResult.unwrap(); Assert.assertEquals(generator.getProperties(null).get("user.name").asText(), "default"); }
@Test public void testTakesChangesUsername() throws Exception { URL resource = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/defaults-oozie-props"); File defaults = new File(resource.toURI()); WorkflowConfigurationParser parser = new WorkflowConfigurationParser(defaults, ImmutableMap.of("CELOS_USER_JS_VAR", "nameIsChanged")); String func = "function (slotId) {" + " return {" + " \"oozie.wf.application.path\": \"/workflow.xml\"," + " \"inputDir\": \"/input\"," + " \"outputDir\": \"/output\"" + " }" + " }"; String str = "importDefaults(\"test\"); celos.makePropertiesGen(" + func + "); "; NativeJavaObject jsResult = (NativeJavaObject) parser.evaluateReader(new StringReader(str), "string", new MemoryStateDatabase().openConnection()); PropertiesGenerator generator = (PropertiesGenerator) jsResult.unwrap(); Assert.assertEquals(generator.getProperties(null).get("user.name").asText(), "nameIsChanged"); }
@Test public void doesntEvaluateAdditionalVar() throws Exception { WorkflowConfigurationParser parser = new WorkflowConfigurationParser(new File("unused"), ImmutableMap.<String, String>of()); try { parser.evaluateReader(new StringReader("var1"), "string", new MemoryStateDatabase().openConnection()); } catch (Exception e) { if (e.getMessage().contains("\"var1\" is not defined")) { return; } } Assert.fail(); }