Skip to content

Commit

Permalink
feat: support root_transform
Browse files Browse the repository at this point in the history
  • Loading branch information
rchen9 committed Jun 27, 2024
1 parent ff49de6 commit 9d3dc97
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.arextest.web.api.service.controller.config;

import com.arextest.common.annotation.AppAuth;
import com.arextest.common.model.response.Response;
import com.arextest.common.utils.ResponseUtils;
import com.arextest.config.model.dto.ModifyType;
import com.arextest.web.core.business.config.replay.ComparisonTransformConfigurableHandler;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonRootTransformConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonTransformConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.QueryComparisonRequestType;
import com.arextest.web.model.mapper.ConfigComparisonTransformMapper;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/config/comparison/rootTransform")
public class ComparisonRootTransformController {

@Resource
ComparisonTransformConfigurableHandler comparisonTransformConfigurableHandler;


@PostMapping("/queryComparisonConfig")
@ResponseBody
public Response queryComparisonConfig(@RequestBody QueryComparisonRequestType request) {
List<ComparisonTransformConfiguration> configs = comparisonTransformConfigurableHandler.
queryComparisonConfig(request.getAppId(), request.getOperationId(),
request.getOperationType(), request.getOperationName());
if (Boolean.TRUE.equals(request.getFilterExpired()) && CollectionUtils.isNotEmpty(configs)) {
configs.removeIf(
config -> comparisonTransformConfigurableHandler.removeDetailsExpired(config));
}
List<ComparisonRootTransformConfiguration> rootTransformConfigurations = configs.stream()
.map(ConfigComparisonTransformMapper.INSTANCE::requestTypeFromDto).filter(item -> {
if (item.getTransformMethodName() != null) {
return true;
}
return false;
}).collect(Collectors.toList());
return ResponseUtils.successResponse(rootTransformConfigurations);
}


@PostMapping("/modify/{modifyType}")
@ResponseBody
@AppAuth
public Response modify(@PathVariable ModifyType modifyType,
@RequestBody ComparisonRootTransformConfiguration configuration)
throws Exception {
if (modifyType == ModifyType.INSERT) {
configuration.validParameters();
ComparisonTransformConfiguration transformConfiguration =
ConfigComparisonTransformMapper.INSTANCE.dotFromRequestType(configuration);
return ResponseUtils.successResponse(
comparisonTransformConfigurableHandler.insert(transformConfiguration));
}
if (modifyType == ModifyType.UPDATE) {
ComparisonTransformConfiguration transformConfiguration =
ConfigComparisonTransformMapper.INSTANCE.dotFromRequestType(configuration);
return ResponseUtils.successResponse(
comparisonTransformConfigurableHandler.update(transformConfiguration));
}
if (modifyType == ModifyType.REMOVE) {
ComparisonTransformConfiguration transformConfiguration =
ConfigComparisonTransformMapper.INSTANCE.dotFromRequestType(configuration);
return ResponseUtils.successResponse(
comparisonTransformConfigurableHandler.remove(transformConfiguration));
}
return ResponseUtils.resourceNotFoundResponse();
}


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

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

@Data
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class ComparisonRootTransformConfiguration extends AbstractComparisonDetailsConfiguration {

private String transformMethodName;

@Override
public void validParameters() throws Exception {
super.validParameters();
if (StringUtils.isEmpty(transformMethodName)) {
throw new Exception("transformMethodName cannot be empty");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.arextest.web.model.mapper;

import com.arextest.web.model.contract.contracts.compare.TransformDetail;
import com.arextest.web.model.contract.contracts.compare.TransformDetail.TransformMethod;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonRootTransformConfiguration;
import com.arextest.web.model.contract.contracts.config.replay.ComparisonTransformConfiguration;
import com.arextest.web.model.dao.mongodb.ConfigComparisonTransformCollection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

/**
Expand All @@ -16,6 +23,8 @@ public interface ConfigComparisonTransformMapper {
ConfigComparisonTransformMapper INSTANCE = Mappers.getMapper(
ConfigComparisonTransformMapper.class);

String ROOT_NODE_PATH = "arex_root";

@Mappings({@Mapping(target = "modifiedTime",
expression = "java(dao.getDataChangeUpdateTime() == null ? null : new java.sql.Timestamp(dao.getDataChangeUpdateTime()))")})
ComparisonTransformConfiguration dtoFromDao(ConfigComparisonTransformCollection dao);
Expand All @@ -24,4 +33,39 @@ public interface ConfigComparisonTransformMapper {
@Mapping(target = "dataChangeCreateTime", expression = "java(System.currentTimeMillis())"),
@Mapping(target = "dataChangeUpdateTime", expression = "java(System.currentTimeMillis())")})
ConfigComparisonTransformCollection daoFromDto(ComparisonTransformConfiguration dto);

@Mappings({
@Mapping(target = "transformDetail", source = "transformMethodName", qualifiedByName = "toTransformDetail")
})
ComparisonTransformConfiguration dotFromRequestType(
ComparisonRootTransformConfiguration requestType);


@Mappings({
@Mapping(target = "transformMethodName", source = "transformDetail", qualifiedByName = "toTransformMethodName")
})
ComparisonRootTransformConfiguration requestTypeFromDto(ComparisonTransformConfiguration dto);

@Named("toTransformDetail")
default TransformDetail toTransformDetail(String transformMethodName) {
TransformDetail transformDetail = new TransformDetail();
transformDetail.setNodePath(Collections.singletonList(ROOT_NODE_PATH));
TransformMethod transformMethod = new TransformMethod();
transformMethod.setMethodName(transformMethodName);
transformDetail.setTransformMethods(Collections.singletonList(transformMethod));
return transformDetail;
}

@Named("toTransformMethodName")
default String toTransformMethodName(TransformDetail transformDetail) {
List<String> nodePath = transformDetail.getNodePath();
List<TransformMethod> transformMethods = transformDetail.getTransformMethods();
if (CollectionUtils.isNotEmpty(nodePath) && nodePath.contains(ROOT_NODE_PATH)
&& CollectionUtils.isNotEmpty(transformMethods)) {
return transformMethods.get(0).getMethodName();
}
return null;
}


}

0 comments on commit 9d3dc97

Please sign in to comment.