Search in sources :

Example 1 with SubscriptionDescriptorException

use of uk.gov.justice.subscription.SubscriptionDescriptorException in project microservice_framework by CJSCommonPlatform.

the class SubscriptionDescriptorParser method read.

public SubscriptionDescriptor read(final Path absolutePath) {
    try {
        subscriptionDescriptorFileValidator.validate(absolutePath);
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()).registerModule(new Jdk8Module()).registerModule(new ParameterNamesModule(PROPERTIES));
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        return mapper.readValue(absolutePath.toFile(), SubscriptionDescriptorDef.class).getSubscriptionDescriptor();
    } catch (final NoSuchFileException e) {
        throw new SubscriptionDescriptorException(format("No such subscriptions YAML file %s ", absolutePath), e);
    } catch (final ValidationException e) {
        throw new SubscriptionDescriptorException(format("Failed to validate subscriptions yaml file %s ", absolutePath), e);
    } catch (final IOException e) {
        throw new SubscriptionDescriptorException(format("Failed to read subscriptions yaml file %s ", absolutePath), e);
    }
}
Also used : SubscriptionDescriptorException(uk.gov.justice.subscription.SubscriptionDescriptorException) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) ValidationException(org.everit.json.schema.ValidationException) SubscriptionDescriptorDef(uk.gov.justice.subscription.domain.SubscriptionDescriptorDef) NoSuchFileException(java.nio.file.NoSuchFileException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with SubscriptionDescriptorException

use of uk.gov.justice.subscription.SubscriptionDescriptorException in project microservice_framework by CJSCommonPlatform.

the class SubscriptionDescriptorParserTest method shouldThrowFileNotFoundException.

@Test
public void shouldThrowFileNotFoundException() {
    final Path path = get("this-subscription-does-not-exist.yaml").toAbsolutePath();
    try {
        subscriptionDescriptorParser.read(path);
        fail();
    } catch (SubscriptionDescriptorException re) {
        assertThat(re, is(instanceOf(SubscriptionDescriptorException.class)));
        assertThat(re.getCause(), is(instanceOf(NoSuchFileException.class)));
    }
}
Also used : Path(java.nio.file.Path) SubscriptionDescriptorException(uk.gov.justice.subscription.SubscriptionDescriptorException) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.Test)

Example 3 with SubscriptionDescriptorException

use of uk.gov.justice.subscription.SubscriptionDescriptorException in project microservice_framework by CJSCommonPlatform.

the class SubscriptionDescriptorParserTest method shouldFailOnIncorrectSubscriptionYaml.

@Test
public void shouldFailOnIncorrectSubscriptionYaml() {
    final Path path = getFromClasspath("incorrect-subscription.yaml");
    try {
        subscriptionDescriptorParser.read(path);
        fail();
    } catch (final SubscriptionDescriptorException re) {
        assertThat(re, is(instanceOf(SubscriptionDescriptorException.class)));
        assertThat(re.getCause(), is(instanceOf(ValidationException.class)));
        assertThat(re.getCause().getMessage(), is("#/subscription_descriptor: required key [spec_version] not found"));
    }
}
Also used : Path(java.nio.file.Path) SubscriptionDescriptorException(uk.gov.justice.subscription.SubscriptionDescriptorException) ValidationException(org.everit.json.schema.ValidationException) Test(org.junit.Test)

Example 4 with SubscriptionDescriptorException

use of uk.gov.justice.subscription.SubscriptionDescriptorException in project microservice_framework by CJSCommonPlatform.

the class SubscriptionDescriptorFileValidator method validate.

public void validate(final Path filePath) {
    try {
        final JSONObject convert = yamlFileToJsonObjectConverter.convert(filePath);
        schema().validate(convert);
    } catch (IOException ex) {
        throw new SubscriptionDescriptorException(format("Unable to convert to JSON file %s ", filePath.toString()), ex);
    }
}
Also used : SubscriptionDescriptorException(uk.gov.justice.subscription.SubscriptionDescriptorException) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Aggregations

SubscriptionDescriptorException (uk.gov.justice.subscription.SubscriptionDescriptorException)4 IOException (java.io.IOException)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 Path (java.nio.file.Path)2 ValidationException (org.everit.json.schema.ValidationException)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)1 JSONObject (org.json.JSONObject)1 SubscriptionDescriptorDef (uk.gov.justice.subscription.domain.SubscriptionDescriptorDef)1