增加open api用例

This commit is contained in:
xiaochun.xxc 2019-01-07 20:41:13 +08:00
parent 77881396a2
commit 0987ff32a8
3 changed files with 235 additions and 3 deletions

View File

@ -42,6 +42,8 @@ public class NamingBase {
public static final String TEST_PORT_4_DOM_2 = "7070"; public static final String TEST_PORT_4_DOM_2 = "7070";
public static final String TETS_TOKEN_4_DOM_2 = "xyz"; 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 final int TEST_PORT = 8080;
public static String randomDomainName() { public static String randomDomainName() {

View File

@ -583,6 +583,162 @@ public class RestAPI_ITCase {
Assert.assertTrue(json.getJSONObject("data").getJSONArray("ips").size() > 0); Assert.assertTrue(json.getJSONObject("data").getJSONArray("ips").size() > 0);
} }
/**
* @TCDescription : 根据serviceName创建服务
* @TestStep :
* @ExpectResult :
*/
@Test
public void createService() throws Exception {
String serviceName = NamingBase.randomDomainName();
ResponseEntity<String> 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<String> 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<String> 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<String> 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<String> 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 <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz) { private <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -596,6 +752,19 @@ public class RestAPI_ITCase {
builder.toUriString(), HttpMethod.GET, entity, clazz); builder.toUriString(), HttpMethod.GET, entity, clazz);
} }
private <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz, HttpMethod httpMethod) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<?> entity = new HttpEntity<T>(headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.base.toString() + path)
.queryParams(params);
return this.restTemplate.exchange(
builder.toUriString(), httpMethod, entity, clazz);
}
private void prepareData() { private void prepareData() {
ResponseEntity<String> responseEntity = request("/nacos/v1/ns/api/regDom", ResponseEntity<String> responseEntity = request("/nacos/v1/ns/api/regDom",

View File

@ -18,9 +18,15 @@ package com.alibaba.nacos.test.naming;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService; 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.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.naming.NamingApp; import com.alibaba.nacos.naming.NamingApp;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; 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 <a href="mailto:zpf.073@gmail.com">nkorange</a> * @author <a href="mailto:zpf.073@gmail.com">nkorange</a>
*/ */
@ -41,13 +52,16 @@ public class ServiceListTest {
private NamingService naming; private NamingService naming;
private volatile List<Instance> instances = Collections.emptyList();
private static int listenseCount = 0;
@LocalServerPort @LocalServerPort
private int port; private int port;
@Before @Before
public void init() throws Exception { public void init() throws Exception {
if (naming == null) { if (naming == null) {
//TimeUnit.SECONDS.sleep(10);
naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
} }
} }
@ -57,15 +71,62 @@ public class ServiceListTest {
naming.getServicesOfServer(1, 10); naming.getServicesOfServer(1, 10);
} }
/**
* @description 获取当前订阅的所有服务
* @throws NacosException
*/
@Test @Test
public void getSubscribeServices() throws NacosException { public void getSubscribeServices() throws NacosException, InterruptedException {
ListView<String> listView = naming.getServicesOfServer(1, 10); ListView<String> listView = naming.getServicesOfServer(1, 10);
if (listView != null && listView.getCount() > 0) { if (listView != null && listView.getCount() > 0) {
naming.getAllInstances(listView.getData().get(0)); naming.getAllInstances(listView.getData().get(0));
} }
List<ServiceInfo> serviceInfoList = naming.getSubscribeServices(); List<ServiceInfo> 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<ServiceInfo> 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());
} }
} }