Search in sources :

Example 1 with PromiseListener

use of io.joynr.provider.PromiseListener in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryTest method addGlobalCapSucceeds_NextAddShallNotAddGlobalAgain.

@SuppressWarnings("unchecked")
@Test(timeout = 1000)
public void addGlobalCapSucceeds_NextAddShallNotAddGlobalAgain() throws InterruptedException {
    ProviderQos providerQos = new ProviderQos();
    providerQos.setScope(ProviderScope.GLOBAL);
    String participantId = LocalCapabilitiesDirectoryTest.class.getName() + ".addGlobalCapSucceeds_NextAddShallNotAddGlobalAgain";
    String domain = "testDomain";
    final DiscoveryEntry discoveryEntry = new DiscoveryEntry(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, participantId, providerQos, System.currentTimeMillis(), expiryDateMs, publicKeyId);
    globalDiscoveryEntry = new GlobalDiscoveryEntry(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, participantId, providerQos, System.currentTimeMillis(), expiryDateMs, publicKeyId, channelAddressSerialized);
    Promise<DeferredVoid> promise = localCapabilitiesDirectory.add(discoveryEntry);
    promise.then(new PromiseListener() {

        @Override
        public void onFulfillment(Object... values) {
            Mockito.doAnswer(createAddAnswerWithSuccess()).when(globalCapabilitiesClient).add(any(Callback.class), eq(globalDiscoveryEntry));
            verify(globalDiscoveryEntryCacheMock).add(eq(globalDiscoveryEntry));
            verify(globalCapabilitiesClient).add(any(Callback.class), eq(globalDiscoveryEntry));
            reset(globalCapabilitiesClient);
            localCapabilitiesDirectory.add(discoveryEntry);
            verify(globalCapabilitiesClient, timeout(200).never()).add(any(Callback.class), eq(globalDiscoveryEntry));
        }

        @Override
        public void onRejection(JoynrException error) {
            Assert.fail("adding capability failed: " + error);
        }
    });
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) Version(joynr.types.Version) PromiseListener(io.joynr.provider.PromiseListener) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) JoynrException(io.joynr.exceptions.JoynrException) Matchers.anyString(org.mockito.Matchers.anyString) ProviderQos(joynr.types.ProviderQos) DeferredVoid(io.joynr.provider.DeferredVoid) Test(org.junit.Test)

Example 2 with PromiseListener

use of io.joynr.provider.PromiseListener in project joynr by bmwcarit.

the class ProviderWrapperTest method testInvokeMultiOutMethod.

@Test
public void testInvokeMultiOutMethod() throws Throwable {
    ProviderWrapper subject = createSubject();
    JoynrProvider proxy = createProxy(subject);
    Method method = TestServiceProviderInterface.class.getMethod("testMultiOutMethod");
    Object result = subject.invoke(proxy, method, new Object[0]);
    assertTrue(result instanceof Promise);
    Promise<?> promise = (Promise<?>) result;
    assertTrue(promise.isFulfilled());
    promise.then(new PromiseListener() {

        @Override
        public void onFulfillment(Object... values) {
            assertArrayEquals(new Object[] { "one", "two" }, values);
        }

        @Override
        public void onRejection(JoynrException error) {
            fail("Shouldn't be here.");
        }
    });
}
Also used : Promise(io.joynr.provider.Promise) PromiseListener(io.joynr.provider.PromiseListener) JoynrException(io.joynr.exceptions.JoynrException) Method(java.lang.reflect.Method) JoynrProvider(io.joynr.provider.JoynrProvider) ProviderWrapper(io.joynr.jeeintegration.ProviderWrapper) Test(org.junit.Test)

Example 3 with PromiseListener

use of io.joynr.provider.PromiseListener in project joynr by bmwcarit.

the class PublicationManagerImpl method triggerPublication.

