Search in sources :

Example 21 with MimeType

use of org.raml.model.MimeType in project microservice_framework by CJSCommonPlatform.

the class RamlMediaTypeToSchemaIdGeneratorTest method createMimeTypeWith.

private MimeType createMimeTypeWith(final String type, final String schemaId) {
    final MimeType mimeType = new MimeType();
    mimeType.setType(type);
    mimeType.setSchema(format(VALID_JSON_SCHEMA, schemaId));
    return mimeType;
}
Also used : MimeType(org.raml.model.MimeType)

Example 22 with MimeType

use of org.raml.model.MimeType in project microservice_framework by CJSCommonPlatform.

the class RamlMediaTypeToSchemaIdGeneratorTest method shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithGet.

@Test
public void shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithGet() throws Exception {
    final String schemaId = "http://justice.gov.uk/test/schema.json";
    final MimeType mimeType_1 = createMimeTypeWith(MEDIA_TYPE_1.toString(), schemaId);
    new RamlMediaTypeToSchemaIdGenerator().generateMediaTypeToSchemaIdMapper(restRamlWithQueryApiDefaults().with(resource("/user").with(httpAction(GET).withResponseTypes(mimeType_1))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> schemaIdMapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "WarnameMediaTypeToSchemaIdMapper");
    final MediaTypeToSchemaIdMapper instance = (MediaTypeToSchemaIdMapper) schemaIdMapperClass.newInstance();
    final Map<MediaType, String> mediaTypeToSchemaIdMap = instance.getMediaTypeToSchemaIdMap();
    assertThat(mediaTypeToSchemaIdMap.size(), is(1));
    assertThat(mediaTypeToSchemaIdMap.get(MEDIA_TYPE_1), is(schemaId));
}
Also used : MediaTypeToSchemaIdMapper(uk.gov.justice.services.core.mapping.MediaTypeToSchemaIdMapper) MediaType(uk.gov.justice.services.core.mapping.MediaType) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) MimeType(org.raml.model.MimeType) Test(org.junit.Test)

Example 23 with MimeType

use of org.raml.model.MimeType in project microservice_framework by CJSCommonPlatform.

the class MimeTypeToEventConverterTest method shouldThrowASchemaParsingExceptionIfTheSchemaIdIsNotFound.

@Test
public void shouldThrowASchemaParsingExceptionIfTheSchemaIdIsNotFound() throws Exception {
    final String eventName = "something-happened";
    final String mimeTypeString = "application/vnd.context.events.something-happened+json";
    final MimeType mimeType = mock(MimeType.class);
    when(mimeType.getType()).thenReturn(mimeTypeString);
    when(eventNameExtractor.extractEventName(mimeType.getType())).thenReturn(eventName);
    when(schemaIdParser.schemaIdFrom(mimeType)).thenReturn(empty());
    try {
        mimeTypeToEventConverter.asEvent(mimeType);
        fail();
    } catch (final SchemaParsingException expected) {
        assertThat(expected.getMessage(), is("Schema for media type: application/vnd.context.events.something-happened+json has no schema id"));
    }
}
Also used : SchemaParsingException(uk.gov.justice.services.generators.commons.mapping.SchemaParsingException) MimeType(org.raml.model.MimeType) Test(org.junit.Test)

Example 24 with MimeType

use of org.raml.model.MimeType in project microservice_framework by CJSCommonPlatform.

the class EventFilterCodeGeneratorTest method shouldGenerateCorrectEventFilterJavaCode.

