From d8bf400e56a7157bd1742f6f76694105e570dd59 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 27 Oct 2021 16:38:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(Mock=E6=9C=9F=E6=9C=9B):=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9Api=E7=9A=84=E5=93=8D=E5=BA=94=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0Mock=E6=9C=9F=E6=9C=9B?= =?UTF-8?q?=E9=87=8C=E3=80=90=E8=B7=9F=E9=9A=8FAPI=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E3=80=91=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改Api的响应时,自动更新Mock期望里【跟随API响应】的内容 --- .../api/service/ApiDefinitionService.java | 2 ++ .../api/service/MockConfigService.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 6c94aec44c..b0c36c399a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -282,6 +282,8 @@ public class ApiDefinitionService { request.setMethod("dubbo://"); } ApiDefinitionWithBLOBs returnModel = updateTest(request); + MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class); + mockConfigService.updateMockReturnMsgByApi(returnModel); FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles); return returnModel; } diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index bbf42009e8..e75e33fe02 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -27,6 +27,7 @@ import io.metersphere.commons.utils.*; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.i18n.Translator; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.SqlSession; import org.springframework.stereotype.Service; @@ -1332,4 +1333,33 @@ public class MockConfigService { } } + + public void updateMockReturnMsgByApi(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs) { + if(apiDefinitionWithBLOBs == null){ + return; + } + Map returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse()); + if(MapUtils.isEmpty(returnMap) || !returnMap.containsKey("returnMsg")){ + return; + } + List updateList = this.selectMockExpectConfigByApiId(apiDefinitionWithBLOBs.getId()); + if(CollectionUtils.isNotEmpty(updateList)){ + for (MockExpectConfigWithBLOBs model: updateList) { + if(StringUtils.isNotEmpty(model.getResponse())){ + try { + JSONObject responseObj = JSONObject.parseObject(model.getResponse()); + if(responseObj.containsKey("responseResult")){ + JSONObject responseResultObject = responseObj.getJSONObject("responseResult"); + if(responseResultObject.containsKey("body")){ + responseResultObject.getJSONObject("body").put("apiRspRaw",returnMap.get("returnMsg")); + + model.setResponse(responseObj.toJSONString()); + mockExpectConfigMapper.updateByPrimaryKeySelective(model); + } + } + }catch (Exception e){} + } + } + } + } }