Search in sources :

Example 1 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryTest method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    channelAddress = new ChannelAddress(TEST_URL, "testChannelId");
    ObjectMapper objectMapper = new ObjectMapper();
    channelAddressSerialized = objectMapper.writeValueAsString(channelAddress);
    Field objectMapperField = CapabilityUtils.class.getDeclaredField("objectMapper");
    objectMapperField.setAccessible(true);
    objectMapperField.set(CapabilityUtils.class, objectMapper);
    Answer<Void> answer = new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            ((Callback<Void>) args[0]).onSuccess(null);
            return null;
        }
    };
    doAnswer(answer).when(globalCapabilitiesClient).add(any(Callback.class), any(GlobalDiscoveryEntry.class));
    String discoveryDirectoriesDomain = "io.joynr";
    String capabilitiesDirectoryParticipantId = "capDir_participantId";
    String capabiltitiesDirectoryChannelId = "dirchannelId";
    String domainAccessControllerParticipantId = "domainAccessControllerParticipantId";
    String domainAccessControllerChannelId = "domainAccessControllerChannelId";
    DiscoveryEntry globalCapabilitiesDirectoryDiscoveryEntry = CapabilityUtils.newGlobalDiscoveryEntry(new Version(0, 1), discoveryDirectoriesDomain, GlobalCapabilitiesDirectory.INTERFACE_NAME, capabilitiesDirectoryParticipantId, new ProviderQos(), System.currentTimeMillis(), expiryDateMs, domainAccessControllerChannelId, new ChannelAddress(TEST_URL, capabiltitiesDirectoryChannelId));
    DiscoveryEntry domainAccessControllerDiscoveryEntry = CapabilityUtils.newGlobalDiscoveryEntry(new Version(0, 1), discoveryDirectoriesDomain, GlobalDomainAccessController.INTERFACE_NAME, domainAccessControllerParticipantId, new ProviderQos(), System.currentTimeMillis(), expiryDateMs, domainAccessControllerChannelId, new ChannelAddress(TEST_URL, domainAccessControllerChannelId));
    when(capabilitiesProvisioning.getDiscoveryEntries()).thenReturn(Sets.newHashSet(globalCapabilitiesDirectoryDiscoveryEntry, domainAccessControllerDiscoveryEntry));
    // use default freshnessUpdateIntervalMs: 3600000ms (1h)
    localCapabilitiesDirectory = new LocalCapabilitiesDirectoryImpl(capabilitiesProvisioning, globalAddressProvider, localDiscoveryEntryStoreMock, globalDiscoveryEntryCacheMock, messageRouter, globalCapabilitiesClient, expiredDiscoveryEntryCacheCleaner, 3600000, capabilitiesFreshnessUpdateExecutor, defaultDiscoveryRetryIntervalMs, shutdownNotifier);
    verify(expiredDiscoveryEntryCacheCleaner).scheduleCleanUpForCaches(Mockito.<ExpiredDiscoveryEntryCacheCleaner.CleanupAction>any(), argThat(new DiscoveryEntryStoreVarargMatcher(globalDiscoveryEntryCacheMock, localDiscoveryEntryStoreMock)));
    verify(capabilitiesFreshnessUpdateExecutor).scheduleAtFixedRate(runnableCaptor.capture(), anyLong(), anyLong(), eq(TimeUnit.MILLISECONDS));
    ProviderQos providerQos = new ProviderQos();
    CustomParameter[] parameterList = { new CustomParameter("key1", "value1"), new CustomParameter("key2", "value2") };
    providerQos.setCustomParameters(parameterList);
    String participantId = "testParticipantId";
    String domain = "domain";
    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);
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) Matchers.anyString(org.mockito.Matchers.anyString) Field(java.lang.reflect.Field) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Callback(io.joynr.proxy.Callback) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DeferredVoid(io.joynr.provider.DeferredVoid) CustomParameter(joynr.types.CustomParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProviderQos(joynr.types.ProviderQos) Before(org.junit.Before)

Example 2 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryTest method testLookupMultipleDomainsLocalThenGlobal.

@SuppressWarnings("unchecked")
@Test
public void testLookupMultipleDomainsLocalThenGlobal() {
    String[] domains = new String[] { "domain1", "domain2", "domain3" };
    String interfaceName = "interface1";
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.LOCAL_THEN_GLOBAL);
    discoveryQos.setCacheMaxAgeMs(ONE_DAY_IN_MS);
    CapabilitiesCallback capabilitiesCallback = mock(CapabilitiesCallback.class);
    DiscoveryEntry localEntry = new DiscoveryEntry();
    localEntry.setDomain("domain1");
    when(localDiscoveryEntryStoreMock.lookup(eq(domains), eq(interfaceName))).thenReturn(Lists.newArrayList(localEntry));
    GlobalDiscoveryEntry globalEntry = new GlobalDiscoveryEntry();
    globalEntry.setDomain("domain2");
    Collection<DiscoveryEntry> entries = Lists.newArrayList((DiscoveryEntry) globalEntry);
    when(globalDiscoveryEntryCacheMock.lookup(eq(domains), eq(interfaceName), eq(discoveryQos.getCacheMaxAgeMs()))).thenReturn(entries);
    final GlobalDiscoveryEntry remoteGlobalEntry = new GlobalDiscoveryEntry(new Version(0, 0), "domain3", interfaceName, "participantIdRemote", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 10000L, "publicKeyId", channelAddressSerialized);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Callback<List<GlobalDiscoveryEntry>> callback = (Callback<List<GlobalDiscoveryEntry>>) invocation.getArguments()[0];
            callback.onSuccess(Lists.newArrayList(remoteGlobalEntry));
            return null;
        }
    }).when(globalCapabilitiesClient).lookup(any(Callback.class), Mockito.<String[]>any(), anyString(), anyLong());
    localCapabilitiesDirectory.lookup(domains, interfaceName, discoveryQos, capabilitiesCallback);
    verify(globalCapabilitiesClient).lookup(any(Callback.class), argThat(Matchers.arrayContainingInAnyOrder(new String[] { "domain3" })), eq(interfaceName), eq(discoveryQos.getDiscoveryTimeoutMs()));
    verify(capabilitiesCallback).processCapabilitiesReceived(capabilitiesCaptor.capture());
    Collection<DiscoveryEntry> captured = CapabilityUtils.convertToDiscoveryEntrySet(capabilitiesCaptor.getValue());
    assertNotNull(captured);
    assertEquals(3, captured.size());
    assertTrue(captured.contains(localEntry));
    assertTrue(captured.contains(new DiscoveryEntry(globalEntry)));
    assertTrue(captured.contains(new DiscoveryEntry(remoteGlobalEntry)));
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) Matchers.anyString(org.mockito.Matchers.anyString) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) Callback(io.joynr.proxy.Callback) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) ArrayList(java.util.ArrayList) DeferredVoid(io.joynr.provider.DeferredVoid) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 3 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalDiscoveryAggregator method lookup.

