Merge remote-tracking branch 'origin/main'

This commit is contained in:
liqiang-fit2cloud 2023-04-28 18:05:51 +08:00
commit e26fe4c417
1 changed files with 102 additions and 69 deletions

View File

@ -41,6 +41,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -816,7 +817,7 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
} else if (mockEnvUpdateDTO.getBaseUrl().startsWith("https:")) { } else if (mockEnvUpdateDTO.getBaseUrl().startsWith("https:")) {
protocal = "https"; protocal = "https";
} }
ApiTestEnvironmentWithBLOBs updateModel = this.updateHttpMockEvn(model, protocal, mockEnvUpdateDTO.getBaseUrl()); ApiTestEnvironmentWithBLOBs updateModel = this.updateMockEvn(model, protocal, mockEnvUpdateDTO.getBaseUrl());
updateList.add(updateModel); updateList.add(updateModel);
} }
} }
@ -836,15 +837,56 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
} }
} }
private ApiTestEnvironmentWithBLOBs updateHttpMockEvn(ApiTestEnvironmentWithBLOBs mockEnv, String protocol, String newUrl) { private ApiTestEnvironmentWithBLOBs updateMockEvn(ApiTestEnvironmentWithBLOBs mockEnv, String protocol, String newUrl) {
ApiTestEnvironmentWithBLOBs updatedEnv = null;
if (mockEnv.getConfig() != null) { if (mockEnv.getConfig() != null) {
try { try {
com.alibaba.fastjson.JSONObject configObj = com.alibaba.fastjson.JSONObject.parseObject(mockEnv.getConfig()); JSONObject configObj = parseObject(mockEnv.getConfig());
if (configObj.containsKey("httpConfig")) { if (configObj.has("httpConfig")) {
com.alibaba.fastjson.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.has("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.has("server")) {
String url = newUrl;
if (url.startsWith("http://")) {
url = url.substring(7);
} else if (url.startsWith("https://")) {
url = url.substring(8);
}
String ipStr = url;
if (url.contains(":") && !url.endsWith(":")) {
String[] urlArr = url.split(":");
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.has("isMock") && httpObj.getBoolean("isMock")) {
if (httpObj.has("conditions")) {
String url = newUrl; String url = newUrl;
if (url.startsWith("http://")) { if (url.startsWith("http://")) {
url = url.substring(7); url = url.substring(7);
@ -865,12 +907,11 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
ipStr = urlArr[0]; ipStr = urlArr[0];
} }
} }
if (httpObj.containsKey("socket") && httpObj.containsKey("protocol") && httpObj.containsKey("port")) { if (httpObj.has("socket") && httpObj.has("protocol") && httpObj.has("port")) {
String httpSocket = httpObj.getString("socket"); String httpSocket = httpObj.optString("socket");
if (httpSocket.contains("/mock")) { if (httpSocket.contains("/api/mock")) {
String[] httpSocketArr = StringUtils.split(httpSocket, "/mock"); String[] httpSocketArr = StringUtils.split(httpSocket, "/api/mock");
httpSocketArr[0] = url; httpSocket = StringUtils.join(url, "/api/mock", httpSocketArr[1]);
httpSocket = StringUtils.join(httpSocketArr);
} }
httpObj.put("socket", httpSocket); httpObj.put("socket", httpSocket);
httpObj.put("protocol", protocol); httpObj.put("protocol", protocol);
@ -881,15 +922,14 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
} }
} }
com.alibaba.fastjson.JSONArray conditions = httpObj.getJSONArray("conditions"); JSONArray conditions = httpObj.getJSONArray("conditions");
for (int i = 0; i < conditions.size(); i++) { for (int i = 0; i < conditions.length(); i++) {
com.alibaba.fastjson.JSONObject httpItem = conditions.getJSONObject(i); JSONObject httpItem = conditions.getJSONObject(i);
if (httpItem.containsKey("socket") && httpItem.containsKey("protocol") && httpItem.containsKey("domain")) { if (httpItem.has("socket") && httpItem.has("protocol") && httpItem.has("domain")) {
String httpSocket = httpItem.getString("socket"); String httpSocket = httpItem.optString("socket");
if (httpSocket.contains("/mock")) { if (httpSocket.contains("/api/mock")) {
String[] httpSocketArr = StringUtils.split(httpSocket, "/mock"); String[] httpSocketArr = httpSocket.split("/api/mock");
httpSocketArr[0] = url; httpSocket = StringUtils.join(url, "/api/mock", httpSocketArr[1]);
httpSocket = StringUtils.join(httpSocketArr);
} }
httpItem.put("socket", httpSocket); httpItem.put("socket", httpSocket);
httpItem.put("domain", ipStr); httpItem.put("domain", ipStr);
@ -901,16 +941,9 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
} }
} }
} }
updatedEnv = mockEnv;
updatedEnv.setConfig(configObj.toJSONString());
} }
} }
} return httpObj;
} catch (Exception e) {
LogUtil.error(e);
}
}
return updatedEnv;
} }
public LinkedHashMap<String, List<String>> selectProjectNameAndEnvName(Map<String, List<String>> projectEnvIdMap) { public LinkedHashMap<String, List<String>> selectProjectNameAndEnvName(Map<String, List<String>> projectEnvIdMap) {