Search in sources :

Example 6 with ConfigChangeItem

use of com.alibaba.nacos.api.config.ConfigChangeItem in project nacos by alibaba.

the class ConfigLongPollReturnChanges_CITCase method testAdd.

@Test
public void testAdd() throws InterruptedException, NacosException {
    CountDownLatch latch = new CountDownLatch(1);
    final String dataId = "test" + System.currentTimeMillis();
    final String group = "DEFAULT_GROUP";
    final String content = "config data";
    configService.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            try {
                ConfigChangeItem cci = event.getChangeItem("content");
                Assert.assertNull(cci.getOldValue());
                Assert.assertEquals(content, cci.getNewValue());
                Assert.assertEquals(PropertyChangeType.ADDED, cci.getType());
                System.out.println(cci);
            } finally {
                latch.countDown();
            }
        }
    });
    boolean result = configService.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    configService.getConfig(dataId, group, 50);
    latch.await(10_000L, TimeUnit.MILLISECONDS);
}
Also used : ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with ConfigChangeItem

use of com.alibaba.nacos.api.config.ConfigChangeItem in project nacos by alibaba.

the class ConfigLongPollReturnChanges_CITCase method testModify.

@Test
public void testModify() throws InterruptedException, NacosException {
    CountDownLatch latch = new CountDownLatch(1);
    final String dataId = "test" + System.currentTimeMillis();
    final String group = "DEFAULT_GROUP";
    final String oldData = "old data";
    final String newData = "new data";
    boolean result = configService.publishConfig(dataId, group, oldData);
    Assert.assertTrue(result);
    configService.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            try {
                ConfigChangeItem cci = event.getChangeItem("content");
                Assert.assertEquals(oldData, cci.getOldValue());
                Assert.assertEquals(newData, cci.getNewValue());
                Assert.assertEquals(PropertyChangeType.MODIFIED, cci.getType());
                System.out.println(cci);
            } finally {
                latch.countDown();
            }
        }
    });
    configService.publishConfig(dataId, group, newData);
    latch.await(10_000L, TimeUnit.MILLISECONDS);
}
Also used : ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with ConfigChangeItem

use of com.alibaba.nacos.api.config.ConfigChangeItem in project nacos by alibaba.

the class TextChangeParser method doParse.

@Override
public Map<String, ConfigChangeItem> doParse(String oldContent, String newContent, String type) throws IOException {
    Map<String, ConfigChangeItem> map = new HashMap<>(4);
    final String key = "content";
    ConfigChangeItem cci = new ConfigChangeItem(key, oldContent, newContent);
    if (null == oldContent && null != newContent) {
        cci.setType(PropertyChangeType.ADDED);
    } else if (null != oldContent && null != newContent && !oldContent.equals(newContent)) {
        cci.setType(PropertyChangeType.MODIFIED);
    } else if (null != oldContent && null == newContent) {
        cci.setType(PropertyChangeType.DELETED);
    }
    map.put(key, cci);
    return map;
}
Also used : HashMap(java.util.HashMap) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem)

Example 9 with ConfigChangeItem

use of com.alibaba.nacos.api.config.ConfigChangeItem in project nacos by alibaba.

the class TextChangeParser method doParse.

@Override
public Map<String, ConfigChangeItem> doParse(String oldContent, String newContent, String type) throws IOException {
    Map<String, ConfigChangeItem> map = new HashMap<>(4);
    final String key = "content";
    ConfigChangeItem cci = new ConfigChangeItem(key, oldContent, newContent);
    if (null == oldContent && null != newContent) {
        cci.setType(PropertyChangeType.ADDED);
    } else if (null != oldContent && null != newContent && !oldContent.equals(newContent)) {
        cci.setType(PropertyChangeType.MODIFIED);
    } else if (null != oldContent && null == newContent) {
        cci.setType(PropertyChangeType.DELETED);
    }
    map.put(key, cci);
    return map;
}
Also used : HashMap(java.util.HashMap) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem)

Aggregations

ConfigChangeItem (com.alibaba.nacos.api.config.ConfigChangeItem)9 ConfigChangeEvent (com.alibaba.nacos.api.config.ConfigChangeEvent)6 AbstractConfigChangeListener (com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 HashMap (java.util.HashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 Map (java.util.Map)1