Search in sources :

Example 31 with CommonGeneratorProperties

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

the class RestAdapterGenerator_MultipartCodeStructureTest method shouldGenerateResourceClassWithInjectedFileInputDetailsFactory.

@Test
public void shouldGenerateResourceClassWithInjectedFileInputDetailsFactory() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().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<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiSomePathResource");
    final Field chainProcess = resourceClass.getDeclaredField("fileInputDetailsFactory");
    assertThat(chainProcess, not(nullValue()));
    assertThat(chainProcess.getType(), equalTo(FileInputDetailsFactory.class));
    assertThat(chainProcess.getAnnotation(Inject.class), not(nullValue()));
    assertThat(chainProcess.getModifiers(), is(0));
}
Also used : Inject(javax.inject.Inject) Field(java.lang.reflect.Field) FileInputDetailsFactory(uk.gov.justice.services.adapter.rest.multipart.FileInputDetailsFactory) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Test(org.junit.Test)

Example 32 with CommonGeneratorProperties

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

the class RestAdapterGenerator_MultipleMethodBodyTest method shouldReturnResponseGeneratedByRestProcessor.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnResponseGeneratedByRestProcessor() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/path").with(httpActionWithDefaultMapping(GET).withDefaultResponseType()).with(httpActionWithDefaultMapping(POST).withHttpActionOfDefaultRequestType()).with(httpActionWithDefaultMapping(PUT).withHttpActionOfDefaultRequestType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiPathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Response processorResponse = Response.ok().build();
    when(restProcessor.process(anyString(), any(Function.class), anyString(), any(HttpHeaders.class), any(Collection.class))).thenReturn(processorResponse);
    when(restProcessor.process(anyString(), any(Function.class), anyString(), any(Optional.class), any(HttpHeaders.class), any(Collection.class))).thenReturn(processorResponse);
    final List<Method> methods = methodsOf(resourceClass);
    for (final Method method : methods) {
        final int parameterCount = method.getParameterCount();
        final boolean isGetMethod = parameterCount == 0;
        final Object result = isGetMethod ? method.invoke(resourceObject) : method.invoke(resourceObject, NOT_USED_JSONOBJECT);
        assertThat(result, is(processorResponse));
    }
}
Also used : Response(javax.ws.rs.core.Response) Function(java.util.function.Function) HttpHeaders(javax.ws.rs.core.HttpHeaders) Optional(java.util.Optional) Collection(java.util.Collection) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonObject(javax.json.JsonObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 33 with CommonGeneratorProperties

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

the class RestAdapterGenerator_POSTMethodBodyTest method shouldInvoke2ndMethodAndPassMapWithOnePathParamToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldInvoke2ndMethodAndPassMapWithOnePathParamToRestProcessor() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/some/path/{p1}", "p1").with(httpActionWithDefaultMapping(POST, "application/vnd.type-aa+json", "application/vnd.type-bb+json").with(mapping().withName("cmd-aa").withRequestType("application/vnd.type-aa+json")).with(mapping().withName("cmd-bb").withRequestType("application/vnd.type-bb+json")))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiSomePathP1Resource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final List<Method> methods = methodsOf(resourceClass);
    final Method secondMethod = methods.get(1);
    secondMethod.invoke(resourceObject, "paramValueXYZ", NOT_USED_JSONOBJECT);
    final ArgumentCaptor<Collection> pathParamsCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(restProcessor).process(anyString(), any(Function.class), anyString(), any(Optional.class), any(HttpHeaders.class), pathParamsCaptor.capture());
    final Collection<Parameter> pathParams = pathParamsCaptor.getValue();
    assertThat(pathParams, hasSize(1));
    final Parameter pathParam = pathParams.iterator().next();
    assertThat(pathParam.getName(), is("p1"));
    assertThat(pathParam.getStringValue(), is("paramValueXYZ"));
}
Also used : Function(java.util.function.Function) HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) Optional(java.util.Optional) Collection(java.util.Collection) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonObject(javax.json.JsonObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 34 with CommonGeneratorProperties

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

the class RestAdapterGenerator_POSTMethodBodyTest method shouldPassJsonObjectToRestProcessor.

@SuppressWarnings("unchecked")
@Test
public void shouldPassJsonObjectToRestProcessor() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/path").with(httpActionWithDefaultMapping(POST).withHttpActionOfDefaultRequestType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiPathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Optional<JsonObject> jsonObject = Optional.of(Json.createObjectBuilder().add("dummy", "abc").build());
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, jsonObject.get());
    verify(restProcessor).process(anyString(), any(Function.class), anyString(), eq(jsonObject), 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) JsonObject(javax.json.JsonObject) Collection(java.util.Collection) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonObject(javax.json.JsonObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 35 with CommonGeneratorProperties

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

the class RestAdapterGenerator_POSTMethodBodyTest method shouldCallInterceptorChainProcessor.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void shouldCallInterceptorChainProcessor() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/path").with(httpActionWithDefaultMapping(POST).withHttpActionOfDefaultRequestType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiPathResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, NOT_USED_JSONOBJECT);
    final ArgumentCaptor<Function> functionCaptor = ArgumentCaptor.forClass(Function.class);
    verify(restProcessor).process(anyString(), functionCaptor.capture(), anyString(), any(Optional.class), any(HttpHeaders.class), any(Collection.class));
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    final InterceptorContext interceptorContext = interceptorContextWithInput(envelope);
    functionCaptor.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) Optional(java.util.Optional) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) Collection(java.util.Collection) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) JsonObject(javax.json.JsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) 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