Search in sources :

Example 1 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class CcMessageRouterTest method testNotRoutableReplyDropped.

@Test
public void testNotRoutableReplyDropped() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final String unknownParticipantId = "unknown_participant_id";
    final String requestReplyId = "some_request_reply_id";
    final Reply reply = new Reply(requestReplyId, new JoynrRuntimeException("TestException"));
    final MutableMessage mutableMessage = messageFactory.createReply(fromParticipantId, unknownParticipantId, reply, new MessagingQos());
    final ImmutableMessage immutableMessage = mutableMessage.getImmutableMessage();
    MessageProcessedListener mockMsgProcessedListener = Mockito.mock(MessageProcessedListener.class);
    messageRouter.registerMessageProcessedListener(mockMsgProcessedListener);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            semaphore.release();
            return null;
        }
    }).when(mockMsgProcessedListener).messageProcessed(anyString());
    messageRouter.route(immutableMessage);
    semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS);
    verify(mockMsgProcessedListener).messageProcessed(immutableMessage.getId());
    verifyNoMoreInteractions(messagingStubMock);
}
Also used : Semaphore(java.util.concurrent.Semaphore) Mockito.anyString(org.mockito.Mockito.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) MutableMessage(joynr.MutableMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) Reply(joynr.Reply) Test(org.junit.Test)

Example 2 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class JoynrMessagingConnectorInvocationHandler method executeSyncMethod.

@CheckForNull
@Override
public Object executeSyncMethod(Method method, Object[] args) throws ApplicationException {
    // TODO does a method with 0 args pass in an empty args array, or null for args?
    if (method == null) {
        throw new IllegalArgumentException("Method cannot be null");
    }
    if (toDiscoveryEntries.size() > 1) {
        throw new JoynrIllegalStateException("You can't execute sync methods for multiple participants.");
    }
    if (toDiscoveryEntries.isEmpty()) {
        throw new JoynrIllegalStateException("You must have exactly one participant to be able to execute a sync method.");
    }
    MethodMetaInformation methodMetaInformation = JoynrMessagingConnectorFactory.ensureMethodMetaInformationPresent(method);
    Request request = new Request(method.getName(), args, method.getParameterTypes());
    Reply reply;
    String requestReplyId = request.getRequestReplyId();
    SynchronizedReplyCaller synchronizedReplyCaller = new SynchronizedReplyCaller(fromParticipantId, requestReplyId, request);
    ExpiryDate expiryDate = DispatcherUtils.convertTtlToExpirationDate(qosSettings.getRoundTripTtl_ms());
    replyCallerDirectory.addReplyCaller(requestReplyId, synchronizedReplyCaller, expiryDate);
    reply = (Reply) requestReplyManager.sendSyncRequest(fromParticipantId, toDiscoveryEntries.iterator().next(), request, synchronizedReplyCaller, qosSettings);
    if (reply.getError() == null) {
        if (method.getReturnType().equals(void.class)) {
            return null;
        }
        Object response = RpcUtils.reconstructReturnedObject(method, methodMetaInformation, reply.getResponse());
        logger.debug("REQUEST returns successful: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), response);
        return response;
    } else if (reply.getError() instanceof ApplicationException) {
        logger.debug("REQUEST returns error: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), reply.getError());
        throw (ApplicationException) reply.getError();
    } else {
        logger.debug("REQUEST returns error: requestReplyId: {}, method {}, response: {}", requestReplyId, method.getName(), reply.getError());
        throw (JoynrRuntimeException) reply.getError();
    }
}
Also used : ExpiryDate(io.joynr.common.ExpiryDate) ApplicationException(joynr.exceptions.ApplicationException) MethodMetaInformation(joynr.MethodMetaInformation) OneWayRequest(joynr.OneWayRequest) Request(joynr.Request) Reply(joynr.Reply) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) CheckForNull(javax.annotation.CheckForNull)

Example 3 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class ConnectorTest method syncMethodCallCallsRequestReplyManagerWithCorrectArguments.

@Test
public void syncMethodCallCallsRequestReplyManagerWithCorrectArguments() {
    ConnectorInvocationHandler connector = createConnector();
    assertNotNull(connector);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    try {
        Method method = TestSyncInterface.class.getDeclaredMethod("methodWithoutParameters");
        when(requestReplyManager.sendSyncRequest(any(String.class), any(DiscoveryEntryWithMetaInfo.class), any(Request.class), any(SynchronizedReplyCaller.class), any(MessagingQos.class))).thenReturn(new Reply());
        connector.executeSyncMethod(method, new Object[] {});
        verify(requestReplyManager, times(1)).sendSyncRequest(eq(fromParticipantId), eq(toDiscoveryEntry), requestCaptor.capture(), isA(SynchronizedReplyCaller.class), eq(qosSettings));
        Request actualRequest = requestCaptor.getValue();
        Request expectedRequest = new Request(method.getName(), new Object[] {}, new String[] {}, actualRequest.getRequestReplyId());
        assertEquals(expectedRequest, actualRequest);
    } catch (Exception e) {
        fail("Unexpected exception from sync method call: " + e);
    }
}
Also used : MessagingQos(io.joynr.messaging.MessagingQos) OneWayRequest(joynr.OneWayRequest) Request(joynr.Request) Reply(joynr.Reply) Method(java.lang.reflect.Method) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) SubscriptionException(io.joynr.exceptions.SubscriptionException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) Test(org.junit.Test)

