Search in sources :

Example 6 with Parameter

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

the class RestAdapterGenerator_POSTMethodBodyTest method shouldPassMapWithOnePathParamToRestProcessor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldPassMapWithOnePathParamToRestProcessor() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/some/path/{paramA}", "paramA").with(httpActionWithDefaultMapping(POST).withHttpActionOfDefaultRequestType())).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> resourceClass = compiler.compiledClassOf(BASE_PACKAGE, "resource", "DefaultCommandApiSomePathParamAResource");
    final Object resourceObject = getInstanceOf(resourceClass);
    final Method method = firstMethodOf(resourceClass).get();
    method.invoke(resourceObject, "paramValue1234", 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("paramA"));
    assertThat(pathParam.getStringValue(), is("paramValue1234"));
}
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 7 with Parameter

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

the class DefaultRestProcessor method process.

private Response process(final String responseStrategyName, final Function<InterceptorContext, Optional<JsonEnvelope>> interceptorChain, final String action, final Optional<JsonObject> initialPayload, final HttpHeaders headers, final Collection<Parameter> params, final Optional<List<FileInputDetails>> fileInputDetails) {
    traceLogger.trace(logger, () -> format("Processing REST message: %s", httpTraceLoggerHelper.toHttpHeaderTrace(headers)));
    final JsonEnvelope envelope = envelopeBuilderFactory.builder().withInitialPayload(initialPayload).withAction(action).withHeaders(headers).withParams(params).build();
    traceLogger.trace(logger, () -> format("REST message converted to envelope: %s", envelope));
    final InterceptorContext interceptorContext = fileInputDetails.map(value -> fileBasedInterceptorContextFactory.create(value, envelope)).orElseGet(() -> interceptorContextWithInput(envelope));
    final Optional<JsonEnvelope> result = interceptorChain.apply(interceptorContext);
    traceLogger.trace(logger, () -> format("REST message processed: %s", envelope));
    return responseStrategyCache.responseStrategyOf(responseStrategyName).responseFor(action, result);
}
Also used : FileInputDetails(uk.gov.justice.services.adapter.rest.multipart.FileInputDetails) Optional.empty(java.util.Optional.empty) JsonObject(javax.json.JsonObject) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) Logger(org.slf4j.Logger) Collection(java.util.Collection) Function(java.util.function.Function) String.format(java.lang.String.format) InterceptorContext.interceptorContextWithInput(uk.gov.justice.services.core.interceptor.InterceptorContext.interceptorContextWithInput) RestEnvelopeBuilderFactory(uk.gov.justice.services.adapter.rest.envelope.RestEnvelopeBuilderFactory) HttpTraceLoggerHelper(uk.gov.justice.services.messaging.logging.HttpTraceLoggerHelper) Inject(javax.inject.Inject) FileBasedInterceptorContextFactory(uk.gov.justice.services.adapter.rest.multipart.FileBasedInterceptorContextFactory) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) TraceLogger(uk.gov.justice.services.messaging.logging.TraceLogger) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Example 8 with Parameter

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

the class DefaultRestProcessorTest method shouldPassEnvelopeWithPayloadToInterceptorChain.

@Test
public void shouldPassEnvelopeWithPayloadToInterceptorChain() throws Exception {
    final String userId = "userId1234";
    final String action = "anAction123";
    final String payloadIdValue = "payloadIdValue1";
    final List<Parameter> pathParams = singletonList(DefaultParameter.valueOf("paramName", "someParamValue", ParameterType.STRING));
    final JsonObject payload = createObjectBuilder().add("payloadId", payloadIdValue).build();
    when(responseStrategyCache.responseStrategyOf(anyString())).thenReturn(responseStrategy);
    restProcessor.process(NOT_USED_RESPONSE_STRATEGY_NAME, interceptorChain, action, Optional.of(payload), headersWithUserId(userId), pathParams);
    final ArgumentCaptor<InterceptorContext> interceptorContextCaptor = forClass(InterceptorContext.class);
    verify(interceptorChain).apply(interceptorContextCaptor.capture());
    final InterceptorContext interceptorContext = interceptorContextCaptor.getValue();
    final JsonEnvelope envelope = interceptorContext.inputEnvelope();
    assertThat(envelope, jsonEnvelope().withMetadataOf(metadata().withName(action).withUserId(userId)).withPayloadOf(payloadIsJson(allOf(withJsonPath("$.payloadId", equalTo(payloadIdValue)), withJsonPath("$.paramName", equalTo("someParamValue"))))));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) DefaultParameter(uk.gov.justice.services.adapter.rest.parameter.DefaultParameter) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) JsonObject(javax.json.JsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 9 with Parameter

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

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

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