Skip to content

Commit

Permalink
#1105 Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Jan 6, 2020
1 parent 41dfcf5 commit 3e312e7
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 44 deletions.
5 changes: 5 additions & 0 deletions test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.alibaba.nacos.test.core.auth;

public class Permission_ITCase {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.alibaba.nacos.test.core.auth;

public class Role_ITCase {
}
179 changes: 179 additions & 0 deletions test/src/test/java/com/alibaba/nacos/test/core/auth/User_ITCase.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
117 changes: 117 additions & 0 deletions test/src/test/java/com/alibaba/nacos/test/naming/Auth_ITCase.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down
Loading

0 comments on commit 3e312e7

Please sign in to comment.