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);
}
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());
}
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;
}
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);
}
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);
}
Aggregations