Search in sources :

Example 16 with Reply

use of joynr.Reply 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 17 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class ProxyTest method createProxyAndCallSyncMethodFailWithApplicationError.

@Test
public void createProxyAndCallSyncMethodFailWithApplicationError() 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, new ApplicationException(ApplicationErrors.ERROR_VALUE_2, "syncMethodCallApplicationException")));
    ProxyBuilder<TestInterface> proxyBuilder = getProxyBuilder(TestInterface.class);
    TestInterface proxy = proxyBuilder.setMessagingQos(messagingQos).setDiscoveryQos(discoveryQos).build();
    ApplicationException exception = null;
    try {
        proxy.methodWithApplicationError();
        Assert.fail("Should throw ApplicationException");
    } catch (ApplicationException e) {
        exception = e;
    }
    Assert.assertEquals(new ApplicationException(ApplicationErrors.ERROR_VALUE_2, "syncMethodCallApplicationException"), exception);
}
Also used : ApplicationException(joynr.exceptions.ApplicationException) Reply(joynr.Reply) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 18 with Reply

use of joynr.Reply 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)

Example 19 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class RpcStubbingTest method setUp.

@Before
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_NULL_PARAM_DEREF", justification = "NPE in test would fail test")
public void setUp() throws JoynrCommunicationException, JoynrSendBufferFullException, JsonGenerationException, JsonMappingException, IOException, JoynrMessageNotSentException {
    Deferred<GpsLocation> deferredGpsLocation = new Deferred<GpsLocation>();
    deferredGpsLocation.resolve(gpsValue);
    when(testMock.returnsGpsLocation()).thenReturn(new Promise<Deferred<GpsLocation>>(deferredGpsLocation));
    Deferred<List<GpsLocation>> deferredGpsLocationList = new Deferred<List<GpsLocation>>();
    deferredGpsLocationList.resolve(gpsList);
    when(testMock.returnsGpsLocationList()).thenReturn(new Promise<Deferred<List<GpsLocation>>>(deferredGpsLocationList));
    DeferredVoid deferredVoid = new DeferredVoid();
    deferredVoid.resolve();
    when(testMock.noParamsNoReturnValue()).thenReturn(new Promise<DeferredVoid>(deferredVoid));
    when(testMock.takesTwoSimpleParams(any(Integer.class), any(String.class))).thenReturn(new Promise<DeferredVoid>(deferredVoid));
    fromParticipantId = UUID.randomUUID().toString();
    toParticipantId = UUID.randomUUID().toString();
    toDiscoveryEntry = new DiscoveryEntryWithMetaInfo();
    toDiscoveryEntry.setParticipantId(toParticipantId);
    // required to inject static members of JoynMessagingConnectorFactory
    injector = Guice.createInjector(new JoynrPropertiesModule(PropertyLoader.loadProperties(MessagingPropertyKeys.DEFAULT_MESSAGING_PROPERTIES_FILE)), new JsonMessageSerializerModule(), new AbstractModule() {

        @Override
        protected void configure() {
            requestStaticInjection(RpcUtils.class);
            install(new JoynrMessageScopeModule());
            MapBinder<Class<? extends Address>, AbstractMiddlewareMessagingStubFactory<? extends IMessagingStub, ? extends Address>> messagingStubFactory;
            messagingStubFactory = MapBinder.newMapBinder(binder(), new TypeLiteral<Class<? extends Address>>() {
            }, new TypeLiteral<AbstractMiddlewareMessagingStubFactory<? extends IMessagingStub, ? extends Address>>() {
            }, Names.named(MessagingStubFactory.MIDDLEWARE_MESSAGING_STUB_FACTORIES));
            messagingStubFactory.addBinding(InProcessAddress.class).to(InProcessMessagingStubFactory.class);
        }
    });
    final RequestInterpreter requestInterpreter = injector.getInstance(RequestInterpreter.class);
    final RequestCallerFactory requestCallerFactory = injector.getInstance(RequestCallerFactory.class);
    when(requestReplyManager.sendSyncRequest(eq(fromParticipantId), eq(toDiscoveryEntry), any(Request.class), any(SynchronizedReplyCaller.class), eq(messagingQos))).thenAnswer(new Answer<Reply>() {

        @Override
        public Reply answer(InvocationOnMock invocation) throws Throwable {
            RequestCaller requestCaller = requestCallerFactory.create(testMock);
            Object[] args = invocation.getArguments();
            Request request = null;
            for (Object arg : args) {
                if (arg instanceof Request) {
                    request = (Request) arg;
                    break;
                }
            }
            final Future<Reply> future = new Future<Reply>();
            ProviderCallback<Reply> callback = new ProviderCallback<Reply>() {

                @Override
                public void onSuccess(Reply result) {
                    future.onSuccess(result);
                }

                @Override
                public void onFailure(JoynrException error) {
                    future.onFailure(error);
                }
            };
            requestInterpreter.execute(callback, requestCaller, request);
            return future.get();
        }
    });
    JoynrMessagingConnectorFactory joynrMessagingConnectorFactory = new JoynrMessagingConnectorFactory(requestReplyManager, replyCallerDirectory, subscriptionManager);
    connector = joynrMessagingConnectorFactory.create(fromParticipantId, Sets.newHashSet(toDiscoveryEntry), messagingQos);
}
Also used : InProcessAddress(io.joynr.messaging.inprocess.InProcessAddress) Address(joynr.system.RoutingTypes.Address) InProcessAddress(io.joynr.messaging.inprocess.InProcessAddress) Deferred(io.joynr.provider.Deferred) DeferredVoid(io.joynr.provider.DeferredVoid) TypeLiteral(com.google.inject.TypeLiteral) JoynrPropertiesModule(io.joynr.common.JoynrPropertiesModule) List(java.util.List) ProviderCallback(io.joynr.provider.ProviderCallback) JsonMessageSerializerModule(io.joynr.messaging.JsonMessageSerializerModule) Request(joynr.Request) GpsLocation(joynr.types.Localisation.GpsLocation) JoynrException(io.joynr.exceptions.JoynrException) AbstractModule(com.google.inject.AbstractModule) JoynrMessageScopeModule(io.joynr.context.JoynrMessageScopeModule) RequestCaller(io.joynr.dispatching.RequestCaller) AbstractMiddlewareMessagingStubFactory(io.joynr.messaging.AbstractMiddlewareMessagingStubFactory) RequestInterpreter(io.joynr.dispatching.rpc.RequestInterpreter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Reply(joynr.Reply) IMessagingStub(io.joynr.messaging.IMessagingStub) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) RequestCallerFactory(io.joynr.dispatching.RequestCallerFactory) Before(org.junit.Before)

Example 20 with Reply

use of joynr.Reply in project joynr by bmwcarit.

the class SerializationTest method serializeReplyWithJoynrCommunicationException.

@Test
public void serializeReplyWithJoynrCommunicationException() throws IOException {
    JoynrCommunicationException error = new JoynrCommunicationException("detail message: JoynrCommunicationException");
    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) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) 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