Skip to content

Commit

Permalink
[Enhance] add some unit tests and some integration tests for address …
Browse files Browse the repository at this point in the history
…module (alibaba#8643)

* [Enhance] add some unit tests and some integration tests for address module

- add spring-boot-starter-test dependency
- use constructor injection instead of field injection

* add teardown method

* default replace with nacos
  • Loading branch information
onewe authored Jul 7, 2022
1 parent fcc9b0a commit 3124f0a
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 308 deletions.
5 changes: 5 additions & 0 deletions address/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -48,14 +47,19 @@
@RequestMapping({AddressServerConstants.ADDRESS_SERVER_REQUEST_URL + "/nodes"})
public class AddressServerClusterController {

@Autowired
private ServiceManager serviceManager;
private final ServiceManager serviceManager;

@Autowired
private AddressServerManager addressServerManager;
private final AddressServerManager addressServerManager;

private final AddressServerGeneratorManager addressServerGeneratorManager;

public AddressServerClusterController(ServiceManager serviceManager, AddressServerManager addressServerManager,
AddressServerGeneratorManager addressServerGeneratorManager) {
this.serviceManager = serviceManager;
this.addressServerManager = addressServerManager;
this.addressServerGeneratorManager = addressServerGeneratorManager;
}

@Autowired
private AddressServerGeneratorManager addressServerGeneratorManager;

/**
* Create new cluster.
Expand Down Expand Up @@ -146,7 +150,7 @@ public ResponseEntity<String> deleteCluster(@RequestParam(required = false) Stri
List<Instance> instanceList = addressServerGeneratorManager
.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false,
instanceList.toArray(new Instance[instanceList.size()]));
instanceList.toArray(new Instance[0]));
} else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.alibaba.nacos.naming.core.Cluster;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -38,11 +37,15 @@
@RestController
public class ServerListController {

@Autowired
private ServiceManager serviceManager;
private final ServiceManager serviceManager;

@Autowired
private AddressServerGeneratorManager addressServerBuilderManager;
private final AddressServerGeneratorManager addressServerBuilderManager;

public ServerListController(ServiceManager serviceManager,
AddressServerGeneratorManager addressServerBuilderManager) {
this.serviceManager = serviceManager;
this.addressServerBuilderManager = addressServerBuilderManager;
}

/**
* Get cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,167 +16,192 @@

package com.alibaba.nacos.address;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ImportAutoConfiguration(exclude = {SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class})
public class AddressServerControllerTests {

private static final String PRODUCT_NACOS = "nacos";

private static final String PRODUCT_CONFIG = "config";

private static final String PRODUCT_NAMING = "naming";

private static final String DEFAULT_URL_CLUSTER = "serverlist";

private static final String GET_SERVERLIST_URL_FORMART = "http://127.0.0.1:8080/%s/%s";
@Autowired
private TestRestTemplate restTemplate;

//-----------------product=nacos,cluster=DEFAULT -------------------//
@BeforeClass
public static void before() {
System.setProperty("nacos.standalone", "true");
System.setProperty("embeddedStorage", "true");
}

@Test
public void postCluster() {
public void postCluster() throws InterruptedException {

String ips = "127.0.0.100,127.0.0.102,127.0.0.104";
HashMap<String, String> params = new HashMap<>();
params.put("ips", ips);
String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
}

@Test
public void getCluster() {
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(1);
params.add("ips", ips);

String getUrl = String.format(GET_SERVERLIST_URL_FORMART, PRODUCT_NACOS, DEFAULT_URL_CLUSTER);
String response = SimpleHttpTestUtils.doGet(getUrl, new HashMap<>(), "UTF-8");
System.err.println(response);
}
final ResponseEntity<String> postClusterResponseEntity = restTemplate.exchange(
RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class);

Assert.assertNotNull(postClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue());

@Test
public void deleteCluster() {
HashMap<String, String> deleteIp = new HashMap<>();
deleteIp.put("ips", "127.0.0.104");
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", deleteIp, "UTF-8");
System.err.println(response);
TimeUnit.MILLISECONDS.sleep(500L);

final ResponseEntity<String> getClusterResponseEntity = restTemplate.exchange(
RequestEntity.get("/nacos/serverlist").build(), String.class);

Assert.assertNotNull(getClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), getClusterResponseEntity.getStatusCodeValue());

}

@Test
public void deleteClusterWithSpecIp() {
HashMap<String, String> params = new HashMap<>();
params.put("ips", "127.0.0.103");
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
}
public void deleteCluster() throws InterruptedException {

LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(1);
params.add("ips", "127.0.0.104");

final ResponseEntity<String> postClusterResponseEntity = restTemplate.exchange(
RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class);

Assert.assertNotNull(postClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue());

@Test
public void putCluster() {
TimeUnit.MILLISECONDS.sleep(500L);

String ips = "127.0.0.114";
HashMap<String, String> params = new HashMap<>();
params.put("ips", ips);
String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
final ResponseEntity<String> deleteClusterResponseEntity = restTemplate.exchange(
RequestEntity.delete("/nacos/v1/as/nodes?ips={ips}", "127.0.0.104").build(), String.class);

Assert.assertNotNull(deleteClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), deleteClusterResponseEntity.getStatusCodeValue());
}

//-----------------product=config,cluster=cluster01 -------------------//

@Test
public void postClusterWithProduct() {
public void postClusterWithProduct() throws InterruptedException {

LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(2);

String ips = "127.0.0.101,127.0.0.102,127.0.0.103";
HashMap<String, String> params = new HashMap<>();
params.put("ips", ips);
params.put("product", PRODUCT_CONFIG);
String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
}

@Test
public void getClusterWithProduct() {
HashMap<String, String> params = new HashMap<>();
String getUrl = String.format(GET_SERVERLIST_URL_FORMART, PRODUCT_CONFIG, DEFAULT_URL_CLUSTER);
String response = SimpleHttpTestUtils.doGet(getUrl, params, "UTF-8");
System.err.println(response);
}
params.add("ips", ips);
params.add("product", PRODUCT_CONFIG);

final ResponseEntity<String> postClusterResponseEntity = restTemplate.exchange(
RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class);
Assert.assertNotNull(postClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue());

@Test
public void deleteClusterWithProduct() {
HashMap<String, String> params = new HashMap<>();
params.put("product", PRODUCT_CONFIG);
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
TimeUnit.MILLISECONDS.sleep(500L);

final ResponseEntity<String> getClusterResponseEntity = restTemplate.exchange(
RequestEntity.get("/{product}/serverlist", PRODUCT_CONFIG).build(), String.class);

Assert.assertNotNull(getClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), getClusterResponseEntity.getStatusCodeValue());

final String body = getClusterResponseEntity.getBody();
Assert.assertNotNull(body);
}

@Test
public void deleteClusterWithProductAndIp() {
HashMap<String, String> params = new HashMap<>();
params.put("product", PRODUCT_CONFIG);
params.put("ips", "127.0.0.196");
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
}
public void deleteClusterWithProduct() throws InterruptedException {

LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(1);
params.add("ips", "127.0.0.104");
params.add("product", PRODUCT_CONFIG);

final ResponseEntity<String> postClusterResponseEntity = restTemplate.exchange(
RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class);
Assert.assertNotNull(postClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue());

@Test
public void putClusterWithProduct() {

String ips = "127.0.0.196";
HashMap<String, String> params = new HashMap<>();
params.put("ips", ips);
params.put("product", PRODUCT_CONFIG);
String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
TimeUnit.MILLISECONDS.sleep(500L);

final ResponseEntity<String> deleteClusterResponseEntity = restTemplate.exchange(
RequestEntity.delete("/nacos/v1/as/nodes?product={product}&ips={ips}", PRODUCT_CONFIG, "127.0.0.104")
.build(), String.class);

Assert.assertNotNull(deleteClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), deleteClusterResponseEntity.getStatusCodeValue());
}

//-----------------product=naming,cluster=cluster01 -------------------//

@Test
public void postClusterWithProductAndCluster() {
public void postClusterWithProductAndCluster() throws InterruptedException {
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(1);

String ips = "127.0.0.100,127.0.0.200,127.0.0.31";
HashMap<String, String> params = new HashMap<>();
params.put("ips", ips);
params.put("product", PRODUCT_NAMING);
params.put("cluster", "cluster01");
String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
}

params.add("ips", ips);
params.add("product", PRODUCT_NAMING);
params.add("cluster", "cluster01");

final ResponseEntity<String> postClusterResponseEntity = restTemplate.exchange(
RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class);
Assert.assertNotNull(postClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue());

@Test
public void getClusterWithProductAndCluster() {
HashMap<String, String> params = new HashMap<>();
String getUrl = String.format(GET_SERVERLIST_URL_FORMART, PRODUCT_NAMING, "cluster01");
String response = SimpleHttpTestUtils.doGet(getUrl, params, "UTF-8");
System.err.println(response);
TimeUnit.MILLISECONDS.sleep(500L);

final ResponseEntity<String> getClusterResponseEntity = restTemplate.exchange(
RequestEntity.get("/{product}/{cluster}", PRODUCT_NAMING, "cluster01").build(), String.class);

Assert.assertNotNull(getClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), getClusterResponseEntity.getStatusCodeValue());

final String body = getClusterResponseEntity.getBody();
Assert.assertNotNull(body);
}

@Test
public void deleteClusterWithProductAndCluster() {
HashMap<String, String> params = new HashMap<>();
params.put("product", PRODUCT_NAMING);
params.put("cluster", "cluster01");
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
}
public void deleteClusterWithProductAndCluster() throws InterruptedException {

LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(1);
params.add("ips", "127.0.0.104");
params.add("product", PRODUCT_NAMING);
params.add("cluster", "cluster01");

final ResponseEntity<String> postClusterResponseEntity = restTemplate.exchange(
RequestEntity.post("/nacos/v1/as/nodes").body(params), String.class);
Assert.assertNotNull(postClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), postClusterResponseEntity.getStatusCodeValue());

@Test
public void deleteClusterWithProductAndClusterAndIp() {
HashMap<String, String> params = new HashMap<>();
params.put("product", PRODUCT_NAMING);
params.put("cluster", "cluster01");
params.put("ips", "127.0.0.200");
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
TimeUnit.MILLISECONDS.sleep(500L);
final ResponseEntity<String> deleteClusterResponseEntity = restTemplate.exchange(
RequestEntity.delete("/nacos/v1/as/nodes?product={product}&cluster={cluster}&ips={ips}", PRODUCT_NAMING,
"cluster01", "127.0.0.104").build(), String.class);

Assert.assertNotNull(deleteClusterResponseEntity);
Assert.assertEquals(HttpStatus.OK.value(), deleteClusterResponseEntity.getStatusCodeValue());
}

@Test
public void putClusterWithProductAndCluster() {

String ips = "127.0.0.171";
HashMap<String, String> params = new HashMap<>();
params.put("ips", ips);
params.put("product", PRODUCT_NAMING);
params.put("cluster", "cluster01");
String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response);
@AfterClass
public static void teardown() {
System.clearProperty("nacos.standalone");
System.clearProperty("embeddedStorage");
}

}
Loading

0 comments on commit 3124f0a

Please sign in to comment.