Skip to content

Commit

Permalink
feat: add the logic about ScriptContent
Browse files Browse the repository at this point in the history
  • Loading branch information
rchen9 committed Nov 18, 2024
1 parent 51eaddb commit d59b51d
Show file tree
Hide file tree
Showing 15 changed files with 320 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.arextest.common.utils.ResponseUtils;
import com.arextest.web.core.business.config.ConfigurableHandler;
import com.arextest.web.core.business.config.replay.ComparisonScriptConfigurableHandler;
import com.arextest.web.core.business.config.replay.ComparisonScriptContentHandler;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonScriptConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonScriptContentRequestType;
import com.arextest.web.model.contract.contracts.config.replay.PageQueryComparisonRequestType;
import com.arextest.web.model.contract.contracts.config.replay.QueryComparisonRequestType;
import jakarta.annotation.Resource;
Expand All @@ -26,6 +28,9 @@ public class ComparisonScriptConfigurableController extends
@Resource
ComparisonScriptConfigurableHandler scriptConfigurableHandler;

@Resource
ComparisonScriptContentHandler scriptContentHandler;

protected ComparisonScriptConfigurableController(
ConfigurableHandler<ComparisonScriptConfiguration> configurableHandler) {
super(configurableHandler);
Expand Down Expand Up @@ -68,5 +73,30 @@ public Response pageQueryComparisonConfig(@RequestBody PageQueryComparisonReques
);
}

@GetMapping("/queryScriptMethodNames")
@ResponseBody
public Response queryAllScriptMethod() {
return ResponseUtils.successResponse(
scriptContentHandler.queryScriptMethodNames()
);
}

@PostMapping("/saveScriptContent")
@ResponseBody
public Response saveScriptContent(
@RequestBody ComparisonScriptContentRequestType scriptContentRequestType) {
return ResponseUtils.successResponse(
scriptContentHandler.saveScriptContent(scriptContentRequestType)
);
}