@Test
public void shouldGenerateCorrectEventFilterJavaCode() throws Exception {
    final String serviceName = "my-context";
    final String componentName = "EVENT_LISTENER";
    final String jmsUri = "jms:topic:my-context.handler.command";
    final Event event_1 = event().withName("my-context.events.something-happened").withSchemaUri("http://justice.gov.uk/json/schemas/domains/example/my-context.events.something-happened.json").build();
    final Event event_2 = event().withName("my-context.events.something-else-happened").withSchemaUri("http://justice.gov.uk/json/schemas/domains/example/my-context.events.something-else-happened.json").build();
    final Subscription subscription = subscription().withName("subscription").withEvent(event_1).withEvent(event_2).withEventsource(eventsource().withName("eventsource").withLocation(location().withJmsUri(jmsUri).withRestUri("http://localhost:8080/example/event-source-api/rest").build()).build()).build();
    final SubscriptionDescriptor subscriptionDescriptor = subscriptionDescriptor().withSpecVersion("1.0.0").withService(serviceName).withServiceComponent(componentName).withSubscription(subscription).build();
    final SubscriptionDescriptorDef subscriptionDescriptorDef = subscriptionDescriptorDef().withSubscriptionDescriptor(subscriptionDescriptor).build();
    final HashMap<ActionType, Action> actions = new HashMap<>();
    final Resource resource = new Resource();
    resource.setActions(actions);
    final Action action = mock(Action.class, RETURNS_DEEP_STUBS.get());
    actions.put(POST, action);
    final MimeType mimeType_1 = new MimeType("application/vnd.my-context.events.something-happened+json");
    final MimeType mimeType_2 = new MimeType("application/vnd.my-context.events.something-else-happened+json");
    final String basePackageName = "uk.gov.moj.base.package.name";
    when(action.getBody().values()).thenReturn(asList(mimeType_1, mimeType_2));
    final ClassNameFactory classNameFactory = new ClassNameFactory(basePackageName, serviceName, componentName, jmsUri);
    final TypeSpec typeSpec = eventFilterCodeGenerator.generate(subscription, classNameFactory);
    assertThat(typeSpec.toString(), is("@javax.enterprise.context.ApplicationScoped\n" + "public class MyContextEventListenerMyContextHandlerCommandEventFilter extends uk.gov.justice.services.event.buffer.api.AbstractEventFilter {\n" + "  public MyContextEventListenerMyContextHandlerCommandEventFilter() {\n" + "    super(\"my-context.events.something-happened\",\"my-context.events.something-else-happened\");}\n" + "}\n"));
}
Also used : Action(org.raml.model.Action) ActionType(org.raml.model.ActionType) HashMap(java.util.HashMap) SubscriptionDescriptorDef(uk.gov.justice.subscription.domain.SubscriptionDescriptorDef) Resource(org.raml.model.Resource) SubscriptionDescriptor(uk.gov.justice.subscription.domain.SubscriptionDescriptor) MimeType(org.raml.model.MimeType) Event(uk.gov.justice.subscription.domain.Event) Subscription(uk.gov.justice.subscription.domain.Subscription) TypeSpec(com.squareup.javapoet.TypeSpec) Test(org.junit.Test)

Example 25 with MimeType

use of org.raml.model.MimeType in project microservice_framework by CJSCommonPlatform.

the class SchemaIdParserTest method shouldReturnEmptyIfJsonSchemaHasNoSchemaId.

@Test
public void shouldReturnEmptyIfJsonSchemaHasNoSchemaId() throws Exception {
    final MimeType mimeType = new MimeType();
    mimeType.setType(MEDIA_TYPE);
    mimeType.setSchema(NO_ID_JSON_SCHEMA);
    assertThat(schemaIdParser.schemaIdFrom(mimeType), is(empty()));
}
Also used : MimeType(org.raml.model.MimeType) Test(org.junit.Test)

Aggregations

MimeType (org.raml.model.MimeType)38 Test (org.junit.Test)30 Action (org.raml.model.Action)6 Event (uk.gov.justice.subscription.domain.Event)6 HttpActionBuilder.httpAction (uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.httpAction)4 MediaType (uk.gov.justice.services.core.mapping.MediaType)3 MediaTypeToSchemaIdMapper (uk.gov.justice.services.core.mapping.MediaTypeToSchemaIdMapper)3 CommonGeneratorProperties (uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties)3 Subscription (uk.gov.justice.subscription.domain.Subscription)3 HashMap (java.util.HashMap)2 Resource (org.raml.model.Resource)2 ActionMimeTypeDefinition (uk.gov.justice.services.generators.commons.client.ActionMimeTypeDefinition)2 Eventsource (uk.gov.justice.subscription.domain.Eventsource)2 Location (uk.gov.justice.subscription.domain.Location)2 TypeSpec (com.squareup.javapoet.TypeSpec)1 ActionType (org.raml.model.ActionType)1 Response (org.raml.model.Response)1 SchemaParsingException (uk.gov.justice.services.generators.commons.mapping.SchemaParsingException)1 MappingBuilder (uk.gov.justice.services.generators.test.utils.builder.MappingBuilder)1 SubscriptionDescriptor (uk.gov.justice.subscription.domain.SubscriptionDescriptor)1