From 3c1d6d00c328c6907257aabffc88f50e84626924 Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Thu, 28 Nov 2019 11:56:04 +0800 Subject: [PATCH] =?UTF-8?q?#1550=20=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9B=E9=83=A8=E5=88=86=E7=B1=BB=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=88=B0=20api=20=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/api/config}/ConfigChangeEvent.java | 4 +- .../nacos/api/config}/ConfigChangeItem.java | 10 +--- .../nacos/api/config/PropertyChangeType.java | 30 ++++++++++ .../config/listener/ConfigChangeParser.java | 4 +- .../impl/AbstractConfigChangeParser.java | 10 ++-- .../nacos/client/config/impl/CacheData.java | 1 + .../config/impl/PropertiesChangeParser.java | 3 +- .../client/config/impl/YmlChangeParser.java | 3 +- .../impl/AbstractConfigChangeListener.java | 2 +- .../impl/ConfigChangeHandlerTest.java | 39 +++++++++++++ .../impl/PropertiesChangeParserTest.java | 58 +++++++++++++++++++ .../listener/impl/YmlChangeParserTest.java | 57 ++++++++++++++++++ 12 files changed, 203 insertions(+), 18 deletions(-) rename {client/src/main/java/com/alibaba/nacos/client/config/impl => api/src/main/java/com/alibaba/nacos/api/config}/ConfigChangeEvent.java (92%) rename {client/src/main/java/com/alibaba/nacos/client/config/impl => api/src/main/java/com/alibaba/nacos/api/config}/ConfigChangeItem.java (90%) create mode 100644 api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ConfigChangeHandlerTest.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/config/listener/impl/PropertiesChangeParserTest.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/config/listener/impl/YmlChangeParserTest.java diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeEvent.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigChangeEvent.java similarity index 92% rename from client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeEvent.java rename to api/src/main/java/com/alibaba/nacos/api/config/ConfigChangeEvent.java index 172056daf..92eccb86c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeEvent.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigChangeEvent.java @@ -13,7 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.client.config.impl; +package com.alibaba.nacos.api.config; + +import com.alibaba.nacos.api.config.ConfigChangeItem; import java.util.Collection; import java.util.Map; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeItem.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigChangeItem.java similarity index 90% rename from client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeItem.java rename to api/src/main/java/com/alibaba/nacos/api/config/ConfigChangeItem.java index 27e6593b1..b29d14ff4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigChangeItem.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigChangeItem.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.client.config.impl; +package com.alibaba.nacos.api.config; /** * ConfigChangeItem @@ -26,14 +26,6 @@ public class ConfigChangeItem { private String newValue; private PropertyChangeType type; - public enum PropertyChangeType { - /** add */ - ADDED, - /** modified */ - MODIFIED, - /** deleted */ - DELETED - } public ConfigChangeItem(String key, String oldValue, String newValue) { this.key = key; diff --git a/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java b/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java new file mode 100644 index 000000000..93c47dfd4 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java @@ -0,0 +1,30 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 = the "License""); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.api.config; + +/** + * Property Change Type + * + * @author rushsky518 + */ +public enum PropertyChangeType { + /** add */ + ADDED, + /** modified */ + MODIFIED, + /** deleted */ + DELETED +} diff --git a/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java b/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java index 9a6f053c0..c7a15f04f 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/listener/ConfigChangeParser.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.api.config.listener; +import com.alibaba.nacos.api.config.ConfigChangeItem; + import java.io.IOException; import java.util.Map; @@ -39,5 +41,5 @@ public interface ConfigChangeParser { * @return * @throws IOException */ - Map doParse(String oldContent, String newContent, String type) throws IOException; + Map doParse(String oldContent, String newContent, String type) throws IOException; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java index 6c8ee6f51..bea9e6d52 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/AbstractConfigChangeParser.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.client.config.impl; +import com.alibaba.nacos.api.config.ConfigChangeItem; +import com.alibaba.nacos.api.config.PropertyChangeType; import com.alibaba.nacos.api.config.listener.ConfigChangeParser; import java.util.HashMap; @@ -38,7 +40,7 @@ public abstract class AbstractConfigChangeParser implements ConfigChangeParser { return this.configType.equalsIgnoreCase(type); } - protected Map filterChangeData(Map oldMap, Map newMap) { + protected Map filterChangeData(Map oldMap, Map newMap) { Map result = new HashMap(16); for (Iterator> entryItr = oldMap.entrySet().iterator(); entryItr.hasNext();) { Map.Entry e = entryItr.next(); @@ -48,10 +50,10 @@ public abstract class AbstractConfigChangeParser implements ConfigChangeParser { continue; } cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), newMap.get(e.getKey()).toString()); - cci.setType(ConfigChangeItem.PropertyChangeType.MODIFIED); + cci.setType(PropertyChangeType.MODIFIED); } else { cci = new ConfigChangeItem(e.getKey(), e.getValue().toString(), null); - cci.setType(ConfigChangeItem.PropertyChangeType.DELETED); + cci.setType(PropertyChangeType.DELETED); } result.put(e.getKey(), cci); @@ -61,7 +63,7 @@ public abstract class AbstractConfigChangeParser implements ConfigChangeParser { Map.Entry e = entryItr.next(); if (!oldMap.containsKey(e.getKey())) { ConfigChangeItem cci = new ConfigChangeItem(e.getKey(), null, e.getValue().toString()); - cci.setType(ConfigChangeItem.PropertyChangeType.ADDED); + cci.setType(PropertyChangeType.ADDED); result.put(e.getKey(), cci); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index 5ebabe084..471ccb2ef 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.config.impl; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.config.ConfigChangeEvent; import com.alibaba.nacos.api.config.listener.AbstractSharedListener; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java index 4d5354869..2aad22346 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.client.config.impl; +import com.alibaba.nacos.api.config.ConfigChangeItem; import com.alibaba.nacos.client.utils.StringUtils; import java.io.IOException; @@ -33,7 +34,7 @@ public class PropertiesChangeParser extends AbstractConfigChangeParser { } @Override - public Map doParse(String oldContent, String newContent, String type) throws IOException { + public Map doParse(String oldContent, String newContent, String type) throws IOException { Properties oldProps = new Properties(); Properties newProps = new Properties(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java index 19eaa2052..f00e4c4fa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java @@ -15,6 +15,7 @@ */ package com.alibaba.nacos.client.config.impl; +import com.alibaba.nacos.api.config.ConfigChangeItem; import com.alibaba.nacos.client.utils.StringUtils; import org.yaml.snakeyaml.Yaml; import java.util.*; @@ -30,7 +31,7 @@ public class YmlChangeParser extends AbstractConfigChangeParser { } @Override - public Map doParse(String oldContent, String newContent, String type) { + public Map doParse(String oldContent, String newContent, String type) { Map oldMap = Collections.emptyMap(); Map newMap = Collections.emptyMap(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/listener/impl/AbstractConfigChangeListener.java b/client/src/main/java/com/alibaba/nacos/client/config/listener/impl/AbstractConfigChangeListener.java index d456d4da0..b4e94364c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/listener/impl/AbstractConfigChangeListener.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/listener/impl/AbstractConfigChangeListener.java @@ -16,7 +16,7 @@ package com.alibaba.nacos.client.config.listener.impl; import com.alibaba.nacos.api.config.listener.AbstractListener; -import com.alibaba.nacos.client.config.impl.ConfigChangeEvent; +import com.alibaba.nacos.api.config.ConfigChangeEvent; /** * AbstractConfigChangeListener diff --git a/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ConfigChangeHandlerTest.java b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ConfigChangeHandlerTest.java new file mode 100644 index 000000000..1e66699b7 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ConfigChangeHandlerTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.config.listener.impl; + + +import com.alibaba.nacos.api.config.ConfigChangeItem; +import com.alibaba.nacos.client.config.impl.ConfigChangeHandler; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.Map; + +public class ConfigChangeHandlerTest { + @Test + public void testParseProperties() throws IOException { + Map properties = ConfigChangeHandler.getInstance().parseChangeData("", "app.name = nacos", "properties"); + Assert.assertEquals("nacos", ((ConfigChangeItem)properties.get("app.name")).getNewValue()); + } + + @Test + public void testParseYaml() throws IOException { + Map properties = ConfigChangeHandler.getInstance().parseChangeData("", "app:\n name: nacos", "yaml"); + Assert.assertEquals("nacos", ((ConfigChangeItem)properties.get("app.name")).getNewValue()); + } +} diff --git a/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/PropertiesChangeParserTest.java b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/PropertiesChangeParserTest.java new file mode 100644 index 000000000..a4d063907 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/PropertiesChangeParserTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.config.listener.impl; + + +import com.alibaba.nacos.api.config.ConfigChangeItem; +import com.alibaba.nacos.client.config.impl.PropertiesChangeParser; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.Map; + +public class PropertiesChangeParserTest { + private PropertiesChangeParser parser = new PropertiesChangeParser(); + private final String type = "properties"; + + @Test + public void testType() { + Assert.assertEquals(true, parser.isResponsibleFor(type)); + } + + @Test + public void testAddKey() throws IOException { + Map map = parser.doParse("", "app.name = nacos", type); + Assert.assertEquals(null, map.get("app.name").getOldValue()); + Assert.assertEquals("nacos", map.get("app.name").getNewValue()); + } + + @Test + public void testRemoveKey() throws IOException { + Map map = parser.doParse("app.name = nacos", "", type); + Assert.assertEquals("nacos", map.get("app.name").getOldValue()); + Assert.assertEquals(null, map.get("app.name").getNewValue()); + } + + @Test + public void testModifyKey() throws IOException { + Map map = parser.doParse("app.name = rocketMQ", "app.name = nacos", type); + Assert.assertEquals("rocketMQ", map.get("app.name").getOldValue()); + Assert.assertEquals("nacos", map.get("app.name").getNewValue()); + } + +} + diff --git a/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/YmlChangeParserTest.java b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/YmlChangeParserTest.java new file mode 100644 index 000000000..5174bf801 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/YmlChangeParserTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.config.listener.impl; + + +import com.alibaba.nacos.api.config.ConfigChangeItem; +import com.alibaba.nacos.client.config.impl.YmlChangeParser; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.Map; + +public class YmlChangeParserTest { + private YmlChangeParser parser = new YmlChangeParser(); + private final String type = "yaml"; + + @Test + public void testType() { + Assert.assertEquals(true, parser.isResponsibleFor(type)); + } + + @Test + public void testAddKey() throws IOException { + Map map = parser.doParse("", "app:\n name: nacos", type); + Assert.assertEquals(null, map.get("app.name").getOldValue()); + Assert.assertEquals("nacos", map.get("app.name").getNewValue()); + } + + @Test + public void testRemoveKey() throws IOException { + Map map = parser.doParse("app:\n name: nacos", "", type); + Assert.assertEquals("nacos", map.get("app.name").getOldValue()); + Assert.assertEquals(null, map.get("app.name").getNewValue()); + } + + @Test + public void testModifyKey() throws IOException { + Map map = parser.doParse("app:\n name: rocketMQ", "app:\n name: nacos", type); + Assert.assertEquals("rocketMQ", map.get("app.name").getOldValue()); + Assert.assertEquals("nacos", map.get("app.name").getNewValue()); + } +} +