@Override
public Future<DiscoveryEntryWithMetaInfo[]> lookup(final Callback<DiscoveryEntryWithMetaInfo[]> callback, String[] domains, String interfaceName, DiscoveryQos discoveryQos) {
    final Set<DiscoveryEntryWithMetaInfo> discoveryEntries = new HashSet<>();
    Set<String> missingDomains = new HashSet<>();
    for (String domain : domains) {
        if (provisionedDiscoveryEntries.containsKey(domain + interfaceName)) {
            DiscoveryEntryWithMetaInfo discoveryEntry = provisionedDiscoveryEntries.get(domain + interfaceName);
            discoveryEntries.add(discoveryEntry);
        } else {
            missingDomains.add(domain);
        }
    }
    logger.trace("Found locally provisioned discovery entries: {}", discoveryEntries);
    final Future<DiscoveryEntryWithMetaInfo[]> discoveryEntryFuture = new Future<>();
    if (!missingDomains.isEmpty()) {
        logger.trace("Did not find entries for the following domains: {}", missingDomains);
        Callback<DiscoveryEntryWithMetaInfo[]> newCallback = new Callback<DiscoveryEntryWithMetaInfo[]>() {

            @Override
            public void onFailure(JoynrRuntimeException error) {
                callback.onFailure(error);
                discoveryEntryFuture.onFailure(error);
            }

            @Override
            public void onSuccess(DiscoveryEntryWithMetaInfo[] entries) {
                assert entries != null : "Entries must not be null.";
                logger.trace("Globally found entries for missing domains: {}", Arrays.toString(entries));
                Collections.addAll(discoveryEntries, entries);
                resolveDiscoveryEntriesFutureWithEntries(discoveryEntryFuture, discoveryEntries, callback);
            }
        };
        String[] missingDomainsArray = new String[missingDomains.size()];
        missingDomains.toArray(missingDomainsArray);
        getDiscoveryProxy(discoveryQos.getDiscoveryTimeout()).lookup(newCallback, missingDomainsArray, interfaceName, discoveryQos);
    } else {
        resolveDiscoveryEntriesFutureWithEntries(discoveryEntryFuture, discoveryEntries, callback);
    }
    return discoveryEntryFuture;
}
Also used : Callback(io.joynr.proxy.Callback) Future(io.joynr.proxy.Future) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) HashSet(java.util.HashSet)

Example 4 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class GlobalCapabilitiesDirectoryClientTest method testLookupDomainsOnFailure.

@Test
public void testLookupDomainsOnFailure() {
    @SuppressWarnings("unchecked") Callback<List<GlobalDiscoveryEntry>> callbackListOfGlobalDiscoveryEntriesMock = (Callback<List<GlobalDiscoveryEntry>>) mock(Callback.class);
    Callback<GlobalDiscoveryEntry[]> callback = lookupDomainsHelper(callbackListOfGlobalDiscoveryEntriesMock);
    JoynrRuntimeException error = new JoynrRuntimeException();
    callback.onFailure(error);
    verify(callbackListOfGlobalDiscoveryEntriesMock).onFailure(eq(error));
    verify(callbackListOfGlobalDiscoveryEntriesMock, times(0)).onSuccess(anyListOf(GlobalDiscoveryEntry.class));
}
Also used : Callback(io.joynr.proxy.Callback) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) ArrayList(java.util.ArrayList) List(java.util.List) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Test(org.junit.Test)

Example 5 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalDiscoveryAggregatorTest method findsProvisionedEntryForMultipleDomains.

@SuppressWarnings("unchecked")
@Test
public void findsProvisionedEntryForMultipleDomains() throws Exception {
    discoveryProviderEntries = new DiscoveryEntryWithMetaInfo[] { anotherDiscoveryProviderEntry };
    allDomains = new String[] { systemServicesDomain, anotherDomain };
    String[] missingDomains = new String[] { anotherDomain };
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.LOCAL_ONLY);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            Callback<DiscoveryEntryWithMetaInfo[]> callback = (Callback<DiscoveryEntryWithMetaInfo[]>) invocation.getArguments()[0];
            callback.onSuccess(discoveryProviderEntries);
            return null;
        }
    }).when(discoveryProxyMock).lookup(any(Callback.class), any(String[].class), anyString(), any(DiscoveryQos.class));
    Future<DiscoveryEntryWithMetaInfo[]> discoveryEntriesFuture = localDiscoveryAggregator.lookup(lookupCallback, allDomains, Discovery.INTERFACE_NAME, discoveryQos);
    assertNotNull(discoveryEntriesFuture);
    DiscoveryEntry[] result = discoveryEntriesFuture.get();
    logger.info("Got discovery entries: " + Arrays.toString(result));
    assertNotNull(result);
    assertEquals(2, result.length);
    assertTrue(containsByInterfaceDomain(result, discoveryProviderEntry.getInterfaceName(), discoveryProviderEntry.getDomain()));
    assertTrue(containsByInterfaceDomain(result, anotherDiscoveryProviderEntry.getInterfaceName(), anotherDiscoveryProviderEntry.getDomain()));
    verify(discoveryProxyMock).lookup(any(Callback.class), eq(missingDomains), eq(Discovery.INTERFACE_NAME), eq(discoveryQos));
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) Callback(io.joynr.proxy.Callback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Mockito.anyString(org.mockito.Mockito.anyString) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) DiscoveryQos(joynr.types.DiscoveryQos) Test(org.junit.Test)

Aggregations

Callback (io.joynr.proxy.Callback)15 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)9 DiscoveryEntry (joynr.types.DiscoveryEntry)9 Test (org.junit.Test)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 GlobalDiscoveryEntry (joynr.types.GlobalDiscoveryEntry)8 DeferredVoid (io.joynr.provider.DeferredVoid)6 ArrayList (java.util.ArrayList)6 DiscoveryEntryWithMetaInfo (joynr.types.DiscoveryEntryWithMetaInfo)6 ProviderQos (joynr.types.ProviderQos)6 Matchers.anyString (org.mockito.Matchers.anyString)6 DiscoveryQos (io.joynr.arbitration.DiscoveryQos)5 Future (io.joynr.proxy.Future)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Version (joynr.types.Version)4 MessagingQos (io.joynr.messaging.MessagingQos)2 ProxyCreatedCallback (io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback)2 Field (java.lang.reflect.Field)2 Semaphore (java.util.concurrent.Semaphore)2