@Test public void shouldReturnOneDotZeroForVersion() { assertThat(new JsonBasedTaskExtensionHandler_V1().version(), is("1.0")); }
@Override public String convertTaskConfigToJson(TaskConfig taskConfig) { return new Gson().toJson(configPropertiesAsMap(taskConfig)); }
@Test public void shouldKeepTheConfigInTheOrderOfDisplayOrder(){ String json = "{\"URL\":{\"default-value\":\"\",\"secure\":false,\"required\":true,\"display-name\":\"Url\",\"display-order\":\"0\"}," + "\"PASSWORD\":{\"display-order\":\"2\"}," + "\"USER\":{\"default-value\":\"foo\",\"secure\":true,\"required\":false,\"display-order\":\"1\"}" + "}"; TaskConfig config = new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json); assertThat(config.list().get(0).getKey(), is("URL")); assertThat(config.list().get(1).getKey(), is("USER")); assertThat(config.list().get(2).getKey(), is("PASSWORD")); }
@Test public void shouldThrowExceptionForWrongJsonWhileConvertingJsonResponseToValidation() { Assert.assertTrue(new JsonBasedTaskExtensionHandler_V1().toValidationResult("{\"errors\":{}}").isSuccessful()); Assert.assertTrue(new JsonBasedTaskExtensionHandler_V1().toValidationResult("{}").isSuccessful()); Assert.assertTrue(new JsonBasedTaskExtensionHandler_V1().toValidationResult("").isSuccessful()); Assert.assertTrue(new JsonBasedTaskExtensionHandler_V1().toValidationResult(null).isSuccessful()); String jsonResponse2 = "{\"errors\":{\"key1\":\"err1\",\"key2\":true}}"; try { new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse2); fail("should throw exception"); } catch (Exception e) { assertThat(e.getMessage(), is("Error occurred while converting the Json to Validation Result. Error: Key: 'key2' - The Json for Validation Request must contain a not-null error message of type String.")); } String jsonResponse3 = "{\"errors\":{\"key1\":null}}"; try { new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse3); fail("should throw exception"); } catch (Exception e) { assertThat(e.getMessage(), is("Error occurred while converting the Json to Validation Result. Error: Key: 'key1' - The Json for Validation Request must contain a not-null error message of type String.")); } String jsonResponse4 = "{\"errors\":{\"key1\":true,\"key2\":\"err2\",\"key3\":null}}"; try { new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse4); fail("should throw exception"); } catch (Exception e) { assertThat(e.getMessage(), is("Error occurred while converting the Json to Validation Result. Error: Key: 'key1' - The Json for Validation Request must contain a not-null error message of type String, Key: 'key3' - The Json for Validation Request must contain a not-null error message of type String.")); } }
String json1 = "{}"; try { new JsonBasedTaskExtensionHandler_V1().toExecutionResult(json1); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toExecutionResult(json2); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toExecutionResult(json3); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toExecutionResult(json4); fail("should throw exception"); } catch (Exception e) {
@Test public void shouldCreateTaskViewFromResponse() { String jsonResponse = "{\"displayValue\":\"MyTaskPlugin\", \"template\":\"<html>junk</html>\"}"; TaskView view = new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse); Assert.assertThat(view.displayValue(), is("MyTaskPlugin")); Assert.assertThat(view.template(), is("<html>junk</html>")); }
@Test public void shouldReturnRequestBodyForTaskExecution() { TaskExecutionContext context = mock(TaskExecutionContext.class); String workingDir = "working-dir"; TaskConfig config = new TaskConfig(); config.add(new TaskConfigProperty("Property1", "Value1")); config.add(new TaskConfigProperty("Property2", "Value2")); when(context.workingDir()).thenReturn(workingDir); when(context.environment()).thenReturn(getEnvironmentVariables()); String requestBody = new JsonBasedTaskExtensionHandler_V1().getTaskExecutionBody(config, context); Map result = (Map) new GsonBuilder().create().fromJson(requestBody, Object.class); Map taskExecutionContextFromRequest = (Map) result.get("context"); assertThat(taskExecutionContextFromRequest.get("workingDirectory"), is(workingDir)); Map environmentVariables = (Map) taskExecutionContextFromRequest.get("environmentVariables"); assertThat(environmentVariables.size(), is(2)); assertThat(environmentVariables.get("ENV1").toString(), is("VAL1")); assertThat(environmentVariables.get("ENV2").toString(), is("VAL2")); Map<String,Object> taskConfigMap = (Map<String,Object>) result.get("config"); assertThat(taskConfigMap.size(), is(2)); Map property1 = (Map) taskConfigMap.get("Property1"); Map property2 = (Map) taskConfigMap.get("Property2"); assertThat(property1.get("value").toString(), is("Value1")); assertThat(property2.get("value").toString(), is("Value2")); }
@Test public void shouldConvertTaskConfigObjectToJson() { TaskConfig taskConfig = new TaskConfig(); TaskConfigProperty p1 = new TaskConfigProperty("k1", "value1"); p1.with(Property.SECURE, true); p1.with(Property.REQUIRED, true); TaskConfigProperty p2 = new TaskConfigProperty("k2", "value2"); p2.with(Property.SECURE, false); p2.with(Property.REQUIRED, true); taskConfig.add(p1); taskConfig.add(p2); String json = new JsonBasedTaskExtensionHandler_V1().convertTaskConfigToJson(taskConfig); Map taskConfigMap = (Map) new GsonBuilder().create().fromJson(json, Object.class); Map property1 = (Map) taskConfigMap.get("k1"); assertThat(property1.get("value").toString(), is("value1")); assertThat(property1.get("secure"), is(true)); assertThat(property1.get("required"), is(true)); Map property2 = (Map) taskConfigMap.get("k2"); assertThat(property2.get("value").toString(), is("value2")); assertThat(property2.get("secure"), is(false)); assertThat(property2.get("required"), is(true)); }
@Autowired public TaskExtension(PluginManager pluginManager) { super(pluginManager, new PluginRequestHelper(pluginManager, supportedVersions, PLUGGABLE_TASK_EXTENSION), PLUGGABLE_TASK_EXTENSION); registerHandler(JsonBasedTaskExtensionHandler_V1.VERSION, new PluginSettingsJsonMessageHandler1_0()); messageHandlerMap.put(JsonBasedTaskExtensionHandler_V1.VERSION, new JsonBasedTaskExtensionHandler_V1()); }
String json1 = "{}"; try { new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json1); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json2); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json3); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json4); fail("should throw exception"); } catch (Exception e) { "\"URL3\":{\"default-value\":\"some value\",\"secure\":true,\"required\":\"some-string\"}}"; try { new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json5); fail("should throw exception"); } catch (Exception e) { assertThat(new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig("{\"URL\":{\"display-order\":\"1\"}}").get("URL").getOption(Property.DISPLAY_ORDER), is(1)); new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig("{\"URL\":{\"display-order\":\"first\"}}"); fail("should throw exception"); } catch (Exception e) {
@Test public void shouldConvertJsonResponseToValidationResultWhenValidationPasses() { String jsonResponse = "{}"; TaskConfig configuration = new TaskConfig(); TaskConfigProperty property = new TaskConfigProperty("URL", "http://foo"); property.with(Property.SECURE, false); property.with(Property.REQUIRED, true); configuration.add(property); ValidationResult result = new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse); Assert.assertThat(result.isSuccessful(), is(true)); }
@Test public void shouldConstructExecutionResultFromFailureExecutionResponse() { GoPluginApiResponse response = mock(GoPluginApiResponse.class); when(response.responseBody()).thenReturn("{\"success\":false,\"message\":\"error1\"}"); ExecutionResult result = new JsonBasedTaskExtensionHandler_V1().toExecutionResult(response.responseBody()); assertThat(result.isSuccessful(), is(false)); assertThat(result.getMessagesForDisplay(), is("error1")); }
String jsonResponse1 = "{}"; try { new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse1); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse2); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse3); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse4); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse5); fail("should throw exception"); } catch (Exception e) { new JsonBasedTaskExtensionHandler_V1().toTaskView(jsonResponse6); fail("should throw exception"); } catch (Exception e) {
@Before public void setup() { pluginManager = mock(PluginManager.class); pluginId = "plugin-id"; final List<String> goSupportedVersions = asList("1.0"); final HashMap<String, JsonBasedTaskExtensionHandler> handlerMap = new HashMap<>(); handlerMap.put("1.0", new JsonBasedTaskExtensionHandler_V1()); task = new JsonBasedPluggableTask(pluginId, new PluginRequestHelper(pluginManager, goSupportedVersions, PLUGGABLE_TASK_EXTENSION), handlerMap); goPluginApiResponse = mock(GoPluginApiResponse.class); when(pluginManager.submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), any(GoPluginApiRequest.class))).thenReturn(goPluginApiResponse); when(pluginManager.resolveExtensionVersion(pluginId, PLUGGABLE_TASK_EXTENSION, goSupportedVersions)).thenReturn("1.0"); when(goPluginApiResponse.responseCode()).thenReturn(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE); when(pluginManager.isPluginOfType(PLUGGABLE_TASK_EXTENSION, pluginId)).thenReturn(true); }
@Test public void shouldConvertTaskConfigJsonToTaskConfig() { String json = "{\"URL\":{\"default-value\":\"\",\"secure\":false,\"required\":true,\"display-name\":\"Url\",\"display-order\":\"0\"}," + "\"USER\":{\"default-value\":\"foo\",\"secure\":true,\"required\":false,\"display-order\":\"1\"}," + "\"PASSWORD\":{}," + "\"FOO\":null" + "}"; TaskConfig config = new JsonBasedTaskExtensionHandler_V1().convertJsonToTaskConfig(json); Property url = config.get("URL"); assertThat(url.getOption(Property.REQUIRED), is(true)); assertThat(url.getOption(Property.SECURE), is(false)); assertThat(url.getOption(Property.DISPLAY_NAME), is("Url")); assertThat(url.getOption(Property.DISPLAY_ORDER), is(0)); Property user = config.get("USER"); assertThat(user.getOption(Property.REQUIRED), is(false)); assertThat(user.getOption(Property.SECURE), is(true)); assertThat(user.getOption(Property.DISPLAY_NAME), is("USER")); assertThat(user.getOption(Property.DISPLAY_ORDER), is(1)); Property password = config.get("PASSWORD"); assertThat(password.getOption(Property.REQUIRED), is(true)); assertThat(password.getOption(Property.SECURE), is(false)); assertThat(password.getOption(Property.DISPLAY_NAME), is("PASSWORD")); assertThat(password.getOption(Property.DISPLAY_ORDER), is(0)); Property foo = config.get("FOO"); assertThat(foo.getOption(Property.REQUIRED), is(true)); assertThat(foo.getOption(Property.SECURE), is(false)); assertThat(foo.getOption(Property.DISPLAY_NAME), is("FOO")); assertThat(foo.getOption(Property.DISPLAY_ORDER), is(0)); }
@Test public void shouldConvertJsonResponseToValidationResultWhenValidationFails() { String jsonResponse = "{\"errors\":{\"key1\":\"err1\",\"key2\":\"err2\"}}"; TaskConfig configuration = new TaskConfig(); TaskConfigProperty property = new TaskConfigProperty("URL", "http://foo"); property.with(Property.SECURE, false); property.with(Property.REQUIRED, true); configuration.add(property); ValidationResult result = new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse); Assert.assertThat(result.isSuccessful(), is(false)); ValidationError error1 = result.getErrors().get(0); ValidationError error2 = result.getErrors().get(1); Assert.assertThat(error1.getKey(), is("key1")); Assert.assertThat(error1.getMessage(), is("err1")); Assert.assertThat(error2.getKey(), is("key2")); Assert.assertThat(error2.getMessage(), is("err2")); }
@Test public void shouldConstructExecutionResultFromSuccessfulExecutionResponse() { GoPluginApiResponse response = mock(GoPluginApiResponse.class); when(response.responseBody()).thenReturn("{\"success\":true,\"message\":\"message1\"}"); ExecutionResult result = new JsonBasedTaskExtensionHandler_V1().toExecutionResult(response.responseBody()); assertThat(result.isSuccessful(), is(true)); assertThat(result.getMessagesForDisplay(), is("message1")); }
handler = new JsonBasedTaskExtensionHandler_V1(); handlerHashMap.put("1.0", handler); new JsonBasedTaskExecutor(pluginId, pluginRequestHelper, handlerHashMap).execute(config(), context);
@Override public String getTaskExecutionBody(TaskConfig config, TaskExecutionContext taskExecutionContext) { Map requestBody = new HashMap(); Map contextMap = new HashMap(); contextMap.put("environmentVariables", taskExecutionContext.environment().asMap()); contextMap.put("workingDirectory", taskExecutionContext.workingDir()); requestBody.put("context", contextMap); requestBody.put("config", configPropertiesAsMap(config)); return new Gson().toJson(requestBody); }