diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 01ac9e830..50a6f296a 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -15,11 +15,19 @@ */ package com.alibaba.nacos.test.config; +import com.alibaba.fastjson.JSON; import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.listener.AbstractListener; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; +import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor; +import com.alibaba.nacos.client.config.impl.ServerHttpAgent; +import com.alibaba.nacos.client.logger.json.JSONObject; +import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.config.server.Config; import org.junit.After; import org.junit.Assert; @@ -31,6 +39,11 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.util.Arrays; +import java.util.List; import java.util.Properties; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicInteger; @@ -44,6 +57,9 @@ import java.util.concurrent.atomic.AtomicInteger; public class ConfigAPI_ITCase { public static final long TIME_OUT = 3000; public ConfigService iconfig = null; + ServerHttpAgent agent = null; + + static final String CONFIG_CONTROLLER_PATH = "/v1/cs/configs"; String SPECIAL_CHARACTERS = "!@#$%^&*()_+-=_|/'?."; String dataId = "yanlin"; String group = "yanlin"; @@ -59,6 +75,9 @@ public class ConfigAPI_ITCase { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); iconfig = NacosFactory.createConfigService(properties); + + agent = new ServerHttpAgent(properties); + agent.start(); } @After @@ -67,8 +86,8 @@ public class ConfigAPI_ITCase { /** * @TCDescription : nacos_正常获取数据 - * @TestStep : TODO Test steps - * @ExpectResult : TODO expect results + * @TestStep : + * @ExpectResult : */ @Test(timeout = 3*TIME_OUT) public void nacos_getconfig_1() throws Exception { @@ -312,15 +331,13 @@ public class ConfigAPI_ITCase { Listener ml = new Listener() { @Override public void receiveConfigInfo(String configInfo) { - // TODO Auto-generated method stub - System.out.println("recieve2:" + configInfo); + System.out.println("recieve23:" + configInfo); count.incrementAndGet(); Assert.assertEquals(content, configInfo); } @Override public Executor getExecutor() { - // TODO Auto-generated method stub return null; } }; @@ -328,14 +345,14 @@ public class ConfigAPI_ITCase { while (count.get() == 0) { Thread.sleep(2000); } - Assert.assertEquals(1, count.get()); + Assert.assertTrue(count.get() >= 1); iconfig.removeListener(dataId, group, ml); } /** * @TCDescription : nacos_设置监听器为null,抛出异常信息 - * @TestStep : TODO Test steps - * @ExpectResult : TODO expect results + * @TestStep : + * @ExpectResult : * @author xiaochun.xxc * @since 3.6.8 */ @@ -364,19 +381,12 @@ public class ConfigAPI_ITCase { boolean result = iconfig.publishConfig(dataId, group, content); Assert.assertTrue(result); - Listener ml = new Listener() { + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - // TODO Auto-generated method stub count.incrementAndGet(); Assert.assertEquals(content, configInfo); } - - @Override - public Executor getExecutor() { - // TODO Auto-generated method stub - return null; - } }; iconfig.addListener(dataId, group, ml); while (count.get() == 0) { @@ -400,18 +410,11 @@ public class ConfigAPI_ITCase { iconfig.removeConfig(dataId, group); Thread.sleep(TIME_OUT); - Listener ml = new Listener() { + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - // TODO Auto-generated method stub count.incrementAndGet(); } - - @Override - public Executor getExecutor() { - // TODO Auto-generated method stub - return null; - } }; iconfig.addListener(dataId, group, ml); Thread.sleep(TIME_OUT); @@ -435,33 +438,19 @@ public class ConfigAPI_ITCase { */ @Test(timeout = 5*TIME_OUT) public void nacos_removeListener_1() throws Exception { - iconfig.addListener(dataId, group, new Listener() { + iconfig.addListener(dataId, group, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - // TODO Auto-generated method stub Assert.assertTrue(false); } - - @Override - public Executor getExecutor() { - // TODO Auto-generated method stub - return null; - } }); Thread.sleep(TIME_OUT); try { - iconfig.removeListener(dataId, group, new Listener() { + iconfig.removeListener(dataId, group, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - // TODO Auto-generated method stub System.out.println("remove recieve:" + configInfo); } - - @Override - public Executor getExecutor() { - // TODO Auto-generated method stub - return null; - } }); } catch (Exception e) { @@ -479,16 +468,10 @@ public class ConfigAPI_ITCase { public void nacos_removeListener_2() { group += "test.nacos"; try { - iconfig.removeListener(dataId, group, new Listener() { + iconfig.removeListener(dataId, group, new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { - // TODO Auto-generated method stub - } - @Override - public Executor getExecutor() { - // TODO Auto-generated method stub - return null; } }); } catch (Exception e) { @@ -508,23 +491,13 @@ public class ConfigAPI_ITCase { final String contentRemove = "test-abc-two"; final AtomicInteger count = new AtomicInteger(0); - Listener ml = new Listener() { - @Override - public Executor getExecutor() { - return null; - } - + Listener ml = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { count.incrementAndGet(); } }; - Listener ml1 = new Listener() { - @Override - public Executor getExecutor() { - return null; - } - + Listener ml1 = new AbstractListener() { @Override public void receiveConfigInfo(String configInfo) { //System.out.println("ml1 remove listener recieve:" + configInfo); @@ -561,4 +534,237 @@ public class ConfigAPI_ITCase { Assert.assertTrue(true); } + /** + * @TCDescription : nacos_openAPI_配置具体信息 + * @TestStep : + * @ExpectResult : + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_detailConfig_1() { + HttpResult result = null; + + try { + final String content = "test"; + boolean ret = iconfig.publishConfig(dataId, group, content); + Thread.sleep(TIME_OUT); + Assert.assertTrue(ret); + + List params = Arrays.asList("dataId", dataId, "group", group, "show", "all"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + + Assert.assertEquals(content, JSON.parseObject(result.content).getString("content")); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_catalog信息 + * @TestStep : + * @ExpectResult : + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_catalog() { + HttpResult result = null; + + try { + final String content = "test"; + boolean ret = iconfig.publishConfig(dataId, group, content); + Thread.sleep(TIME_OUT); + Assert.assertTrue(ret); + + List params = Arrays.asList("dataId", dataId, "group", group); + result = agent.httpGet(CONFIG_CONTROLLER_PATH+"/catalog", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + + System.out.println(result.content); + Assert.assertNotNull(JSON.parseObject(result.content).getString("data")); + + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_queryBeta信息 + * @TestStep : + * @ExpectResult : + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_queryBeta_1() { + HttpResult result = null; + + try { + final String content = "test-beta"; + List headers = Arrays.asList("betaIps", "127.0.0.1"); + List params1 = Arrays.asList("dataId", dataId, "group", group, "content", content); + result = agent.httpPost(CONFIG_CONTROLLER_PATH + "/", headers, params1, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertEquals("true", result.content); + + + List params = Arrays.asList("dataId", dataId, "group", group, "beta", "true"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertEquals(content, JSON.parseObject(result.content).getJSONObject("data").getString("content")); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_queryBeta删除信息 + * @TestStep : 1. 发布配置 + * 2. 删除Beta配置信息 + * @ExpectResult : + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_queryBeta_delete() { + HttpResult result = null; + + try { + final String content = "test-beta"; + List headers = Arrays.asList("betaIps", "127.0.0.1"); + List params1 = Arrays.asList("dataId", dataId, "group", group, "content", content); + result = agent.httpPost(CONFIG_CONTROLLER_PATH + "/", headers, params1, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertEquals("true", result.content); + + + List params = Arrays.asList("dataId", dataId, "group", group, "beta", "true"); + result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertEquals(true, JSON.parseObject(result.content).getBoolean("data")); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_模糊查询配置信息 + * @TestStep : 1. 发布配置 + * 2. 模糊查询 + * @ExpectResult : 获取查询到配置 + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_fuzzySearchConfig() { + HttpResult result = null; + + try { + final String content = "test123"; + boolean ret = iconfig.publishConfig(dataId, group, content); + Thread.sleep(TIME_OUT); + Assert.assertTrue(ret); + + List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "blur"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + + Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") >= 1); + Assert.assertTrue(JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content").startsWith(content)); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_模糊查询配置信息 + * @TestStep : 1. 发布配置 + * 2. 查询配置信息 + * @ExpectResult : 获取查询到配置 + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_fuzzySearchConfig_1() { + HttpResult result = null; + + try { + final String content = "test123"; + boolean ret = iconfig.publishConfig(dataId, group, content); + Thread.sleep(TIME_OUT); + Assert.assertTrue(ret); + + List params = Arrays.asList("dataId", dataId+"*", "group", group+"*", "pageNo","1", "pageSize","10", "search", "blur"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") >= 1); + Assert.assertEquals(content, JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content")); + + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_查询配置信息 + * @TestStep : 1. 发布配置 + * 2. 查询配置信息 + * @ExpectResult : 获取查询到配置 + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_searchConfig() { + HttpResult result = null; + + try { + final String content = "test123"; + boolean ret = iconfig.publishConfig(dataId, group, content); + Thread.sleep(TIME_OUT); + Assert.assertTrue(ret); + + List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "accurate"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); + + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") == 1); + Assert.assertEquals(content, JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content")); + + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + /** + * @TCDescription : nacos_openAPI_查询配置信息,包含中文,utf-8 + * @TestStep : 1. 发布配置 + * 2. 查询配置信息 + * @ExpectResult : 获取查询到配置 + * @author xiaochun.xxc + * @since 3.6.8 + */ + @Test(timeout = 3*TIME_OUT) + public void nacos_openAPI_searchConfig_2() { + HttpResult result = null; + + try { + final String content = "test测试"; + boolean ret = iconfig.publishConfig(dataId, group, content); + Thread.sleep(TIME_OUT); + Assert.assertTrue(ret); + + List params = Arrays.asList("dataId", dataId, "group", group, "pageNo","1", "pageSize","10", "search", "accurate"); + result = agent.httpGet(CONFIG_CONTROLLER_PATH + "/", null, params, "utf-8", TIME_OUT); + Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); + Assert.assertTrue(JSON.parseObject(result.content).getIntValue("totalCount") == 1); + Assert.assertEquals(content, JSON.parseObject(result.content).getJSONArray("pageItems").getJSONObject(0).getString("content")); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + }