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);
}
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);
}
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());
}
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);
}
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);
}
Aggregations