-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Compatible with replayResult interface logic
- Loading branch information
1 parent
8fb923d
commit 1886dc3
Showing
14 changed files
with
396 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...edule-web-api/src/main/java/com/arextest/schedule/comparer/CompareItemConvertFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.arextest.schedule.comparer; | ||
|
||
import com.arextest.model.mock.MockCategoryType; | ||
import java.util.function.Function; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CompareItemConvertFactory { | ||
private final Map<MockCategoryType, CompareItemConverter> categoryConverters; | ||
public CompareItemConvertFactory(@Autowired List<CompareItemConverter> converters) { | ||
this.categoryConverters = converters.stream().filter(c -> c.getCategoryType() != null) | ||
.collect(Collectors.toMap(CompareItemConverter::getCategoryType, Function.identity())); | ||
} | ||
|
||
public CompareItemConverter getConverts(MockCategoryType categoryType) { | ||
return categoryConverters.getOrDefault(categoryType, null); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
arex-schedule-web-api/src/main/java/com/arextest/schedule/comparer/CompareItemConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.arextest.schedule.comparer; | ||
|
||
import com.arextest.model.mock.AREXMocker; | ||
import com.arextest.model.mock.MockCategoryType; | ||
import com.arextest.model.replay.CompareRelationResult; | ||
|
||
public interface CompareItemConverter { | ||
MockCategoryType getCategoryType(); | ||
|
||
CompareItem convert(AREXMocker mocker); | ||
|
||
CompareItem convert(CompareRelationResult relationResult, boolean recordCompareItem); | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
.../main/java/com/arextest/schedule/comparer/converter/DatabaseCompareItemConverterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.arextest.schedule.comparer.converter; | ||
|
||
import com.arextest.model.constants.MockAttributeNames; | ||
import com.arextest.model.mock.AREXMocker; | ||
import com.arextest.model.mock.MockCategoryType; | ||
import com.arextest.model.mock.Mocker.Target; | ||
import com.arextest.model.replay.CompareRelationResult; | ||
import com.arextest.schedule.common.JsonUtils; | ||
import com.arextest.schedule.comparer.CompareItem; | ||
import com.arextest.schedule.comparer.CompareItemConverter; | ||
import com.arextest.schedule.comparer.impl.PrepareCompareItemBuilder.CompareItemImpl; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author xinyuan_wang. | ||
* @create 2024/9/2 16:46 | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class DatabaseCompareItemConverterImpl implements CompareItemConverter { | ||
|
||
@Override | ||
public MockCategoryType getCategoryType() { | ||
return MockCategoryType.DATABASE; | ||
} | ||
|
||
@Override | ||
public CompareItem convert(AREXMocker mocker) { | ||
MockCategoryType categoryType = mocker.getCategoryType(); | ||
String operationKey = getOperationName(mocker.getTargetRequest(), mocker.getOperationName()); | ||
return new CompareItemImpl(operationKey, buildAttributes(mocker.getTargetRequest()).toString(), | ||
mocker.getId(), mocker.getCreationTime(), categoryType.isEntryPoint()); | ||
} | ||
@Override | ||
public CompareItem convert(CompareRelationResult relationResult, boolean recordCompareItem) { | ||
String message = recordCompareItem ? relationResult.getRecordMessage() : relationResult.getReplayMessage(); | ||
long createTime = recordCompareItem ? relationResult.getRecordTime() : relationResult.getReplayTime(); | ||
return new CompareItemImpl(relationResult.getOperationName(), processMessage(message), null, | ||
createTime, relationResult.getCategoryType().isEntryPoint()); | ||
} | ||
|
||
private String processMessage(String message) { | ||
if (StringUtils.isBlank(message)) { | ||
return message; | ||
} | ||
|
||
Target target = JsonUtils.jsonStringToObject(message, Target.class); | ||
if (target != null) { | ||
message = buildAttributes(target).toString(); | ||
} | ||
return message; | ||
} | ||
|
||
private ObjectNode buildAttributes(Target target) { | ||
ObjectNode obj = JsonNodeFactory.instance.objectNode(); | ||
if (target == null) { | ||
return obj; | ||
} | ||
Map<String, Object> attributes = target.getAttributes(); | ||
if (attributes != null) { | ||
for (Entry<String, Object> entry : attributes.entrySet()) { | ||
Object value = entry.getValue(); | ||
if (value instanceof String) { | ||
obj.put(entry.getKey(), (String) value); | ||
} else { | ||
obj.putPOJO(entry.getKey(), value); | ||
} | ||
} | ||
} | ||
if (StringUtils.isNotEmpty(target.getBody())) { | ||
obj.put("body", target.getBody()); | ||
} | ||
return obj; | ||
} | ||
|
||
private String getOperationName(Target target, String operationName) { | ||
// The "@" in the operationName of DATABASE indicates that the SQL statement has been parsed and returned directly. | ||
String compareOperationName = StringUtils.contains(operationName, "@") ? operationName | ||
: target.attributeAsString(MockAttributeNames.DB_NAME); | ||
if (StringUtils.isNotBlank(compareOperationName)) { | ||
return compareOperationName; | ||
} | ||
return operationName; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...src/main/java/com/arextest/schedule/comparer/converter/RedisCompareItemConverterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.arextest.schedule.comparer.converter; | ||
|
||
import com.arextest.model.constants.MockAttributeNames; | ||
import com.arextest.model.mock.AREXMocker; | ||
import com.arextest.model.mock.MockCategoryType; | ||
import com.arextest.model.mock.Mocker.Target; | ||
import com.arextest.model.replay.CompareRelationResult; | ||
import com.arextest.schedule.comparer.CompareItem; | ||
import com.arextest.schedule.comparer.CompareItemConverter; | ||
import com.arextest.schedule.comparer.impl.PrepareCompareItemBuilder.CompareItemImpl; | ||
import java.util.Objects; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author xinyuan_wang. | ||
* @create 2024/9/2 16:46 | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class RedisCompareItemConverterImpl implements CompareItemConverter { | ||
|
||
@Override | ||
public MockCategoryType getCategoryType() { | ||
return MockCategoryType.REDIS; | ||
} | ||
|
||
@Override | ||
public CompareItem convert(AREXMocker mocker) { | ||
MockCategoryType categoryType = mocker.getCategoryType(); | ||
String operationKey = getOperationName(mocker.getTargetRequest(), mocker.getOperationName()); | ||
String compareMessage = Objects.isNull(mocker.getTargetRequest()) ? null | ||
: mocker.getTargetRequest().getBody(); | ||
|
||
return new CompareItemImpl(operationKey, compareMessage, mocker.getId(), | ||
mocker.getCreationTime(), categoryType.isEntryPoint()); | ||
} | ||
@Override | ||
public CompareItem convert(CompareRelationResult relationResult, boolean recordCompareItem) { | ||
String message = recordCompareItem ? relationResult.getRecordMessage() : relationResult.getReplayMessage(); | ||
long createTime = recordCompareItem ? relationResult.getRecordTime() : relationResult.getReplayTime(); | ||
return new CompareItemImpl(relationResult.getOperationName(), message, null, | ||
createTime, relationResult.getCategoryType().isEntryPoint()); | ||
} | ||
|
||
private String getOperationName(Target target, String operationName) { | ||
String compareOperationName = target.attributeAsString(MockAttributeNames.CLUSTER_NAME); | ||
if (StringUtils.isNotBlank(compareOperationName)) { | ||
return compareOperationName; | ||
} | ||
return operationName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.