Search in sources :

Example 1 with IResponseService

use of org.jowidgets.invocation.common.api.IResponseService in project jo-client-platform by jo-source.

the class InvocationClientImpl method getResponseService.

@Override
public IResponseService getResponseService() {
    return new IResponseService() {

        @Override
        public void response(final Object requestId, final Object response) {
            Assert.paramNotNull(requestId, "requestId");
            final TimeStampedObject<Tuple<Object, IMessageChannel>> request = interimRequests.remove(requestId);
            if (request != null) {
                final Tuple<Object, IMessageChannel> tuple = request.getObject();
                final ResponseMessage message = new ResponseMessage(requestId, response);
                tuple.getSecond().send(message, new ExceptionCallback(invocationClientServiceRegistry, tuple.getFirst()));
            } else {
                throw new IllegalStateException("The request id '" + requestId + "' is not known");
            }
        }
    };
}
Also used : IResponseService(org.jowidgets.invocation.common.api.IResponseService) ResponseMessage(org.jowidgets.invocation.common.impl.ResponseMessage) IMessageChannel(org.jowidgets.message.api.IMessageChannel) Tuple(org.jowidgets.util.Tuple)

Example 2 with IResponseService

use of org.jowidgets.invocation.common.api.IResponseService in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testRequestResponse.

@Test
public void testRequestResponse() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final Integer requestId = Integer.valueOf(1);
    final String invocationParameter = "InvocationParameter";
    final String request = "Request";
    final String response = "Response";
    final String methodResult = "Result";
    // setup server
    final IInvocationCallbackService serverInvocationCallback = InvocationServerToolkit.getServer(BROKER_ID).getInvocationCallbackService();
    final IMethod serverMethod = Mockito.mock(IMethod.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            serverInvocationCallback.interimRequest(invocationId, requestId, request);
            return null;
        }
    }).when(serverMethod).invoke(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethod);
    final IResponseService serverResponseService = Mockito.mock(IResponseService.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            final String responseArg = invocation.getArgumentAt(1, String.class);
            serverInvocationCallback.finished(invocationId, responseArg + methodResult);
            return null;
        }
    }).when(serverResponseService).response(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverResponseService);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            final Integer requestIdArg = invocation.getArgumentAt(1, Integer.class);
            final String requestArg = invocation.getArgumentAt(2, String.class);
            InvocationClientToolkit.getClient(BROKER_ID).getResponseService().response(requestIdArg, requestArg + response);
            return null;
        }
    }).when(clientInvocationCallback).interimRequest(Mockito.any(), Mockito.any(), Mockito.any());
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IMethod clientMethod = InvocationClientToolkit.getClient(BROKER_ID).getMethod(methodName);
    // invoke method on client
    clientMethod.invoke(invocationId, invocationParameter);
    Mockito.verify(serverMethod, Mockito.never()).invoke(invocationId, invocationParameter);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverMethod, Mockito.times(1)).invoke(invocationId, invocationParameter);
    Mockito.verify(clientInvocationCallback, Mockito.never()).interimRequest(invocationId, requestId, request);
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(clientInvocationCallback, Mockito.times(1)).interimRequest(invocationId, requestId, request);
    Mockito.verify(serverResponseService, Mockito.never()).response(requestId, request + response);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverResponseService, Mockito.times(1)).response(requestId, request + response);
    Mockito.verify(clientInvocationCallback, Mockito.never()).finished(invocationId, request + response + methodResult);
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(clientInvocationCallback, Mockito.times(1)).finished(invocationId, request + response + methodResult);
    assertNoLogErrorOrWarning();
}
Also used : IResponseService(org.jowidgets.invocation.common.api.IResponseService) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IMethod(org.jowidgets.invocation.common.api.IMethod) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Aggregations

IResponseService (org.jowidgets.invocation.common.api.IResponseService)2 IInvocationCallbackService (org.jowidgets.invocation.common.api.IInvocationCallbackService)1 IMethod (org.jowidgets.invocation.common.api.IMethod)1 ResponseMessage (org.jowidgets.invocation.common.impl.ResponseMessage)1 IMessageChannel (org.jowidgets.message.api.IMessageChannel)1 Tuple (org.jowidgets.util.Tuple)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1