Skip to content

Commit

Permalink
feat: optimize name
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-myx committed Jan 19, 2024
1 parent 82213ec commit d09da46
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@

public abstract class AbstractMatchStrategy {
static final String MATCH_TITLE = "replay.match.fail";
static final int ACCURATE_MATCH_ORDER = 10;
static final int FUZZY_MATCH_ORDER = 20;
static final int EIGEN_MATCH_ORDER = 30;

public void match(MatchStrategyContext context) {
try {
if (check(context)) {
if (support(context)) {
process(context);
}
} catch (Exception e) {
LogManager.warn(MATCH_TITLE, e);

Check warning on line 16 in arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/AbstractMatchStrategy.java

View check run for this annotation

Codecov / codecov/patch

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/AbstractMatchStrategy.java#L15-L16

Added lines #L15 - L16 were not covered by tests
}
}

private boolean check(MatchStrategyContext context) {
private boolean support(MatchStrategyContext context) {
if (context == null || context.getRequestMocker() == null || context.isInterrupt()) {
return false;
}
return valid(context);
return internalCheck(context);
}

boolean valid(MatchStrategyContext context) {
boolean internalCheck(MatchStrategyContext context) {
return true;

Check warning on line 28 in arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/AbstractMatchStrategy.java

View check run for this annotation

Codecov / codecov/patch

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/AbstractMatchStrategy.java#L28

Added line #L28 was not covered by tests
}
abstract int order();
abstract void process(MatchStrategyContext context) throws Exception;

Mocker buildMatchedMocker(Mocker requestMocker, MergeDTO mergeReplayDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void process(MatchStrategyContext context) {
Mocker requestMocker = context.getRequestMocker();
List<MergeDTO> mergeReplayList = context.getMergeReplayList();
int methodSignatureHash = MockUtils.methodSignatureHash(requestMocker);
List<MergeDTO> matchedList = new ArrayList<>();
List<MergeDTO> matchedList = new ArrayList<>(mergeReplayList.size());
for (MergeDTO mergeDTO : mergeReplayList) {
if (methodSignatureHash == mergeDTO.getMethodSignatureHash()) {
matchedList.add(mergeDTO);
Expand All @@ -41,24 +41,22 @@ void process(MatchStrategyContext context) {
}
// other modes can only be matched once, so interrupt and not continue next fuzzy match
context.setInterrupt(true);
return;
}
// matched multiple result(like as redis: incr、decr) only retain matched item for next fuzzy match
if (matchedCount > 1) {
context.setMergeReplayList(matchedList);
return;
}
// if strict match mode and not matched, interrupt and not continue next fuzzy match
if (matchedCount == 0 && MockStrategyEnum.STRICT_MATCH == context.getMockStrategy()) {
if (MockStrategyEnum.STRICT_MATCH == context.getMockStrategy()) {
context.setInterrupt(true);
}
}

@Override
boolean valid(MatchStrategyContext context) {
boolean internalCheck(MatchStrategyContext context) {
// if no request params, do next fuzzy match directly
return StringUtil.isNotEmpty(context.getRequestMocker().getTargetRequest().getBody());
}

int order() {
return ACCURATE_MATCH_ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ public class EigenMatchStrategy extends AbstractMatchStrategy{
void process(MatchStrategyContext context) {
// to be implemented after database merge replay support
}

Check warning on line 10 in arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/EigenMatchStrategy.java

View check run for this annotation

Codecov / codecov/patch

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/EigenMatchStrategy.java#L10

Added line #L10 was not covered by tests

int order() {
return EIGEN_MATCH_ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ void process(MatchStrategyContext context) {
}

@Override
boolean valid(MatchStrategyContext context) {
boolean internalCheck(MatchStrategyContext context) {
return CollectionUtil.isNotEmpty(context.getMergeReplayList());
}

int order() {
return FUZZY_MATCH_ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;

class AbstractMatchStrategyTest {

static AbstractMatchStrategy target;
static ArexMocker mocker;

@BeforeAll
static void setUp() {
target = new FuzzyMatchStrategy();
target = new AccurateMatchStrategy();
mocker = new ArexMocker();
mocker.setTargetResponse(new Mocker.Target());
mocker.setTargetRequest(new Mocker.Target());
mocker.getTargetRequest().setBody("mock");
}

@AfterAll
Expand All @@ -25,15 +32,12 @@ static void tearDown() {
@Test
void match() {
assertDoesNotThrow(() -> target.match(null));
MatchStrategyContext context = new MatchStrategyContext(new ArexMocker(), null, null);
MatchStrategyContext context = new MatchStrategyContext(mocker, new ArrayList<>(), null);
assertDoesNotThrow(() -> target.match(context));
}

@Test
void buildMatchedMocker() {
ArexMocker mocker = new ArexMocker();
mocker.setTargetResponse(new Mocker.Target());
mocker.setTargetRequest(new Mocker.Target());
Mocker result = target.buildMatchedMocker(mocker, null);
assertNull(result);
result = target.buildMatchedMocker(mocker, new MergeDTO());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,9 @@ static Stream<Arguments> processCase() {
}

@Test
void valid() {
void internalCheck() {
ArexMocker mocker = new ArexMocker();
mocker.setTargetRequest(new Mocker.Target());
assertFalse(accurateMatchStrategy.valid(new MatchStrategyContext(mocker, null, null)));
}

@Test
void order() {
assertEquals(10, accurateMatchStrategy.order());
assertFalse(accurateMatchStrategy.internalCheck(new MatchStrategyContext(mocker, null, null)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@ static void setUp() {
static void tearDown() {
eigenMatchStrategy = null;
}

@Test
void order() {
assertEquals(30, eigenMatchStrategy.order());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ void process() {
}

@Test
void valid() {
assertFalse(fuzzyMatchStrategy.valid(new MatchStrategyContext(null, null, null)));
}

@Test
void order() {
assertEquals(20, fuzzyMatchStrategy.order());
void internalCheck() {
assertFalse(fuzzyMatchStrategy.internalCheck(new MatchStrategyContext(null, null, null)));
}
}

0 comments on commit d09da46

Please sign in to comment.