Search in sources :

Example 76 with JsonEnvelope

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

the class RestClientGenerator_MethodBodyTest method shouldCallRestClientWithEndpointDefinitionContainingMediaType.

@Test
@SuppressWarnings("unchecked")
public void shouldCallRestClientWithEndpointDefinitionContainingMediaType() throws Exception {
    generateRemoteSomeComponent2ServiceCommandApi();
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    final JsonEnvelope outputEnvelope = mock(JsonEnvelope.class);
    final Function function = mock(Function.class);
    final Class<?> clazz = compiler.compiledClassOf(BASE_PACKAGE, "RemoteSomeComponent2ServiceCommandApi");
    final Object remoteClient = instanceOfRemoteClient(clazz);
    final Method method = firstMethodOf(clazz).get();
    when(enveloper.withMetadataFrom(envelope, "ctx.defcmd")).thenReturn(function);
    when(function.apply(envelope.payload())).thenReturn(outputEnvelope);
    method.invoke(remoteClient, envelope);
    assertThat(capturedPostEndpointDefinition().getResponseMediaType(), is("ctx.defcmd"));
}
Also used : Function(java.util.function.Function) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 77 with JsonEnvelope

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

the class RestClientGenerator_MethodBodyTest method shouldSetNameAndPassEnvelopeToRestClient.

@Test
@SuppressWarnings("unchecked")
public void shouldSetNameAndPassEnvelopeToRestClient() throws Exception {
    generateRemoteSomeComponent2ServiceCommandApi();
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    final JsonEnvelope outputEnvelope = mock(JsonEnvelope.class);
    final Function function = mock(Function.class);
    final Class<?> clazz = compiler.compiledClassOf(BASE_PACKAGE, "RemoteSomeComponent2ServiceCommandApi");
    final Object remoteClient = instanceOfRemoteClient(clazz);
    final Method method = firstMethodOf(clazz).get();
    when(enveloper.withMetadataFrom(envelope, "ctx.defcmd")).thenReturn(function);
    when(function.apply(envelope.payload())).thenReturn(outputEnvelope);
    method.invoke(remoteClient, envelope);
    verify(restClientProcessor).post(any(EndpointDefinition.class), eq(outputEnvelope));
}
Also used : Function(java.util.function.Function) EndpointDefinition(uk.gov.justice.services.clients.core.EndpointDefinition) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 78 with JsonEnvelope

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

the class RestClientGenerator_MethodBodyTest method shouldCallSynchronousPutRestClientMethod.

@Test
@SuppressWarnings("unchecked")
public void shouldCallSynchronousPutRestClientMethod() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/pathabc/{anId}").with(httpAction().withHttpActionType(PUT).withMediaTypeWithDefaultSchema("application/vnd.ctx.defcmd+json").withResponseTypes("application/vnd.ctx.response+json").with(mapping().withName("action1").withRequestType("application/vnd.ctx.defcmd+json").withResponseType("application/vnd.ctx.response+json")))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, generatorProperties().withServiceComponentOf("SOME_COMPONENT")));
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    final JsonEnvelope outputEnvelope = mock(JsonEnvelope.class);
    final Function function = mock(Function.class);
    final Class<?> clazz = compiler.compiledClassOf(BASE_PACKAGE, "RemoteSomeComponent2ServiceCommandApi");
    final Object remoteClient = instanceOfRemoteClient(clazz);
    final Method method = firstMethodOf(clazz).get();
    when(enveloper.withMetadataFrom(envelope, "ctx.defcmd")).thenReturn(function);
    when(function.apply(envelope.payload())).thenReturn(outputEnvelope);
    method.invoke(remoteClient, envelope);
    verify(restClientProcessor).synchronousPut(any(EndpointDefinition.class), eq(outputEnvelope));
}
Also used : Function(java.util.function.Function) EndpointDefinition(uk.gov.justice.services.clients.core.EndpointDefinition) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 79 with JsonEnvelope

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

the class RestClientGenerator_MethodBodyTest method shouldCallSynchronousPostRestClientMethod.

@Test
@SuppressWarnings("unchecked")
public void shouldCallSynchronousPostRestClientMethod() throws Exception {
    generator.run(restRamlWithCommandApiDefaults().with(resource("/pathabc/{anId}").with(httpAction().withHttpActionType(POST).withMediaTypeWithDefaultSchema("application/vnd.ctx.defcmd+json").withResponseTypes("application/vnd.ctx.response+json").with(mapping().withName("action1").withRequestType("application/vnd.ctx.defcmd+json").withResponseType("application/vnd.ctx.response+json")))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, generatorProperties().withServiceComponentOf("SOME_COMPONENT")));
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    final JsonEnvelope outputEnvelope = mock(JsonEnvelope.class);
    final Function function = mock(Function.class);
    final Class<?> clazz = compiler.compiledClassOf(BASE_PACKAGE, "RemoteSomeComponent2ServiceCommandApi");
    final Object remoteClient = instanceOfRemoteClient(clazz);
    final Method method = firstMethodOf(clazz).get();
    when(enveloper.withMetadataFrom(envelope, "ctx.defcmd")).thenReturn(function);
    when(function.apply(envelope.payload())).thenReturn(outputEnvelope);
    method.invoke(remoteClient, envelope);
    verify(restClientProcessor).synchronousPost(any(EndpointDefinition.class), eq(outputEnvelope));
}
Also used : Function(java.util.function.Function) EndpointDefinition(uk.gov.justice.services.clients.core.EndpointDefinition) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 80 with JsonEnvelope

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

the class EventBufferInterceptorTest method ensureStreamCLosedIfExceptionOccurs.

@Test
public void ensureStreamCLosedIfExceptionOccurs() throws Exception {
    final Deque<Interceptor> interceptors = new LinkedList<>();
    interceptors.add(eventBufferInterceptor);
    interceptors.add(new ExceptionThrowingInterceptor());
    target = new TestTarget();
    interceptorChain = new DefaultInterceptorChain(interceptors, target);
    final InterceptorContext inputContext = interceptorContextWithInput(envelope_1);
    final StreamCloseSpy streamSpy = new StreamCloseSpy();
    final Stream<JsonEnvelope> envelopes = Stream.of(this.envelope_1, envelope_2).onClose(streamSpy);
    when(eventBufferService.currentOrderedEventsWith(this.envelope_1)).thenReturn(envelopes);
    try {
        interceptorChain.processNext(inputContext);
    } catch (TestException expected) {
    // do nothing
    }
    assertThat(streamSpy.streamClosed(), is(true));
}
Also used : StreamCloseSpy(uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Interceptor(uk.gov.justice.services.core.interceptor.Interceptor) LinkedList(java.util.LinkedList) DefaultInterceptorChain(uk.gov.justice.services.core.interceptor.DefaultInterceptorChain) Test(org.junit.Test)

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