Search in sources :

Example 71 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldThrowExceptionIfRequiredQueryParamIsNull.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldThrowExceptionIfRequiredQueryParamIsNull() throws Exception {
    exception.expect(InvocationTargetException.class);
    exception.expectCause(isA(BadRequestException.class));
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path").with(httpActionWithDefaultMapping(GET).with(queryParam("queryParam1").required(true)).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, NULL_STRING_VALUE);
}
Also used : BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 72 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassStringAndNumericParamsToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldPassStringAndNumericParamsToRestProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path").with(httpActionWithDefaultMapping(GET).with(queryParam("queryParam1").withType(STRING), queryParam("queryParam2").withType(INTEGER)).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();
    boolean queryParam1IsFirstMethodParameter = method.getParameters()[0].getName().equals("queryParam1");
    if (queryParam1IsFirstMethodParameter) {
        method.invoke(resourceObject, "paramValueABC", "2");
    } else {
        method.invoke(resourceObject, "2", "paramValueABC");
    }
    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(2));
    assertThat(queryParams, hasItems(allOf(hasProperty("name", equalTo("queryParam1")), hasProperty("stringValue", equalTo("paramValueABC"))), allOf(hasProperty("name", equalTo("queryParam2")), hasProperty("numericValue", equalTo(BigDecimal.valueOf(2))))));
}
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 73 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassOnePathParamAndOneQueryParamToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldPassOnePathParamAndOneQueryParamToRestProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path/{param}", "param").with(httpActionWithDefaultMapping(GET).with(queryParam("queryParam")).withDefaultResponseType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultQueryApiSomePathParamResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, "paramValueABC", "paramValueDEF");
    final ArgumentCaptor<Collection> paramsCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(restProcessor).process(anyString(), any(Function.class), anyString(), any(HttpHeaders.class), paramsCaptor.capture());
    final Collection<Parameter> params = paramsCaptor.getValue();
    assertThat(params, hasSize(2));
    assertThat(params, hasItems(allOf(hasProperty("name", equalTo("param")), hasProperty("stringValue", equalTo("paramValueABC"))), allOf(hasProperty("name", equalTo("queryParam")), hasProperty("stringValue", equalTo("paramValueDEF")))));
}
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 74 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldReturnResponseGeneratedByRestProcessor.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnResponseGeneratedByRestProcessor() 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 Response processorResponse = Response.ok().build();
    when(restProcessor.process(anyString(), any(Function.class), anyString(), any(HttpHeaders.class), any(Collection.class))).thenReturn(processorResponse);
    final Method method = firstMethodOf(resourceClass).get();
    final Object result = method.invoke(resourceObject);
    assertThat(result, is(processorResponse));
}
Also used : Response(javax.ws.rs.core.Response) 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) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 75 with CommonGeneratorProperties

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

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassActionToRestProcessor2.

@SuppressWarnings("unchecked")
@Test
public void shouldPassActionToRestProcessor2() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/recipe").with(httpActionWithDefaultMapping(GET).with(mapping().withName("contextB.action1").withResponseType("application/vnd.ctx.query.mediatype1+json")).withResponseTypes("application/vnd.ctx.query.mediatype1+json"))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultQueryApiRecipeResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Class<?> actionMapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "DefaultQueryApiRecipeResourceActionMapper");
    final Object actionMapperObject = actionMapperClass.getConstructor(ActionMapperHelper.class).newInstance(new BasicActionMapperHelper());
    setField(resourceObject, "actionMapper", actionMapperObject);
    setField(resourceObject, "headers", headersWith("Accept", "application/vnd.ctx.query.mediatype1+json"));
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject);
    verify(restProcessor).process(anyString(), any(Function.class), eq("contextB.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)

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