Search in sources :

Example 11 with IMethod

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

the class InvocationIntegrationTest method testExceptionAfterFinsihed.

@Test
public void testExceptionAfterFinsihed() {
    JUnitLoggerProvider.getConsoleLoggerEnablement().setEnabled(false);
    final String methodName = "Method";
    final String methodResult = "Result";
    final RuntimeException expectedException = new RuntimeException("Something goes wrong");
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final IInvocationCallbackService serverInvocationCallback = InvocationServerToolkit.getServer(BROKER_ID).getInvocationCallbackService();
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            final Integer invocationIdArg = invocation.getArgumentAt(0, Integer.class);
            try {
                serverInvocationCallback.finished(invocationIdArg, methodResult);
                throw expectedException;
            } catch (final Exception e) {
                serverInvocationCallback.exeption(invocationIdArg, e);
            }
            return null;
        }
    }).when(serverMethodMock).invoke(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IMethod clientMethod = InvocationClientToolkit.getClient(BROKER_ID).getMethod(methodName);
    // invoke method on client
    clientMethod.invoke(invocationId, invocationParameter);
    messaging.getMessageReceiverBroker().dispatchMessages();
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(clientInvocationCallback, Mockito.never()).exeption(invocationId, expectedException);
    Mockito.verify(clientInvocationCallback, Mockito.times(1)).finished(invocationId, methodResult);
    assertNoLogErrorOrWarning();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) IMethod(org.jowidgets.invocation.common.api.IMethod) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Example 12 with IMethod

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

the class InvocationIntegrationTest method testException.

@Test
public void testException() {
    final String methodName = "Method";
    final String methodResult = "Result";
    final RuntimeException expectedException = new RuntimeException("Something goes wrong");
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final IInvocationCallbackService serverInvocationCallback = InvocationServerToolkit.getServer(BROKER_ID).getInvocationCallbackService();
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            final Integer invocationIdArg = invocation.getArgumentAt(0, Integer.class);
            try {
                throwException();
                serverInvocationCallback.finished(invocationIdArg, methodResult);
            } catch (final Exception e) {
                serverInvocationCallback.exeption(invocationIdArg, e);
            }
            return null;
        }

        private void throwException() {
            throw expectedException;
        }
    }).when(serverMethodMock).invoke(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    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(serverMethodMock, Mockito.never()).invoke(invocationId, invocationParameter);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverMethodMock, Mockito.times(1)).invoke(invocationId, invocationParameter);
    Mockito.verify(clientInvocationCallback, Mockito.never()).exeption(invocationId, expectedException);
    Mockito.verify(clientInvocationCallback, Mockito.never()).finished(invocationId, methodResult);
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(clientInvocationCallback, Mockito.times(1)).exeption(invocationId, expectedException);
    Mockito.verify(clientInvocationCallback, Mockito.never()).finished(invocationId, methodResult);
    assertNoLogErrorOrWarning();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) IMethod(org.jowidgets.invocation.common.api.IMethod) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Example 13 with IMethod

use of org.jowidgets.invocation.common.api.IMethod 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)

Example 14 with IMethod

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

the class InvocationIntegrationTest method testExceptionBeforeFinsihed.

@Test
public void testExceptionBeforeFinsihed() {
    JUnitLoggerProvider.getConsoleLoggerEnablement().setEnabled(false);
    final String methodName = "Method";
    final String methodResult = "Result";
    final RuntimeException expectedException = new RuntimeException("Something goes wrong");
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final IInvocationCallbackService serverInvocationCallback = InvocationServerToolkit.getServer(BROKER_ID).getInvocationCallbackService();
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            final Integer invocationIdArg = invocation.getArgumentAt(0, Integer.class);
            try {
                throw expectedException;
            } catch (final Exception e) {
                serverInvocationCallback.exeption(invocationIdArg, e);
            }
            serverInvocationCallback.finished(invocationIdArg, methodResult);
            return null;
        }
    }).when(serverMethodMock).invoke(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IMethod clientMethod = InvocationClientToolkit.getClient(BROKER_ID).getMethod(methodName);
    // invoke method on client
    clientMethod.invoke(invocationId, invocationParameter);
    messaging.getMessageReceiverBroker().dispatchMessages();
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(clientInvocationCallback, Mockito.times(1)).exeption(invocationId, expectedException);
    Mockito.verify(clientInvocationCallback, Mockito.never()).finished(invocationId, methodResult);
    assertLogError();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) IMethod(org.jowidgets.invocation.common.api.IMethod) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Aggregations

IMethod (org.jowidgets.invocation.common.api.IMethod)14 Test (org.junit.Test)10 IInvocationCallbackService (org.jowidgets.invocation.common.api.IInvocationCallbackService)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 IInvocationClient (org.jowidgets.invocation.client.api.IInvocationClient)3 ICancelService (org.jowidgets.invocation.common.api.ICancelService)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IResponseService (org.jowidgets.invocation.common.api.IResponseService)1 MethodInvocationMessage (org.jowidgets.invocation.common.impl.MethodInvocationMessage)1 IInterimRequestCallback (org.jowidgets.invocation.service.common.api.IInterimRequestCallback)1 IInterimResponseCallback (org.jowidgets.invocation.service.common.api.IInterimResponseCallback)1 IInvocationCallback (org.jowidgets.invocation.service.common.api.IInvocationCallback)1 IMethodInvocationService (org.jowidgets.invocation.service.common.api.IMethodInvocationService)1 JUnitLogger (org.jowidgets.logging.tools.JUnitLogger)1