GeneralScriptHandler extends JsonBasedFunctionHandler { @Override public Output<JsonObject> handle(Input<JsonObject> input) throws Exception { pushLocalApplicationContext(FORKED_BRANCH, getCurrentContext().getClassLoader(), "step-functions-plugins-java-keyword-handler.jar"); pushRemoteApplicationContext(FORKED_BRANCH, ScriptHandler.PLUGIN_LIBRARIES_FILE, input.getProperties()); pushRemoteApplicationContext(FORKED_BRANCH, ScriptHandler.LIBRARIES_FILE, input.getProperties()); String scriptLanguage = input.getProperties().get(ScriptHandler.SCRIPT_LANGUAGE); Class<?> handlerClass = scriptLanguage.equals("java")?JavaJarHandler.class:ScriptHandler.class; return delegate(handlerClass.getName(), input); }  @Override Output<JsonObject> handle(Input<JsonObject> input);  }
@Test public void testJava() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "java"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "java-plugin-handler-test.jar"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("MyValue", output.getPayload().getString("MyKey")); }
@Test public void testJS223NashornHappyPath() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); input.setPayload(Json.createObjectBuilder().add("key1", "MyValue").build()); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "javascript"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "scripts/test1.js"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("MyValue", output.getPayload().getString("key1")); }
@Test public void testJS223GroovyHappyPath() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); input.setPayload(Json.createObjectBuilder().add("key1", "MyValue").build()); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "scripts/testGroovyUTF8.groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("kéÿ1", output.getPayload().getString("key1")); }
@Test public void testJS223GroovyErrorWithoutHandler() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); input.setPayload(Json.createObjectBuilder().add("key1", "MyValue").build()); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "scripts/throwable.groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("Error while running script throwable.groovy: assert false\n", output.getError().getMsg()); }
@Test public void testJS223GroovyErrorWithHandler() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); input.setPayload(Json.createObjectBuilder().add("key1", "MyValue").build()); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "scripts/throwable.groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); properties.put(ScriptHandler.ERROR_HANDLER_FILE+".id", "scripts/errorHandler.groovy"); properties.put(ScriptHandler.ERROR_HANDLER_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("Error handler called", output.getError().getMsg()); }
@Test public void testJS223GroovyErrorWithFailingHandler() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); input.setPayload(Json.createObjectBuilder().add("key1", "MyValue").build()); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "scripts/throwable.groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); properties.put(ScriptHandler.ERROR_HANDLER_FILE+".id", "scripts/throwable.groovy"); properties.put(ScriptHandler.ERROR_HANDLER_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("Error while running error handler script: throwable.groovy. assert false\n", output.getError().getMsg()); }
@Test public void testUnknownScriptLanguage() throws Exception { GeneralScriptHandler handler = createHandler(); Input<JsonObject> input = new Input<>(); input.setFunction("MyKeyword1"); input.setPayload(Json.createObjectBuilder().add("key1", "MyValue").build()); HashMap<String, String> properties = new HashMap<>(); properties.put(ScriptHandler.SCRIPT_LANGUAGE, "invalidScriptLanguage"); properties.put(ScriptHandler.SCRIPT_FILE+".id", "scripts/throwable.groovy"); properties.put(ScriptHandler.SCRIPT_FILE+".version", ""); input.setProperties(properties); Output<JsonObject> output = handler.handle(input); Assert.assertEquals("Unsupported script language: invalidScriptLanguage", output.getError().getMsg()); }