From 4425ba97db50053eb0d0521debf8fcb7c70d50b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Mon, 5 Feb 2024 16:34:21 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E4=BF=AE=E6=AD=A3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cmhi/cf/misc/HelperUtils.java | 4 +- .../cmhi/cf/misc/JsonParameterizedType.java | 12 +- .../java/com/cmhi/cf/tools/ToolsTest.java | 163 ------------------ .../cf/common/TestBaseAuthentication.java | 2 +- .../authentication/TestCommonExtension.java | 89 ---------- 5 files changed, 7 insertions(+), 263 deletions(-) delete mode 100644 src/test/utest/com/cmhi/cf/common/authentication/TestCommonExtension.java diff --git a/src/main/java/com/cmhi/cf/misc/HelperUtils.java b/src/main/java/com/cmhi/cf/misc/HelperUtils.java index f9418ef..e24d7d6 100644 --- a/src/main/java/com/cmhi/cf/misc/HelperUtils.java +++ b/src/main/java/com/cmhi/cf/misc/HelperUtils.java @@ -152,7 +152,7 @@ public class HelperUtils { }); } - private static Type createRespType(Class[] c) { + private static Type createRespType(Class[] c) { if (c.length == 1) { return JsonParameterizedType.make(ProtocolResp.class, new Type[] {c[0]}, @@ -183,7 +183,7 @@ public class HelperUtils { return type; } - public static ProtocolResp jsonGetProtocolResp(String jsonString, + public static ProtocolResp jsonGetProtocolResp(String jsonString, Class[] subClass) throws JsonProcessingException { return OBJ_MAPPER.readValue(jsonString, new TypeReference<>() { @Override diff --git a/src/main/java/com/cmhi/cf/misc/JsonParameterizedType.java b/src/main/java/com/cmhi/cf/misc/JsonParameterizedType.java index 46cc931..9671d22 100644 --- a/src/main/java/com/cmhi/cf/misc/JsonParameterizedType.java +++ b/src/main/java/com/cmhi/cf/misc/JsonParameterizedType.java @@ -15,17 +15,14 @@ public class JsonParameterizedType implements ParameterizedType { private JsonParameterizedType(Class var1, Type[] var2, Type var3) { this.actualTypeArguments = var2; this.rawType = var1; - this.ownerType = (Type)(var3 != null ? var3 : var1.getDeclaringClass()); + this.ownerType = (var3 != null ? var3 : var1.getDeclaringClass()); this.validateConstructorArguments(); } private void validateConstructorArguments() { - TypeVariable[] var1 = this.rawType.getTypeParameters(); + TypeVariable[] var1 = this.rawType.getTypeParameters(); if (var1.length != this.actualTypeArguments.length) { throw new MalformedParameterizedTypeException(); - } else { - for(int var2 = 0; var2 < this.actualTypeArguments.length; ++var2) { - } } } @@ -71,7 +68,7 @@ public class JsonParameterizedType implements ParameterizedType { if (this.ownerType instanceof Class) { var1.append(((Class)this.ownerType).getName()); } else { - var1.append(this.ownerType.toString()); + var1.append(this.ownerType); } var1.append("$"); @@ -90,8 +87,7 @@ public class JsonParameterizedType implements ParameterizedType { Type[] var3 = this.actualTypeArguments; int var4 = var3.length; - for(int var5 = 0; var5 < var4; ++var5) { - Type var6 = var3[var5]; + for (Type var6 : var3) { if (!var2) { var1.append(", "); } diff --git a/src/test/java/com/cmhi/cf/tools/ToolsTest.java b/src/test/java/com/cmhi/cf/tools/ToolsTest.java index c0760b3..78f5330 100644 --- a/src/test/java/com/cmhi/cf/tools/ToolsTest.java +++ b/src/test/java/com/cmhi/cf/tools/ToolsTest.java @@ -1,16 +1,9 @@ package com.cmhi.cf.tools; -import com.cmhi.cf.misc.JsonParameterizedType; -import com.cmhi.cf.restapi.pojo.base.BasePageResultResp; -import com.cmhi.cf.restapi.pojo.po.OperationLogSummary; -import com.cmhi.cf.restapi.pojo.vo.ProtocolResp; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; -import java.lang.reflect.Type; import java.util.UUID; @Slf4j @@ -38,160 +31,4 @@ public class ToolsTest { log.info("{}: {}", i, UUID.randomUUID()); } } - - @Test - public void jsonToObject() throws Exception { - final String json = """ - { - "ver": 1, - "cryptoType": 0, - "timeStamp": 1707101107092, - "code": 200, - "msgContent": { - "items": { - "pageNumber": 1, - "pageSize": 10, - "totalPage": 1, - "totalRow": 7, - "items": [ - { - "id": 1, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 453, - "transmitDelay": 139, - "operationTime": "2024-02-05T02:23:57.589+00:00" - }, - { - "id": 3, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 464, - "transmitDelay": 128, - "operationTime": "2024-02-05T02:24:32.212+00:00" - }, - { - "id": 5, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 435, - "transmitDelay": 137, - "operationTime": "2024-02-05T02:25:39.344+00:00" - }, - { - "id": 7, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 470, - "transmitDelay": 129, - "operationTime": "2024-02-05T02:26:37.216+00:00" - }, - { - "id": 9, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 411, - "transmitDelay": 124, - "operationTime": "2024-02-05T02:39:48.214+00:00" - }, - { - "id": 11, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 468, - "transmitDelay": 124, - "operationTime": "2024-02-05T02:40:16.012+00:00" - }, - { - "id": 13, - "module": "系统安全模块", - "operationType": "认证/鉴权", - "operationStatus": "成功", - "description": "[admin] login", - "requestIp": "172.21.96.1", - "callFunction": "login", - "httpMethod": "POST", - "httpPath": "/api/login", - "userId": 1, - "expendTime": 473, - "transmitDelay": 121, - "operationTime": "2024-02-05T02:45:05.815+00:00" - } - ] - }, - "status": 0, - "message": [ - "成功" - ] - } - } - """; - - ProtocolResp> ret = null; - - JsonParameterizedType type = JsonParameterizedType.make(BasePageResultResp.class, - new java.lang.Class[] {OperationLogSummary.class}, - BasePageResultResp.class.getDeclaringClass()); - - ret = new ObjectMapper().readValue(json, new TypeReference<>() { - @Override - public Type getType() { - return JsonParameterizedType.make(ProtocolResp.class, - new java.lang.Class[] {(Class) type.getRawType()}, - ProtocolResp.class.getDeclaringClass()); - } - }); - - log.info("{}", ret); - - ret = new ObjectMapper().readValue(json, new TypeReference<>() { - }); - - log.info("{}", ret); - - Class[] classes = { String.class, Integer.class, Double.class }; - } - } diff --git a/src/test/utest/com/cmhi/cf/common/TestBaseAuthentication.java b/src/test/utest/com/cmhi/cf/common/TestBaseAuthentication.java index 92ed459..5308e47 100644 --- a/src/test/utest/com/cmhi/cf/common/TestBaseAuthentication.java +++ b/src/test/utest/com/cmhi/cf/common/TestBaseAuthentication.java @@ -89,7 +89,7 @@ public abstract class TestBaseAuthentication { headersThreadLocal.set(headers); } - public Object performanceRestful(RequestMethod reqType, T reqObject, String urlPath, Class[] subRespClass) throws Exception { + public Object performanceRestful(RequestMethod reqType, T reqObject, String urlPath, Class[] subRespClass) throws Exception { MockHttpServletRequestBuilder build = createMockMvcBuilder(urlPath, reqType); if (Objects.nonNull(reqObject)) { diff --git a/src/test/utest/com/cmhi/cf/common/authentication/TestCommonExtension.java b/src/test/utest/com/cmhi/cf/common/authentication/TestCommonExtension.java deleted file mode 100644 index 5454d41..0000000 --- a/src/test/utest/com/cmhi/cf/common/authentication/TestCommonExtension.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.cmhi.cf.common.authentication; - -import com.cmhi.cf.authentication.pojo.dto.LoginReq; -import com.cmhi.cf.misc.HelperUtils; -import com.cmhi.cf.restapi.config.ProtocolConfigure; -import com.cmhi.cf.restapi.pojo.dto.ProtocolReq; -import jakarta.annotation.Resource; -import lombok.Getter; -import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.ResultMatcher; - -import static org.springframework.test.web.client.match.MockRestRequestMatchers.content; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@ExtendWith({SpringExtension.class}) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -@AutoConfigureMockMvc -public class TestCommonExtension implements BeforeEachCallback { - @Getter - private static final TestRestTemplate restTemplate = new TestRestTemplate(); - private static final ThreadLocal headersThreadLocal = new ThreadLocal<>(); - - @Resource - private MockMvc mockMvc; - - public static HttpHeaders getHeaders() { - return headersThreadLocal.get(); - } - - - @Override - public void beforeEach(ExtensionContext extensionContext) throws Exception { - HttpHeaders headers = new HttpHeaders(); - MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); - headers.setContentType(type); - headers.add("Accept", MediaType.APPLICATION_JSON.toString()); - //headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br"); - - LoginReq logReq = LoginReq.builder() - .username("admin") - .password("8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918") - .build(); - - ProtocolReq loginPro = new ProtocolReq<>(); - loginPro.setMsgContent(logReq); - loginPro.setVer(1); - loginPro.setTimeStamp(System.currentTimeMillis()); - loginPro.setCryptoType(ProtocolConfigure.SECURITY_PROTOCOL_TYPE); - - MvcResult mvcResult = mockMvc.perform(post("/api/login") - .contentType(MediaType.APPLICATION_JSON) // Set the content type to JSON - .content(HelperUtils.getJson(loginPro))) // Pass the JSON request body - .andExpect(status().isOk()) // Expect that the status is 200 OK - .andExpect((ResultMatcher) content().contentType( - MediaType.APPLICATION_JSON)) // Optional: Expect JSON response - .andReturn(); - -// HttpEntity formEntity = new HttpEntity<>(HelperUtils.getJson(loginPro), headers); -// -// ResponseEntity response = restTemplate.postForEntity(CommonConfigure.BASEURL + "/api/login", formEntity, String.class); -// Assertions.assertNotNull(response); -// -// ProtocolResp resp = HelperUtils.jsonGetProtocolResp(response.getBody(), LoginResp.class); -// Assertions.assertNotNull(resp); -// Assertions.assertNotNull(resp.getMsgContent()); -// Assertions.assertEquals(resp.getCode(), HttpStatus.OK.value()); -// -// LoginResp loginResp = (LoginResp) resp.getMsgContent(); -// -// Assertions.assertEquals(loginResp.getStatus(), ErrorCode.ERR_OK.getCode()); -// Assertions.assertNotNull(loginResp.getToken()); -// Assertions.assertNotEquals(loginResp.getToken().length(), 0); -// -// headers.setBearerAuth(loginResp.getToken()); -// - headersThreadLocal.set(headers); - } -}