OCT
REM: 1. 更新测试用配置文件 2. 统一异常HTTP返回状态值为400 3. 增加设备接口返回数据类型定义 4. 增加协议内容加密接口 5. 增加单元测试controller框架 6. 增加部分P1测试用例
This commit is contained in:
parent
e25785d366
commit
3ac91b3def
|
@ -4,11 +4,11 @@ server.tomcat.basedir=./basedir
|
||||||
# 多个项目放在nginx下同个端口,通过该配置区分
|
# 多个项目放在nginx下同个端口,通过该配置区分
|
||||||
server.servlet.context-path=/dispose
|
server.servlet.context-path=/dispose
|
||||||
# 配置数据源
|
# 配置数据源
|
||||||
spring.datasource.url=jdbc:mysql://172.28.72.118:33061/dispose_test?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior\
|
spring.datasource.url=jdbc:mysql://10.88.77.65:33061/ci_dispose_v2?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior\
|
||||||
=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
|
=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
|
||||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
spring.datasource.username=phoenix
|
spring.datasource.username=root
|
||||||
spring.datasource.password=Hy@rfph32
|
spring.datasource.password=h0K0_8u
|
||||||
# 配置连接池
|
# 配置连接池
|
||||||
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
|
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
|
||||||
spring.datasource.dbcp2.max-total=128
|
spring.datasource.dbcp2.max-total=128
|
||||||
|
|
|
@ -35,12 +35,17 @@ public class AuthConfigValue {
|
||||||
public static final String MYSQL_REGEX_CHARS = "^((?!(--|\\s|\\*|%|\\+|'|;])).)*$";
|
public static final String MYSQL_REGEX_CHARS = "^((?!(--|\\s|\\*|%|\\+|'|;])).)*$";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constant TRUST_INFO_CACHE.
|
* The constant TRUST_TOKEN_CACHE.
|
||||||
*/
|
*/
|
||||||
public static final HashMap<String, Long> TRUST_INFO_CACHE = new HashMap<>();
|
public static final HashMap<String, Long> TRUST_TOKEN_CACHE = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constant AUTH_CHECK.
|
* The constant TRUST_HOST_CACHE.
|
||||||
|
*/
|
||||||
|
public static final HashMap<String, Long> TRUST_HOST_CACHE = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constant AUTH_WHITE_LIST_CHECK.
|
||||||
*/
|
*/
|
||||||
public static volatile boolean AUTH_WHITE_LIST_CHECK = true;
|
public static volatile boolean AUTH_WHITE_LIST_CHECK = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,11 +50,11 @@ public class TrustHostConfig implements WebMvcConfigurer {
|
||||||
AuthConfigValue.AUTH_WHITE_LIST_CHECK = Optional.ofNullable(authWhiteListCheck).orElse(true);
|
AuthConfigValue.AUTH_WHITE_LIST_CHECK = Optional.ofNullable(authWhiteListCheck).orElse(true);
|
||||||
|
|
||||||
for (String s : Optional.ofNullable(authHostToken).orElse(new String[]{""})) {
|
for (String s : Optional.ofNullable(authHostToken).orElse(new String[]{""})) {
|
||||||
AuthConfigValue.TRUST_INFO_CACHE.put(s, System.currentTimeMillis());
|
AuthConfigValue.TRUST_TOKEN_CACHE.put(s, System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String s : Optional.ofNullable(authHosts).orElse(new String[]{"127.0.0.1"})) {
|
for (String s : Optional.ofNullable(authHosts).orElse(new String[]{"127.0.0.1"})) {
|
||||||
AuthConfigValue.TRUST_INFO_CACHE.put(Helper.ipAddressNormalize(s), System.currentTimeMillis());
|
AuthConfigValue.TRUST_HOST_CACHE.put(Helper.ipAddressNormalize(s), System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,8 +55,8 @@ public class TokenInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
// 检测是否在白名单内
|
// 检测是否在白名单内
|
||||||
if (AuthConfigValue.AUTH_WHITE_LIST_CHECK &&
|
if (AuthConfigValue.AUTH_WHITE_LIST_CHECK &&
|
||||||
AuthConfigValue.TRUST_INFO_CACHE.containsKey(ipAddr) &&
|
AuthConfigValue.TRUST_HOST_CACHE.containsKey(ipAddr) &&
|
||||||
AuthConfigValue.TRUST_INFO_CACHE.containsKey(token)) {
|
AuthConfigValue.TRUST_TOKEN_CACHE.containsKey(token)) {
|
||||||
log.debug("White list access: {} --> {}", ipAddr, token);
|
log.debug("White list access: {} --> {}", ipAddr, token);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ public class TokenInterceptor implements HandlerInterceptor {
|
||||||
if (err != ErrorCode.ERR_OK) {
|
if (err != ErrorCode.ERR_OK) {
|
||||||
response.setCharacterEncoding("UTF-8");
|
response.setCharacterEncoding("UTF-8");
|
||||||
response.setContentType("application/json;charset=UTF-8");
|
response.setContentType("application/json;charset=UTF-8");
|
||||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||||
response.getWriter().write(new ObjectMapper().writeValueAsString(ProtocolRespDTO.result(err)));
|
response.getWriter().write(new ObjectMapper().writeValueAsString(ProtocolRespDTO.result(err)));
|
||||||
log.error("Http request token [{}] is error: {}", token, err);
|
log.error("Http request token [{}] is error: {}", token, err);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -42,13 +42,13 @@ public class TrustHostInterceptor implements HandlerInterceptor {
|
||||||
// 获取访问接口的客户端IP
|
// 获取访问接口的客户端IP
|
||||||
String remoteIp = request.getRemoteAddr();
|
String remoteIp = request.getRemoteAddr();
|
||||||
// 判断该IP是否在信任列表内
|
// 判断该IP是否在信任列表内
|
||||||
if (AuthConfigValue.TRUST_INFO_CACHE.containsKey(Helper.ipAddressNormalize(remoteIp))) {
|
if (AuthConfigValue.TRUST_HOST_CACHE.containsKey(Helper.ipAddressNormalize(remoteIp))) {
|
||||||
// 提取header中的Authorization字段里面的token值
|
// 提取header中的Authorization字段里面的token值
|
||||||
String token = request.getHeader("Authorization");
|
String token = request.getHeader("Authorization");
|
||||||
|
|
||||||
if (token != null && token.length() > 0) {
|
if (token != null && token.length() > 0) {
|
||||||
// 判断token是否在信任列表中
|
// 判断token是否在信任列表中
|
||||||
if (AuthConfigValue.TRUST_INFO_CACHE.containsKey(token)) {
|
if (AuthConfigValue.TRUST_TOKEN_CACHE.containsKey(token)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
err = ErrorCode.ERR_UNTRUSTTOKEN;
|
err = ErrorCode.ERR_UNTRUSTTOKEN;
|
||||||
|
@ -66,7 +66,7 @@ public class TrustHostInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
response.setCharacterEncoding("UTF-8");
|
response.setCharacterEncoding("UTF-8");
|
||||||
response.setContentType("application/json;charset=UTF-8");
|
response.setContentType("application/json;charset=UTF-8");
|
||||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||||
response.getWriter().write(new ObjectMapper().writeValueAsString(ProtocolRespDTO.result(err)));
|
response.getWriter().write(new ObjectMapper().writeValueAsString(ProtocolRespDTO.result(err)));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.dispose.pojo.dto.protocol.device.manager;
|
package com.dispose.pojo.dto.protocol.device.manager;
|
||||||
|
|
||||||
|
import com.dispose.pojo.dto.protocol.base.BaseRespStatus;
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -11,9 +13,10 @@ import java.util.List;
|
||||||
*
|
*
|
||||||
* @author <huangxin@cmhi.chinamoblie.com>
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
*/
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class AddDeviceRsp {
|
public class AddDeviceRsp extends BaseRespStatus {
|
||||||
/**
|
/**
|
||||||
* The Items.
|
* The Items.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -31,6 +31,15 @@ public interface ProtocolSecurityService {
|
||||||
*/
|
*/
|
||||||
DecryptRequestProtocol decryptProtocol(HttpInputMessage httpInputMessage) throws IOException;
|
DecryptRequestProtocol decryptProtocol(HttpInputMessage httpInputMessage) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt protocol string string.
|
||||||
|
*
|
||||||
|
* @param plainText the plain text
|
||||||
|
* @param cryptoType the crypto type
|
||||||
|
* @return the string
|
||||||
|
*/
|
||||||
|
String encryptProtocolString(String plainText, int cryptoType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt protocol protocol resp dto.
|
* Encrypt protocol protocol resp dto.
|
||||||
*
|
*
|
||||||
|
|
|
@ -109,6 +109,44 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
|
||||||
return new DecryptRequestProtocol(httpInputMessage, decryptProtocol(reqMessage));
|
return new DecryptRequestProtocol(httpInputMessage, decryptProtocol(reqMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt protocol string string.
|
||||||
|
*
|
||||||
|
* @param plainText the plain text
|
||||||
|
* @param cryptoType the crypto type
|
||||||
|
* @return the string
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encryptProtocolString(String plainText, int cryptoType) {
|
||||||
|
String cipherText;
|
||||||
|
if (cryptoType == ProtoCryptoType.CRYPTO_BASE64.getCode()) {
|
||||||
|
cipherText = CryptoHelper.base64Encryption(plainText.getBytes(StandardCharsets.UTF_8));
|
||||||
|
} else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) {
|
||||||
|
try {
|
||||||
|
byte[] encode = CryptoHelper.aes256Encryption(plainText.getBytes(StandardCharsets.UTF_8),
|
||||||
|
SecurityConfigValue.AES_KEY);
|
||||||
|
cipherText = CryptoHelper.base64Encryption(encode);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("AES256 encode message error: {}", plainText);
|
||||||
|
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256);
|
||||||
|
}
|
||||||
|
} else if (cryptoType == ProtoCryptoType.CRYPTO_DES.getCode()) {
|
||||||
|
try {
|
||||||
|
byte[] encode = CryptoHelper.desEncryption(plainText.getBytes(StandardCharsets.UTF_8),
|
||||||
|
SecurityConfigValue.DES_KEY);
|
||||||
|
cipherText= CryptoHelper.base64Encryption(encode);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("DES256 encode message error: {}", plainText);
|
||||||
|
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_3DES);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("Unknown protocol security type: {}, {}", cryptoType, plainText);
|
||||||
|
throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cipherText;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt protocol protocol resp dto.
|
* Encrypt protocol protocol resp dto.
|
||||||
|
@ -136,30 +174,7 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService {
|
||||||
throw new SecurityProtocolException(ErrorCode.ERR_SYSTEMEXCEPTION);
|
throw new SecurityProtocolException(ErrorCode.ERR_SYSTEMEXCEPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cryptoType == ProtoCryptoType.CRYPTO_BASE64.getCode()) {
|
cryptoObject.setMsgContent(encryptProtocolString(msgContentJsonString, cryptoType));
|
||||||
cryptoObject.setMsgContent(CryptoHelper.base64Encryption(msgContentJsonString.getBytes(StandardCharsets.UTF_8)));
|
|
||||||
} else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) {
|
|
||||||
try {
|
|
||||||
byte[] encode = CryptoHelper.aes256Encryption(msgContentJsonString.getBytes(StandardCharsets.UTF_8),
|
|
||||||
SecurityConfigValue.AES_KEY);
|
|
||||||
cryptoObject.setMsgContent(CryptoHelper.base64Encryption(encode));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("AES256 encode message error: {}", msgContentJsonString);
|
|
||||||
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256);
|
|
||||||
}
|
|
||||||
} else if (cryptoType == ProtoCryptoType.CRYPTO_DES.getCode()) {
|
|
||||||
try {
|
|
||||||
byte[] encode = CryptoHelper.desEncryption(msgContentJsonString.getBytes(StandardCharsets.UTF_8),
|
|
||||||
SecurityConfigValue.DES_KEY);
|
|
||||||
cryptoObject.setMsgContent(CryptoHelper.base64Encryption(encode));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("DES256 encode message error: {}", msgContentJsonString);
|
|
||||||
throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_3DES);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.error("Unknown protocol security type: {}, {}", cryptoType, msgContentJsonString);
|
|
||||||
throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_UNKNOWN);
|
|
||||||
}
|
|
||||||
|
|
||||||
return cryptoObject;
|
return cryptoObject;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.dispose.qa.test.common;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Common environment.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@ActiveProfiles("test,dispose")
|
||||||
|
public class CommonEnvironment {
|
||||||
|
}
|
|
@ -0,0 +1,292 @@
|
||||||
|
package com.dispose.qa.test.common;
|
||||||
|
|
||||||
|
import com.dispose.common.AuthConfigValue;
|
||||||
|
import com.dispose.common.ConstValue;
|
||||||
|
import com.dispose.common.ErrorCode;
|
||||||
|
import com.dispose.common.ProtoCryptoType;
|
||||||
|
import com.dispose.mapper.UserAccountMapper;
|
||||||
|
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
|
||||||
|
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||||
|
import com.dispose.pojo.entity.UserAccount;
|
||||||
|
import com.dispose.pojo.po.MulReturnType;
|
||||||
|
import com.dispose.service.ProtocolSecurityService;
|
||||||
|
import com.dispose.service.UserAccountService;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type Common restful.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
public class CommonRestful extends CommonEnvironment {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Login token.
|
||||||
|
*/
|
||||||
|
private String loginToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The User name.
|
||||||
|
*/
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Password.
|
||||||
|
*/
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The User account service.
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private UserAccountService userAccountService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Protocol security service.
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private ProtocolSecurityService protocolSecurityService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The User account mapper.
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private UserAccountMapper userAccountMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Mock mvc.
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object mapper.
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init global value.
|
||||||
|
*
|
||||||
|
* @throws NoSuchAlgorithmException the no such algorithm exception
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
private void initGlobalValue() throws NoSuchAlgorithmException {
|
||||||
|
Optional<UserAccount> userAccount = userAccountMapper.selectAll().stream().findFirst();
|
||||||
|
|
||||||
|
if (userAccount.isPresent()) {
|
||||||
|
this.userName = userAccount.get().getUsername();
|
||||||
|
this.password = userAccount.get().getPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
MulReturnType<ErrorCode, String> ret = userAccountService.loginService(userName, password);
|
||||||
|
|
||||||
|
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
|
||||||
|
this.loginToken = ret.getSecondParam();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create resp type type.
|
||||||
|
*
|
||||||
|
* @param <T> the type parameter
|
||||||
|
* @param c the c
|
||||||
|
* @return the type
|
||||||
|
*/
|
||||||
|
private <T> Type createRespType(Class<T> c) {
|
||||||
|
Type[] types = new Type[1];
|
||||||
|
types[0] = c;
|
||||||
|
return ParameterizedTypeImpl.make(ProtocolRespDTO.class, types,
|
||||||
|
ProtocolRespDTO.class.getDeclaringClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets white list token.
|
||||||
|
*
|
||||||
|
* @return the white list token
|
||||||
|
*/
|
||||||
|
public String getWhiteListToken() {
|
||||||
|
for (String v : AuthConfigValue.TRUST_TOKEN_CACHE.keySet()) {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String restfulRun(RequestMethod reqType, String urlPath, String loginToken, String sendMsgContent,
|
||||||
|
ErrorCode errCode) throws Exception {
|
||||||
|
MockHttpServletRequestBuilder build;
|
||||||
|
int httpCode = (errCode == ErrorCode.ERR_OK) ? HttpServletResponse.SC_OK : HttpServletResponse.SC_BAD_REQUEST;
|
||||||
|
|
||||||
|
switch (reqType) {
|
||||||
|
case PUT:
|
||||||
|
build = MockMvcRequestBuilders.put(urlPath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GET:
|
||||||
|
build = MockMvcRequestBuilders.get(urlPath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DELETE:
|
||||||
|
build = MockMvcRequestBuilders.delete(urlPath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case POST:
|
||||||
|
default:
|
||||||
|
build = MockMvcRequestBuilders.post(urlPath);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loginToken != null && loginToken.length() > 0) {
|
||||||
|
build.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + loginToken)
|
||||||
|
.content(sendMsgContent);
|
||||||
|
} else {
|
||||||
|
build.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(sendMsgContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mockMvc.perform(build)
|
||||||
|
.andDo(print()).andExpect(status().is(httpCode))
|
||||||
|
.andExpect(jsonPath("$.code").value(errCode.getHttpCode()))
|
||||||
|
.andReturn()
|
||||||
|
.getResponse()
|
||||||
|
.getContentAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performance restful protocol resp dto.
|
||||||
|
*
|
||||||
|
* @param <T> the type parameter
|
||||||
|
* @param <K> the type parameter
|
||||||
|
* @param reqObject the req object
|
||||||
|
* @param urlPath the url path
|
||||||
|
* @param subClass the sub class
|
||||||
|
* @param reqCryptoType the req crypto type
|
||||||
|
* @param errCode the http code
|
||||||
|
* @param loginToken the login token
|
||||||
|
* @param autoDecrypt the auto decrypt
|
||||||
|
* @return the protocol resp dto
|
||||||
|
* @throws Exception the exception
|
||||||
|
*/
|
||||||
|
public <T, K> ProtocolRespDTO<K> performanceRestful(T reqObject, String urlPath,
|
||||||
|
Class<K> subClass,
|
||||||
|
ProtoCryptoType reqCryptoType,
|
||||||
|
ErrorCode errCode,
|
||||||
|
String loginToken,
|
||||||
|
boolean autoDecrypt,
|
||||||
|
RequestMethod reqType) throws Exception {
|
||||||
|
String sendMsgContent;
|
||||||
|
String rspValue;
|
||||||
|
|
||||||
|
ProtocolReqDTO<T> reqInfo = new ProtocolReqDTO<>();
|
||||||
|
reqInfo.setVer(ConstValue.Protocol.VERSION);
|
||||||
|
reqInfo.setCryptoType(reqCryptoType.getCode());
|
||||||
|
reqInfo.setTimeStamp(System.currentTimeMillis());
|
||||||
|
reqInfo.setMsgContent(reqObject);
|
||||||
|
|
||||||
|
if (reqCryptoType != ProtoCryptoType.CRYPTO_NONE) {
|
||||||
|
String cipherText =
|
||||||
|
protocolSecurityService.encryptProtocolString(objectMapper.writeValueAsString(reqObject),
|
||||||
|
reqCryptoType.getCode());
|
||||||
|
ProtocolReqDTO<String> cipherInfo = new ProtocolReqDTO<>();
|
||||||
|
cipherInfo.setVer(ConstValue.Protocol.VERSION);
|
||||||
|
cipherInfo.setCryptoType(reqCryptoType.getCode());
|
||||||
|
cipherInfo.setTimeStamp(System.currentTimeMillis());
|
||||||
|
cipherInfo.setMsgContent(cipherText);
|
||||||
|
sendMsgContent = objectMapper.writeValueAsString(cipherInfo);
|
||||||
|
} else {
|
||||||
|
sendMsgContent = objectMapper.writeValueAsString(reqInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
rspValue = restfulRun(reqType, urlPath, loginToken, sendMsgContent, errCode);
|
||||||
|
|
||||||
|
if (autoDecrypt) {
|
||||||
|
rspValue = protocolSecurityService.decryptProtocol(rspValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return objectMapper.readValue(rspValue,
|
||||||
|
new TypeReference<ProtocolRespDTO<K>>() {
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return createRespType(subClass);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performance restful success protocol resp dto.
|
||||||
|
*
|
||||||
|
* @param <T> the type parameter
|
||||||
|
* @param <K> the type parameter
|
||||||
|
* @param reqObject the req object
|
||||||
|
* @param urlPath the url path
|
||||||
|
* @param subClass the sub class
|
||||||
|
* @param loginToken the login token
|
||||||
|
* @return the protocol resp dto
|
||||||
|
* @throws Exception the exception
|
||||||
|
*/
|
||||||
|
public <T, K> ProtocolRespDTO<K> performanceRestfulSuccess(T reqObject, String urlPath, Class<K> subClass,
|
||||||
|
String loginToken, RequestMethod reqType) throws Exception {
|
||||||
|
return performanceRestful(reqObject,
|
||||||
|
urlPath,
|
||||||
|
subClass,
|
||||||
|
ProtoCryptoType.CRYPTO_NONE,
|
||||||
|
ErrorCode.ERR_OK,
|
||||||
|
loginToken,
|
||||||
|
true,
|
||||||
|
reqType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performance restful fail protocol resp dto.
|
||||||
|
*
|
||||||
|
* @param <T> the type parameter
|
||||||
|
* @param <K> the type parameter
|
||||||
|
* @param reqObject the req object
|
||||||
|
* @param urlPath the url path
|
||||||
|
* @param subClass the sub class
|
||||||
|
* @param loginToken the login token
|
||||||
|
* @param errCode the http err code
|
||||||
|
* @return the protocol resp dto
|
||||||
|
* @throws Exception the exception
|
||||||
|
*/
|
||||||
|
public <T, K> ProtocolRespDTO<K> performanceRestfulFail(T reqObject, String urlPath, Class<K> subClass,
|
||||||
|
String loginToken,
|
||||||
|
ErrorCode errCode, RequestMethod reqType) throws Exception {
|
||||||
|
return performanceRestful(reqObject,
|
||||||
|
urlPath,
|
||||||
|
subClass,
|
||||||
|
ProtoCryptoType.CRYPTO_NONE,
|
||||||
|
errCode,
|
||||||
|
loginToken,
|
||||||
|
true,
|
||||||
|
reqType);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
package com.dispose.qa.test.controller;
|
||||||
|
|
||||||
|
import com.dispose.common.ErrorCode;
|
||||||
|
import com.dispose.common.ProtoCryptoType;
|
||||||
|
import com.dispose.pojo.dto.protocol.auth.LoginReq;
|
||||||
|
import com.dispose.pojo.dto.protocol.auth.LoginRsp;
|
||||||
|
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||||
|
import com.dispose.qa.test.common.CommonRestful;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type P 1 auth controller test.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
|
@Slf4j
|
||||||
|
public class P1AuthControllerTest extends CommonRestful {
|
||||||
|
/**
|
||||||
|
* A 1 login.
|
||||||
|
*
|
||||||
|
* @throws Exception the exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void a1_login() throws Exception {
|
||||||
|
LoginReq logReq = LoginReq.builder()
|
||||||
|
.userName(getUserName())
|
||||||
|
.password(getPassword())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ProtocolRespDTO<LoginRsp> rsp = performanceRestfulSuccess(logReq,
|
||||||
|
"/auth/login",
|
||||||
|
LoginRsp.class,
|
||||||
|
null,
|
||||||
|
RequestMethod.POST);
|
||||||
|
|
||||||
|
Assert.assertNotNull(rsp);
|
||||||
|
Assert.assertNotNull(rsp.getMsgContent());
|
||||||
|
Assert.assertEquals(rsp.getMsgContent().getUserName(), getUserName());
|
||||||
|
Assert.assertEquals((long) rsp.getMsgContent().getStatus(), ErrorCode.ERR_OK.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A 2 login user base 64 crypto.
|
||||||
|
*
|
||||||
|
* @throws Exception the exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void a2_loginUserBase64Crypto() throws Exception {
|
||||||
|
LoginReq logReq = LoginReq.builder()
|
||||||
|
.userName(getUserName())
|
||||||
|
.password(getPassword())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ProtocolRespDTO<LoginRsp> rsp = performanceRestful(logReq,
|
||||||
|
"/auth/login",
|
||||||
|
LoginRsp.class,
|
||||||
|
ProtoCryptoType.CRYPTO_BASE64,
|
||||||
|
ErrorCode.ERR_OK,
|
||||||
|
null,
|
||||||
|
true,
|
||||||
|
RequestMethod.POST);
|
||||||
|
|
||||||
|
Assert.assertNotNull(rsp);
|
||||||
|
Assert.assertNotNull(rsp.getMsgContent());
|
||||||
|
Assert.assertEquals(rsp.getMsgContent().getUserName(), getUserName());
|
||||||
|
Assert.assertEquals((long) rsp.getMsgContent().getStatus(), ErrorCode.ERR_OK.getCode());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.dispose.qa.test.controller;
|
||||||
|
|
||||||
|
import com.dispose.common.DisposeCapacityType;
|
||||||
|
import com.dispose.common.DisposeDeviceType;
|
||||||
|
import com.dispose.common.DisposeObjectType;
|
||||||
|
import com.dispose.common.ErrorCode;
|
||||||
|
import com.dispose.common.HttpType;
|
||||||
|
import com.dispose.common.IpAddrType;
|
||||||
|
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||||
|
import com.dispose.pojo.dto.protocol.device.manager.AddCapacityInfo;
|
||||||
|
import com.dispose.pojo.dto.protocol.device.manager.AddDeviceInfo;
|
||||||
|
import com.dispose.pojo.dto.protocol.device.manager.AddDeviceReq;
|
||||||
|
import com.dispose.pojo.dto.protocol.device.manager.AddDeviceRsp;
|
||||||
|
import com.dispose.qa.test.common.CommonRestful;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.annotation.Rollback;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type P 1 device manager controller test.
|
||||||
|
*
|
||||||
|
* @author <huangxin@cmhi.chinamoblie.com>
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
|
@Slf4j
|
||||||
|
@Transactional
|
||||||
|
@Rollback
|
||||||
|
public class P1DeviceManagerControllerTest extends CommonRestful {
|
||||||
|
/**
|
||||||
|
* A 1 un login request.
|
||||||
|
*
|
||||||
|
* @throws Exception the exception
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void a1_unLoginRequest() throws Exception {
|
||||||
|
AddDeviceReq addReq = AddDeviceReq.builder()
|
||||||
|
.items(new ArrayList<>())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<AddCapacityInfo> newCapList = new ArrayList<>();
|
||||||
|
|
||||||
|
newCapList.add(AddCapacityInfo.builder()
|
||||||
|
.capacityType(DisposeCapacityType.CLEANUP.getValue())
|
||||||
|
.objectType(DisposeObjectType.IP.getValue())
|
||||||
|
.ipType(IpAddrType.IPV4_IPV6.getValue())
|
||||||
|
.protectIp("")
|
||||||
|
.build());
|
||||||
|
|
||||||
|
addReq.getItems().add(AddDeviceInfo.builder()
|
||||||
|
.ipAddr("127.0.0.1")
|
||||||
|
.ipPort("1000")
|
||||||
|
.deviceType(DisposeDeviceType.VIRTUAL_DISPOSE.getValue())
|
||||||
|
.areaCode(0)
|
||||||
|
.deviceName("实验室虚拟清洗设备")
|
||||||
|
.manufacturer("CMCC")
|
||||||
|
.model("Virtual")
|
||||||
|
.version("v0.0.1")
|
||||||
|
.userName("admin")
|
||||||
|
.password("admin")
|
||||||
|
.urlPath("")
|
||||||
|
.urlType(HttpType.HTTP.getValue())
|
||||||
|
.readme("实验室虚拟测试设备")
|
||||||
|
.capacity(newCapList)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
ProtocolRespDTO<AddDeviceRsp> rspInfo = performanceRestfulFail(addReq,
|
||||||
|
"/manager/device",
|
||||||
|
AddDeviceRsp.class,
|
||||||
|
"123245125123532",
|
||||||
|
ErrorCode.ERR_LOGOUT,
|
||||||
|
RequestMethod.PUT);
|
||||||
|
|
||||||
|
Assert.assertNotNull(rspInfo);
|
||||||
|
Assert.assertNotNull(rspInfo.getMsgContent());
|
||||||
|
Assert.assertEquals((long)rspInfo.getMsgContent().getStatus(), ErrorCode.ERR_LOGOUT.getCode());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue