diff --git a/config/application-test.properties b/config/application-test.properties index 08b79532..1ab6d3c3 100644 --- a/config/application-test.properties +++ b/config/application-test.properties @@ -4,11 +4,11 @@ server.tomcat.basedir=./basedir # 多个项目放在nginx下同个端口,通过该配置区分 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 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.username=phoenix -spring.datasource.password=Hy@rfph32 +spring.datasource.username=root +spring.datasource.password=h0K0_8u # 配置连接池 spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource spring.datasource.dbcp2.max-total=128 diff --git a/src/main/java/com/dispose/common/AuthConfigValue.java b/src/main/java/com/dispose/common/AuthConfigValue.java index 0022d3ca..ab37bfe7 100644 --- a/src/main/java/com/dispose/common/AuthConfigValue.java +++ b/src/main/java/com/dispose/common/AuthConfigValue.java @@ -35,12 +35,17 @@ public class AuthConfigValue { public static final String MYSQL_REGEX_CHARS = "^((?!(--|\\s|\\*|%|\\+|'|;])).)*$"; /** - * The constant TRUST_INFO_CACHE. + * The constant TRUST_TOKEN_CACHE. */ - public static final HashMap TRUST_INFO_CACHE = new HashMap<>(); + public static final HashMap TRUST_TOKEN_CACHE = new HashMap<>(); /** - * The constant AUTH_CHECK. + * The constant TRUST_HOST_CACHE. + */ + public static final HashMap TRUST_HOST_CACHE = new HashMap<>(); + + /** + * The constant AUTH_WHITE_LIST_CHECK. */ public static volatile boolean AUTH_WHITE_LIST_CHECK = true; } diff --git a/src/main/java/com/dispose/config/TrustHostConfig.java b/src/main/java/com/dispose/config/TrustHostConfig.java index 18db1665..6d97c909 100644 --- a/src/main/java/com/dispose/config/TrustHostConfig.java +++ b/src/main/java/com/dispose/config/TrustHostConfig.java @@ -50,11 +50,11 @@ public class TrustHostConfig implements WebMvcConfigurer { AuthConfigValue.AUTH_WHITE_LIST_CHECK = Optional.ofNullable(authWhiteListCheck).orElse(true); 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"})) { - AuthConfigValue.TRUST_INFO_CACHE.put(Helper.ipAddressNormalize(s), System.currentTimeMillis()); + AuthConfigValue.TRUST_HOST_CACHE.put(Helper.ipAddressNormalize(s), System.currentTimeMillis()); } } diff --git a/src/main/java/com/dispose/interceptor/TokenInterceptor.java b/src/main/java/com/dispose/interceptor/TokenInterceptor.java index 98c48d81..f1b91680 100644 --- a/src/main/java/com/dispose/interceptor/TokenInterceptor.java +++ b/src/main/java/com/dispose/interceptor/TokenInterceptor.java @@ -55,8 +55,8 @@ public class TokenInterceptor implements HandlerInterceptor { // 检测是否在白名单内 if (AuthConfigValue.AUTH_WHITE_LIST_CHECK && - AuthConfigValue.TRUST_INFO_CACHE.containsKey(ipAddr) && - AuthConfigValue.TRUST_INFO_CACHE.containsKey(token)) { + AuthConfigValue.TRUST_HOST_CACHE.containsKey(ipAddr) && + AuthConfigValue.TRUST_TOKEN_CACHE.containsKey(token)) { log.debug("White list access: {} --> {}", ipAddr, token); return true; } @@ -66,7 +66,7 @@ public class TokenInterceptor implements HandlerInterceptor { if (err != ErrorCode.ERR_OK) { response.setCharacterEncoding("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))); log.error("Http request token [{}] is error: {}", token, err); return false; diff --git a/src/main/java/com/dispose/interceptor/TrustHostInterceptor.java b/src/main/java/com/dispose/interceptor/TrustHostInterceptor.java index 28299c08..2d8540d6 100644 --- a/src/main/java/com/dispose/interceptor/TrustHostInterceptor.java +++ b/src/main/java/com/dispose/interceptor/TrustHostInterceptor.java @@ -42,13 +42,13 @@ public class TrustHostInterceptor implements HandlerInterceptor { // 获取访问接口的客户端IP String remoteIp = request.getRemoteAddr(); // 判断该IP是否在信任列表内 - if (AuthConfigValue.TRUST_INFO_CACHE.containsKey(Helper.ipAddressNormalize(remoteIp))) { + if (AuthConfigValue.TRUST_HOST_CACHE.containsKey(Helper.ipAddressNormalize(remoteIp))) { // 提取header中的Authorization字段里面的token值 String token = request.getHeader("Authorization"); if (token != null && token.length() > 0) { // 判断token是否在信任列表中 - if (AuthConfigValue.TRUST_INFO_CACHE.containsKey(token)) { + if (AuthConfigValue.TRUST_TOKEN_CACHE.containsKey(token)) { return true; } else { err = ErrorCode.ERR_UNTRUSTTOKEN; @@ -66,7 +66,7 @@ public class TrustHostInterceptor implements HandlerInterceptor { response.setCharacterEncoding("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))); return false; diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java index 3a0ef51d..1dd357ae 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/manager/AddDeviceRsp.java @@ -1,7 +1,9 @@ package com.dispose.pojo.dto.protocol.device.manager; +import com.dispose.pojo.dto.protocol.base.BaseRespStatus; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; +import lombok.EqualsAndHashCode; import java.util.ArrayList; import java.util.List; @@ -11,9 +13,10 @@ import java.util.List; * * @author */ +@EqualsAndHashCode(callSuper = true) @Data @JsonInclude(JsonInclude.Include.NON_NULL) -public class AddDeviceRsp { +public class AddDeviceRsp extends BaseRespStatus { /** * The Items. */ diff --git a/src/main/java/com/dispose/service/ProtocolSecurityService.java b/src/main/java/com/dispose/service/ProtocolSecurityService.java index decbd825..17eea53a 100644 --- a/src/main/java/com/dispose/service/ProtocolSecurityService.java +++ b/src/main/java/com/dispose/service/ProtocolSecurityService.java @@ -31,6 +31,15 @@ public interface ProtocolSecurityService { */ 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. * diff --git a/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java b/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java index 9d545dee..ac29af73 100644 --- a/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/ProtocolSecurityServiceImpl.java @@ -109,6 +109,44 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService { 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. @@ -136,30 +174,7 @@ public class ProtocolSecurityServiceImpl implements ProtocolSecurityService { throw new SecurityProtocolException(ErrorCode.ERR_SYSTEMEXCEPTION); } - if (cryptoType == ProtoCryptoType.CRYPTO_BASE64.getCode()) { - 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); - } + cryptoObject.setMsgContent(encryptProtocolString(msgContentJsonString, cryptoType)); return cryptoObject; } diff --git a/src/test/java/com/dispose/qa/test/common/CommonEnvironment.java b/src/test/java/com/dispose/qa/test/common/CommonEnvironment.java new file mode 100644 index 00000000..e348e7c6 --- /dev/null +++ b/src/test/java/com/dispose/qa/test/common/CommonEnvironment.java @@ -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 + */ +@Slf4j +@ActiveProfiles("test,dispose") +public class CommonEnvironment { +} diff --git a/src/test/java/com/dispose/qa/test/common/CommonRestful.java b/src/test/java/com/dispose/qa/test/common/CommonRestful.java new file mode 100644 index 00000000..2727a3cd --- /dev/null +++ b/src/test/java/com/dispose/qa/test/common/CommonRestful.java @@ -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 + */ +@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 = userAccountMapper.selectAll().stream().findFirst(); + + if (userAccount.isPresent()) { + this.userName = userAccount.get().getUsername(); + this.password = userAccount.get().getPassword(); + } + + MulReturnType ret = userAccountService.loginService(userName, password); + + if (ret.getFirstParam() == ErrorCode.ERR_OK) { + this.loginToken = ret.getSecondParam(); + } + } + + /** + * Create resp type type. + * + * @param the type parameter + * @param c the c + * @return the type + */ + private Type createRespType(Class 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 the type parameter + * @param 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 ProtocolRespDTO performanceRestful(T reqObject, String urlPath, + Class subClass, + ProtoCryptoType reqCryptoType, + ErrorCode errCode, + String loginToken, + boolean autoDecrypt, + RequestMethod reqType) throws Exception { + String sendMsgContent; + String rspValue; + + ProtocolReqDTO 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 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>() { + @Override + public Type getType() { + return createRespType(subClass); + } + }); + } + + /** + * Performance restful success protocol resp dto. + * + * @param the type parameter + * @param 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 ProtocolRespDTO performanceRestfulSuccess(T reqObject, String urlPath, Class 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 the type parameter + * @param 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 ProtocolRespDTO performanceRestfulFail(T reqObject, String urlPath, Class subClass, + String loginToken, + ErrorCode errCode, RequestMethod reqType) throws Exception { + return performanceRestful(reqObject, + urlPath, + subClass, + ProtoCryptoType.CRYPTO_NONE, + errCode, + loginToken, + true, + reqType); + } +} diff --git a/src/test/java/com/dispose/qa/test/controller/P1AuthControllerTest.java b/src/test/java/com/dispose/qa/test/controller/P1AuthControllerTest.java new file mode 100644 index 00000000..6175f08d --- /dev/null +++ b/src/test/java/com/dispose/qa/test/controller/P1AuthControllerTest.java @@ -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 + */ + +@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 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 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()); + } +} diff --git a/src/test/java/com/dispose/qa/test/controller/P1DeviceManagerControllerTest.java b/src/test/java/com/dispose/qa/test/controller/P1DeviceManagerControllerTest.java new file mode 100644 index 00000000..1c922918 --- /dev/null +++ b/src/test/java/com/dispose/qa/test/controller/P1DeviceManagerControllerTest.java @@ -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 + */ +@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 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 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()); + } +}