refactor(接口测试): 优化接口包含脚本的通知

This commit is contained in:
wxg0103 2023-05-19 18:07:56 +08:00 committed by fit2-zhao
parent b28c939ae8
commit a137fbc09a
3 changed files with 74 additions and 70 deletions

View File

@ -1109,17 +1109,17 @@ public class ElementUtil {
return false;
}
public static Map<String, String> scriptMap(String request) {
Map<String, String> map = new HashMap<>();
public static List<String> scriptList(String request) {
List<String> list = new ArrayList<>();
if (StringUtils.isBlank(request)) {
return map;
return list;
}
JSONObject element = JSONUtil.parseObject(request);
toMap(element.getJSONArray(ElementConstants.HASH_TREE), scriptList, map);
return map;
toList(element.getJSONArray(ElementConstants.HASH_TREE), scriptList, list);
return list;
}
private static void toMap(JSONArray hashTree, List<String> scriptList, Map<String, String> map) {
private static void toList(JSONArray hashTree, List<String> scriptList, List<String> list) {
for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.optJSONObject(i);
if (element == null) {
@ -1131,36 +1131,34 @@ public class ElementUtil {
elementTarget.remove(ElementConstants.HASH_TREE);
}
elementTarget.remove(MsHashTreeService.ACTIVE);
elementTarget.remove(MsHashTreeService.NAME);
map.put(StringUtils.join(element.optString(MsHashTreeService.ID),
element.optString(MsHashTreeService.INDEX)),
elementTarget.toString());
elementTarget.remove(MsHashTreeService.INDEX);
list.add(elementTarget.toString());
}
JSONArray jsrArray = element.optJSONArray(JSR);
if (jsrArray != null) {
for (int j = 0; j < jsrArray.length(); j++) {
JSONObject jsr223 = jsrArray.optJSONObject(j);
if (jsr223 != null) {
map.put(StringUtils.join(JSR, j),
jsr223.toString());
list.add(jsr223.toString());
}
}
}
if (element.has(ElementConstants.HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE);
toMap(elementJSONArray, scriptList, map);
toList(elementJSONArray, scriptList, list);
}
}
}
public static boolean isSend(Map<String, String> org, Map<String, String> target) {
public static boolean isSend(List<String> org, List<String> target) {
if (org.size() != target.size() && target.size() > 0) {
return true;
}
for (Map.Entry<String, String> entry : org.entrySet()) {
if (target.containsKey(entry.getKey()) && !StringUtils.equals(entry.getValue(), target.get(entry.getKey()))) {
return true;
}
List<String> diff = org.stream()
.filter(s -> !target.contains(s))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(diff)) {
return true;
}
return false;
}

View File

@ -1336,27 +1336,31 @@ public class ApiTestCaseService {
//检查并发送脚本审核的通知
@Async
public void checkAndSendReviewMessage(String id,
String name,
String projectId,
String title,
String resourceType,
String requestOrg,
String requestTarget) {
ProjectApplication reviewLoadTestScript = baseProjectApplicationService.getProjectApplication(
projectId, ProjectApplicationType.API_REVIEW_TEST_SCRIPT.name());
if (BooleanUtils.toBoolean(reviewLoadTestScript.getTypeValue())) {
ProjectApplication reviewerConfig = baseProjectApplicationService.getProjectApplication(
projectId, ProjectApplicationType.API_SCRIPT_REVIEWER.name());
if (StringUtils.isNotEmpty(reviewerConfig.getTypeValue()) &&
baseProjectService.isProjectMember(projectId, reviewerConfig.getTypeValue())) {
Map<String, String> org = ElementUtil.scriptMap(requestOrg);
Map<String, String> target = ElementUtil.scriptMap(requestTarget);
public void checkAndSendReviewMessage(
String id,
String name,
String projectId,
String title,
String resourceType,
String requestOrg,
String requestTarget) {
ProjectApplication scriptEnable = baseProjectApplicationService
.getProjectApplication(projectId, ProjectApplicationType.API_REVIEW_TEST_SCRIPT.name());
if (BooleanUtils.toBoolean(scriptEnable.getTypeValue())) {
ProjectApplication reviewer = baseProjectApplicationService
.getProjectApplication(projectId, ProjectApplicationType.API_SCRIPT_REVIEWER.name());
if (StringUtils.isNotEmpty(reviewer.getTypeValue()) &&
baseProjectService.isProjectMember(projectId, reviewer.getTypeValue())) {
List<String> org = ElementUtil.scriptList(requestOrg);
List<String> target = ElementUtil.scriptList(requestTarget);
boolean isSend = ElementUtil.isSend(org, target);
if (isSend) {
Notification notification = new Notification();
notification.setTitle(title);
notification.setOperator(SessionUtils.getUserId());
notification.setOperator(reviewer.getTypeValue());
notification.setOperation(NoticeConstants.Event.REVIEW);
notification.setResourceId(id);
notification.setResourceName(name);
@ -1364,7 +1368,7 @@ public class ApiTestCaseService {
notification.setType(NotificationConstants.Type.SYSTEM_NOTICE.name());
notification.setStatus(NotificationConstants.Status.UNREAD.name());
notification.setCreateTime(System.currentTimeMillis());
notification.setReceiver(reviewerConfig.getTypeValue());
notification.setReceiver(reviewer.getTypeValue());
notificationService.sendAnnouncement(notification);
}
}

View File

@ -1008,26 +1008,29 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
}
@Async
public void checkAndSendReviewMessage(String id,
String name,
String projectId,
String resourceType,
String requestOrg,
String requestTarget) {
ProjectApplication reviewLoadTestScript = baseProjectApplicationService.getProjectApplication(
projectId, ProjectApplicationType.API_REVIEW_TEST_SCRIPT.name());
if (BooleanUtils.toBoolean(reviewLoadTestScript.getTypeValue())) {
ProjectApplication reviewerConfig = baseProjectApplicationService.getProjectApplication(
projectId, ProjectApplicationType.API_SCRIPT_REVIEWER.name());
if (StringUtils.isNotEmpty(reviewerConfig.getTypeValue()) &&
baseProjectService.isProjectMember(projectId, reviewerConfig.getTypeValue())) {
Map<String, String> org = scriptMap(requestOrg);
Map<String, String> target = scriptMap(requestTarget);
public void checkAndSendReviewMessage(
String id,
String name,
String projectId,
String resourceType,
String requestOrg,
String requestTarget) {
ProjectApplication scriptEnable = baseProjectApplicationService
.getProjectApplication(projectId, ProjectApplicationType.API_REVIEW_TEST_SCRIPT.name());
if (BooleanUtils.toBoolean(scriptEnable.getTypeValue())) {
ProjectApplication reviewer = baseProjectApplicationService
.getProjectApplication(projectId, ProjectApplicationType.API_SCRIPT_REVIEWER.name());
if (StringUtils.isNotEmpty(reviewer.getTypeValue()) &&
baseProjectService.isProjectMember(projectId, reviewer.getTypeValue())) {
List<String> org = scriptList(requestOrg);
List<String> target = scriptList(requestTarget);
boolean isSend = isSend(org, target);
if (isSend) {
Notification notification = new Notification();
notification.setTitle("环境设置");
notification.setOperator(SessionUtils.getUserId());
notification.setOperator(reviewer.getTypeValue());
notification.setOperation(NoticeConstants.Event.REVIEW);
notification.setResourceId(id);
notification.setResourceName(name);
@ -1035,56 +1038,55 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
notification.setType(NotificationConstants.Type.SYSTEM_NOTICE.name());
notification.setStatus(NotificationConstants.Status.UNREAD.name());
notification.setCreateTime(System.currentTimeMillis());
notification.setReceiver(reviewerConfig.getTypeValue());
notification.setReceiver(reviewer.getTypeValue());
notificationService.sendAnnouncement(notification);
}
}
}
}
public static Map<String, String> scriptMap(String request) {
Map<String, String> map = new HashMap<>();
public static List<String> scriptList(String request) {
List<String> list = new ArrayList<>();
if (StringUtils.isNotBlank(request)){
Map<Object, Object> configMap = JSON.parseObject(request, Map.class);
JSONObject configObj = new JSONObject(configMap);
toMap(map, configObj, POST_STEP, PRE_STEP);
toMap(map, configObj, PRE, POST);
toList(list, configObj, POST_STEP, PRE_STEP);
toList(list, configObj, PRE, POST);
JSONObject object = configObj.optJSONObject(ASSERTIONS);
JSONArray jsrArray = object.optJSONArray(JSR);
if (jsrArray != null) {
for (int j = 0; j < jsrArray.length(); j++) {
JSONObject jsr223 = jsrArray.optJSONObject(j);
if (jsr223 != null) {
map.put(StringUtils.join(JSR, j),
jsr223.toString());
list.add(jsr223.optString(SCRIPT));
}
}
}
}
return map;
return list;
}
private static void toMap(Map<String, String> map, JSONObject configObj, String pre, String post) {
private static void toList(List<String> list, JSONObject configObj, String pre, String post) {
JSONObject preProcessor = configObj.optJSONObject(pre);
if (StringUtils.isNotBlank(preProcessor.optString(SCRIPT))) {
map.put(pre, preProcessor.optString(SCRIPT));
list.add(StringUtils.join(pre,preProcessor.optString(SCRIPT)));
}
JSONObject postProcessor = configObj.optJSONObject(post);
if (StringUtils.isNotBlank(postProcessor.optString(SCRIPT))) {
map.put(post, postProcessor.optString(SCRIPT));
list.add(StringUtils.join(post,postProcessor.optString(SCRIPT)));
}
}
public static boolean isSend(Map<String, String> orgMap, Map<String, String> targetMap) {
if (orgMap.size() != targetMap.size() &&
targetMap.size() > 0) {
public static boolean isSend(List<String> orgList, List<String> targetList) {
if (orgList.size() != targetList.size() && targetList.size() > 0) {
return true;
}
for (Map.Entry<String, String> entry : orgMap.entrySet()) {
if (targetMap.containsKey(entry.getKey()) && !StringUtils.equals(entry.getValue(), targetMap.get(entry.getKey()))) {
return true;
}
List<String> diff = orgList.stream()
.filter(s -> !targetList.contains(s))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(diff)) {
return true;
}
return false;
}