-
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 a96aa85
Showing
15 changed files
with
475 additions
and
105 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
26 changes: 26 additions & 0 deletions
26
...api/src/main/java/com/arextest/schedule/comparer/converter/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,26 @@ | ||
package com.arextest.schedule.comparer.converter; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Resource; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CompareItemConvertFactory { | ||
private final Map<String, CompareItemConverter> categoryConverters; | ||
@Resource | ||
private CompareItemConverter defaultCompareItemConverterImpl; | ||
|
||
public CompareItemConvertFactory(@Autowired List<CompareItemConverter> converters) { | ||
this.categoryConverters = converters.stream().filter(c -> StringUtils.isNotBlank(c.getCategoryName())) | ||
.collect(Collectors.toMap(CompareItemConverter::getCategoryName, Function.identity())); | ||
} | ||
|
||
public CompareItemConverter getConvert(String categoryName) { | ||
return categoryConverters.getOrDefault(categoryName, defaultCompareItemConverterImpl); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...-web-api/src/main/java/com/arextest/schedule/comparer/converter/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,29 @@ | ||
package com.arextest.schedule.comparer.converter; | ||
|
||
import com.arextest.model.mock.AREXMocker; | ||
import com.arextest.model.replay.CompareRelationResult; | ||
import com.arextest.schedule.comparer.CompareItem; | ||
|
||
public interface CompareItemConverter { | ||
String DEFAULT_CATEGORY_NAME = "default"; | ||
|
||
default String getCategoryName() { | ||
return DEFAULT_CATEGORY_NAME; | ||
} | ||
|
||
/** | ||
* Convert mocker to compare item, The agent handles the compatibility needs before the match | ||
* @param mocker | ||
* @return | ||
*/ | ||
CompareItem convert(AREXMocker mocker); | ||
|
||
/** | ||
* Convert relation result to compare item | ||
* @param relationResult | ||
* @param recordCompareItem | ||
* @return | ||
*/ | ||
CompareItem convert(CompareRelationResult relationResult, boolean recordCompareItem); | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
.../java/com/arextest/schedule/comparer/converter/impl/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,99 @@ | ||
package com.arextest.schedule.comparer.converter.impl; | ||
|
||
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.converter.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 String getCategoryName() { | ||
return MockCategoryType.DATABASE.getName(); | ||
} | ||
|
||
@Override | ||
public CompareItem convert(AREXMocker mocker) { | ||
if (mocker == null || mocker.getCategoryType() == null) { | ||
return null; | ||
} | ||
|
||
String operationKey = getOperationName(mocker.getTargetRequest(), mocker.getOperationName()); | ||
return new CompareItemImpl(operationKey, buildAttributes(mocker.getTargetRequest()).toString(), | ||
mocker.getId(), mocker.getCreationTime(), mocker.getCategoryType().isEntryPoint()); | ||
} | ||
|
||
@Override | ||
public CompareItem convert(CompareRelationResult relationResult, boolean recordCompareItem) { | ||
if (relationResult == null || relationResult.getCategoryType() == null) { | ||
return null; | ||
} | ||
|
||
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
...n/java/com/arextest/schedule/comparer/converter/impl/DefaultCompareItemConverterImpl.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.impl; | ||
|
||
import com.arextest.model.mock.AREXMocker; | ||
import com.arextest.model.mock.MockCategoryType; | ||
import com.arextest.model.replay.CompareRelationResult; | ||
import com.arextest.schedule.comparer.CompareItem; | ||
import com.arextest.schedule.comparer.converter.CompareItemConverter; | ||
import com.arextest.schedule.comparer.impl.PrepareCompareItemBuilder.CompareItemImpl; | ||
import java.util.Objects; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author xinyuan_wang. | ||
* @create 2024/9/2 16:46 | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class DefaultCompareItemConverterImpl implements CompareItemConverter { | ||
|
||
@Override | ||
public CompareItem convert(AREXMocker mocker) { | ||
if (mocker == null || mocker.getCategoryType() == null) { | ||
return null; | ||
} | ||
|
||
MockCategoryType categoryType = mocker.getCategoryType(); | ||
String operationKey = mocker.getOperationName(); | ||
long createTime = mocker.getCreationTime(); | ||
String body; | ||
String compareKey = mocker.getId(); | ||
boolean entryPointCategory = false; | ||
if (categoryType.isEntryPoint()) { | ||
body = Objects.isNull(mocker.getTargetResponse()) ? null | ||
: mocker.getTargetResponse().getBody(); | ||
compareKey = null; | ||
entryPointCategory = true; | ||
} else { | ||
body = Objects.isNull(mocker.getTargetRequest()) ? null | ||
: mocker.getTargetRequest().getBody(); | ||
} | ||
return new CompareItemImpl(operationKey, body, compareKey, createTime, entryPointCategory); | ||
} | ||
|
||
@Override | ||
public CompareItem convert(CompareRelationResult relationResult, boolean recordCompareItem) { | ||
if (relationResult == null) { | ||
return null; | ||
} | ||
|
||
return new CompareItemImpl(relationResult, recordCompareItem); | ||
} | ||
|
||
} |
Oops, something went wrong.