ConsulConfigurationWatcherRegister extends ConfigWatcherRegister { @Override public Optional<ConfigTable> readConfig(Set<String> keys) { removeUninterestedKeys(keys); registerKeyListeners(keys); final ConfigTable table = new ConfigTable(); configItemKeyedByName.forEach((key, value) -> { if (value.isPresent()) { table.add(new ConfigTable.ConfigItem(key, value.get())); } else { table.add(new ConfigTable.ConfigItem(key, null)); } }); return Optional.of(table); } ConsulConfigurationWatcherRegister(ConsulConfigurationCenterSettings settings); @Override Optional<ConfigTable> readConfig(Set<String> keys);  }
@Test public void shouldUpdateCachesWhenNotified() { cacheByKey = new ConcurrentHashMap<>(); configItemKeyedByName = new ConcurrentHashMap<>(); Whitebox.setInternalState(register, "cachesByKey", cacheByKey); Whitebox.setInternalState(register, "configItemKeyedByName", configItemKeyedByName); KVCache cache1 = mock(KVCache.class); KVCache cache2 = mock(KVCache.class); ArgumentCaptor<ConsulCache.Listener> listener1 = ArgumentCaptor.forClass(ConsulCache.Listener.class); ArgumentCaptor<ConsulCache.Listener> listener2 = ArgumentCaptor.forClass(ConsulCache.Listener.class); PowerMockito.mockStatic(KVCache.class); PowerMockito.when(KVCache.newCache(any(), eq("key1"))).thenReturn(cache1); PowerMockito.when(KVCache.newCache(any(), eq("key2"))).thenReturn(cache2); when(register.readConfig(any(Set.class))).thenCallRealMethod(); register.readConfig(Sets.newHashSet("key1", "key2")); verify(cache1).addListener(listener1.capture()); verify(cache2).addListener(listener2.capture()); listener1.getValue() .notify(ImmutableMap.of("key1", ImmutableValue.builder() .createIndex(0) .modifyIndex(0) .lockIndex(0) .key("key1") .flags(0) .value(BaseEncoding.base64().encode("val1".getBytes())) .build())); listener2.getValue() .notify(ImmutableMap.of("key2", ImmutableValue.builder() .createIndex(0) .modifyIndex(0) .lockIndex(0) .key("key2") .flags(0) .value(BaseEncoding.base64().encode("val2".getBytes())) .build())); assertEquals(2, configItemKeyedByName.size()); assertEquals("val1", configItemKeyedByName.get("key1").get()); assertEquals("val2", configItemKeyedByName.get("key2").get()); }
@Test public void shouldUnsubscribeWhenKeyRemoved() { cacheByKey = new ConcurrentHashMap<>(); KVCache existedCache = mock(KVCache.class); cacheByKey.put("existedKey", existedCache); configItemKeyedByName = new ConcurrentHashMap<>(); Whitebox.setInternalState(register, "cachesByKey", cacheByKey); Whitebox.setInternalState(register, "configItemKeyedByName", configItemKeyedByName); KVCache cache1 = mock(KVCache.class); KVCache cache2 = mock(KVCache.class); ArgumentCaptor<ConsulCache.Listener> listener1 = ArgumentCaptor.forClass(ConsulCache.Listener.class); ArgumentCaptor<ConsulCache.Listener> listener2 = ArgumentCaptor.forClass(ConsulCache.Listener.class); PowerMockito.mockStatic(KVCache.class); PowerMockito.when(KVCache.newCache(any(), eq("key1"))).thenReturn(cache1); PowerMockito.when(KVCache.newCache(any(), eq("key2"))).thenReturn(cache2); when(register.readConfig(any(Set.class))).thenCallRealMethod(); register.readConfig(Sets.newHashSet("key1", "key2")); verify(cache1).addListener(listener1.capture()); verify(cache2).addListener(listener2.capture()); verify(existedCache).stop(); }