From 0987ff32a8ca17df6e7b33c56f02defc60a79072 Mon Sep 17 00:00:00 2001 From: "xiaochun.xxc" Date: Mon, 7 Jan 2019 20:41:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0open=20api=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/test/naming/NamingBase.java | 2 + .../nacos/test/naming/RestAPI_ITCase.java | 169 ++++++++++++++++++ .../nacos/test/naming/ServiceListTest.java | 67 ++++++- 3 files changed, 235 insertions(+), 3 deletions(-) diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java index 7c0fb47ed..fb211a837 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java @@ -42,6 +42,8 @@ public class NamingBase { public static final String TEST_PORT_4_DOM_2 = "7070"; public static final String TETS_TOKEN_4_DOM_2 = "xyz"; + static final String NAMING_CONTROLLER_PATH = "/nacos/v1/ns"; + public static final int TEST_PORT = 8080; public static String randomDomainName() { diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java index 71fbe809e..a0d9a38b8 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/RestAPI_ITCase.java @@ -583,6 +583,162 @@ public class RestAPI_ITCase { Assert.assertTrue(json.getJSONObject("data").getJSONArray("ips").size() > 0); } + /** + * @TCDescription : 根据serviceName创建服务 + * @TestStep : + * @ExpectResult : + */ + @Test + public void createService() throws Exception { + String serviceName = NamingBase.randomDomainName(); + ResponseEntity response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class, + HttpMethod.PUT); + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + Assert.assertEquals("ok", response.getBody()); + + namingServiceDelete(serviceName); + } + + /** + * @TCDescription : 根据serviceName获取服务信息 + * @TestStep : + * @ExpectResult : + */ + @Test + public void getService() throws Exception { + String serviceName = NamingBase.randomDomainName(); + ResponseEntity response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class, + HttpMethod.PUT); + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + Assert.assertEquals("ok", response.getBody()); + + //get service + response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class); + + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + + JSONObject json = JSON.parseObject(response.getBody()); + Assert.assertEquals(serviceName, json.getString("name")); + + namingServiceDelete(serviceName); + } + + /** + * @TCDescription : 获取服务list信息 + * @TestStep : + * @ExpectResult : + */ + @Test + public void listService() throws Exception { + String serviceName = NamingBase.randomDomainName(); + //get service + ResponseEntity response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service/list", + Params.newParams() + .appendParam("serviceName", serviceName) + .appendParam("pageNo", "1") + .appendParam("pageSize", "15") + .done(), + String.class); + + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + JSONObject json = JSON.parseObject(response.getBody()); + int count = json.getIntValue("count"); + Assert.assertTrue(count >= 0); + + response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class, + HttpMethod.PUT); + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + Assert.assertEquals("ok", response.getBody()); + + response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service/list", + Params.newParams() + .appendParam("serviceName", serviceName) + .appendParam("pageNo", "1") + .appendParam("pageSize", "15") + .done(), + String.class); + + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + json = JSON.parseObject(response.getBody()); + Assert.assertEquals(count+1, json.getIntValue("count")); + + namingServiceDelete(serviceName); + } + + /** + * @TCDescription : 更新serviceName获取服务信息 + * @TestStep : + * @ExpectResult : + */ + @Test + public void updateService() throws Exception { + String serviceName = NamingBase.randomDomainName(); + ResponseEntity response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class, + HttpMethod.PUT); + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + Assert.assertEquals("ok", response.getBody()); + + //update service + response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .appendParam("healthCheckMode", "server") + .appendParam("protectThreshold", "3") + .done(), + String.class, + HttpMethod.POST); + + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + Assert.assertEquals("ok", response.getBody()); + + //get service + response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class); + + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + JSONObject json = JSON.parseObject(response.getBody()); + System.out.println(json); + Assert.assertEquals(3.0f, json.getFloatValue("protectThreshold"), 0.0f); + + namingServiceDelete(serviceName); + } + + private void namingServiceDelete(String serviceName) { + //delete service + ResponseEntity response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service", + Params.newParams() + .appendParam("serviceName", serviceName) + .done(), + String.class, + HttpMethod.DELETE); + + Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); + Assert.assertEquals("ok", response.getBody()); + } + private ResponseEntity request(String path, MultiValueMap params, Class clazz) { HttpHeaders headers = new HttpHeaders(); @@ -596,6 +752,19 @@ public class RestAPI_ITCase { builder.toUriString(), HttpMethod.GET, entity, clazz); } + private ResponseEntity request(String path, MultiValueMap params, Class clazz, HttpMethod httpMethod) { + + HttpHeaders headers = new HttpHeaders(); + + HttpEntity entity = new HttpEntity(headers); + + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.base.toString() + path) + .queryParams(params); + + return this.restTemplate.exchange( + builder.toUriString(), httpMethod, entity, clazz); + } + private void prepareData() { ResponseEntity responseEntity = request("/nacos/v1/ns/api/regDom", diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java b/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java index 94dc13d61..25af75031 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/ServiceListTest.java @@ -18,9 +18,15 @@ package com.alibaba.nacos.test.naming; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.listener.Event; +import com.alibaba.nacos.api.naming.listener.EventListener; +import com.alibaba.nacos.api.naming.listener.NamingEvent; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.naming.NamingApp; + +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -28,9 +34,14 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; +import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; +import static com.alibaba.nacos.test.naming.NamingBase.TEST_PORT; +import static com.alibaba.nacos.test.naming.NamingBase.randomDomainName; +import static com.alibaba.nacos.test.naming.NamingBase.verifyInstanceList; + /** * @author nkorange */ @@ -41,13 +52,16 @@ public class ServiceListTest { private NamingService naming; + private volatile List instances = Collections.emptyList(); + + private static int listenseCount = 0; + @LocalServerPort private int port; @Before public void init() throws Exception { if (naming == null) { - //TimeUnit.SECONDS.sleep(10); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } } @@ -57,15 +71,62 @@ public class ServiceListTest { naming.getServicesOfServer(1, 10); } + /** + * @description 获取当前订阅的所有服务 + * @throws NacosException + */ @Test - public void getSubscribeServices() throws NacosException { + public void getSubscribeServices() throws NacosException, InterruptedException { ListView listView = naming.getServicesOfServer(1, 10); if (listView != null && listView.getCount() > 0) { naming.getAllInstances(listView.getData().get(0)); } List serviceInfoList = naming.getSubscribeServices(); + int count = serviceInfoList.size(); - System.out.println(serviceInfoList); + String serviceName = randomDomainName(); + naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1"); + naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c2"); + + Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName))); + serviceInfoList = naming.getSubscribeServices(); + + System.out.println("dfdfdfd = " + serviceInfoList); + Assert.assertEquals(count+1, serviceInfoList.size()); + } + + /** + * @description 删除注册,获取当前订阅的所有服务 + * @throws NacosException + */ + @Test + public void getSubscribeServices_deregisterInstance() throws NacosException, InterruptedException { + listenseCount = 0; + EventListener listener = new EventListener() { + @Override + public void onEvent(Event event) { + System.out.println(((NamingEvent)event).getServiceName()); + System.out.println(((NamingEvent)event).getInstances()); + listenseCount++; + } + }; + + List serviceInfoList = naming.getSubscribeServices(); + int count = serviceInfoList.size(); + + String serviceName = randomDomainName(); + naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1"); + naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c2"); + + Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName))); + serviceInfoList = naming.getSubscribeServices(); + + Assert.assertEquals(count+1, serviceInfoList.size()); + + naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c1"); + naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c2"); + + Assert.assertEquals(count+1, serviceInfoList.size()); } }