Search in sources :

Example 6 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class JsonEnvelopeListMatcherTest method shouldNotMatchJsonEnvelopesInAListIfThereAreTooMany.

@Test(expected = AssertionError.class)
public void shouldNotMatchJsonEnvelopesInAListIfThereAreTooMany() throws Exception {
    final JsonEnvelope event_1 = jsonEnvelopeWith(ID_1, NAME_1);
    final JsonEnvelope event_2 = jsonEnvelopeWith(ID_2, NAME_2);
    final List<JsonEnvelope> events = asList(event_1, event_2);
    assertThat(events, JsonEnvelopeListMatcher.listContaining(jsonEnvelope(metadata().withName("event.action"), payloadIsJson(allOf(withJsonPath("$.someId", equalTo(ID_1.toString())), withJsonPath("$.name", equalTo(NAME_1)))))));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 7 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class JsonEnvelopeMetadataMatcherTest method shouldMatchAGivenMetadataWhereEnvelopedFromJsonEnvelope.

@Test
public void shouldMatchAGivenMetadataWhereEnvelopedFromJsonEnvelope() throws Exception {
    final Metadata testMetadata = defaultMetadataRandomIdWithName(EVENT_NAME).withCausation(ID, CAUSATION_ID).build();
    final JsonEnvelope jsonEnvelope = envelope().with(defaultMetadataWithName(COMMAND_ACTION)).withPayloadOf("Test", "value").build();
    assertThat(testMetadata, JsonEnvelopeMetadataMatcher.withMetadataEnvelopedFrom(jsonEnvelope));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 8 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class ServiceComponents method verifyPassThroughCommandHandlerMethod.

/**
 * Verify a list of method names of a Command handler.  Verifies there is a ServiceComponent
 * annotation, each method has a Handles annotation, the Sender field is present, and the
 * sender.send 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 verifyPassThroughCommandHandlerMethod(final Class<?> handlerClass, final List<Method> methods) throws Exception {
    assertIsServiceComponent(handlerClass);
    assertHandlerHasMethods(handlerClass, methods);
    for (final Method method : methods) {
        assertMethodHasHandlesAnnotation(method);
        final Sender sender = mock(Sender.class, withSettings().name(format("%s.sender.send", method.getName())).invocationListeners(SKIP_JSON_VALIDATION_LISTENER).defaultAnswer(RETURNS_DEFAULTS.get()));
        final JsonEnvelope command = envelope().with(metadataWithDefaults()).build();
        final Object handlerInstance = handlerClass.newInstance();
        final Field senderField = findField(handlerClass, Sender.class);
        senderField.setAccessible(true);
        senderField.set(handlerInstance, sender);
        method.invoke(handlerInstance, command);
        verify(sender).send(command);
    }
}
Also used : Sender(uk.gov.justice.services.core.sender.Sender) Field(java.lang.reflect.Field) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Method(java.lang.reflect.Method)

Example 9 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope 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;
}
Also used : Field(java.lang.reflect.Field) Requester(uk.gov.justice.services.core.requester.Requester) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Example 10 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class HandlerMethodMatcher method isSenderPassThrough.

private boolean isSenderPassThrough(final Class<?> handlerClass, final Method method) throws Exception {
    final Sender sender = mock(Sender.class, withSettings().name(format("%s.sender.send", method.getName())).invocationListeners(SKIP_JSON_VALIDATION_LISTENER).defaultAnswer(RETURNS_DEFAULTS.get()));
    final JsonEnvelope command = envelope().with(metadataWithDefaults()).build();
    final Object handlerInstance = handlerClass.newInstance();
    final Field senderField = findField(handlerClass, Sender.class);
    senderField.setAccessible(true);
    senderField.set(handlerInstance, sender);
    method.invoke(handlerInstance, command);
    verify(sender, times(ONCE)).send(command);
    return true;
}
Also used : Sender(uk.gov.justice.services.core.sender.Sender) Field(java.lang.reflect.Field) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

Aggregations

JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)276 Test (org.junit.Test)249 UUID (java.util.UUID)69 UUID.randomUUID (java.util.UUID.randomUUID)64 JsonObject (javax.json.JsonObject)30 JsonValue (javax.json.JsonValue)26 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)24 Metadata (uk.gov.justice.services.messaging.Metadata)18 Method (java.lang.reflect.Method)17 Function (java.util.function.Function)14 StreamBufferEvent (uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent)10 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)9 JsonObjects.getJsonObject (uk.gov.justice.services.messaging.JsonObjects.getJsonObject)9 EndpointDefinition (uk.gov.justice.services.clients.core.EndpointDefinition)8 MediaType (uk.gov.justice.services.core.mapping.MediaType)8 StreamStatus (uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus)7 ZonedDateTime (java.time.ZonedDateTime)6 Collection (java.util.Collection)6 Optional (java.util.Optional)6 HttpHeaders (javax.ws.rs.core.HttpHeaders)6