@GetMapping("/queryAllScriptContent")
@ResponseBody
public Response queryAllScriptContent() {
return ResponseUtils.successResponse(
scriptContentHandler.queryAll()
);
}


}
1 change: 1 addition & 0 deletions arex-web-api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ arex:
uuidIgnore: true
ipIgnore: true
ignoreNodeSet: ""
scriptTemplate: "function %s(context, baseValue, testValue, arg) {%s}"
# arex sending email domain
email:
domain: https://mail.arextest.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
import com.arextest.config.model.dto.system.SystemConfiguration;
import com.arextest.config.repository.SystemConfigurationRepository;
import com.arextest.diff.service.DecompressService;
import com.arextest.web.core.business.config.replay.ComparisonScriptContentHandler;
import com.arextest.web.model.contract.contracts.config.SystemConfigWithProperties;
import com.arextest.web.model.contract.contracts.config.SystemConfigWithProperties.ScriptContentInfo;
import com.arextest.web.model.dto.config.ComparisonScriptContent;
import com.arextest.web.model.mapper.ScriptInfoMapper;
import jakarta.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
Expand All @@ -34,6 +40,12 @@ public class SystemConfigurationService {
@Resource
private ConfigLoadService configLoadService;

@Resource
private ComparisonScriptContentHandler comparisonScriptContentHandler;

@Value("${arex.compare.scriptTemplate}")
private String compareTemplate;

public boolean saveConfig(SystemConfiguration systemConfiguration) {
List<SystemConfiguration> systemConfigurations = new ArrayList<>();
List<String> removeKeys = new ArrayList<>();
Expand All @@ -55,7 +67,8 @@ public boolean saveConfig(SystemConfiguration systemConfiguration) {
removeKeys.add(SystemConfigurationCollection.KeySummary.DESERIALIZATION_JAR);
}
if (systemConfiguration.getComparePluginInfo() != null
&& StringUtils.isNotBlank(systemConfiguration.getComparePluginInfo().getComparePluginUrl())) {
&& StringUtils.isNotBlank(
systemConfiguration.getComparePluginInfo().getComparePluginUrl())) {
SystemConfiguration comparePluginInfoConfig = new SystemConfiguration();
ComparePluginInfo comparePluginInfo = systemConfiguration.getComparePluginInfo();
comparePluginInfo.setTransMethodList(identifyTransformMethod(comparePluginInfo));
Expand Down Expand Up @@ -152,6 +165,26 @@ private SystemConfigWithProperties appendGlobalCompareConfig(ConfigLoadService c
} catch (RuntimeException e) {
LOGGER.error("getCompareIgnoredTimePrecisionMillis error", e);
}

List<ComparisonScriptContent> comparisonScriptContents = comparisonScriptContentHandler.queryAll();
if (CollectionUtils.isNotEmpty(comparisonScriptContents)) {
List<ScriptContentInfo> scriptContentInfos = new ArrayList<>();
for (ComparisonScriptContent item : comparisonScriptContents) {
ScriptContentInfo scriptContentInfo = new ScriptContentInfo();
String functionId = item.getId();
scriptContentInfo.setFunctionName(
ScriptInfoMapper.INSTANCE.functionIdToFunctionName(functionId)
);

String scriptContent = item.getContent();
scriptContentInfo.setScriptContent(
String.format(compareTemplate, scriptContentInfo.getFunctionName(), scriptContent)
);
scriptContentInfos.add(scriptContentInfo);
}
systemConfigWithProperties.setScriptContentInfos(scriptContentInfos);
}

return systemConfigWithProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,4 @@ private List<PageQueryComparisonResponseType.ScriptInfo> contractFromDto(
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.arextest.web.core.business.config.replay;

import com.arextest.web.core.repository.mongo.ComparisonScriptContentRepository;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonScriptContentRequestType;
import com.arextest.web.model.dto.config.ComparisonScriptContent;
import com.arextest.web.model.mapper.ConfigComparisonScriptContentMapper;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class ComparisonScriptContentHandler {


private final ComparisonScriptContentRepository comparisonScriptContentRepository;

public List<ComparisonScriptContent> queryScriptMethodNames() {
return comparisonScriptContentRepository.queryScriptMethodNames();
}

public boolean saveScriptContent(ComparisonScriptContentRequestType request) {
ComparisonScriptContent dto =
ConfigComparisonScriptContentMapper.INSTANCE.dtoFromContract(request);
return comparisonScriptContentRepository.save(dto);
}

public List<ComparisonScriptContent> queryAll() {
return comparisonScriptContentRepository.queryAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.arextest.web.core.business.config.ConfigurableHandler;
import com.arextest.web.core.business.config.application.ApplicationOperationConfigurableHandler;
import com.arextest.web.core.repository.AppContractRepository;
import com.arextest.web.model.contract.contracts.common.enums.ExpirationType;
import com.arextest.web.model.contract.contracts.compare.CategoryDetail;
import com.arextest.web.model.contract.contracts.compare.TransformDetail;
import com.arextest.web.model.contract.contracts.config.replay.AbstractComparisonDetailsConfiguration;
Expand All @@ -19,7 +18,9 @@
import com.arextest.web.model.contract.contracts.config.replay.ComparisonInclusionsConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonListSortConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonReferenceConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonScriptConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonSummaryConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonSummaryConfiguration.ReplayScriptMethod;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonTransformConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.QueryConfigOfCategoryRequestType;
import com.arextest.web.model.contract.contracts.config.replay.QueryConfigOfCategoryResponseType;
Expand All @@ -29,7 +30,9 @@
import com.arextest.web.model.dao.mongodb.AppContractCollection;
import com.arextest.web.model.dto.AppContractDto;
import com.arextest.web.model.enums.ContractTypeEnum;
import com.arextest.web.model.mapper.ScriptInfoMapper;
import com.google.common.collect.ImmutableMap;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -44,7 +47,6 @@
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -76,6 +78,9 @@ public class ComparisonSummaryService {
@Resource
ConfigLoadService configLoadService;

@Resource
ComparisonScriptConfigurableHandler comparisonScriptConfigurableHandler;

public ComparisonSummaryService(
@Autowired ComparisonExclusionsConfigurableHandler exclusionsConfigurableHandler,
@Autowired ComparisonInclusionsConfigurableHandler inclusionsConfigurableHandler,
Expand Down Expand Up @@ -105,6 +110,7 @@ public ComparisonSummaryConfiguration getComparisonDetailsSummary(String interfa
getComparisonInclusionsConfiguration(interfaceId, comparisonSummaryConfiguration);
getComparisonListSortConfiguration(interfaceId, comparisonSummaryConfiguration);
getComparisonReferenceConfiguration(interfaceId, comparisonSummaryConfiguration);
getComparisonScriptConfiguration(interfaceId, comparisonSummaryConfiguration);
getAdditionalComparisonConfiguration(interfaceId, comparisonSummaryConfiguration);
return comparisonSummaryConfiguration;
}
Expand Down Expand Up @@ -260,6 +266,24 @@ protected void getComparisonReferenceConfiguration(String interfaceId,
comparisonSummaryConfiguration.setReferenceMap(referenceMap);
}

protected void getComparisonScriptConfiguration(String interfaceId,
ComparisonSummaryConfiguration comparisonSummaryConfiguration) {
Map<List<String>, ReplayScriptMethod> scriptMethodMap = new HashMap<>();
List<ComparisonScriptConfiguration> comparisonScriptConfigurationList =
comparisonScriptConfigurableHandler.queryByInterfaceId(interfaceId);
comparisonScriptConfigurableHandler.removeDetailsExpired(
comparisonScriptConfigurationList, true);
Optional.ofNullable(comparisonScriptConfigurationList).orElse(Collections.emptyList())
.forEach(item -> {
if (CollectionUtils.isNotEmpty(item.getNodePath())) {
ReplayScriptMethod scriptMethodInfo = ScriptInfoMapper.INSTANCE.toScriptMethodInfo(
item.getScriptMethod());
scriptMethodMap.put(item.getNodePath(), scriptMethodInfo);
}
});
comparisonSummaryConfiguration.setScriptMethodMap(scriptMethodMap);
}

protected void getAdditionalComparisonConfiguration(String interfaceId,
ComparisonSummaryConfiguration comparisonSummaryConfiguration) {
}
Expand Down Expand Up @@ -339,6 +363,7 @@ protected Map<String, ReplayCompareConfig.ReplayComparisonItem> buildMultiConfig
}));
summaryConfiguration.setReferenceMap(operationReferenceMap);
}, operationInfoMap, appContractDtoMap);

buildComparisonConfig(replayConfigurationMap,
transformConfigurableHandler.useResultAsList(appId),
(configurations, summaryConfiguration) -> {
Expand All @@ -348,6 +373,21 @@ protected Map<String, ReplayCompareConfig.ReplayComparisonItem> buildMultiConfig
summaryConfiguration.setTransformDetails(transformDetails);
}, operationInfoMap, appContractDtoMap);

buildComparisonConfig(replayConfigurationMap,
comparisonScriptConfigurableHandler.useResultAsList(appId),
(configurations, summaryConfiguration) -> {
Map<List<String>, ReplayScriptMethod> scriptMethodMap = configurations.stream()
.filter(item -> CollectionUtils.isNotEmpty(item.getNodePath()))
.collect(Collectors.toMap(ComparisonScriptConfiguration::getNodePath,
item -> ScriptInfoMapper.INSTANCE.toScriptMethodInfo(item.getScriptMethod()),
(r1, r2) -> {
LogUtils.warn(LOGGER, "scriptMethod duplicate key",
ImmutableMap.of("appId", appId));
return r2;
}));
summaryConfiguration.setScriptMethodMap(scriptMethodMap);
}, operationInfoMap, appContractDtoMap);

return replayConfigurationMap;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.arextest.web.core.repository.mongo;

import com.arextest.web.model.dao.mongodb.ComparisonScriptContentCollection;
import com.arextest.web.model.dao.mongodb.ComparisonScriptContentCollection.Fields;
import com.arextest.web.model.dto.config.ComparisonScriptContent;
import com.arextest.web.model.mapper.ConfigComparisonScriptContentMapper;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class ComparisonScriptContentRepository {

private final MongoTemplate mongoTemplate;


public List<ComparisonScriptContent> queryScriptMethodNames() {
Query query = new Query();
query.fields().exclude(Fields.content);
List<ComparisonScriptContentCollection> daos = mongoTemplate.find(query,
ComparisonScriptContentCollection.class);

if (CollectionUtils.isNotEmpty(daos)) {
return daos.stream()
.map(ConfigComparisonScriptContentMapper.INSTANCE::dtoFromDao)
.collect(Collectors.toList());
}
return Collections.emptyList();
}

public boolean save(ComparisonScriptContent dto) {
ComparisonScriptContentCollection dao =
ConfigComparisonScriptContentMapper.INSTANCE.daoFromDto(dto);
mongoTemplate.save(dao);
return true;
}

public List<ComparisonScriptContent> queryAll() {
List<ComparisonScriptContentCollection> daos = mongoTemplate.findAll(
ComparisonScriptContentCollection.class);

if (CollectionUtils.isNotEmpty(daos)) {
return daos.stream()
.map(ConfigComparisonScriptContentMapper.INSTANCE::dtoFromDao)
.collect(Collectors.toList());
}
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.arextest.web.model.contract.contracts.config;

import com.arextest.config.model.dto.system.SystemConfiguration;
import java.util.List;
import java.util.Set;
import lombok.Data;
import lombok.ToString;
Expand Down Expand Up @@ -50,4 +51,12 @@ public class SystemConfigWithProperties extends SystemConfiguration {
* ignore the compare of uuid
*/
private Boolean ipIgnore;

private List<ScriptContentInfo> scriptContentInfos;

@Data
public static class ScriptContentInfo {
private String functionName;
private String scriptContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.arextest.web.model.contract.contracts.config.replay;

import lombok.Data;

@Data
public class ComparisonScriptContentRequestType {

private String id;

private String aliasName;

private String content;

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class ComparisonSummaryConfiguration {

private List<TransformDetail> transformDetails;

@JsonDeserialize(keyUsing = MapKeyDeserializerUtils.class)
@JsonSerialize(keyUsing = MapKeySerializerUtils.class)
private Map<List<String>, ReplayScriptMethod> scriptMethodMap;

private Map<String, Object> additionalConfig;

private static class MapKeyDeserializerUtils extends KeyDeserializer {
Expand All @@ -87,4 +91,12 @@ public void serialize(List<String> stringList, JsonGenerator jsonGenerator,
jsonGenerator.writeFieldName(string);
}
}

@Data
public static class ReplayScriptMethod {

private String functionName;
private String functionArgs;
}

}
Loading

0 comments on commit d59b51d

Please sign in to comment.