DelegateFactory { public Delegate parseDelegate(final String delegateName, final Buffer buffer) throws ValidationException { final String configString; try { configString = StringUtils.replaceWildcardConfigs(buffer.toString("UTF-8"), properties); } catch(Exception e){ throw new ValidationException(e); } ValidationResult validationResult = Validator.validateStatic(Buffer.buffer(configString), delegatesSchema, LOG); if(!validationResult.isSuccess()){ throw new ValidationException(validationResult); } return createDelegate(delegateName, configString); } DelegateFactory(final MonitoringHandler monitoringHandler, final HttpClient selfClient, final Map<String, Object> properties, final String delegatesSchema); Delegate parseDelegate(final String delegateName, final Buffer buffer);  }
@Test public void testInvalidJson() throws ValidationException { thrown.expect( ValidationException.class ); thrown.expectMessage("Unable to parse json"); delegateFactory.parseDelegate("someDelegate", Buffer.buffer(INVALID_JSON)); }
@Test public void testValidDelegateConfig(TestContext context) throws ValidationException { Delegate delegate = delegateFactory.parseDelegate("someDelegate", Buffer.buffer(VALID_DELEGATE_RESOURCE)); context.assertNotNull(delegate); context.assertEquals("someDelegate", delegate.getName()); }
@Test public void testMissingProperty(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(MISSING_PROPERTY_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'pattern' property is missing"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; context.assertEquals("required", jsonObject.getString("type")); context.assertEquals("pattern", jsonObject.getJsonArray("arguments").getString(0)); } } }
@Test public void testUnwantedAdditionalProperty(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(UNWANTED_PROPERTY_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since additional 'foo' property is not allowed"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; context.assertEquals("additionalProperties", jsonObject.getString("type")); context.assertEquals("foo", jsonObject.getJsonArray("arguments").getString(0)); } } }
@Test public void testPayloadAndTransformPropertyConcurrently(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(PAYLOAD_TRANSFORM_PROPERTY_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'payload' and 'transform' properties are not allowed concurrently"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; context.assertEquals("oneOf", jsonObject.getString("type")); } } }
@Test public void testTransformPropertyNotObject(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(TRANSFORM_PROPERTY_NOT_OBJECT_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'transform' property must be an object"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; context.assertEquals("type", jsonObject.getString("type")); context.assertEquals("integer", jsonObject.getJsonArray("arguments").getString(0)); context.assertEquals("array", jsonObject.getJsonArray("arguments").getString(1)); } } }
@Test public void testInvalidTransformDelegateConfig(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(INVALID_TRANSFORM_PROPERTY_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'transform' spec is not valid"); } catch(ValidationException ex){ context.assertEquals("Could not parse json transform specification of delegate someDelegate", ex.getMessage()); } }
@Test public void testValidTransformDelegateConfig(TestContext context) throws ValidationException { Delegate delegate = delegateFactory.parseDelegate("someDelegate", Buffer.buffer(VALID_TRANSFORM_PROPERTY_DELEGATE_RESOURCE)); context.assertNotNull(delegate); context.assertEquals("someDelegate", delegate.getName()); }
@Test public void testTransformWithMetadataPropertyNotObject(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(TRANSFORM_WITH_METADATA_PROPERTY_NOT_OBJECT_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'transformWithMetadata' property must be an object"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; context.assertEquals("type", jsonObject.getString("type")); context.assertEquals("integer", jsonObject.getJsonArray("arguments").getString(0)); context.assertEquals("array", jsonObject.getJsonArray("arguments").getString(1)); } } }
@Test public void testInvalidTransformWithMetadataDelegateConfig(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(INVALID_TRANSFORM_WITH_METADATA_PROPERTY_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'transformWithMetadata' spec is not valid"); } catch(ValidationException ex){ context.assertEquals("Could not parse json transformWithMetadata specification of delegate someDelegate", ex.getMessage()); } }
@Test public void testTransformAndTransformWithMetadataPropertyConcurrently(TestContext context) throws ValidationException { try { delegateFactory.parseDelegate("someDelegate", Buffer.buffer(TRANSFORM_TRANSFORM_WITH_METADATA_PROPERTY_DELEGATE_RESOURCE)); context.fail("Should have thrown a ValidationException since 'transform' and 'transformWithMetadata' properties are not allowed concurrently"); } catch(ValidationException ex){ context.assertNotNull(ex.getValidationDetails()); context.assertEquals(1, ex.getValidationDetails().size()); for(Object obj : ex.getValidationDetails()){ JsonObject jsonObject = (JsonObject) obj; context.assertEquals("oneOf", jsonObject.getString("type")); } } }
@Test public void testValidTransformWithMetadataDelegateConfig(TestContext context) throws ValidationException { Delegate delegate = delegateFactory.parseDelegate("someDelegate", Buffer.buffer(VALID_TRANSFORM_WITH_METADATA_PROPERTY_DELEGATE_RESOURCE)); context.assertNotNull(delegate); context.assertEquals("someDelegate", delegate.getName()); }