Search in sources :

Example 1 with MetaDataSubscriber

use of org.apache.shenyu.sync.data.api.MetaDataSubscriber in project incubator-shenyu by apache.

the class ZookeeperSyncDataServiceTest method testWatchMetaDataWhenDataChange.

@Test
public void testWatchMetaDataWhenDataChange() throws Exception {
    final MetaData changedMetaData = MetaData.builder().id(MOCK_META_DATA_ID).enabled(Boolean.TRUE).build();
    final List<MetaData> subscribeList = new ArrayList<>(2);
    MetaDataSubscriber metaDataSubscriber = new MetaDataSubscriber() {

        @Override
        public void onSubscribe(final MetaData metaData) {
            subscribeList.add(metaData);
        }

        @Override
        public void unSubscribe(final MetaData metaData) {
        }
    };
    syncDataService = new ZookeeperSyncDataService(zkClient, null, Lists.newArrayList(metaDataSubscriber), Collections.emptyList());
    assertEquals(1, subscribeList.size());
    ArgumentCaptor<IZkDataListener> captor = ArgumentCaptor.forClass(IZkDataListener.class);
    verify(zkClient).subscribeDataChanges(eq(MOCK_META_DATA_PATH), captor.capture());
    captor.getValue().handleDataChange(MOCK_META_DATA_PATH, GsonUtils.getInstance().toJson(changedMetaData));
    assertThat(subscribeList.size(), is(2));
    assertTrue(subscribeList.get(1).getEnabled());
}
Also used : MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) MetaData(org.apache.shenyu.common.dto.MetaData) ArrayList(java.util.ArrayList) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) Test(org.junit.jupiter.api.Test)

Example 2 with MetaDataSubscriber

use of org.apache.shenyu.sync.data.api.MetaDataSubscriber in project incubator-shenyu by apache.

the class ZookeeperSyncDataServiceTest method testWatchMetaDataWhenInit.

@Test
public void testWatchMetaDataWhenInit() {
    final List<MetaData> subscribeList = new ArrayList<>(1);
    MetaDataSubscriber metaDataSubscriber = new MetaDataSubscriber() {

        @Override
        public void onSubscribe(final MetaData metaData) {
            subscribeList.add(metaData);
        }

        @Override
        public void unSubscribe(final MetaData metaData) {
        }
    };
    syncDataService = new ZookeeperSyncDataService(zkClient, null, Lists.newArrayList(metaDataSubscriber), Collections.emptyList());
    assertThat(subscribeList.size(), is(1));
}
Also used : MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) MetaData(org.apache.shenyu.common.dto.MetaData) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with MetaDataSubscriber

use of org.apache.shenyu.sync.data.api.MetaDataSubscriber in project incubator-shenyu by apache.

the class NacosCacheHandlerTest method testUpdateMetaDataMap.

@Test
public void testUpdateMetaDataMap() throws NacosException {
    String metadataPath1 = "METADATA_PATH_1";
    String metadataPath2 = "METADATA_PATH_2";
    MetaData metaData1 = MetaData.builder().path(metadataPath1).id("meta_1").build();
    MetaData metaData2 = MetaData.builder().path(metadataPath2).id("meta_2").build();
    changeMetaData(ImmutableList.of(metaData1, metaData2));
    String metaDataParam = GsonUtils.getInstance().toJson(ImmutableMap.of(metadataPath1, metaData1, metadataPath2, metaData2));
    final CountDownLatch latch = new CountDownLatch(2);
    final List<MetaData> subscribeList = new ArrayList<>();
    final List<MetaData> unsubscribeList = new ArrayList<>();
    MetaDataSubscriber metaDataSubscriber = new MetaDataSubscriber() {

        @Override
        public void onSubscribe(final MetaData metaData) {
            subscribeList.add(metaData);
            latch.countDown();
        }

        @Override
        public void unSubscribe(final MetaData metaData) {
            unsubscribeList.add(metaData);
            latch.countDown();
        }
    };
    nacosCacheHandlerService = new NacosCacheHandler(configService, null, Lists.newArrayList(metaDataSubscriber), Collections.emptyList());
    nacosCacheHandlerService.updateMetaDataMap(metaDataParam);
    assertEquals(2, subscribeList.size());
    assertEquals(2, unsubscribeList.size());
    assertEquals(configService.getConfig(META_DATA_ID, GROUP, 1), GsonUtils.getInstance().toJson(ImmutableMap.of(metadataPath1, metaData1, metadataPath2, metaData2)));
}
Also used : MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) MetaData(org.apache.shenyu.common.dto.MetaData) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with MetaDataSubscriber

use of org.apache.shenyu.sync.data.api.MetaDataSubscriber in project incubator-shenyu by apache.

the class ConsulCacheHandlerTest method testUpdateMetaDataMap.

@Test
public void testUpdateMetaDataMap() {
    String metadataPath1 = "METADATA_PATH_1";
    String metadataPath2 = "METADATA_PATH_2";
    MetaData metaData1 = MetaData.builder().path(metadataPath1).id("meta_1").build();
    MetaData metaData2 = MetaData.builder().path(metadataPath2).id("meta_2").build();
    String metaDataParam = GsonUtils.getInstance().toJson(ImmutableMap.of(metadataPath1, metaData1, metadataPath2, metaData2));
    final List<MetaData> subscribeList = new ArrayList<>();
    final List<MetaData> unsubscribeList = new ArrayList<>();
    MetaDataSubscriber metaDataSubscriber = new MetaDataSubscriber() {

        @Override
        public void onSubscribe(final MetaData metaData) {
            subscribeList.add(metaData);
        }

        @Override
        public void unSubscribe(final MetaData metaData) {
            unsubscribeList.add(metaData);
        }
    };
    ConsulCacheHandler consulCacheHandler = new ConsulCacheHandler(null, Lists.newArrayList(metaDataSubscriber), Collections.emptyList());
    consulCacheHandler.updateMetaDataMap(metaDataParam);
    assertEquals(2, subscribeList.size());
    assertEquals(2, unsubscribeList.size());
}
Also used : MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) MetaData(org.apache.shenyu.common.dto.MetaData) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 5 with MetaDataSubscriber

use of org.apache.shenyu.sync.data.api.MetaDataSubscriber in project incubator-shenyu by apache.

the class GlobalPluginConfigurationTest method testMetaDataCacheSubscriber.

@Test
public void testMetaDataCacheSubscriber() {
    applicationContextRunner.run(context -> {
        MetaDataSubscriber subscriber = context.getBean("metaDataCacheSubscriber", MetaDataSubscriber.class);
        assertNotNull(subscriber);
    });
}
Also used : MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) Test(org.junit.jupiter.api.Test)

Aggregations

MetaDataSubscriber (org.apache.shenyu.sync.data.api.MetaDataSubscriber)13 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)6 MetaData (org.apache.shenyu.common.dto.MetaData)6 IZkDataListener (org.I0Itec.zkclient.IZkDataListener)2 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 ResponseTemplateTransformer (com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AuthDataSubscriber (org.apache.shenyu.sync.data.api.AuthDataSubscriber)1 PluginDataSubscriber (org.apache.shenyu.sync.data.api.PluginDataSubscriber)1 HttpConfig (org.apache.shenyu.sync.data.http.config.HttpConfig)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1