From a5156299dfbafa42c9b2d571852d1127eb3ce939 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 11 Jul 2024 14:12:55 +0800 Subject: [PATCH] =?UTF-8?q?test:=20mock=20server=20=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/MockServerTestService.java | 108 ++++++++---------- .../api/socket/WebSocketHandlerTest.java | 7 +- 2 files changed, 51 insertions(+), 64 deletions(-) diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java b/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java index ef001bcaaa..f70ff2f3b4 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/service/MockServerTestService.java @@ -86,6 +86,7 @@ public class MockServerTestService { if (fileIds != null) { ApiFileResourceService apiFileResourceService = CommonBeanFactory.getBean(ApiFileResourceService.class); // 验证文件的关联关系,以及是否存入对象存储 + assert apiFileResourceService != null; List apiFileResources = apiFileResourceService.getByResourceId(id); Assertions.assertEquals(apiFileResources.size(), fileIds.size()); @@ -114,6 +115,7 @@ public class MockServerTestService { */ public static void assertLinkFile(String id, List fileIds) { FileAssociationService fileAssociationService = CommonBeanFactory.getBean(FileAssociationService.class); + assert fileAssociationService != null; List linkFileIds = fileAssociationService.getFiles(id) .stream() .map(FileInfo::getFileId) @@ -226,7 +228,7 @@ public class MockServerTestService { this.setBodyType(Body.BodyType.XML.name()); this.getXmlBody().setValue("input123"); }}); - } else if(StringUtils.equalsIgnoreCase(bodyParamType, "kv")) { + } else if (StringUtils.equalsIgnoreCase(bodyParamType, "kv")) { mockMatchRule.setBody(new BodyParamMatchRule() {{ this.setBodyType(Body.BodyType.FORM_DATA.name()); this.setFormDataBody(new MockFormDataBody() {{ @@ -254,74 +256,62 @@ public class MockServerTestService { public MockResponse genMockResponse(String returnType, int status, String valueKeyWord, String fileId, String fileName, ApiDefinitionBlob apiDefinitionBlob) { MockResponse mockResponse = new MockResponse(); mockResponse.setStatusCode(status); + if (apiDefinitionBlob != null) { mockResponse.setUseApiResponse(true); List msHttpResponseList = JSON.parseArray(new String(apiDefinitionBlob.getResponse()), HttpResponse.class); - msHttpResponseList.forEach(item -> { - if (!item.isDefaultFlag()) { - //特意使用非默认的响应 - mockResponse.setApiResponseId(item.getId()); - } - }); + msHttpResponseList.stream() + .filter(item -> !item.isDefaultFlag()) + .findFirst() + .ifPresent(item -> mockResponse.setApiResponseId(item.getId())); } else { ResponseBody body = new ResponseBody(); - switch (returnType) { - case "file": - 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 "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.setJsonBody(new JsonBody() {{ - this.setJsonValue("{\"inputAge\":123, \"testKeyWord\":\"" + valueKeyWord + "\"}"); - }}); - break; - case "xml": - body.setBodyType(Body.BodyType.XML.name()); - body.setXmlBody(new XmlBody() {{ - this.setValue("" + valueKeyWord + ""); - }}); - break; - case "raw": - body.setBodyType(Body.BodyType.RAW.name()); - body.setRawBody(new RawBody() {{ - this.setValue("Raw body content:" + valueKeyWord); - }}); - break; + + if ("file".equals(returnType) || "file-body".equals(returnType)) { + body.setBodyType(Body.BodyType.BINARY.name()); + body.setBinaryBody(new ResponseBinaryBody() {{ + setSendAsBody(false); + setFile(new ApiFile()); + getFile().setFileId(fileId); + getFile().setFileName(fileName); + }}); + } else if ("json".equals(returnType)) { + body.setBodyType(Body.BodyType.JSON.name()); + body.setJsonBody(new JsonBody() {{ + setJsonValue("{\"inputAge\":123, \"testKeyWord\":\"" + valueKeyWord + "\"}"); + }}); + } else if ("xml".equals(returnType)) { + body.setBodyType(Body.BodyType.XML.name()); + body.setXmlBody(new XmlBody() {{ + setValue("" + valueKeyWord + ""); + }}); + } else if ("raw".equals(returnType)) { + body.setBodyType(Body.BodyType.RAW.name()); + body.setRawBody(new RawBody() {{ + setValue("Raw body content:" + valueKeyWord); + }}); } + mockResponse.setBody(body); } - List headers = new ArrayList<>() {{ - this.add(new MsHeader() {{ - this.setKey("rspHeaderA"); - this.setValue("header-1"); - }}); - this.add(new MsHeader() {{ - this.setKey("rspHeaderB"); - this.setValue("header-2"); - this.setEnable(false); - }}); - this.add(new MsHeader() {{ - this.setKey("rspHeaderC"); - this.setValue("header-3"); - }}); - }}; + List headers = List.of( + new MsHeader() {{ + this.setKey("rspHeaderA"); + this.setValue("header-1"); + }}, + new MsHeader() {{ + this.setKey("rspHeaderB"); + this.setValue("header-2"); + this.setEnable(false); + }}, + new MsHeader() {{ + this.setKey("rspHeaderC"); + this.setValue("header-3"); + }} + ); mockResponse.setHeaders(headers); + return mockResponse; } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/socket/WebSocketHandlerTest.java b/backend/services/api-test/src/test/java/io/metersphere/api/socket/WebSocketHandlerTest.java index 2c92393915..977eff1bf2 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/socket/WebSocketHandlerTest.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/socket/WebSocketHandlerTest.java @@ -34,7 +34,7 @@ public class WebSocketHandlerTest { @Test @Order(1) - void testOpenSession() throws IOException { + void testOpenSession() { // 模拟参数 String reportId = "123"; when(session.getAsyncRemote()).thenReturn(async); @@ -57,14 +57,11 @@ public class WebSocketHandlerTest { // 调用被测试方法 webSocketHandler.onMessage(reportId, message); - - // 验证行为 - // 这里可以添加更多的验证,例如检查 sendMessageSingle 的调用情况等 } @Test @Order(3) - void testOnHeartbeatCheck() throws IOException { + void testOnHeartbeatCheck() { // 模拟参数 when(session.getAsyncRemote()).thenReturn(async);