#1105 Add test cases

This commit is contained in:
nkorange 2020-01-06 16:02:32 +08:00
parent 41dfcf5596
commit 3e312e7537
15 changed files with 371 additions and 44 deletions

View File

@ -75,6 +75,11 @@
<artifactId>nacos-core</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-console</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -0,0 +1,52 @@
package com.alibaba.nacos.test.base;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URL;
/**
* Http client for test module
*
* @author nkorange
* @since 1.2.0
*/
public class HttpClient4Test {
protected URL base;
@Autowired
protected TestRestTemplate restTemplate;
protected <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz) {
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.GET, entity, clazz);
}
protected <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);
}
}

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.test.naming;
package com.alibaba.nacos.test.base;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

View File

@ -20,7 +20,7 @@ import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.config.server.Config;
import com.alibaba.nacos.test.naming.Params;
import com.alibaba.nacos.test.base.Params;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

View File

@ -0,0 +1,4 @@
package com.alibaba.nacos.test.core.auth;
public class Permission_ITCase {
}

View File

@ -0,0 +1,4 @@
package com.alibaba.nacos.test.core.auth;
public class Role_ITCase {
}

View File

@ -0,0 +1,179 @@
package com.alibaba.nacos.test.core.auth;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.model.User;
import com.alibaba.nacos.console.utils.PasswordEncoderUtil;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.test.base.HttpClient4Test;
import com.alibaba.nacos.test.base.Params;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;
/**
* @author nkorange
* @since 1.2.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class User_ITCase extends HttpClient4Test {
protected URL base;
@LocalServerPort
private int port;
@Before
public void init() throws Exception {
String url = String.format("http://localhost:%d/", port);
this.base = new URL(url);
}
@Test
public String login() {
ResponseEntity<String> response = request("/nacos/v1/ns/auth/users/login",
Params.newParams()
.appendParam("username", "username1")
.appendParam("password", "password1")
.done(),
String.class,
HttpMethod.POST);
Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
JSONObject json = JSON.parseObject(response.getBody());
Assert.assertTrue(json.containsKey("accessToken"));
return json.getString("accessToken");
}
@Test
public void createUpdateDeleteUser() {
String accessToken = login();
// Create a user:
ResponseEntity<String> response = request("/nacos/v1/ns/auth/users",
Params.newParams()
.appendParam("username", "username1")
.appendParam("password", "password1")
.appendParam("accessToken", accessToken)
.done(),
String.class,
HttpMethod.POST);
Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
// Query a user:
response = request("/nacos/v1/ns/auth/users",
Params.newParams()
.appendParam("pageNo", "1")
.appendParam("pageSize", String.valueOf(Integer.MAX_VALUE))
.appendParam("accessToken", accessToken)
.done(),
String.class);
Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
Page<User> userPage = JSON.parseObject(response.getBody(), new TypeReference<>());
Assert.assertNotNull(userPage);
Assert.assertNotNull(userPage.getPageItems());
Assert.assertTrue(userPage.getPageItems().size() > 0);
boolean found = false;
for (User user : userPage.getPageItems()) {
if ("username1".equals(user.getUsername()) &&
PasswordEncoderUtil.encode("password1").equals(user.getPassword())) {
found = true;
break;
}
}
Assert.assertTrue(found);
// Update a user:
response = request("/nacos/v1/ns/auth/users",
Params.newParams()
.appendParam("username", "username1")
.appendParam("newPassword", "password2")
.appendParam("accessToken", accessToken)
.done(),
String.class,
HttpMethod.PUT);
Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
// Query a user:
response = request("/nacos/v1/ns/auth/users",
Params.newParams()
.appendParam("pageNo", "1")
.appendParam("pageSize", String.valueOf(Integer.MAX_VALUE))
.appendParam("accessToken", accessToken)
.done(),
String.class);
userPage = JSON.parseObject(response.getBody(), new TypeReference<>());
Assert.assertNotNull(userPage);
Assert.assertNotNull(userPage.getPageItems());
Assert.assertTrue(userPage.getPageItems().size() > 0);
found = false;
for (User user : userPage.getPageItems()) {
if ("username1".equals(user.getUsername()) &&
PasswordEncoderUtil.encode("password2").equals(user.getPassword())) {
found = true;
break;
}
}
Assert.assertTrue(found);
// Delete a user:
response = request("/nacos/v1/ns/auth/users",
Params.newParams()
.appendParam("username", "username1")
.appendParam("accessToken", accessToken)
.done(),
String.class,
HttpMethod.DELETE);
Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
// Query a user:
response = request("/nacos/v1/ns/auth/users",
Params.newParams()
.appendParam("pageNo", "1")
.appendParam("pageSize", String.valueOf(Integer.MAX_VALUE))
.appendParam("accessToken", accessToken)
.done(),
String.class);
Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
userPage = JSON.parseObject(response.getBody(), new TypeReference<>());
Assert.assertNotNull(userPage);
Assert.assertNotNull(userPage.getPageItems());
Assert.assertTrue(userPage.getPageItems().size() > 0);
found = false;
for (User user : userPage.getPageItems()) {
if ("username1".equals(user.getUsername())) {
found = true;
break;
}
}
Assert.assertFalse(found);
}
}

View File

@ -0,0 +1,117 @@
/*
* 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.test.naming;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.naming.NamingApp;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;
import java.util.Arrays;
/**
* Auth related cases
*
* @author nkorange
* @since 1.2.0
*/
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Auth_ITCase extends NamingBase {
// private NamingService naming;
//
// @LocalServerPort
// private int port;
//
// @Before
// public void init() throws Exception {
// NamingBase.prepareServer(port);
//
// if (naming == null) {
// naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
// }
// while (true) {
// if (!"UP".equals(naming.getServerStatus())) {
// Thread.sleep(1000L);
// continue;
// }
// break;
// }
// String url = String.format("http://localhost:%d/", port);
// this.base = new URL(url);
// }
@Test
public void testAuth() {
}
public int numSimilarGroups(String[] A) {
char[][] css = new char[A.length][];
int i = 0;
for (String a : A) {
css[i++] = a.toCharArray();
}
boolean[] used = new boolean[A.length];
int res=0;
for (i = 0; i < A.length; i++) {
if (!used[i]) {
res++;
used[i] = true;
bfs(used, css, i);
}
}
return res;
}
public void bfs(boolean[] used, char[][] css, int start) {
int[] nexts = new int[]{start};
int size = 1;
while (size > 0) {
int[] tmp = new int[used.length];
int size2 = 0;
for (int j = 0; j < size; j++) {
for (int i = 0; i < used.length; i++) {
if (!used[i] && similar(css[nexts[j]], css[i])) {
tmp[size2++] = i;
used[i] = true;
}
}
}
nexts = tmp;
size = size2;
}
}
public boolean similar(char[] cs1, char[] cs2) {
int dif = 0;
for (int i = 0; i < cs1.length && dif <= 2; i++) {
if (cs1[i] != cs2[i]) {
dif++;
}
}
return dif <= 2;
}
}

