OCT 1. 修正代码语法检查
This commit is contained in:
parent
c64615f22b
commit
4425ba97db
|
@ -152,7 +152,7 @@ public class HelperUtils {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> Type createRespType(Class<?>[] c) {
|
private static Type createRespType(Class<?>[] c) {
|
||||||
if (c.length == 1) {
|
if (c.length == 1) {
|
||||||
return JsonParameterizedType.make(ProtocolResp.class,
|
return JsonParameterizedType.make(ProtocolResp.class,
|
||||||
new Type[] {c[0]},
|
new Type[] {c[0]},
|
||||||
|
@ -183,7 +183,7 @@ public class HelperUtils {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ProtocolResp<? extends BaseRespStatus> jsonGetProtocolResp(String jsonString,
|
public static ProtocolResp<? extends BaseRespStatus> jsonGetProtocolResp(String jsonString,
|
||||||
Class<?>[] subClass) throws JsonProcessingException {
|
Class<?>[] subClass) throws JsonProcessingException {
|
||||||
return OBJ_MAPPER.readValue(jsonString, new TypeReference<>() {
|
return OBJ_MAPPER.readValue(jsonString, new TypeReference<>() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -15,17 +15,14 @@ public class JsonParameterizedType implements ParameterizedType {
|
||||||
private JsonParameterizedType(Class<?> var1, Type[] var2, Type var3) {
|
private JsonParameterizedType(Class<?> var1, Type[] var2, Type var3) {
|
||||||
this.actualTypeArguments = var2;
|
this.actualTypeArguments = var2;
|
||||||
this.rawType = var1;
|
this.rawType = var1;
|
||||||
this.ownerType = (Type)(var3 != null ? var3 : var1.getDeclaringClass());
|
this.ownerType = (var3 != null ? var3 : var1.getDeclaringClass());
|
||||||
this.validateConstructorArguments();
|
this.validateConstructorArguments();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateConstructorArguments() {
|
private void validateConstructorArguments() {
|
||||||
TypeVariable[] var1 = this.rawType.getTypeParameters();
|
TypeVariable<?>[] var1 = this.rawType.getTypeParameters();
|
||||||
if (var1.length != this.actualTypeArguments.length) {
|
if (var1.length != this.actualTypeArguments.length) {
|
||||||
throw new MalformedParameterizedTypeException();
|
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) {
|
if (this.ownerType instanceof Class) {
|
||||||
var1.append(((Class)this.ownerType).getName());
|
var1.append(((Class)this.ownerType).getName());
|
||||||
} else {
|
} else {
|
||||||
var1.append(this.ownerType.toString());
|
var1.append(this.ownerType);
|
||||||
}
|
}
|
||||||
|
|
||||||
var1.append("$");
|
var1.append("$");
|
||||||
|
@ -90,8 +87,7 @@ public class JsonParameterizedType implements ParameterizedType {
|
||||||
Type[] var3 = this.actualTypeArguments;
|
Type[] var3 = this.actualTypeArguments;
|
||||||
int var4 = var3.length;
|
int var4 = var3.length;
|
||||||
|
|
||||||
for(int var5 = 0; var5 < var4; ++var5) {
|
for (Type var6 : var3) {
|
||||||
Type var6 = var3[var5];
|
|
||||||
if (!var2) {
|
if (!var2) {
|
||||||
var1.append(", ");
|
var1.append(", ");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
package com.cmhi.cf.tools;
|
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 com.jayway.jsonpath.JsonPath;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -38,160 +31,4 @@ public class ToolsTest {
|
||||||
log.info("{}: {}", i, UUID.randomUUID());
|
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<BasePageResultResp<OperationLogSummary>> 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 };
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,7 +89,7 @@ public abstract class TestBaseAuthentication {
|
||||||
headersThreadLocal.set(headers);
|
headersThreadLocal.set(headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T, K> Object performanceRestful(RequestMethod reqType, T reqObject, String urlPath, Class<?>[] subRespClass) throws Exception {
|
public <T> Object performanceRestful(RequestMethod reqType, T reqObject, String urlPath, Class<?>[] subRespClass) throws Exception {
|
||||||
MockHttpServletRequestBuilder build = createMockMvcBuilder(urlPath, reqType);
|
MockHttpServletRequestBuilder build = createMockMvcBuilder(urlPath, reqType);
|
||||||
|
|
||||||
if (Objects.nonNull(reqObject)) {
|
if (Objects.nonNull(reqObject)) {
|
||||||
|
|
|
@ -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<HttpHeaders> 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<LoginReq> 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<String> formEntity = new HttpEntity<>(HelperUtils.getJson(loginPro), headers);
|
|
||||||
//
|
|
||||||
// ResponseEntity<String> 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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue