mirror of
https://github.com/yanlongqi/jhinno-openapi-java-sdk.git
synced 2026-03-22 06:15:10 +08:00
增加单元测试用例,及其代码的优化
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.jhinno.sdk.openapi.api.organization;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -47,8 +49,7 @@ public class JHDepartmentApiExecution extends JHApiExecution {
|
||||
* @param departmentInfo 部门信息
|
||||
*/
|
||||
public void addDepartment(String username, AddUpdateDepartment departmentInfo) {
|
||||
String path = DepartmentPathConstant.DEPARTMENT_NAME_PATH.replace("{depName}", departmentInfo.getDepName());
|
||||
post(path, username, departmentInfo);
|
||||
post(DepartmentPathConstant.DEPARTMENT_PATH, username, departmentInfo);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +60,9 @@ public class JHDepartmentApiExecution extends JHApiExecution {
|
||||
* @param departmentInfo 部门信息
|
||||
*/
|
||||
public void updateDepartment(String username, AddUpdateDepartment departmentInfo) {
|
||||
if (StringUtils.isBlank(departmentInfo.getDepName())) {
|
||||
throw new ArgsException("departmentInfo中的depName不能为空!");
|
||||
}
|
||||
String path = DepartmentPathConstant.DEPARTMENT_NAME_PATH.replace("{depName}", departmentInfo.getDepName());
|
||||
put(path, username, departmentInfo);
|
||||
}
|
||||
@@ -71,6 +75,9 @@ public class JHDepartmentApiExecution extends JHApiExecution {
|
||||
* @param departmentName 部门名称
|
||||
*/
|
||||
public void deleteDepartment(String username, String departmentName) {
|
||||
if (StringUtils.isBlank(departmentName)) {
|
||||
throw new ArgsException("departmentName不能为空!");
|
||||
}
|
||||
String path = DepartmentPathConstant.DEPARTMENT_NAME_PATH.replace("{depName}", departmentName);
|
||||
delete(path, username);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.jhinno.sdk.openapi.api.organization;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.PageResult;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
@@ -71,6 +72,9 @@ public class JHUserApiExecution extends JHApiExecution {
|
||||
* @param userInfo 用户信息
|
||||
*/
|
||||
public void updateUser(String username, AddUpdateUserInfo userInfo) {
|
||||
if (StringUtils.isBlank(userInfo.getUserName())) {
|
||||
throw new ArgsException("userInfo中userName的值不能为空");
|
||||
}
|
||||
String path = UserPathConstant.USERS_USERNAME_PATH.replace("{username}", userInfo.getUserName());
|
||||
put(path, username, userInfo);
|
||||
}
|
||||
@@ -80,9 +84,9 @@ public class JHUserApiExecution extends JHApiExecution {
|
||||
* 修改或重置用户密码
|
||||
*
|
||||
* <ul>
|
||||
* <li>当type值为{@link UpdateUserPasswordType#FORCE_UPDATE_PASSWORD_TYPE} 时会怎样</li>
|
||||
* <li>当type值为{@link UpdateUserPasswordType#RESET_UPDATE_PASSWORD_TYPE} 时会怎样</li>
|
||||
* <li>当type值为空时会怎样</li>
|
||||
* <li>当type值为{@link UpdateUserPasswordType#FORCE_UPDATE_PASSWORD_TYPE}重置密码后用户再次登录需要修改密码</li>
|
||||
* <li>当type值为{@link UpdateUserPasswordType#RESET_UPDATE_PASSWORD_TYPE}重置用户的密码</li>
|
||||
* <li>当type值为空时修改用户密码</li>
|
||||
* </ul>
|
||||
* 参数怎么传,还需进一步确认,此处需要增加三个重构,方便开发者调用
|
||||
*
|
||||
@@ -93,6 +97,9 @@ public class JHUserApiExecution extends JHApiExecution {
|
||||
* @param type 类型,(非必填,取值见{@link UpdateUserPasswordType})
|
||||
*/
|
||||
public void updateUserPassword(String username, String updatePasswordUsername, String oldPassword, String password, String type) {
|
||||
if (StringUtils.isBlank(updatePasswordUsername)) {
|
||||
throw new ArgsException("updatePasswordUsername不能为空");
|
||||
}
|
||||
String path = UserPathConstant.USERS_RESET_PASSWORD_PATH.replace("{username}", updatePasswordUsername);
|
||||
Map<String, Object> params = new HashMap<>(4);
|
||||
if (StringUtils.isNotBlank(oldPassword)) {
|
||||
@@ -148,6 +155,9 @@ public class JHUserApiExecution extends JHApiExecution {
|
||||
* @param deleteUsername 被删除的用户名
|
||||
*/
|
||||
public void deleteUser(String username, String deleteUsername) {
|
||||
if (StringUtils.isBlank(deleteUsername)) {
|
||||
throw new ArgsException("deleteUsername不能为空");
|
||||
}
|
||||
delete(UserPathConstant.USERS_USERNAME_PATH.replace("{username}", deleteUsername), username);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.ClientErrorCode;
|
||||
import com.jhinno.sdk.openapi.ClientException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -102,6 +103,7 @@ public class JHApiClient {
|
||||
/**
|
||||
* 通过{@link DefaultHttpClientConfig}默认配置的最大连接数和服务每次能并行接收的请求数量构建一个JHApiClient实例
|
||||
* <p>
|
||||
*
|
||||
* @param baseUrl 景行接口服务的基础地址
|
||||
* @return JHApiClient的实例
|
||||
*/
|
||||
@@ -112,6 +114,7 @@ public class JHApiClient {
|
||||
/**
|
||||
* 通过外部传入的{@link CloseableHttpClient}构建一个请求客户端.
|
||||
* <p>
|
||||
*
|
||||
* @param httpClient 请求连接池
|
||||
* @param baseUrl 景行接口服务的基础地址
|
||||
* @return JHApiClient的实例
|
||||
@@ -254,7 +257,7 @@ public class JHApiClient {
|
||||
*/
|
||||
public <T> T get(String path, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("url不能为空");
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(getUrl(path));
|
||||
return request(httpGet, headers, type);
|
||||
@@ -336,7 +339,7 @@ public class JHApiClient {
|
||||
*/
|
||||
public <T, K> T post(String path, K body, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("path不能为空");
|
||||
throw new ArgsException("path不能为空");
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(getUrl(path));
|
||||
try {
|
||||
@@ -346,7 +349,7 @@ public class JHApiClient {
|
||||
}
|
||||
return request(httpPost, headers, type);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,7 +367,7 @@ public class JHApiClient {
|
||||
*/
|
||||
public <T, K> T put(String path, K body, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("url不能为空");
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
HttpPut httpPost = new HttpPut(getUrl(path));
|
||||
try {
|
||||
@@ -374,7 +377,7 @@ public class JHApiClient {
|
||||
}
|
||||
return request(httpPost, headers, type);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,7 +420,7 @@ public class JHApiClient {
|
||||
*/
|
||||
public <T> T delete(String path, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("url不能为空");
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
HttpDelete httpDelete = new HttpDelete(getUrl(path));
|
||||
return request(httpDelete, headers, type);
|
||||
|
||||
@@ -99,7 +99,7 @@ public class FileApiTest {
|
||||
|
||||
|
||||
/**
|
||||
* 测试伤上传文件,(不覆盖源文件,如果isCover是true,上传后的文件有数字下标)
|
||||
* 测试上传文件,(不覆盖源文件,如果isCover是true,上传后的文件有数字下标)
|
||||
*/
|
||||
@Test
|
||||
public void testUploadFileNoCover() throws IOException {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.jhinno.sdk.openapi.test.organization;
|
||||
|
||||
import com.jhinno.sdk.openapi.api.organization.AddUpdateDepartment;
|
||||
import com.jhinno.sdk.openapi.api.organization.JHDepartmentApiExecution;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 组织接口相关单元测试
|
||||
@@ -12,4 +14,45 @@ import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
public class DepartmentApiTest {
|
||||
|
||||
private static final JHDepartmentApiExecution execution = new JHDepartmentApiExecution(JHClientConfig.client);
|
||||
|
||||
|
||||
/**
|
||||
* 测试获取部门
|
||||
*/
|
||||
@Test
|
||||
public void testGetDepartment() {
|
||||
System.out.println(execution.getDepartmentList("jhadmin"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试添加部门
|
||||
*/
|
||||
@Test
|
||||
public void testAddDepartment() {
|
||||
AddUpdateDepartment addUpdateDepartment = new AddUpdateDepartment();
|
||||
addUpdateDepartment.setDepName("test2");
|
||||
addUpdateDepartment.setDepNameCN("测试部门2");
|
||||
addUpdateDepartment.setParentDepName("defaultDep");
|
||||
execution.addDepartment("jhadmin", addUpdateDepartment);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试修改部门
|
||||
*/
|
||||
@Test
|
||||
public void testUpdateDepartment() {
|
||||
AddUpdateDepartment addUpdateDepartment = new AddUpdateDepartment();
|
||||
addUpdateDepartment.setDepName("test2");
|
||||
addUpdateDepartment.setDepNameCN("测试部门2111");
|
||||
addUpdateDepartment.setParentDepName("defaultDep");
|
||||
execution.updateDepartment("jhadmin", addUpdateDepartment);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试删除部门
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteDepartment() {
|
||||
execution.deleteDepartment("jhadmin", "test2");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.jhinno.sdk.openapi.test.organization;
|
||||
|
||||
import com.jhinno.sdk.openapi.api.PageResult;
|
||||
import com.jhinno.sdk.openapi.api.organization.AddUpdateUserInfo;
|
||||
import com.jhinno.sdk.openapi.api.organization.JHUserApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.organization.UserInfo;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
@@ -25,4 +26,56 @@ public class UserApiTest {
|
||||
PageResult<UserInfo> result = execution.getUserList("jhadmin", null, null, null);
|
||||
System.out.println("result = " + result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试添加用户,密码可以不传
|
||||
*/
|
||||
@Test
|
||||
public void addUser() {
|
||||
AddUpdateUserInfo addUpdateUserInfo = new AddUpdateUserInfo();
|
||||
addUpdateUserInfo.setUserName("zhangsan3");
|
||||
addUpdateUserInfo.setUserNameCn("张三3");
|
||||
addUpdateUserInfo.setUserPassword("Jhadmin123");
|
||||
addUpdateUserInfo.setDepName("defaultDep");
|
||||
execution.addUser("jhadmin", addUpdateUserInfo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试修改用户,需要修改,只传我需要修改的值,并且用户修改的接口报错
|
||||
*/
|
||||
@Test
|
||||
public void updateUser() {
|
||||
AddUpdateUserInfo addUpdateUserInfo = new AddUpdateUserInfo();
|
||||
addUpdateUserInfo.setUserName("zhangsan");
|
||||
addUpdateUserInfo.setUserNameCn("张三1");
|
||||
addUpdateUserInfo.setDepName("defaultDep");
|
||||
addUpdateUserInfo.setUserPassword("Jhadmin123");
|
||||
execution.updateUser("jhadmin", addUpdateUserInfo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试删除用户
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteUser() {
|
||||
execution.deleteUser("jhadmin", "zhangsan1");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试修改用户密码
|
||||
*/
|
||||
@Test
|
||||
public void testUpdateUserPassword() {
|
||||
// 修改用户密码,应该是自己的密码需要自己的token修改
|
||||
execution.updateUserPassword("jhadmin", "zhangsan1", "Jhadmin123", "Jhadmin124");
|
||||
|
||||
// 管理员重置密码
|
||||
execution.resetPassword("jhadmin", "zhangsan2", "Jhadmin125");
|
||||
|
||||
// 管理员重置密码后,强制让用户修改密码(改接口调用报错,不应该传入旧密码)
|
||||
execution.resetForceUpdatePassword("jhadmin", "zhangsan3", "Jhadmin127");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user