test: mock server 补充单测

This commit is contained in:
fit2-zhao 2024-07-11 14:12:55 +08:00 committed by Craftsman
parent b9303de3bd
commit a5156299df
2 changed files with 51 additions and 64 deletions

View File

@ -86,6 +86,7 @@ public class MockServerTestService {
if (fileIds != null) { if (fileIds != null) {
ApiFileResourceService apiFileResourceService = CommonBeanFactory.getBean(ApiFileResourceService.class); ApiFileResourceService apiFileResourceService = CommonBeanFactory.getBean(ApiFileResourceService.class);
// 验证文件的关联关系以及是否存入对象存储 // 验证文件的关联关系以及是否存入对象存储
assert apiFileResourceService != null;
List<ApiFileResource> apiFileResources = apiFileResourceService.getByResourceId(id); List<ApiFileResource> apiFileResources = apiFileResourceService.getByResourceId(id);
Assertions.assertEquals(apiFileResources.size(), fileIds.size()); Assertions.assertEquals(apiFileResources.size(), fileIds.size());
@ -114,6 +115,7 @@ public class MockServerTestService {
*/ */
public static void assertLinkFile(String id, List<String> fileIds) { public static void assertLinkFile(String id, List<String> fileIds) {
FileAssociationService fileAssociationService = CommonBeanFactory.getBean(FileAssociationService.class); FileAssociationService fileAssociationService = CommonBeanFactory.getBean(FileAssociationService.class);
assert fileAssociationService != null;
List<String> linkFileIds = fileAssociationService.getFiles(id) List<String> linkFileIds = fileAssociationService.getFiles(id)
.stream() .stream()
.map(FileInfo::getFileId) .map(FileInfo::getFileId)
@ -254,74 +256,62 @@ public class MockServerTestService {
public MockResponse genMockResponse(String returnType, int status, String valueKeyWord, String fileId, String fileName, ApiDefinitionBlob apiDefinitionBlob) { public MockResponse genMockResponse(String returnType, int status, String valueKeyWord, String fileId, String fileName, ApiDefinitionBlob apiDefinitionBlob) {
MockResponse mockResponse = new MockResponse(); MockResponse mockResponse = new MockResponse();
mockResponse.setStatusCode(status); mockResponse.setStatusCode(status);
if (apiDefinitionBlob != null) { if (apiDefinitionBlob != null) {
mockResponse.setUseApiResponse(true); mockResponse.setUseApiResponse(true);
List<HttpResponse> msHttpResponseList = JSON.parseArray(new String(apiDefinitionBlob.getResponse()), HttpResponse.class); List<HttpResponse> msHttpResponseList = JSON.parseArray(new String(apiDefinitionBlob.getResponse()), HttpResponse.class);
msHttpResponseList.forEach(item -> { msHttpResponseList.stream()
if (!item.isDefaultFlag()) { .filter(item -> !item.isDefaultFlag())
//特意使用非默认的响应 .findFirst()
mockResponse.setApiResponseId(item.getId()); .ifPresent(item -> mockResponse.setApiResponseId(item.getId()));
}
});
} else { } else {
ResponseBody body = new ResponseBody(); ResponseBody body = new ResponseBody();
switch (returnType) {
case "file": if ("file".equals(returnType) || "file-body".equals(returnType)) {
body.setBodyType(Body.BodyType.BINARY.name()); body.setBodyType(Body.BodyType.BINARY.name());
body.setBinaryBody(new ResponseBinaryBody() {{ body.setBinaryBody(new ResponseBinaryBody() {{
this.setSendAsBody(false); setSendAsBody(false);
this.setFile(new ApiFile()); setFile(new ApiFile());
this.getFile().setFileId(fileId); getFile().setFileId(fileId);
this.getFile().setFileName(fileName); getFile().setFileName(fileName);
}}); }});
break; } else if ("json".equals(returnType)) {
case "file-body":
body.setBodyType(Body.BodyType.BINARY.name());
body.setBinaryBody(new ResponseBinaryBody() {{
this.setSendAsBody(false);
this.setFile(new ApiFile());
this.getFile().setFileId(fileId);
this.getFile().setFileName(fileName);
}});
break;
case "json":
body.setBodyType(Body.BodyType.JSON.name()); body.setBodyType(Body.BodyType.JSON.name());
body.setJsonBody(new JsonBody() {{ body.setJsonBody(new JsonBody() {{
this.setJsonValue("{\"inputAge\":123, \"testKeyWord\":\"" + valueKeyWord + "\"}"); setJsonValue("{\"inputAge\":123, \"testKeyWord\":\"" + valueKeyWord + "\"}");
}}); }});
break; } else if ("xml".equals(returnType)) {
case "xml":
body.setBodyType(Body.BodyType.XML.name()); body.setBodyType(Body.BodyType.XML.name());
body.setXmlBody(new XmlBody() {{ body.setXmlBody(new XmlBody() {{
this.setValue("<xml>" + valueKeyWord + "</xml>"); setValue("<xml>" + valueKeyWord + "</xml>");
}}); }});
break; } else if ("raw".equals(returnType)) {
case "raw":
body.setBodyType(Body.BodyType.RAW.name()); body.setBodyType(Body.BodyType.RAW.name());
body.setRawBody(new RawBody() {{ body.setRawBody(new RawBody() {{
this.setValue("Raw body content:" + valueKeyWord); setValue("Raw body content:" + valueKeyWord);
}}); }});
break;
} }
mockResponse.setBody(body); mockResponse.setBody(body);
} }
List<MsHeader> headers = new ArrayList<>() {{ List<MsHeader> headers = List.of(
this.add(new MsHeader() {{ new MsHeader() {{
this.setKey("rspHeaderA"); this.setKey("rspHeaderA");
this.setValue("header-1"); this.setValue("header-1");
}}); }},
this.add(new MsHeader() {{ new MsHeader() {{
this.setKey("rspHeaderB"); this.setKey("rspHeaderB");
this.setValue("header-2"); this.setValue("header-2");
this.setEnable(false); this.setEnable(false);
}}); }},
this.add(new MsHeader() {{ new MsHeader() {{
this.setKey("rspHeaderC"); this.setKey("rspHeaderC");
this.setValue("header-3"); this.setValue("header-3");
}}); }}
}}; );
mockResponse.setHeaders(headers); mockResponse.setHeaders(headers);
return mockResponse; return mockResponse;
} }

View File

@ -34,7 +34,7 @@ public class WebSocketHandlerTest {
@Test @Test
@Order(1) @Order(1)
void testOpenSession() throws IOException { void testOpenSession() {
// 模拟参数 // 模拟参数
String reportId = "123"; String reportId = "123";
when(session.getAsyncRemote()).thenReturn(async); when(session.getAsyncRemote()).thenReturn(async);
@ -57,14 +57,11 @@ public class WebSocketHandlerTest {
// 调用被测试方法 // 调用被测试方法
webSocketHandler.onMessage(reportId, message); webSocketHandler.onMessage(reportId, message);
// 验证行为
// 这里可以添加更多的验证例如检查 sendMessageSingle 的调用情况等
} }
@Test @Test
@Order(3) @Order(3)
void testOnHeartbeatCheck() throws IOException { void testOnHeartbeatCheck() {
// 模拟参数 // 模拟参数
when(session.getAsyncRemote()).thenReturn(async); when(session.getAsyncRemote()).thenReturn(async);