use of joynr.types.GlobalDiscoveryEntry 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);
}
use of joynr.types.GlobalDiscoveryEntry 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)));
}
use of joynr.types.GlobalDiscoveryEntry in project joynr by bmwcarit.
the class LocalCapabilitiesDirectoryTest method addCapability.
@SuppressWarnings("unchecked")
@Test(timeout = 1000)
public void addCapability() throws InterruptedException {
when(globalAddressProvider.get()).thenReturn(channelAddress);
localCapabilitiesDirectory.add(discoveryEntry);
ArgumentCaptor<GlobalDiscoveryEntry> argumentCaptor = ArgumentCaptor.forClass(GlobalDiscoveryEntry.class);
verify(globalCapabilitiesClient, timeout(200)).add(any(Callback.class), argumentCaptor.capture());
GlobalDiscoveryEntry capturedGlobalDiscoveryEntry = argumentCaptor.getValue();
assertNotNull(capturedGlobalDiscoveryEntry);
assertEquals(discoveryEntry.getDomain(), capturedGlobalDiscoveryEntry.getDomain());
assertEquals(discoveryEntry.getInterfaceName(), capturedGlobalDiscoveryEntry.getInterfaceName());
}
use of joynr.types.GlobalDiscoveryEntry 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);
}
});
}
use of joynr.types.GlobalDiscoveryEntry in project joynr by bmwcarit.
the class LocalCapabilitiesDirectoryTest method lookupWithScopeGlobalOnly.
@SuppressWarnings("unchecked")
@Test(timeout = 1000)
public void lookupWithScopeGlobalOnly() throws InterruptedException {
List<GlobalDiscoveryEntry> caps = new ArrayList<GlobalDiscoveryEntry>();
String domain1 = "domain1";
String interfaceName1 = "interfaceName1";
DiscoveryQos discoveryQos = new DiscoveryQos(30000, ArbitrationStrategy.HighestPriority, 1000, DiscoveryScope.GLOBAL_ONLY);
CapabilitiesCallback capabilitiesCallback = mock(CapabilitiesCallback.class);
when(globalDiscoveryEntryCacheMock.lookup(eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getCacheMaxAgeMs()))).thenReturn(new ArrayList<DiscoveryEntry>());
doAnswer(createAnswer(caps)).when(globalCapabilitiesClient).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
localCapabilitiesDirectory.lookup(new String[] { domain1 }, interfaceName1, discoveryQos, capabilitiesCallback);
verify(globalCapabilitiesClient, times(1)).lookup(any(Callback.class), any(String[].class), any(String.class), eq(discoveryQos.getDiscoveryTimeoutMs()));
verify(capabilitiesCallback, times(1)).processCapabilitiesReceived(argThat(hasNEntries(0)));
verify(capabilitiesCallback, times(0)).processCapabilitiesReceived(argThat(hasNEntries(1)));
reset(capabilitiesCallback);
// add local entry
ProviderQos providerQos = new ProviderQos();
providerQos.setScope(ProviderScope.LOCAL);
DiscoveryEntry discoveryEntry = new DiscoveryEntry(new Version(47, 11), domain1, interfaceName1, "localParticipant", providerQos, System.currentTimeMillis(), expiryDateMs, publicKeyId);
localCapabilitiesDirectory.add(discoveryEntry);
localCapabilitiesDirectory.lookup(new String[] { domain1 }, interfaceName1, discoveryQos, capabilitiesCallback);
verify(globalCapabilitiesClient, times(2)).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
verify(capabilitiesCallback, times(1)).processCapabilitiesReceived(argThat(hasNEntries(0)));
verify(capabilitiesCallback, times(0)).processCapabilitiesReceived(argThat(hasNEntries(1)));
reset(capabilitiesCallback);
// even deleting local cap entries shall have no effect, the global cap dir shall be invoked
localCapabilitiesDirectory.remove(discoveryEntry);
localCapabilitiesDirectory.lookup(new String[] { domain1 }, interfaceName1, discoveryQos, capabilitiesCallback);
verify(globalCapabilitiesClient, times(3)).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
verify(capabilitiesCallback, times(1)).processCapabilitiesReceived(argThat(hasNEntries(0)));
verify(capabilitiesCallback, times(0)).processCapabilitiesReceived(argThat(hasNEntries(1)));
reset(capabilitiesCallback);
// add global entry
GlobalDiscoveryEntry capInfo = new GlobalDiscoveryEntry(new Version(47, 11), domain1, interfaceName1, "globalParticipant", new ProviderQos(), System.currentTimeMillis(), expiryDateMs, publicKeyId, channelAddressSerialized);
caps.add(capInfo);
doAnswer(createAnswer(caps)).when(globalCapabilitiesClient).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
localCapabilitiesDirectory.lookup(new String[] { domain1 }, interfaceName1, discoveryQos, capabilitiesCallback);
verify(globalCapabilitiesClient, times(4)).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
verify(capabilitiesCallback, times(0)).processCapabilitiesReceived(argThat(hasNEntries(0)));
verify(capabilitiesCallback, times(1)).processCapabilitiesReceived(argThat(hasNEntries(1)));
reset(capabilitiesCallback);
// now, another lookup call shall take the cached for the global cap call, and no longer call the global cap dir
// (as long as the cache is not expired)
reset(globalDiscoveryEntryCacheMock);
when(globalDiscoveryEntryCacheMock.lookup(eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getCacheMaxAgeMs()))).thenReturn(Lists.newArrayList((DiscoveryEntry) capInfo));
localCapabilitiesDirectory.lookup(new String[] { domain1 }, interfaceName1, discoveryQos, capabilitiesCallback);
verify(globalCapabilitiesClient, times(4)).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
verify(capabilitiesCallback, times(0)).processCapabilitiesReceived(argThat(hasNEntries(0)));
verify(capabilitiesCallback, times(1)).processCapabilitiesReceived(argThat(hasNEntries(1)));
reset(capabilitiesCallback);
// and now, invalidate the existing cached global values, resulting in another call to glocalcapclient
discoveryQos.setCacheMaxAgeMs(0);
Thread.sleep(1);
// now, another lookup call shall call the globalCapabilitiesClient, as the global cap dir is expired
localCapabilitiesDirectory.lookup(new String[] { domain1 }, interfaceName1, discoveryQos, capabilitiesCallback);
verify(globalCapabilitiesClient, times(5)).lookup(any(Callback.class), eq(new String[] { domain1 }), eq(interfaceName1), eq(discoveryQos.getDiscoveryTimeoutMs()));
verify(capabilitiesCallback, times(0)).processCapabilitiesReceived(argThat(hasNEntries(0)));
verify(capabilitiesCallback, times(1)).processCapabilitiesReceived(argThat(hasNEntries(1)));
reset(capabilitiesCallback);
reset(globalCapabilitiesClient);
}
Aggregations