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"));
}
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);
}
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"))))));
}
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))))));
}
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")))));
}
Aggregations