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 14, 2024
1 parent ef6aa37 commit 59c7291
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
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;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -25,6 +28,9 @@ public class ComparisonScriptConfigurableController extends
@Resource
ComparisonScriptConfigurableHandler scriptConfigurableHandler;

@Resource
ComparisonScriptContentHandler scriptContentHandler;

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

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


@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 func_%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,22 @@
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 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 +39,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 +66,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 +164,24 @@ 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();
scriptContentInfo.setAliasName(item.getAliasName());

String funcName = item.getId();
scriptContentInfo.setFunctionName("func_" + funcName);

String scriptContent = item.getContent();
scriptContentInfo.setScriptContent(String.format(compareTemplate, funcName, 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<String> queryAllScriptMethodNames() {
return comparisonScriptContentRepository.queryAllScriptMethodNames();
}

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,6 +18,8 @@
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.ComparisonScriptConfiguration.ScriptMethod;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonSummaryConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonTransformConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.QueryConfigOfCategoryRequestType;
Expand All @@ -30,6 +31,7 @@
import com.arextest.web.model.dto.AppContractDto;
import com.arextest.web.model.enums.ContractTypeEnum;
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 +46,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 +77,9 @@ public class ComparisonSummaryService {
@Resource
ConfigLoadService configLoadService;

@Resource
ComparisonScriptConfigurableHandler comparisonScriptConfigurableHandler;

public ComparisonSummaryService(
@Autowired ComparisonExclusionsConfigurableHandler exclusionsConfigurableHandler,
@Autowired ComparisonInclusionsConfigurableHandler inclusionsConfigurableHandler,
Expand Down Expand Up @@ -105,6 +109,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 +265,22 @@ protected void getComparisonReferenceConfiguration(String interfaceId,
comparisonSummaryConfiguration.setReferenceMap(referenceMap);
}

protected void getComparisonScriptConfiguration(String interfaceId,
ComparisonSummaryConfiguration comparisonSummaryConfiguration) {
Map<List<String>, ScriptMethod> 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())) {
scriptMethodMap.put(item.getNodePath(), item.getScriptMethod());
}
});
comparisonSummaryConfiguration.setScriptMethodMap(scriptMethodMap);
}

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

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

buildComparisonConfig(replayConfigurationMap,
comparisonScriptConfigurableHandler.useResultAsList(appId),
(configurations, summaryConfiguration) -> {
Map<List<String>, ScriptMethod> scriptMethodMap = configurations.stream()
.filter(item -> CollectionUtils.isNotEmpty(item.getNodePath()))
.collect(Collectors.toMap(ComparisonScriptConfiguration::getNodePath,
ComparisonScriptConfiguration::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,55 @@
package com.arextest.web.core.repository.mongo;

import com.arextest.web.model.dao.mongodb.ComparisonScriptContentCollection;
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<String> queryAllScriptMethodNames() {
Query query = new Query();
query.fields().include(ComparisonScriptContentCollection.Fields.aliasName);
List<ComparisonScriptContentCollection> daos = mongoTemplate.find(query,
ComparisonScriptContentCollection.class);

if (CollectionUtils.isNotEmpty(daos)) {
return daos.stream()
.map(ComparisonScriptContentCollection::getAliasName)
.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,14 @@ public class SystemConfigWithProperties extends SystemConfiguration {
* ignore the compare of uuid
*/
private Boolean ipIgnore;

private List<ScriptContentInfo> scriptContentInfos;

@Data
public static class ScriptContentInfo {

private String aliasName;
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 @@ -2,6 +2,7 @@

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.ComparisonScriptConfiguration.ScriptMethod;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand Down Expand Up @@ -64,6 +65,10 @@ public class ComparisonSummaryConfiguration {

private List<TransformDetail> transformDetails;

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

private Map<String, Object> additionalConfig;

private static class MapKeyDeserializerUtils extends KeyDeserializer {
Expand Down
Loading

0 comments on commit 59c7291

Please sign in to comment.