Search in sources :

Example 1 with ConfigChangeItem

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

the class ConfigAuth_ITCase method readWithWritePermission.

@Test
public void readWithWritePermission() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    properties.put(PropertyKeyConst.NAMESPACE, namespace1);
    properties.put(PropertyKeyConst.USERNAME, username2);
    properties.put(PropertyKeyConst.PASSWORD, password2);
    iconfig = NacosFactory.createConfigService(properties);
    final String content = "test" + System.currentTimeMillis();
    iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            ConfigChangeItem cci = event.getChangeItem("content");
            System.out.println("content:" + cci);
            if (!content.equals(cci.getNewValue())) {
                return;
            }
            latch.countDown();
        }
    });
    TimeUnit.SECONDS.sleep(3L);
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    TimeUnit.SECONDS.sleep(5L);
    try {
        iconfig.getConfig(dataId, group, TIME_OUT);
        fail();
    } catch (NacosException ne) {
        Assert.assertEquals(HttpStatus.SC_FORBIDDEN, ne.getErrCode());
    }
    latch.await(5L, TimeUnit.SECONDS);
    Assert.assertTrue(latch.getCount() > 0);
}
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) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ConfigChangeItem

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

the class ConfigAuth_ITCase method readWithReadPermission.

@Test
public void readWithReadPermission() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger ai = new AtomicInteger(0);
    properties.put(PropertyKeyConst.USERNAME, username1);
    properties.put(PropertyKeyConst.PASSWORD, password1);
    iconfig = NacosFactory.createConfigService(properties);
    final String content = "test" + System.currentTimeMillis();
    System.out.println(content);
    iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            ConfigChangeItem cci = event.getChangeItem("content");
            System.out.println("content:" + cci);
            if (!content.equals(cci.getNewValue())) {
                return;
            }
            latch.countDown();
        }
    });
    TimeUnit.SECONDS.sleep(3L);
    properties.put(PropertyKeyConst.USERNAME, username2);
    properties.put(PropertyKeyConst.PASSWORD, password2);
    ConfigService configService = NacosFactory.createConfigService(properties);
    boolean result = configService.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    TimeUnit.SECONDS.sleep(5L);
    String res = iconfig.getConfig(dataId, group, TIME_OUT);
    Assert.assertEquals(content, res);
    latch.await(5L, TimeUnit.SECONDS);
    Assert.assertEquals(0, latch.getCount());
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 3 with ConfigChangeItem

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

the class AbstractConfigChangeParser method filterChangeData.

protected Map<String, ConfigChangeItem> filterChangeData(Map oldMap, Map newMap) {
    Map<String, ConfigChangeItem> result = new HashMap<String, ConfigChangeItem>(16);
    for (Map.Entry<String, Object> e : (Iterable<Map.Entry<String, Object>>) oldMap.entrySet()) {
        ConfigChangeItem cci;
        if (newMap.containsKey(e.getKey())) {
            if (e.getValue().equals(newMap.get(e.getKey()))) {
                continue;
            }
            cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), newMap.get(e.getKey()).toString());
            cci.setType(PropertyChangeType.MODIFIED);
        } else {
            cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), null);
            cci.setType(PropertyChangeType.DELETED);
        }
        result.put(e.getKey(), cci);
    }
    for (Map.Entry<String, Object> e : (Iterable<Map.Entry<String, Object>>) newMap.entrySet()) {
        if (!oldMap.containsKey(e.getKey())) {
            ConfigChangeItem cci = new ConfigChangeItem(e.getKey(), null, e.getValue().toString());
            cci.setType(PropertyChangeType.ADDED);
            result.put(e.getKey(), cci);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with ConfigChangeItem

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

the class ConfigAuth_ITCase method ReadWriteWithFullPermission.

@Test
public void ReadWriteWithFullPermission() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger ai = new AtomicInteger(0);
    properties.put(PropertyKeyConst.USERNAME, username3);
    properties.put(PropertyKeyConst.PASSWORD, password3);
    iconfig = NacosFactory.createConfigService(properties);
    final String content = "test" + System.currentTimeMillis();
    iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            ConfigChangeItem cci = event.getChangeItem("content");
            System.out.println("content:" + cci);
            if (!content.equals(cci.getNewValue())) {
                return;
            }
            latch.countDown();
        }
    });
    TimeUnit.SECONDS.sleep(3L);
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    TimeUnit.SECONDS.sleep(5L);
    String res = iconfig.getConfig(dataId, group, TIME_OUT);
    Assert.assertEquals(content, res);
    latch.await(5L, TimeUnit.SECONDS);
    Assert.assertEquals(0, latch.getCount());
    result = iconfig.removeConfig(dataId, group);
    Assert.assertTrue(result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 5 with ConfigChangeItem

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

the class ConfigLongPollReturnChanges_CITCase method testDelete.

@Test
public void testDelete() throws InterruptedException, NacosException {
    CountDownLatch latch = new CountDownLatch(1);
    final String dataId = "test" + System.currentTimeMillis();
    final String group = "DEFAULT_GROUP";
    final String oldData = "old 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.assertNull(cci.getNewValue());
                Assert.assertEquals(PropertyChangeType.DELETED, cci.getType());
                System.out.println(cci);
            } finally {
                latch.countDown();
            }
        }
    });
    configService.removeConfig(dataId, group);
    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)

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