Search in sources :

Example 26 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassBooleanParamToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldPassBooleanParamToRestProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path").with(httpActionWithDefaultMapping(GET).with(queryParam("queryParam").withType(BOOLEAN)).withDefaultResponseType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultQueryApiSomePathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, "false");
    final ArgumentCaptor<Collection> queryParamsCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(restProcessor).process(anyString(), any(Function.class), anyString(), any(HttpHeaders.class), queryParamsCaptor.capture());
    final Collection<Parameter> queryParams = queryParamsCaptor.getValue();
    assertThat(queryParams, hasSize(1));
    assertThat(queryParams, hasItems(allOf(hasProperty("name", equalTo("queryParam")), hasProperty("booleanValue", equalTo(false)))));
}
Also used : Function(java.util.function.Function) HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) Collection(java.util.Collection) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 27 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldCallInterceptorChainProcessor.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void shouldCallInterceptorChainProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/path").with(httpActionWithDefaultMapping(GET).withDefaultResponseType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultQueryApiPathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject);
    ArgumentCaptor<Function> consumerCaptor = ArgumentCaptor.forClass(Function.class);
    verify(restProcessor).process(anyString(), consumerCaptor.capture(), anyString(), any(HttpHeaders.class), any(Collection.class));
    final InterceptorContext interceptorContext = interceptorContextWithInput(mock(JsonEnvelope.class));
    consumerCaptor.getValue().apply(interceptorContext);
    verify(interceptorChainProcessor).process(interceptorContext);
}
Also used : Function(java.util.function.Function) HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) Collection(java.util.Collection) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 28 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassActionToRestProcessor.

@SuppressWarnings("unchecked")
@Test
public void shouldPassActionToRestProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/cake").with(httpActionWithDefaultMapping(GET).with(mapping().withName("contextA.action1").withResponseType("application/vnd.ctx.query.somemediatype1+json")).with(mapping().withName("contextA.action1").withResponseType("application/vnd.ctx.query.somemediatype2+json")).with(mapping().withName("contextA.action2").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<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultQueryApiCakeResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Class<?> actionMapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "DefaultQueryApiCakeResourceActionMapper");
    final Object actionMapperObject = actionMapperClass.getConstructor(ActionMapperHelper.class).newInstance(new BasicActionMapperHelper());
    setField(resourceObject, "actionMapper", actionMapperObject);
    setField(resourceObject, "headers", headersWith("Accept", "application/vnd.ctx.query.somemediatype1+json"));
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject);
    verify(restProcessor).process(anyString(), any(Function.class), eq("contextA.action1"), any(HttpHeaders.class), any(Collection.class));
}
Also used : Function(java.util.function.Function) HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) Collection(java.util.Collection) 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 29 with CommonGeneratorProperties

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

the class RestAdapterGenerator_MultipartCodeStructureTest method shouldGenerateResourceInterfaceWithOnePOSTMethod.

@Test
public void shouldGenerateResourceInterfaceWithOnePOSTMethod() throws Exception {
    generator.run(restRamlWithDefaults().with(resource("/some/path").with(httpAction().withHttpActionType(POST).withMediaTypeWithoutSchema(multipartWithFileFormParameter("photoId")).with(mapping().withName("upload").withRequestType(MULTIPART_FORM_DATA)))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> interfaceClass = compiler.compiledInterfaceOf(RESOURCE_PACKAGE);
    final List<Method> methods = methodsOf(interfaceClass);
    assertThat(methods, hasSize(1));
    final Method method = methods.get(0);
    assertThat(method.getReturnType(), equalTo(Response.class));
    assertThat(method.getAnnotation(javax.ws.rs.POST.class), not(nullValue()));
    assertThat(method.getAnnotation(Consumes.class), not(nullValue()));
    assertThat(method.getAnnotation(Consumes.class).value(), is(new String[] { MULTIPART_FORM_DATA }));
    assertThat(method.getAnnotation(Produces.class), is(nullValue()));
}
Also used : Response(javax.ws.rs.core.Response) POST(org.raml.model.ActionType.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 30 with CommonGeneratorProperties

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

the class RestAdapterGenerator_MultipartCodeStructureTest method shouldGenerateInterfaceThatContainsMethodWithTwoPathParamsAndMultipartParameter.

@Test
public void shouldGenerateInterfaceThatContainsMethodWithTwoPathParamsAndMultipartParameter() throws Exception {
    generator.run(restRamlWithDefaults().with(resource("/some/path").with(httpAction().withHttpActionType(POST).withMediaTypeWithoutSchema(multipartWithFileFormParameter("photoId")).with(mapping().withName("upload").withRequestType(MULTIPART_FORM_DATA))).withRelativeUri("/some/path/{paramA}/abc/{paramB}").withPathParam("paramA").withPathParam("paramB")).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> interfaceClass = compiler.compiledInterfaceOf(RESOURCE_PACKAGE);
    final List<Method> methods = methodsOf(interfaceClass);
    assertThat(methods, hasSize(1));
    final Method method = methods.get(0);
    assertThat(method.getParameterCount(), is(3));
    final Parameter methodParam1 = method.getParameters()[0];
    assertThat(methodParam1.getType(), equalTo(String.class));
    assertThat(methodParam1.getAnnotations(), arrayWithSize(1));
    assertThat(methodParam1.getAnnotations()[0].annotationType(), equalTo(PathParam.class));
    assertThat(methodParam1.getAnnotation(PathParam.class).value(), is("paramA"));
    final Parameter methodParam2 = method.getParameters()[1];
    assertThat(methodParam2.getType(), equalTo(String.class));
    assertThat(methodParam2.getAnnotations(), arrayWithSize(1));
    assertThat(methodParam2.getAnnotations()[0].annotationType(), equalTo(PathParam.class));
    assertThat(methodParam2.getAnnotation(PathParam.class).value(), is("paramB"));
    final Parameter methodParam3 = method.getParameters()[2];
    assertThat(methodParam3.getType(), equalTo(MultipartFormDataInput.class));
    assertThat(methodParam3.getAnnotation(MultipartForm.class), not(nullValue()));
}
Also used : MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) MimeTypeBuilder.multipartWithFileFormParameter(uk.gov.justice.services.generators.test.utils.builder.MimeTypeBuilder.multipartWithFileFormParameter) Parameter(java.lang.reflect.Parameter) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) MultipartForm(org.jboss.resteasy.annotations.providers.multipart.MultipartForm) Method(java.lang.reflect.Method) PathParam(javax.ws.rs.PathParam) 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