Search in sources :

Example 1 with ActionType

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

the class JaxRsImplementationGenerator method methodsFor.

/**
 * Process the body or bodies for each httpAction.
 *
 * @param action the httpAction to process
 * @param component
 * @return the list of {@link MethodSpec} that represents each method for the httpAction
 */
private List<MethodSpec> methodsFor(final Action action, final Optional<String> component) {
    final ActionType actionType = action.getType();
    final String responseStrategyName = nameOfResponseStrategyFor(action, component);
    if (isSupportedActionType(actionType)) {
        if (isSupportedActionTypeWithResponseTypeOnly(actionType)) {
            return singletonList(processNoActionBody(action, responseStrategyName));
        } else {
            return processOneOrMoreActionBodies(action, responseStrategyName);
        }
    }
    throw new IllegalStateException(format("Unsupported httpAction type %s", actionType));
}
Also used : ActionType(org.raml.model.ActionType) Actions.isSupportedActionType(uk.gov.justice.services.generators.commons.helper.Actions.isSupportedActionType)

Example 2 with ActionType

use of org.raml.model.ActionType 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 3 with ActionType

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

the class ResourceBuilder method build.

public Resource build() {
    final Resource resource = new Resource();
    resource.setParentUri(parentUri);
    resource.setRelativeUri(relativeUri);
    resource.setUriParameters(uriParameters);
    final Map<ActionType, Action> actions = new HashMap<>();
    for (final HttpActionBuilder httpActionBuilder : httpActionBuilders) {
        final Action action = httpActionBuilder.build();
        action.setResource(resource);
        actions.put(action.getType(), action);
    }
    resource.setActions(actions);
    return resource;
}
Also used : HttpActionBuilder.defaultPostAction(uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultPostAction) HttpActionBuilder.defaultGetAction(uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultGetAction) HttpActionBuilder.defaultPutAction(uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultPutAction) HttpActionBuilder.defaultPatchAction(uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultPatchAction) HttpActionBuilder.defaultDeleteAction(uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultDeleteAction) Action(org.raml.model.Action) ActionType(org.raml.model.ActionType) HashMap(java.util.HashMap) Resource(org.raml.model.Resource)

Example 4 with ActionType

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

the class ActionMappingGenerator method mapperConstructorCodeFor.

private CodeBlock mapperConstructorCodeFor(final Resource resource) {
    final CodeBlock.Builder constructorCode = CodeBlock.builder().addStatement("this.$L = $L", ACTION_MAPPER_HELPER_FIELD, ACTION_MAPPER_HELPER_FIELD);
    // NOTE: there's a bit of ambiguity here: ramlActions (http methods) are not framework actions
    resource.getActions().values().forEach(ramlAction -> {
        final List<ActionMapping> actionMappings = new ActionMappingParser().listOf(ramlAction.getDescription());
        final ActionType actionType = ramlAction.getType();
        if (isSupportedActionType(actionType)) {
            actionMappings.forEach(actionMapping -> {
                final String mediaType = actionMapping.mimeTypeFor(ramlAction.getType());
                constructorCode.addStatement("$L.add($S, $S, $S)", ACTION_MAPPER_HELPER_FIELD, methodNameForAction(ramlAction, actionType, mediaType), mediaType, actionMapping.getName());
            });
        } else {
            throw new IllegalStateException(format("Http Method of type %s is not supported by the Action Mapper", actionType.toString()));
        }
    });
    return constructorCode.build();
}
Also used : ActionMappingParser(uk.gov.justice.services.generators.commons.mapping.ActionMappingParser) ActionMapping(uk.gov.justice.services.generators.commons.mapping.ActionMapping) ActionType(org.raml.model.ActionType) Actions.isSupportedActionType(uk.gov.justice.services.generators.commons.helper.Actions.isSupportedActionType) CodeBlock(com.squareup.javapoet.CodeBlock)

Aggregations

ActionType (org.raml.model.ActionType)4 HashMap (java.util.HashMap)2 Action (org.raml.model.Action)2 Resource (org.raml.model.Resource)2 Actions.isSupportedActionType (uk.gov.justice.services.generators.commons.helper.Actions.isSupportedActionType)2 CodeBlock (com.squareup.javapoet.CodeBlock)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Test (org.junit.Test)1 MimeType (org.raml.model.MimeType)1 ActionMapping (uk.gov.justice.services.generators.commons.mapping.ActionMapping)1 ActionMappingParser (uk.gov.justice.services.generators.commons.mapping.ActionMappingParser)1 HttpActionBuilder.defaultDeleteAction (uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultDeleteAction)1 HttpActionBuilder.defaultGetAction (uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultGetAction)1 HttpActionBuilder.defaultPatchAction (uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultPatchAction)1 HttpActionBuilder.defaultPostAction (uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultPostAction)1 HttpActionBuilder.defaultPutAction (uk.gov.justice.services.generators.test.utils.builder.HttpActionBuilder.defaultPutAction)1 Event (uk.gov.justice.subscription.domain.Event)1 Subscription (uk.gov.justice.subscription.domain.Subscription)1 SubscriptionDescriptor (uk.gov.justice.subscription.domain.SubscriptionDescriptor)1 SubscriptionDescriptorDef (uk.gov.justice.subscription.domain.SubscriptionDescriptorDef)1