Search in sources :

Example 86 with CommonGeneratorProperties

use of uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_ActionMapperTest method shouldReturnActionNameForGETResource.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnActionNameForGETResource() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/user").with(httpActionWithDefaultMapping(GET).with(mapping().withName("contextA.someAction").withResponseType("application/vnd.ctx.query.somemediatype1+json")).with(mapping().withName("contextA.someAction").withResponseType("application/vnd.ctx.query.somemediatype2+json")).with(mapping().withName("contextA.someOtherAction").withResponseType("application/vnd.ctx.query.somemediatype3+json")).withResponseTypes("application/vnd.ctx.query.somemediatype1+json", "application/vnd.ctx.query.somemediatype2+json", "application/vnd.ctx.query.somemediatype3+json"))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> mapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "DefaultQueryApiUserResourceActionMapper");
    final Object mapperObject = mapperClass.getConstructor(ActionMapperHelper.class).newInstance(new BasicActionMapperHelper());
    final Method actionMethod = methodOf(mapperClass, "actionOf").get();
    Object action = actionMethod.invoke(mapperObject, "getUser", "GET", headersWith("Accept", "application/vnd.ctx.query.somemediatype1+json"));
    assertThat(action, is("contextA.someAction"));
    action = actionMethod.invoke(mapperObject, "getUser", "GET", headersWith("Accept", "application/vnd.ctx.query.somemediatype2+json"));
    assertThat(action, is("contextA.someAction"));
    action = actionMethod.invoke(mapperObject, "getUser", "GET", headersWith("Accept", "application/vnd.ctx.query.somemediatype3+json"));
    assertThat(action, is("contextA.someOtherAction"));
}
Also used : CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) ActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.ActionMapperHelper) BasicActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper) Method(java.lang.reflect.Method) BasicActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper) Test(org.junit.Test)

Example 87 with CommonGeneratorProperties

use of uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_ActionMapperTest method shouldReturnActionNameForPOSTResource.

@Test
public void shouldReturnActionNameForPOSTResource() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/case").with(httpActionWithDefaultMapping(POST).with(mapping().withName("contextB.someAction").withRequestType("application/vnd.ctx.command.somemediatype1+json")).with(mapping().withName("contextB.someOtherAction").withRequestType("application/vnd.ctx.command.somemediatype2+json")).withMediaTypeWithDefaultSchema("application/vnd.ctx.command.somemediatype1+json").withMediaTypeWithDefaultSchema("application/vnd.ctx.command.somemediatype2+json"))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> mapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "DefaultCommandApiCaseResourceActionMapper");
    final Object mapperObject = mapperClass.getConstructor(ActionMapperHelper.class).newInstance(new BasicActionMapperHelper());
    final Method actionMethod = methodOf(mapperClass, "actionOf").get();
    Object action = actionMethod.invoke(mapperObject, "postContextBSomeActionCase", "POST", headersWith("Content-Type", "application/vnd.ctx.command.somemediatype1+json"));
    assertThat(action, is("contextB.someAction"));
    action = actionMethod.invoke(mapperObject, "postContextBSomeOtherActionCase", "POST", headersWith("Content-Type", "application/vnd.ctx.command.somemediatype2+json"));
    assertThat(action, is("contextB.someOtherAction"));
}
Also used : CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) ActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.ActionMapperHelper) BasicActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper) Method(java.lang.reflect.Method) BasicActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper) Test(org.junit.Test)

Example 88 with CommonGeneratorProperties

use of uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_ActionMapperTest method shouldReturnActionNameForPOSTResourceSameActionMappedToTwoMediaTypes.

@Test
public void shouldReturnActionNameForPOSTResourceSameActionMappedToTwoMediaTypes() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/case").with(httpActionWithDefaultMapping(POST).with(mapping().withName("contextC.someAction").withRequestType("application/vnd.ctx.command.somemediatype1+json")).with(mapping().withName("contextD.someAction").withRequestType("application/vnd.ctx.command.somemediatype2+json")).withMediaTypeWithDefaultSchema("application/vnd.ctx.command.somemediatype1+json").withMediaTypeWithDefaultSchema("application/vnd.ctx.command.somemediatype2+json"))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> mapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "DefaultCommandApiCaseResourceActionMapper");
    final Object mapperObject = mapperClass.getConstructor(ActionMapperHelper.class).newInstance(new BasicActionMapperHelper());
    final Method actionMethod = methodOf(mapperClass, "actionOf").get();
    Object action = actionMethod.invoke(mapperObject, "postContextCSomeActionCase", "POST", headersWith("Content-Type", "application/vnd.ctx.command.somemediatype1+json"));
    assertThat(action, is("contextC.someAction"));
    action = actionMethod.invoke(mapperObject, "postContextDSomeActionCase", "POST", headersWith("Content-Type", "application/vnd.ctx.command.somemediatype2+json"));
    assertThat(action, is("contextD.someAction"));
}
Also used : CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) ActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.ActionMapperHelper) BasicActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper) Method(java.lang.reflect.Method) BasicActionMapperHelper(uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper) Test(org.junit.Test)