Example 4 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class ProxyTest method createProxyAndCallSyncMethodSuccess.

@Test
public void createProxyAndCallSyncMethodSuccess() throws Exception {
    String requestReplyId = "createProxyAndCallSyncMethod_requestReplyId";
    Mockito.when(requestReplyManager.sendSyncRequest(Mockito.<String>any(), Mockito.<DiscoveryEntryWithMetaInfo>any(), Mockito.<Request>any(), Mockito.<SynchronizedReplyCaller>any(), Mockito.<MessagingQos>any())).thenReturn(new Reply(requestReplyId, "Answer"));
    ProxyBuilder<TestInterface> proxyBuilder = getProxyBuilder(TestInterface.class);
    TestInterface proxy = proxyBuilder.setMessagingQos(messagingQos).setDiscoveryQos(discoveryQos).build();
    String result = proxy.method1();
    Assert.assertEquals("Answer", result);
}
Also used : Reply(joynr.Reply) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 5 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class SerializationTest method serializeReplyWithProviderRuntimenException.

@Test
public void serializeReplyWithProviderRuntimenException() throws IOException {
    ProviderRuntimeException error = new ProviderRuntimeException("detail message: ProviderRuntimeException");
    Reply reply = new Reply(UUID.randomUUID().toString(), error);
    String writeValueAsString = objectMapper.writeValueAsString(reply);
    System.out.println(writeValueAsString);
    Reply receivedReply = objectMapper.readValue(writeValueAsString, Reply.class);
    Assert.assertEquals(reply, receivedReply);
}
Also used : Reply(joynr.Reply) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) Test(org.junit.Test)

Aggregations

Reply (joynr.Reply)38 Test (org.junit.Test)33 Request (joynr.Request)6 SynchronizedReplyCaller (io.joynr.dispatching.rpc.SynchronizedReplyCaller)5 ApplicationException (joynr.exceptions.ApplicationException)5 Matchers.anyString (org.mockito.Matchers.anyString)5 ReplyCaller (io.joynr.dispatching.rpc.ReplyCaller)4 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)4 MessagingQos (io.joynr.messaging.MessagingQos)4 IOException (java.io.IOException)4 MutableMessage (joynr.MutableMessage)4 OneWayRequest (joynr.OneWayRequest)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 ImmutableMessage (joynr.ImmutableMessage)3 MulticastSubscriptionRequest (joynr.MulticastSubscriptionRequest)3 SubscriptionRequest (joynr.SubscriptionRequest)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 AbstractModule (com.google.inject.AbstractModule)2 TypeLiteral (com.google.inject.TypeLiteral)2