View File

@ -30,6 +30,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.test.base.Params;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

View File

@ -21,6 +21,7 @@ import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.test.base.Params;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Before;

View File

@ -27,6 +27,7 @@ import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.test.base.Params;
import org.junit.Ignore;
import org.springframework.http.HttpMethod;
import com.alibaba.nacos.naming.NamingApp;

View File

@ -16,6 +16,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.NacosNamingService;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.test.base.Params;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.test.naming;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.net.HttpClient;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.test.base.HttpClient4Test;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.junit.Assert;
@ -36,7 +37,7 @@ import java.util.*;
/**
* @author nkorange
*/
public class NamingBase {
public class NamingBase extends HttpClient4Test {
public static final String TEST_DOM_1 = "nacos.test.1";
@ -66,13 +67,6 @@ public class NamingBase {
public static final int TIME_OUT = 3000;
@Autowired
protected TestRestTemplate restTemplate;
protected URL base;
public static String randomDomainName() {
StringBuilder sb = new StringBuilder();
sb.append("jinhan");
@ -216,30 +210,4 @@ public class NamingBase {
Assert.assertEquals(HttpStatus.SC_OK, result.code);
}
protected <T> ResponseEntity<T> request(String path, MultiValueMap<String, String> params, Class<T> clazz) {
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.GET, entity, clazz);
}
protected <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);
}
}

View File

@ -21,22 +21,17 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.test.base.Params;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
/**
* @author nkorange

View File

@ -24,6 +24,7 @@ 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.naming.NamingApp;
import com.alibaba.nacos.test.base.Params;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -38,8 +39,6 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.test.naming.NamingBase.*;
/**
* Created by wangtong.wt on 2018/6/20.
*