Example 89 with CommonGeneratorProperties

use of uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_ApplicationTest method shouldReturnSetOfResourceClasses.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnSetOfResourceClasses() throws Exception {
    generator.run(raml().withBaseUri("http://localhost:8080/warname/command/api/rest/service").with(resource("/pathA").with(httpActionWithDefaultMapping(GET).withDefaultResponseType())).with(resource("/pathB").with(httpActionWithDefaultMapping(GET).withDefaultResponseType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    Set<Class<?>> compiledClasses = compiler.compiledClassesOf(BASE_PACKAGE);
    Class<?> applicationClass = compiler.classOf(compiledClasses, BASE_PACKAGE, "CommandApiRestServiceApplication");
    Object application = applicationClass.newInstance();
    CommonProviders commonProviders = mock(CommonProviders.class);
    when(commonProviders.providers()).thenReturn(newHashSet(JaxRsProviderA.class));
    setField(application, "commonProviders", commonProviders);
    Method method = applicationClass.getDeclaredMethod("getClasses");
    Object result = method.invoke(application);
    assertThat(result, is(instanceOf(Set.class)));
    Set<Class<?>> classes = (Set<Class<?>>) result;
    assertThat(classes, hasItems(compiler.classOf(compiledClasses, BASE_PACKAGE, "resource", "DefaultCommandApiPathAResource"), compiler.classOf(compiledClasses, BASE_PACKAGE, "resource", "DefaultCommandApiPathBResource")));
}
Also used : Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Set(java.util.Set) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Method(java.lang.reflect.Method) CommonProviders(uk.gov.justice.services.adapter.rest.application.CommonProviders) Test(org.junit.Test)

Example 90 with CommonGeneratorProperties

use of uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_ApplicationTest method shouldGenerateApplicationClassWithGetClassesMethod.

@Test
public void shouldGenerateApplicationClassWithGetClassesMethod() throws Exception {
    generator.run(raml().withBaseUri("http://localhost:8080/warname/command/api/rest/service").with(defaultPostResource()).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    Class<?> applicationClass = compiler.compiledClassOf(BASE_PACKAGE, "CommandApiRestServiceApplication");
    Method method = applicationClass.getDeclaredMethod("getClasses");
    assertThat(method, not(nullValue()));
    assertThat(method.getParameterCount(), equalTo(0));
    assertThat(isPublic(method.getModifiers()), is(true));
    assertThat(isStatic(method.getModifiers()), is(false));
    assertThat(isAbstract(method.getModifiers()), is(false));
    assertThat(method.getGenericReturnType(), equalTo(new TypeToken<Set<Class<?>>>() {
    }.getType()));
}
Also used : Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Set(java.util.Set) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

CommonGeneratorProperties (uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties)96 Test (org.junit.Test)93 Method (java.lang.reflect.Method)72 Collection (java.util.Collection)33 HttpHeaders (javax.ws.rs.core.HttpHeaders)33 Function (java.util.function.Function)32 JsonObject (javax.json.JsonObject)31 Response (javax.ws.rs.core.Response)30 ThreadLocalHttpHeaders (org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders)21 Optional (java.util.Optional)18 ActionMapperHelper (uk.gov.justice.services.adapter.rest.mapping.ActionMapperHelper)11 BasicActionMapperHelper (uk.gov.justice.services.adapter.rest.mapping.BasicActionMapperHelper)11 Parameter (uk.gov.justice.services.adapter.rest.parameter.Parameter)10 Consumes (javax.ws.rs.Consumes)9 Field (java.lang.reflect.Field)7 Parameter (java.lang.reflect.Parameter)7 Produces (javax.ws.rs.Produces)6 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)6 MediaType (uk.gov.justice.services.core.mapping.MediaType)6 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)6