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

View File

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

View File

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