fix(Mock期望): 修改Api的响应时,自动更新Mock期望里【跟随API响应】的内容

修改Api的响应时,自动更新Mock期望里【跟随API响应】的内容
This commit is contained in:
song-tianyang 2021-10-27 16:38:49 +08:00 committed by BugKing
parent 2958ce1291
commit d8bf400e56
2 changed files with 32 additions and 0 deletions

View File

@ -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;
}

View File

@ -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<String, String> returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
if(MapUtils.isEmpty(returnMap) || !returnMap.containsKey("returnMsg")){
return;
}
List<MockExpectConfigWithBLOBs> 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){}
}
}
}
}
}