use of org.apache.shenyu.sync.data.api.PluginDataSubscriber in project incubator-shenyu by apache.
the class ZookeeperSyncDataServiceTest method testWatchSelectorWhenDataChange.
@Test
public void testWatchSelectorWhenDataChange() throws Exception {
final SelectorData changedSelectorData = SelectorData.builder().name(MOCK_SELECTOR_NAME).enabled(Boolean.TRUE).build();
final List<SelectorData> subscribeList = new ArrayList<>(2);
syncDataService = new ZookeeperSyncDataService(zkClient, new PluginDataSubscriber() {
@Override
public void onSelectorSubscribe(final SelectorData selectorData) {
subscribeList.add(selectorData);
}
}, Collections.emptyList(), Collections.emptyList());
ArgumentCaptor<IZkDataListener> captor = ArgumentCaptor.forClass(IZkDataListener.class);
verify(zkClient).subscribeDataChanges(eq(MOCK_SELECTOR_PATH), captor.capture());
captor.getValue().handleDataChange(MOCK_SELECTOR_PATH, GsonUtils.getInstance().toJson(changedSelectorData));
assertThat(subscribeList.size(), is(2));
assertTrue(subscribeList.get(1).getEnabled());
}
use of org.apache.shenyu.sync.data.api.PluginDataSubscriber in project incubator-shenyu by apache.
the class ZookeeperSyncDataServiceTest method testWatchPluginWhenDataChange.
@Test
public void testWatchPluginWhenDataChange() throws Exception {
final PluginData changedPluginData = PluginData.builder().name(MOCK_PLUGIN_NAME).enabled(Boolean.TRUE).build();
final List<PluginData> subscribeList = new ArrayList<>(2);
syncDataService = new ZookeeperSyncDataService(zkClient, new PluginDataSubscriber() {
@Override
public void onSubscribe(final PluginData pluginData) {
subscribeList.add(pluginData);
}
}, Collections.emptyList(), Collections.emptyList());
ArgumentCaptor<IZkDataListener> captor = ArgumentCaptor.forClass(IZkDataListener.class);
verify(zkClient).subscribeDataChanges(eq(MOCK_PLUGIN_PATH), captor.capture());
captor.getValue().handleDataChange(MOCK_PLUGIN_PATH, GsonUtils.getInstance().toJson(changedPluginData));
assertThat(subscribeList.size(), is(2));
assertTrue(subscribeList.get(1).getEnabled());
}
use of org.apache.shenyu.sync.data.api.PluginDataSubscriber in project incubator-shenyu by apache.
the class ConsulCacheHandlerTest method testUpdateSelectorMap.
@Test
public void testUpdateSelectorMap() {
String selectorDataPluginName1 = "SELECTOR_DATA_1";
String selectorDataPluginName2 = "SELECTOR_DATA_2";
SelectorData selectorData1 = SelectorData.builder().pluginName(selectorDataPluginName1).id("select_1").name("SELECT_DATA_NAME_1").build();
SelectorData selectorData2 = SelectorData.builder().pluginName(selectorDataPluginName2).id("select_2").name("SELECT_DATA_NAME_2").build();
String selectorDataParam = GsonUtils.getInstance().toJson(ImmutableMap.of(selectorDataPluginName2, ImmutableList.of(selectorData2), selectorDataPluginName1, ImmutableList.of(selectorData1)));
final List<SelectorData> subscribeList = new ArrayList<>();
final List<SelectorData> unsubscribeList = new ArrayList<>();
ConsulCacheHandler consulCacheHandler = new ConsulCacheHandler(new PluginDataSubscriber() {
@Override
public void onSelectorSubscribe(final SelectorData selectorData) {
subscribeList.add(selectorData);
}
@Override
public void unSelectorSubscribe(final SelectorData selectorData) {
unsubscribeList.add(selectorData);
}
}, Collections.emptyList(), Collections.emptyList());
consulCacheHandler.updateSelectorMap(selectorDataParam);
assertEquals(2, subscribeList.size());
assertEquals(2, unsubscribeList.size());
}
use of org.apache.shenyu.sync.data.api.PluginDataSubscriber in project incubator-shenyu by apache.
the class EtcdSyncDataService method subscribePluginDataChanges.
private void subscribePluginDataChanges(final String pluginPath, final String pluginName) {
etcdClient.watchDataChange(pluginPath, (updatePath, updateValue) -> {
final String dataPath = buildRealPath(pluginPath, updatePath);
final String dataStr = etcdClient.get(dataPath);
final PluginData data = GsonUtils.getInstance().fromJson(dataStr, PluginData.class);
Optional.ofNullable(data).ifPresent(d -> Optional.ofNullable(pluginDataSubscriber).ifPresent(e -> e.onSubscribe(d)));
}, deleteNode -> deletePlugin(pluginName));
}
use of org.apache.shenyu.sync.data.api.PluginDataSubscriber in project incubator-shenyu by apache.
the class EtcdSyncDataService method cacheRuleData.
private void cacheRuleData(final String dataString) {
final RuleData ruleData = GsonUtils.getInstance().fromJson(dataString, RuleData.class);
Optional.ofNullable(ruleData).ifPresent(data -> Optional.ofNullable(pluginDataSubscriber).ifPresent(e -> e.onRuleSubscribe(data)));
}
Aggregations