Search in sources :

Example 1 with PluginDataSubscriber

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());
}
Also used : PluginDataSubscriber(org.apache.shenyu.sync.data.api.PluginDataSubscriber) ArrayList(java.util.ArrayList) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 2 with PluginDataSubscriber

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());
}
Also used : PluginDataSubscriber(org.apache.shenyu.sync.data.api.PluginDataSubscriber) PluginData(org.apache.shenyu.common.dto.PluginData) ArrayList(java.util.ArrayList) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) Test(org.junit.jupiter.api.Test)

Example 3 with PluginDataSubscriber

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());
}
Also used : PluginDataSubscriber(org.apache.shenyu.sync.data.api.PluginDataSubscriber) ArrayList(java.util.ArrayList) SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 4 with PluginDataSubscriber

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));
}
Also used : URLDecoder(java.net.URLDecoder) LoggerFactory(org.slf4j.LoggerFactory) CollectionUtils(org.apache.commons.collections4.CollectionUtils) GsonUtils(org.apache.shenyu.common.utils.GsonUtils) MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) Lists(com.google.common.collect.Lists) PluginData(org.apache.shenyu.common.dto.PluginData) PluginDataSubscriber(org.apache.shenyu.sync.data.api.PluginDataSubscriber) Splitter(com.google.common.base.Splitter) SelectorData(org.apache.shenyu.common.dto.SelectorData) DefaultPathConstants(org.apache.shenyu.common.constant.DefaultPathConstants) AppAuthData(org.apache.shenyu.common.dto.AppAuthData) Logger(org.slf4j.Logger) RuleData(org.apache.shenyu.common.dto.RuleData) SyncDataService(org.apache.shenyu.sync.data.api.SyncDataService) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) AuthDataSubscriber(org.apache.shenyu.sync.data.api.AuthDataSubscriber) List(java.util.List) Optional(java.util.Optional) ConfigGroupEnum(org.apache.shenyu.common.enums.ConfigGroupEnum) MetaData(org.apache.shenyu.common.dto.MetaData) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) PluginData(org.apache.shenyu.common.dto.PluginData)

Example 5 with PluginDataSubscriber

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)));
}
Also used : URLDecoder(java.net.URLDecoder) LoggerFactory(org.slf4j.LoggerFactory) CollectionUtils(org.apache.commons.collections4.CollectionUtils) GsonUtils(org.apache.shenyu.common.utils.GsonUtils) MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) Lists(com.google.common.collect.Lists) PluginData(org.apache.shenyu.common.dto.PluginData) PluginDataSubscriber(org.apache.shenyu.sync.data.api.PluginDataSubscriber) Splitter(com.google.common.base.Splitter) SelectorData(org.apache.shenyu.common.dto.SelectorData) DefaultPathConstants(org.apache.shenyu.common.constant.DefaultPathConstants) AppAuthData(org.apache.shenyu.common.dto.AppAuthData) Logger(org.slf4j.Logger) RuleData(org.apache.shenyu.common.dto.RuleData) SyncDataService(org.apache.shenyu.sync.data.api.SyncDataService) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) AuthDataSubscriber(org.apache.shenyu.sync.data.api.AuthDataSubscriber) List(java.util.List) Optional(java.util.Optional) ConfigGroupEnum(org.apache.shenyu.common.enums.ConfigGroupEnum) MetaData(org.apache.shenyu.common.dto.MetaData) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) RuleData(org.apache.shenyu.common.dto.RuleData)

Aggregations

PluginDataSubscriber (org.apache.shenyu.sync.data.api.PluginDataSubscriber)15 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)9 PluginData (org.apache.shenyu.common.dto.PluginData)7 RuleData (org.apache.shenyu.common.dto.RuleData)7 SelectorData (org.apache.shenyu.common.dto.SelectorData)7 AuthDataSubscriber (org.apache.shenyu.sync.data.api.AuthDataSubscriber)5 MetaDataSubscriber (org.apache.shenyu.sync.data.api.MetaDataSubscriber)5 Splitter (com.google.common.base.Splitter)4 Lists (com.google.common.collect.Lists)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URLDecoder (java.net.URLDecoder)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Collections (java.util.Collections)4 List (java.util.List)4 Objects (java.util.Objects)4 Optional (java.util.Optional)4 ExecutionException (java.util.concurrent.ExecutionException)4 CollectionUtils (org.apache.commons.collections4.CollectionUtils)4 DefaultPathConstants (org.apache.shenyu.common.constant.DefaultPathConstants)4