fix(系统设置): 修复修改当前站点时没有更新tcp-mock环境的问题

修复修改当前站点时没有更新tcp-mock环境的问题
This commit is contained in:
song-tianyang 2023-04-25 11:57:24 +08:00 committed by 建国
parent d91db5fe43
commit 60121feefb
1 changed files with 99 additions and 70 deletions

View File

@ -38,6 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*; import java.util.*;
@Service @Service
@ -198,28 +199,62 @@ public class ApiTestEnvironmentService {
return returnModel; return returnModel;
} }
private ApiTestEnvironmentWithBLOBs updateHttpMockEvn(ApiTestEnvironmentWithBLOBs mockEnv, String protocol, String oldUrl, String newUrl) { private ApiTestEnvironmentWithBLOBs updateMockEvn(ApiTestEnvironmentWithBLOBs mockEnv, String protocol, String newUrl) {
ApiTestEnvironmentWithBLOBs updatedEnv = null;
if (mockEnv.getConfig() != null) { if (mockEnv.getConfig() != null) {
try { try {
JSONObject configObj = JSONObject.parseObject(mockEnv.getConfig()); JSONObject configObj = JSONObject.parseObject(mockEnv.getConfig());
if (configObj.containsKey("httpConfig")) { if (configObj.containsKey("httpConfig")) {
JSONObject httpObj = configObj.getJSONObject("httpConfig"); JSONObject httpObj = configObj.getJSONObject("httpConfig");
if (httpObj.containsKey("isMock") && httpObj.getBoolean("isMock")) { JSONObject httpObject = this.genHttpMockConfig(httpObj, newUrl, protocol);
if (httpObj.containsKey("conditions")) { configObj.put("httpConfig", httpObject);
}
if (configObj.containsKey("tcpConfig")) {
JSONObject tcpObj = configObj.getJSONObject("tcpConfig");
JSONObject tcpObject = this.genTcpMockConfig(tcpObj, newUrl);
configObj.put("tcpConfig", tcpObject);
}
mockEnv.setConfig(configObj.toString());
} catch (Exception e) {
LogUtil.error(e);
}
}
return mockEnv;
}
private JSONObject genTcpMockConfig(@NotNull JSONObject tcpObj, String newUrl) {
if (tcpObj.containsKey("server")) {
String url = newUrl; String url = newUrl;
String oldEnvUrl = oldUrl;
if (url.startsWith("http://")) { if (url.startsWith("http://")) {
url = url.substring(7); url = url.substring(7);
} else if (url.startsWith("https://")) { } else if (url.startsWith("https://")) {
url = url.substring(8); url = url.substring(8);
} }
if (oldEnvUrl.startsWith("http://")) { String ipStr = url;
oldEnvUrl = oldEnvUrl.substring(7); if (url.contains(":") && !url.endsWith(":")) {
} else if (oldEnvUrl.startsWith("https://")) { String[] urlArr = url.split(":");
oldEnvUrl = oldEnvUrl.substring(8); int port = -1;
try {
port = Integer.parseInt(urlArr[urlArr.length - 1]);
} catch (Exception e) {
}
if (port > -1) {
ipStr = urlArr[0];
}
}
tcpObj.put("server", ipStr);
}
return tcpObj;
} }
private JSONObject genHttpMockConfig(@NotNull JSONObject httpObj, String newUrl, String protocol) {
if (httpObj.containsKey("isMock") && httpObj.getBoolean("isMock")) {
if (httpObj.containsKey("conditions")) {
String url = newUrl;
if (url.startsWith("http://")) {
url = url.substring(7);
} else if (url.startsWith("https://")) {
url = url.substring(8);
}
String ipStr = url; String ipStr = url;
String portStr = ""; String portStr = "";
if (url.contains(":") && !url.endsWith(":")) { if (url.contains(":") && !url.endsWith(":")) {
@ -234,13 +269,13 @@ public class ApiTestEnvironmentService {
ipStr = urlArr[0]; ipStr = urlArr[0];
} }
} }
if (httpObj.containsKey("socket") && httpObj.containsKey("protocol") && httpObj.containsKey("port")) { if (httpObj.containsKey("socket") && httpObj.containsKey("protocol") && httpObj.containsKey("port")) {
String oldHttpItemSocket = httpObj.getString("socket"); String httpSocket = httpObj.getString("socket");
if (oldHttpItemSocket.contains(oldEnvUrl)) { if (httpSocket.contains("/mock")) {
oldHttpItemSocket = oldHttpItemSocket.replace(oldEnvUrl, url); String[] httpSocketArr = StringUtils.split(httpSocket, "/mock");
httpSocket = StringUtils.join(url, "/mock", httpSocketArr[1]);
} }
httpObj.put("socket", oldHttpItemSocket); httpObj.put("socket", httpSocket);
httpObj.put("protocol", protocol); httpObj.put("protocol", protocol);
if (StringUtils.isNotEmpty(portStr)) { if (StringUtils.isNotEmpty(portStr)) {
httpObj.put("port", portStr); httpObj.put("port", portStr);
@ -253,11 +288,12 @@ public class ApiTestEnvironmentService {
for (int i = 0; i < conditions.size(); i++) { for (int i = 0; i < conditions.size(); i++) {
JSONObject httpItem = conditions.getJSONObject(i); JSONObject httpItem = conditions.getJSONObject(i);
if (httpItem.containsKey("socket") && httpItem.containsKey("protocol") && httpItem.containsKey("domain")) { if (httpItem.containsKey("socket") && httpItem.containsKey("protocol") && httpItem.containsKey("domain")) {
String oldHttpItemSocket = httpItem.getString("socket"); String httpSocket = httpItem.getString("socket");
if (oldHttpItemSocket.contains(oldEnvUrl)) { if (httpSocket.contains("/mock")) {
oldHttpItemSocket = oldHttpItemSocket.replace(oldEnvUrl, url); String[] httpSocketArr = httpSocket.split("/mock");
httpSocket = StringUtils.join(url, "/mock", httpSocketArr[1]);
} }
httpItem.put("socket", oldHttpItemSocket); httpItem.put("socket", httpSocket);
httpItem.put("domain", ipStr); httpItem.put("domain", ipStr);
httpItem.put("protocol", protocol); httpItem.put("protocol", protocol);
if (StringUtils.isNotEmpty(portStr)) { if (StringUtils.isNotEmpty(portStr)) {
@ -267,16 +303,9 @@ public class ApiTestEnvironmentService {
} }
} }
} }
updatedEnv = mockEnv;
updatedEnv.setConfig(configObj.toJSONString());
} }
} }
} return httpObj;
} catch (Exception e) {
LogUtil.error(e);
}
}
return updatedEnv;
} }
private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String protocal, String projectId, String projectNumber, String name, String url) { private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String protocal, String projectId, String projectNumber, String name, String url) {
@ -505,7 +534,7 @@ public class ApiTestEnvironmentService {
} else if (mockEnvUpdateDTO.getBaseUrl().startsWith("https:")) { } else if (mockEnvUpdateDTO.getBaseUrl().startsWith("https:")) {
protocal = "https"; protocal = "https";
} }
ApiTestEnvironmentWithBLOBs updateModel = this.updateHttpMockEvn(model, protocal, mockEnvUpdateDTO.getOldBaseUrl(), mockEnvUpdateDTO.getBaseUrl()); ApiTestEnvironmentWithBLOBs updateModel = this.updateMockEvn(model, protocal, mockEnvUpdateDTO.getBaseUrl());
updateList.add(updateModel); updateList.add(updateModel);
} }
} }