private void triggerPublication(final PublicationInformation publicationInformation, ProviderContainer providerContainer, Method method) {
    try {
        Promise<?> attributeGetterPromise = attributePollInterpreter.execute(providerContainer, method);
        attributeGetterPromise.then(new PromiseListener() {

            @Override
            public void onRejection(JoynrException error) {
                if (error instanceof JoynrRuntimeException) {
                    sendPublicationError((JoynrRuntimeException) error, publicationInformation);
                } else {
                    sendPublicationError(new ProviderRuntimeException("Unexpected exception while calling getter for attribute " + publicationInformation.getSubscribedToName()), publicationInformation);
                }
            }

            @Override
            public void onFulfillment(Object... values) {
                // attribute getters only return a single value
                sendPublication(prepareAttributePublication(values[0], publicationInformation.getSubscriptionId()), publicationInformation);
            }
        });
    } catch (JoynrRuntimeException error) {
        sendPublicationError(error, publicationInformation);
    }
}
Also used : PromiseListener(io.joynr.provider.PromiseListener) JoynrException(io.joynr.exceptions.JoynrException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 4 with PromiseListener

use of io.joynr.provider.PromiseListener in project joynr by bmwcarit.

the class RequestInterpreter method execute.

public void execute(final ProviderCallback<Reply> callback, RequestCaller requestCaller, final Request request) {
    Promise<? extends AbstractDeferred> promise;
    logger.debug("execute request on provider: {}", request);
    try {
        promise = (Promise<?>) invokeMethod(requestCaller, request);
    } catch (MethodInvocationException | ProviderRuntimeException e) {
        logger.warn("execute request on provider failed with exception: {}, request: {}", e, request);
        callback.onFailure(e);
        return;
    } catch (Exception e) {
        JoynrVersion joynrVersion = AnnotationUtil.getAnnotation(requestCaller.getProxy().getClass(), JoynrVersion.class);
        MethodInvocationException methodInvocationException = new MethodInvocationException(e, new Version(joynrVersion.major(), joynrVersion.minor()));
        logger.warn("execute request on provider failed with exception: {}, request: {}", methodInvocationException, request);
        callback.onFailure(methodInvocationException);
        return;
    }
    promise.then(new PromiseListener() {

        @Override
        public void onRejection(JoynrException error) {
            logger.debug("execute request on provider onRejection: {}, request: {}", error, request);
            callback.onFailure(error);
        }

        @Override
        public void onFulfillment(Object... values) {
            logger.debug("execute request on provider onFulfillment: {}, request: {}", values, request);
            callback.onSuccess(createReply(request, values));
        }
    });
}
Also used : JoynrVersion(io.joynr.JoynrVersion) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) PromiseListener(io.joynr.provider.PromiseListener) JoynrException(io.joynr.exceptions.JoynrException) MethodInvocationException(joynr.exceptions.MethodInvocationException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) MethodInvocationException(joynr.exceptions.MethodInvocationException) JoynrException(io.joynr.exceptions.JoynrException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 5 with PromiseListener

use of io.joynr.provider.PromiseListener in project joynr by bmwcarit.

the class ProviderWrapperTest method testInvokeMethodThrowingProviderRuntimeException.

@Test
public void testInvokeMethodThrowingProviderRuntimeException() throws Throwable {
    ProviderWrapper subject = createSubject();
    JoynrProvider proxy = createProxy(subject);
    Method method = TestServiceProviderInterface.class.getMethod("testThrowsProviderRuntimeException");
    Object result = subject.invoke(proxy, method, new Object[0]);
    assertNotNull(result);
    assertTrue(result instanceof Promise);
    assertTrue(((Promise<?>) result).isRejected());
    ((Promise<?>) result).then(new PromiseListener() {

        @Override
        public void onFulfillment(Object... values) {
            fail("Should never get here");
        }

        @Override
        public void onRejection(JoynrException error) {
            assertTrue(error instanceof ProviderRuntimeException);
        }
    });
}
Also used : Promise(io.joynr.provider.Promise) PromiseListener(io.joynr.provider.PromiseListener) JoynrException(io.joynr.exceptions.JoynrException) Method(java.lang.reflect.Method) JoynrProvider(io.joynr.provider.JoynrProvider) ProviderWrapper(io.joynr.jeeintegration.ProviderWrapper) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) Test(org.junit.Test)

Aggregations

JoynrException (io.joynr.exceptions.JoynrException)8 PromiseListener (io.joynr.provider.PromiseListener)8 Test (org.junit.Test)5 Promise (io.joynr.provider.Promise)4 ProviderWrapper (io.joynr.jeeintegration.ProviderWrapper)3 JoynrProvider (io.joynr.provider.JoynrProvider)3 Method (java.lang.reflect.Method)3 ProviderRuntimeException (joynr.exceptions.ProviderRuntimeException)3 Version (joynr.types.Version)3 DeferredVoid (io.joynr.provider.DeferredVoid)2 DiscoveryEntry (joynr.types.DiscoveryEntry)2 GlobalDiscoveryEntry (joynr.types.GlobalDiscoveryEntry)2 ProviderQos (joynr.types.ProviderQos)2 Matchers.anyString (org.mockito.Matchers.anyString)2 JoynrVersion (io.joynr.JoynrVersion)1 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)1 Callback (io.joynr.proxy.Callback)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ApplicationException (joynr.exceptions.ApplicationException)1 MethodInvocationException (joynr.exceptions.MethodInvocationException)1