Search in sources :

Example 1 with ReplyCaller

use of io.joynr.dispatching.rpc.ReplyCaller in project joynr by bmwcarit.

the class RequestReplyManagerImpl method handleReply.

@Override
public void handleReply(final Reply reply) {
    final ReplyCaller callBack = replyCallerDirectory.remove(reply.getRequestReplyId());
    if (callBack == null) {
        logger.warn("No reply caller found for id: " + reply.getRequestReplyId());
        return;
    }
    callBack.messageCallBack(reply);
}
Also used : ReplyCaller(io.joynr.dispatching.rpc.ReplyCaller) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller)

Example 2 with ReplyCaller

use of io.joynr.dispatching.rpc.ReplyCaller in project joynr by bmwcarit.

the class ProxyTest method createProxyAndCallAsyncMethodFail.

@Test
public void createProxyAndCallAsyncMethodFail() throws Exception {
    // Expect this exception to be passed back to the callback onFailure and thrown in the future
    final JoynrCommunicationException expectedException = new JoynrCommunicationException();
    // final JoynCommunicationException expectedException = null;
    TestInterface proxy = getTestInterfaceProxy();
    // when joynrMessageSender1.sendRequest is called, get the replyCaller from the mock dispatcher and call
    // messageCallback on it.
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws JsonParseException, JsonMappingException, IOException {
            // capture the replyCaller passed into the dispatcher for calling later
            ArgumentCaptor<ReplyCaller> replyCallerCaptor = ArgumentCaptor.forClass(ReplyCaller.class);
            verify(replyCallerDirectory).addReplyCaller(anyString(), replyCallerCaptor.capture(), any(ExpiryDate.class));
            // pass the exception to the replyCaller
            replyCallerCaptor.getValue().error(expectedException);
            return null;
        }
    }).when(requestReplyManager).sendRequest(Mockito.<String>any(), Mockito.<DiscoveryEntryWithMetaInfo>any(), Mockito.<Request>any(), Mockito.<MessagingQos>any());
    boolean exceptionThrown = false;
    String reply = "";
    final Future<String> future = proxy.asyncMethod(callback);
    try {
        // the test usually takes only 200 ms, so if we wait 1 sec, something has gone wrong
        reply = future.get(1000);
    } catch (JoynrCommunicationException e) {
        exceptionThrown = true;
    }
    Assert.assertTrue("exception must be thrown from get", exceptionThrown);
    verify(callback).onFailure(expectedException);
    verifyNoMoreInteractions(callback);
    Assert.assertEquals(RequestStatusCode.ERROR, future.getStatus().getCode());
    Assert.assertEquals("", reply);
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ReplyCaller(io.joynr.dispatching.rpc.ReplyCaller) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Test(org.junit.Test)

Example 3 with ReplyCaller

use of io.joynr.dispatching.rpc.ReplyCaller in project joynr by bmwcarit.

the class RequestReplyManagerTest method requestReplyMessagesRemoveCallBackByTtl.

@Test
public void requestReplyMessagesRemoveCallBackByTtl() throws Exception {
    TestProvider testResponder = new TestProvider(1);
    ExpiryDate ttlReplyCaller = ExpiryDate.fromRelativeTtl(1000L);
    final ReplyCaller replyCaller = mock(ReplyCaller.class);
    replyCallerDirectory.addReplyCaller(request1.getRequestReplyId(), replyCaller, ttlReplyCaller);
    Thread.sleep(ttlReplyCaller.getRelativeTtl() + 100);
    requestReplyManager.handleReply(new Reply(request1.getRequestReplyId(), testResponder.getSentPayloadFor(request1)));
    verify(replyCaller, never()).messageCallBack(any(Reply.class));
}
Also used : ExpiryDate(io.joynr.common.ExpiryDate) Reply(joynr.Reply) ReplyCaller(io.joynr.dispatching.rpc.ReplyCaller) Test(org.junit.Test)

Example 4 with ReplyCaller

use of io.joynr.dispatching.rpc.ReplyCaller in project joynr by bmwcarit.

the class ProxyTest method createProxyAndCallAsyncMethodSuccess.

