增加单元测试用例,及其代码的优化

This commit is contained in:
lqyan
2024-02-21 12:57:20 +08:00
parent fdeaa95cd7
commit 62a163ba5d
7 changed files with 129 additions and 13 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);