From 3a915103d1276caa18977a9b998882a90b7b6a77 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Thu, 10 Sep 2020 18:13:16 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=9A=84QA=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/common/CommonRestfulJson.java | 15 +-- .../java/com/dispose/test/common/Helper.java | 2 +- .../com/dispose/test/common/QATestItem.java | 53 ++++++++++ .../com/dispose/test/common/TestPriority.java | 65 +++++++++++++ .../test/common/VerifyProtoRespCallback.java | 20 ++++ .../com/dispose/test/qa/exec/TestCaseRun.java | 95 ++++++++++++++++++ .../com/dispose/test/qa/testcase/v20/P1.java | 96 +++++++++++++++++++ 7 files changed, 338 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/dispose/test/common/QATestItem.java create mode 100644 src/test/java/com/dispose/test/common/TestPriority.java create mode 100644 src/test/java/com/dispose/test/common/VerifyProtoRespCallback.java create mode 100644 src/test/java/com/dispose/test/qa/exec/TestCaseRun.java create mode 100644 src/test/java/com/dispose/test/qa/testcase/v20/P1.java diff --git a/src/test/java/com/dispose/test/common/CommonRestfulJson.java b/src/test/java/com/dispose/test/common/CommonRestfulJson.java index e4f0998b..596f57c1 100644 --- a/src/test/java/com/dispose/test/common/CommonRestfulJson.java +++ b/src/test/java/com/dispose/test/common/CommonRestfulJson.java @@ -1,6 +1,7 @@ package com.dispose.test.common; import com.dispose.common.ErrorCode; +import com.dispose.pojo.dto.protocol.base.BaseRespStatus; import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; import com.dispose.service.ProtocolSecurityService; import com.fasterxml.jackson.core.type.TypeReference; @@ -57,12 +58,12 @@ public class CommonRestfulJson extends CommonEnvironment { * @return the protocol resp dto * @throws Exception the exception */ - public ProtocolRespDTO performanceRestful(String reqJson, String urlPath, - Class subClass, - ErrorCode errCode, - String loginToken, - boolean autoDecrypt, - RequestMethod reqType) throws Exception { + public ProtocolRespDTO performanceRestful(String reqJson, String urlPath, + Class subClass, + ErrorCode errCode, + String loginToken, + boolean autoDecrypt, + RequestMethod reqType) throws Exception { String rspValue = Helper.restfulRun(mockMvc, reqType, urlPath, loginToken, reqJson, errCode); if (autoDecrypt) { @@ -70,7 +71,7 @@ public class CommonRestfulJson extends CommonEnvironment { } return objectMapper.readValue(rspValue, - new TypeReference>() { + new TypeReference>() { @Override public Type getType() { return Helper.createRespType(subClass); diff --git a/src/test/java/com/dispose/test/common/Helper.java b/src/test/java/com/dispose/test/common/Helper.java index 4c900521..2d9be0d8 100644 --- a/src/test/java/com/dispose/test/common/Helper.java +++ b/src/test/java/com/dispose/test/common/Helper.java @@ -60,7 +60,7 @@ public class Helper { break; } - if (loginToken != null && loginToken.length() > 0) { + if (loginToken != null) { build.contentType(MediaType.APPLICATION_JSON) .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + loginToken) .content(sendMsgContent); diff --git a/src/test/java/com/dispose/test/common/QATestItem.java b/src/test/java/com/dispose/test/common/QATestItem.java new file mode 100644 index 00000000..729192aa --- /dev/null +++ b/src/test/java/com/dispose/test/common/QATestItem.java @@ -0,0 +1,53 @@ +package com.dispose.test.common; + +import com.dispose.common.ErrorCode; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * The type Qa test item. + * + * @author + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class QATestItem { + /** + * The Priority. + */ + private TestPriority priority; + /** + * The Url path. + */ + private String urlPath; + /** + * The Method. + */ + private RequestMethod method; + /** + * The Case json value. + */ + private String caseJsonValue; + /** + * The Rsp class. + */ + private Class rspClass; + /** + * The Rsp code. + */ + private ErrorCode rspCode; + /** + * The Auto login. + */ + private Boolean autoLogin; + + /** + * The Verify callback. + */ + private VerifyProtoRespCallback verifyCallback; +} diff --git a/src/test/java/com/dispose/test/common/TestPriority.java b/src/test/java/com/dispose/test/common/TestPriority.java new file mode 100644 index 00000000..92a59c99 --- /dev/null +++ b/src/test/java/com/dispose/test/common/TestPriority.java @@ -0,0 +1,65 @@ +package com.dispose.test.common; + +import com.dispose.common.BaseEnum; + +/** + * The enum Test priority. + * + * @author + */ +public enum TestPriority implements BaseEnum { + + /** + * P 1 priority test priority. + */ + P1_PRIORITY(1, "P1 优先级"), + /** + * P 2 priority test priority. + */ + P2_PRIORITY(2, "P2 优先级"), + /** + * P 3 priority test priority. + */ + P3_PRIORITY(3, "P3 优先级"), + ; + + /** + * The Code. + */ + private final Integer code; + /** + * The Readme. + */ + private final String readme; + + /** + * Instantiates a new Test priority. + * + * @param code the code + * @param readme the readme + */ + TestPriority(int code, String readme) { + this.code = code; + this.readme = readme; + } + + /** + * Gets value. + * + * @return the value + */ + @Override + public Integer getValue() { + return this.code; + } + + /** + * Gets description. + * + * @return the description + */ + @Override + public String getDescription() { + return this.readme; + } +} diff --git a/src/test/java/com/dispose/test/common/VerifyProtoRespCallback.java b/src/test/java/com/dispose/test/common/VerifyProtoRespCallback.java new file mode 100644 index 00000000..dda3e3e7 --- /dev/null +++ b/src/test/java/com/dispose/test/common/VerifyProtoRespCallback.java @@ -0,0 +1,20 @@ +package com.dispose.test.common; + +import com.dispose.common.ErrorCode; +import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; + +/** + * The interface Verify proto resp callback. + * + * @param the type parameter + * @author + */ +public interface VerifyProtoRespCallback { + + /** + * Verify callback. + * + * @param obj the obj + */ + void verifyCallback(ProtocolRespDTO obj, ErrorCode errorCode); +} diff --git a/src/test/java/com/dispose/test/qa/exec/TestCaseRun.java b/src/test/java/com/dispose/test/qa/exec/TestCaseRun.java new file mode 100644 index 00000000..91c480fd --- /dev/null +++ b/src/test/java/com/dispose/test/qa/exec/TestCaseRun.java @@ -0,0 +1,95 @@ +package com.dispose.test.qa.exec; + +import com.dispose.pojo.dto.protocol.base.BaseRespStatus; +import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; +import com.dispose.test.common.CommonRestfulJson; +import com.dispose.test.common.QATestItem; +import com.dispose.test.qa.testcase.v20.P1; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.Rollback; +import org.springframework.test.context.TestContextManager; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Arrays; +import java.util.Collection; + +/** + * The type Test case run. + * + * @author + */ +@RunWith(Parameterized.class) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Slf4j +@Transactional +@Rollback +public class TestCaseRun extends CommonRestfulJson { + + /** + * The Rsp object. + */ + private final QATestItem rspObject; + + /** + * Instantiates a new Test case run. + * + * @param rspObject the rsp object + */ + public TestCaseRun(QATestItem rspObject) { + this.rspObject = rspObject; + } + + /** + * Param data collection. + * + * @return the collection + */ + @Parameters + public static Collection paramData() { + return Arrays.asList(P1.getTestCase()); + } + + /** + * Sets up. + * + * @throws Exception the exception + */ + @Before + public void setUp() throws Exception { + TestContextManager testContextManager = new TestContextManager(getClass()); + testContextManager.prepareTestInstance(this); + } + + /** + * Run qa p 1 test case. + * + * @throws Exception the exception + */ + @Test + public void runQaP1TestCase() throws Exception { + ProtocolRespDTO o = performanceRestful(rspObject.getCaseJsonValue(), + rspObject.getUrlPath(), + rspObject.getRspClass(), + rspObject.getRspCode(), + rspObject.getAutoLogin() ? getLoginToken() : "", + true, + rspObject.getMethod()); + + Assert.assertNotNull(o); + + if (rspObject.getVerifyCallback() != null) { + rspObject.getVerifyCallback().verifyCallback(o, rspObject.getRspCode()); + } + + } +} diff --git a/src/test/java/com/dispose/test/qa/testcase/v20/P1.java b/src/test/java/com/dispose/test/qa/testcase/v20/P1.java new file mode 100644 index 00000000..aa0705df --- /dev/null +++ b/src/test/java/com/dispose/test/qa/testcase/v20/P1.java @@ -0,0 +1,96 @@ +package com.dispose.test.qa.testcase.v20; + +import com.dispose.common.ErrorCode; +import com.dispose.pojo.dto.protocol.auth.LoginRsp; +import com.dispose.pojo.dto.protocol.device.manager.AddDeviceRsp; +import com.dispose.test.common.QATestItem; +import com.dispose.test.common.TestPriority; +import com.dispose.test.common.VerifyProtoRespCallback; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * The type P 1. + * + * @author + */ +@Slf4j +public class P1 { + /** + * The constant testItemArray. + */ + private static final QATestItem[] testItemArray = new QATestItem[]{ + QATestItem.builder() + .priority(TestPriority.P1_PRIORITY) + .urlPath("/auth/login") + .method(RequestMethod.POST) + .caseJsonValue("{\"ver\":3,\"cryptoType\":0,\"timeStamp\":1598580612302," + + "\"msgContent\":{\"password" + + "\":\"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\"," + + "\"userName\":\"admin\"}}") + .rspClass(LoginRsp.class) + .rspCode(ErrorCode.ERR_OK) + .autoLogin(true) + .verifyCallback((VerifyProtoRespCallback) (v, e) -> { + Assert.assertNotNull(v); + Assert.assertNotNull(v.getMsgContent()); + Assert.assertEquals((long)v.getMsgContent().getStatus(), e.getCode()); + Assert.assertEquals("admin", v.getMsgContent().getUserName()); + Assert.assertNotEquals(v.getMsgContent().getToken().length(), 0); + }) + .build(), + + QATestItem.builder() + .priority(TestPriority.P1_PRIORITY) + .urlPath("/auth/login") + .method(RequestMethod.POST) + .caseJsonValue("{\"ver\":3,\"cryptoType\":1,\"timeStamp\":1599116519744,\"code\":200," + + "\"msgContent" + + "\":\"eyJwYXNzd29yZCI6ImMzODU1ZTZiNmJiMTIwNDUwZjE2MGJhOTExMzQ1MjI4N" + + "jhmODlkMzYwNjJmMjA2MWViZWVmZDgwODE3ZTFkNTgiLCJ1c2VyTmFtZSI6ImFkbWluIn0\"}") + .rspClass(LoginRsp.class) + .rspCode(ErrorCode.ERR_OK) + .autoLogin(true) + .verifyCallback((VerifyProtoRespCallback) (v, e) -> { + Assert.assertNotNull(v); + Assert.assertNotNull(v.getMsgContent()); + Assert.assertEquals((long)v.getMsgContent().getStatus(), e.getCode()); + Assert.assertEquals("admin", v.getMsgContent().getUserName()); + Assert.assertNotEquals(v.getMsgContent().getToken().length(), 0); + }) + .build(), + + QATestItem.builder() + .priority(TestPriority.P1_PRIORITY) + .urlPath("/manager/device") + .method(RequestMethod.PUT) + .caseJsonValue("{\"ver\":3,\"cryptoType\":0,\"timeStamp\":1599094454403," + + "\"msgContent\":{\"items\":[{\"ipAddr\":\"10.88.77.15\",\"ipPort\":\"\"," + + "\"deviceType\":0,\"areaCode\":0,\"deviceName\":\"中移杭研实验室迪普清洗设备\"," + + "\"manufacturer\":\"DPTech\",\"model\":\"UMC\",\"version\":\"5.7.13\"," + + "\"userName\":\"test\",\"password\":\"testpassword\"," + + "\"urlPath\":\"UMC/service/AbnormalFlowCleaningService\",\"urlType\":0," + + "\"readme\":\"实验室测试设备\",\"capacity\":[{\"capacityType\":0,\"objectType\":1," + + "\"ipType\":1,\"protectIp\":\"192.168.20.1/24\",\"reserveNetflow\":10}," + + "{\"capacityType\":1,\"objectType\":4,\"ipType\":2,\"reserveNetflow\":20}]}]}}") + .rspClass(AddDeviceRsp.class) + .rspCode(ErrorCode.ERR_LOGOUT) + .autoLogin(false) + .verifyCallback((VerifyProtoRespCallback) (v, e) -> { + Assert.assertNotNull(v); + Assert.assertNotNull(v.getMsgContent()); + Assert.assertEquals((long)v.getMsgContent().getStatus(), e.getCode()); + }) + .build() + }; + + /** + * Get test case qa test item [ ]. + * + * @return the qa test item [ ] + */ + public static QATestItem[] getTestCase() { + return testItemArray; + } +}