@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.")); } }
@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 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)); }