Search in sources :

Example 1 with Parameter

use of uk.gov.justice.services.adapter.rest.parameter.Parameter in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassOneQueryParamToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldPassOneQueryParamToRestProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path").with(httpActionWithDefaultMapping(GET).with(queryParam("queryParam")).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, "paramValue1234");
    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("stringValue", equalTo("paramValue1234")))));
}
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 2 with Parameter

use of uk.gov.justice.services.adapter.rest.parameter.Parameter in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_GETMethodBodyTest method shouldPassOnePathParamToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldPassOnePathParamToRestProcessor() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path/{paramA}", "paramA").with(httpActionWithDefaultMapping(GET).withDefaultResponseType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultQueryApiSomePathParamAResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, "paramValue1234");
    final ArgumentCaptor<Collection> pathParamsCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(restProcessor).process(anyString(), any(Function.class), anyString(), any(HttpHeaders.class), pathParamsCaptor.capture());
    Collection<Parameter> pathParams = pathParamsCaptor.getValue();
    assertThat(pathParams, hasSize(1));
    final Parameter pathParam = pathParams.iterator().next();
    assertThat(pathParam.getName(), equalTo("paramA"));
    assertThat(pathParam.getStringValue(), equalTo("paramValue1234"));
}
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 3 with Parameter

use of uk.gov.justice.services.adapter.rest.parameter.Parameter in project microservice_framework by CJSCommonPlatform.

the class RestAdapterGenerator_GETMethodBodyTest method shouldRemoveOptionalQueryParamIfSetToNull.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldRemoveOptionalQueryParamIfSetToNull() throws Exception {
    generator.run(restRamlWithQueryApiDefaults().with(resource("/some/path").with(httpActionWithDefaultMapping(GET).with(queryParam("queryParam1").required(true), queryParam("queryParam2").required(false)).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", NULL_STRING_VALUE);
    } else {
        method.invoke(resourceObject, NULL_STRING_VALUE, "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(1));
    assertThat(queryParams, hasItems(allOf(hasProperty("name", equalTo("queryParam1")), hasProperty("stringValue", equalTo("paramValueABC")))));
}
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 4 with Parameter

use of uk.gov.justice.services.adapter.rest.parameter.Parameter 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 5 with Parameter

use of uk.gov.justice.services.adapter.rest.parameter.Parameter 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)

Aggregations

Parameter (uk.gov.justice.services.adapter.rest.parameter.Parameter)13 Test (org.junit.Test)12 Collection (java.util.Collection)11 Function (java.util.function.Function)11 HttpHeaders (javax.ws.rs.core.HttpHeaders)11 Method (java.lang.reflect.Method)10 ThreadLocalHttpHeaders (org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders)10 CommonGeneratorProperties (uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties)10 JsonObject (javax.json.JsonObject)5 Optional (java.util.Optional)4 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)3 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)3 Matchers.anyString (org.mockito.Matchers.anyString)2 DefaultParameter (uk.gov.justice.services.adapter.rest.parameter.DefaultParameter)2 String.format (java.lang.String.format)1 List (java.util.List)1 Optional.empty (java.util.Optional.empty)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 Response (javax.ws.rs.core.Response)1