use of uk.gov.justice.services.core.requester.Requester in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidatingMockTest method shouldPassWhenPayloadReturnedByRequesterAdheresToSchema.
@Test
public void shouldPassWhenPayloadReturnedByRequesterAdheresToSchema() throws Exception {
final Requester requester = mock(Requester.class);
when(requester.request(any(JsonEnvelope.class))).thenReturn(envelope().with(metadataWithRandomUUID("example.get-recipe")).withPayloadOf("someName", "name").withPayloadOf(true, "glutenFree").build());
new RequestingHandler(requester).handle(envelope().build());
}
use of uk.gov.justice.services.core.requester.Requester in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidatingMockTest method shouldThrowExceptionWhenPayloadReturnedByRequesterDoesNotAdhereToSchema.
@Test
public void shouldThrowExceptionWhenPayloadReturnedByRequesterDoesNotAdhereToSchema() {
exception.expect(MockitoException.class);
exception.expectCause(allOf(instanceOf(JsonSchemaValidationException.class), hasProperty("message", containsString("#: required key [glutenFree] not found"))));
final Requester requester = mock(Requester.class);
when(requester.request(any(JsonEnvelope.class))).thenReturn(envelope().with(metadataWithRandomUUID("example.get-recipe")).withPayloadOf("someName", "name").build());
new RequestingHandler(requester).handle(envelope().build());
}
use of uk.gov.justice.services.core.requester.Requester in project microservice_framework by CJSCommonPlatform.
the class HandlerMethodMatcher method isRequesterPassThrough.
private boolean isRequesterPassThrough(final Class<?> handlerClass, final Method method) throws Exception {
final Requester requester = mock(Requester.class, withSettings().name(format("%s.requester.request", method.getName())).invocationListeners(SKIP_JSON_VALIDATION_LISTENER).defaultAnswer(RETURNS_DEFAULTS.get()));
final JsonEnvelope query = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope response = envelope().with(metadataWithDefaults()).build();
final Object handlerInstance = handlerClass.newInstance();
final Field requesterField = findField(handlerClass, Requester.class);
requesterField.setAccessible(true);
requesterField.set(handlerInstance, requester);
when(requester.request(query)).thenReturn(response);
assertThat(method.invoke(handlerInstance, query), is(response));
verify(requester, times(ONCE)).request(query);
return true;
}
use of uk.gov.justice.services.core.requester.Requester in project microservice_framework by CJSCommonPlatform.
the class ServiceComponents method verifyPassThroughQueryHandlerMethod.
/**
* Verify a list of method names of a Query handler. Verifies there is a ServiceComponent
* annotation, each method has a Handles annotation, the Requester field is present, and the
* requester.request method is called with the original command passed to the handler method.
*
* @param handlerClass the handler class to verify
* @param methods the method names to verify
* @throws Exception if non pass through method or an error occurs
*/
public static void verifyPassThroughQueryHandlerMethod(final Class<?> handlerClass, final List<Method> methods) throws Exception {
assertIsServiceComponent(handlerClass);
assertHandlerHasMethods(handlerClass, methods);
for (final Method method : methods) {
assertMethodHasHandlesAnnotation(method);
final Requester requester = mock(Requester.class, withSettings().name(format("%s.requester", method.getName())).invocationListeners(SKIP_JSON_VALIDATION_LISTENER).defaultAnswer(RETURNS_DEFAULTS.get()));
final JsonEnvelope query = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope response = envelope().with(metadataWithDefaults()).build();
final Object handlerInstance = handlerClass.newInstance();
final Field requesterField = findField(handlerClass, Requester.class);
requesterField.setAccessible(true);
requesterField.set(handlerInstance, requester);
when(requester.request(query)).thenReturn(response);
JsonEnvelope actualResponse = (JsonEnvelope) method.invoke(handlerInstance, query);
if (actualResponse == null || !actualResponse.equals(response)) {
throw new AssertionError(format("JsonEnvelope response does not match expected response in method %s.", method.getName()));
}
verify(requester).request(query);
}
}
Aggregations