@Test
public void createProxyAndCallAsyncMethodSuccess() throws Exception {
    TestInterface proxy = getTestInterfaceProxy();
    // when joynrMessageSender1.sendRequest is called, get the replyCaller from the mock dispatcher and call
    // messageCallback on it.
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws JsonParseException, JsonMappingException, IOException {
            // capture the replyCaller passed into the dispatcher for calling later
            ArgumentCaptor<ReplyCaller> replyCallerCaptor = ArgumentCaptor.forClass(ReplyCaller.class);
            verify(replyCallerDirectory).addReplyCaller(anyString(), replyCallerCaptor.capture(), Mockito.any(ExpiryDate.class));
            String requestReplyId = "createProxyAndCallAsyncMethodSuccess_requestReplyId";
            // pass the response to the replyCaller
            replyCallerCaptor.getValue().messageCallBack(new Reply(requestReplyId, new TextNode(asyncReplyText)));
            return null;
        }
    }).when(requestReplyManager).sendRequest(Mockito.<String>any(), Mockito.<DiscoveryEntryWithMetaInfo>any(), Mockito.<Request>any(), Mockito.<MessagingQos>any());
    final Future<String> future = proxy.asyncMethod(callback);
    // the test usually takes only 200 ms, so if we wait 1 sec, something has gone wrong
    String reply = future.get(1000);
    verify(callback).resolve(asyncReplyText);
    Assert.assertEquals(RequestStatusCode.OK, future.getStatus().getCode());
    Assert.assertEquals(asyncReplyText, reply);
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) TextNode(com.fasterxml.jackson.databind.node.TextNode) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ReplyCaller(io.joynr.dispatching.rpc.ReplyCaller) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Reply(joynr.Reply) Test(org.junit.Test)

Example 5 with ReplyCaller

use of io.joynr.dispatching.rpc.ReplyCaller in project joynr by bmwcarit.

the class ProxyTest method createProxyAndCallAsyncMethodFailWithApplicationError.

@SuppressWarnings({ "unchecked" })
@Test
public void createProxyAndCallAsyncMethodFailWithApplicationError() throws Exception {
    final ApplicationException expected = new ApplicationException(ApplicationErrors.ERROR_VALUE_3, "TEST: createProxyAndCallAsyncMethodFailWithApplicationError");
    TestInterface proxy = getTestInterfaceProxy();
    // when joynrMessageSender1.sendRequest is called, get the replyCaller from the mock dispatcher and call
    // messageCallback on it.
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws JsonParseException, JsonMappingException, IOException {
            // capture the replyCaller passed into the dispatcher for calling later
            ArgumentCaptor<ReplyCaller> replyCallerCaptor = ArgumentCaptor.forClass(ReplyCaller.class);
            verify(replyCallerDirectory).addReplyCaller(anyString(), replyCallerCaptor.capture(), any(ExpiryDate.class));
            String requestReplyId = "createProxyAndCallAsyncMethodSuccess_requestReplyId";
            // pass the response to the replyCaller
            replyCallerCaptor.getValue().messageCallBack(new Reply(requestReplyId, expected));
            return null;
        }
    }).when(requestReplyManager).sendRequest(Mockito.<String>any(), Mockito.<DiscoveryEntryWithMetaInfo>any(), Mockito.<Request>any(), Mockito.<MessagingQos>any());
    CallbackWithModeledError<String, Enum<?>> callbackWithApplicationException = Mockito.mock(CallbackWithModeledError.class);
    final Future<String> future = proxy.asyncMethodWithApplicationError(callbackWithApplicationException);
    // the test usually takes only 200 ms, so if we wait 1 sec, something has gone wrong
    try {
        future.get(1000);
        Assert.fail("Should throw ApplicationException");
    } catch (ApplicationException e) {
        Assert.assertEquals(expected, e);
    }
    verify(callbackWithApplicationException).onFailure(expected.getError());
    Assert.assertEquals(RequestStatusCode.ERROR, future.getStatus().getCode());
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ReplyCaller(io.joynr.dispatching.rpc.ReplyCaller) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) ApplicationException(joynr.exceptions.ApplicationException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Reply(joynr.Reply) Test(org.junit.Test)

Aggregations

ReplyCaller (io.joynr.dispatching.rpc.ReplyCaller)6 Test (org.junit.Test)5 SynchronizedReplyCaller (io.joynr.dispatching.rpc.SynchronizedReplyCaller)4 Reply (joynr.Reply)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 IOException (java.io.IOException)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 ExpiryDate (io.joynr.common.ExpiryDate)1 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)1 ApplicationException (joynr.exceptions.ApplicationException)1