Skip to content

Commit a7259a3

Browse files
committed
feat: add unit test
1 parent b9d1d51 commit a7259a3

16 files changed

+591
-180
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ private boolean support(MatchStrategyContext context) {
2424
return internalCheck(context);
2525
}
2626

27-
boolean internalCheck(MatchStrategyContext context) {
27+
public boolean internalCheck(MatchStrategyContext context) {
2828
return true;
2929
}
30-
abstract void process(MatchStrategyContext context) throws Exception;
3130

32-
void setContextResult(MatchStrategyContext context, Mocker resultMocker, String failReason) {
31+
public abstract void process(MatchStrategyContext context) throws Exception;
32+
33+
public void setContextResult(MatchStrategyContext context, Mocker resultMocker, String failReason) {
3334
if (resultMocker != null) {
3435
resultMocker.setMatched(true);
3536
} else {

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/strategy/AccurateMatchStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AccurateMatchStrategy extends AbstractMatchStrategy{
1919
* 3. if matched multiple result, give next match
2020
* 4. if strict match mode and not matched, interrupt
2121
*/
22-
void process(MatchStrategyContext context) {
22+
public void process(MatchStrategyContext context) {
2323
context.setMatchStrategy(MatchStrategyEnum.ACCURATE);
2424
Mocker requestMocker = context.getRequestMocker();
2525
List<Mocker> recordList = context.getRecordList();
@@ -54,7 +54,7 @@ void process(MatchStrategyContext context) {
5454
}
5555

5656
@Override
57-
boolean internalCheck(MatchStrategyContext context) {
57+
public boolean internalCheck(MatchStrategyContext context) {
5858
// if no request params, do next match directly
5959
return StringUtil.isNotEmpty(context.getRequestMocker().getTargetRequest().getBody());
6060
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class EigenMatchStrategy extends AbstractMatchStrategy{
2424
/**
2525
* search by eigen value of request
2626
*/
27-
void process(MatchStrategyContext context) {
27+
public void process(MatchStrategyContext context) {
2828
context.setMatchStrategy(MatchStrategyEnum.EIGEN);
2929
Mocker replayMocker = context.getRequestMocker();
3030
List<Mocker> recordList = context.getRecordList();

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/match/strategy/FuzzyMatchStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class FuzzyMatchStrategy extends AbstractMatchStrategy {
1414
* replayList is arranged in ascending order by creationTime
1515
* @return not matched before or last one
1616
*/
17-
void process(MatchStrategyContext context) {
17+
public void process(MatchStrategyContext context) {
1818
context.setMatchStrategy(MatchStrategyEnum.FUZZY);
1919
List<Mocker> recordList = context.getRecordList();
2020
Mocker resultMocker = null;
@@ -33,7 +33,7 @@ void process(MatchStrategyContext context) {
3333
}
3434

3535
@Override
36-
boolean internalCheck(MatchStrategyContext context) {
36+
public boolean internalCheck(MatchStrategyContext context) {
3737
return CollectionUtil.isNotEmpty(context.getRecordList());
3838
}
3939
}

arex-instrumentation-api/src/test/java/io/arex/inst/runtime/match/AbstractMatchStrategyTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.arex.inst.runtime.match;
22

33
import io.arex.agent.bootstrap.model.ArexMocker;
4+
import io.arex.agent.bootstrap.model.MockCategoryType;
45
import io.arex.agent.bootstrap.model.Mocker;
56
import io.arex.inst.runtime.match.strategy.AbstractMatchStrategy;
67
import io.arex.inst.runtime.match.strategy.AccurateMatchStrategy;
@@ -19,7 +20,7 @@ class AbstractMatchStrategyTest {
1920
@BeforeAll
2021
static void setUp() {
2122
target = new AccurateMatchStrategy();
22-
mocker = new ArexMocker();
23+
mocker = new ArexMocker(MockCategoryType.DYNAMIC_CLASS);
2324
mocker.setTargetResponse(new Mocker.Target());
2425
mocker.setTargetRequest(new Mocker.Target());
2526
mocker.getTargetRequest().setBody("mock");
@@ -34,6 +35,13 @@ static void tearDown() {
3435
void match() {
3536
assertDoesNotThrow(() -> target.match(null));
3637
MatchStrategyContext context = new MatchStrategyContext(mocker, null);
38+
context.setRecordList(new ArrayList<>());
3739
assertDoesNotThrow(() -> target.match(context));
3840
}
41+
42+
@Test
43+
void setContextResult() {
44+
assertDoesNotThrow(() -> target.setContextResult(new MatchStrategyContext(mocker, null), mocker, null));
45+
assertDoesNotThrow(() -> target.setContextResult(new MatchStrategyContext(mocker, null), null, null));
46+
}
3947
}
Lines changed: 110 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,110 @@
1-
//package io.arex.inst.runtime.match;
2-
//
3-
//import io.arex.agent.bootstrap.model.ArexMocker;
4-
//import io.arex.agent.bootstrap.model.MockCategoryType;
5-
//import io.arex.agent.bootstrap.model.MockStrategyEnum;
6-
//import io.arex.agent.bootstrap.model.Mocker;
7-
//import io.arex.inst.runtime.match.strategy.AccurateMatchStrategy;
8-
//import io.arex.inst.runtime.util.MockUtils;
9-
//import org.junit.jupiter.api.AfterAll;
10-
//import org.junit.jupiter.api.BeforeAll;
11-
//import org.junit.jupiter.api.Test;
12-
//import org.junit.jupiter.params.ParameterizedTest;
13-
//import org.junit.jupiter.params.provider.Arguments;
14-
//import org.junit.jupiter.params.provider.MethodSource;
15-
//import org.mockito.Mockito;
16-
//
17-
//import java.util.ArrayList;
18-
//import java.util.List;
19-
//import java.util.function.Predicate;
20-
//import java.util.function.Supplier;
21-
//import java.util.stream.Stream;
22-
//
23-
//import static org.junit.jupiter.api.Assertions.*;
24-
//import static org.junit.jupiter.params.provider.Arguments.arguments;
25-
//
26-
//class AccurateMatchStrategyTest {
27-
//
28-
// static AccurateMatchStrategy accurateMatchStrategy;
29-
//
30-
// @BeforeAll
31-
// static void setUp() {
32-
// accurateMatchStrategy = new AccurateMatchStrategy();
33-
// Mockito.mockStatic(MockUtils.class);
34-
// }
35-
//
36-
// @AfterAll
37-
// static void tearDown() {
38-
// accurateMatchStrategy = null;
39-
// Mockito.clearAllCaches();
40-
// }
41-
//
42-
// @ParameterizedTest
43-
// @MethodSource("processCase")
44-
// void process(MatchStrategyContext context, Predicate<MatchStrategyContext> asserts) {
45-
// accurateMatchStrategy.process(context);
46-
// asserts.test(context);
47-
// }
48-
//
49-
// static Stream<Arguments> processCase() {
50-
// Supplier<MatchStrategyContext> contextSupplier1 = () -> {
51-
// ArexMocker mocker = new ArexMocker();
52-
// mocker.setTargetResponse(new Mocker.Target());
53-
// mocker.setTargetRequest(new Mocker.Target());
54-
// mocker.setCategoryType(MockCategoryType.DYNAMIC_CLASS);
55-
// List<Mocker> mergeReplayList = new ArrayList<>();
56-
// mergeReplayList.add(new ArexMocker());
57-
// return new MatchStrategyContext(mocker, mergeReplayList, MockStrategyEnum.FIND_LAST);
58-
// };
59-
// Supplier<MatchStrategyContext> contextSupplier2 = () -> {
60-
// MatchStrategyContext context = contextSupplier1.get();
61-
// context.getReplayList().get(0).setMatched(true);
62-
// context.setMockStrategy(MockStrategyEnum.STRICT_MATCH);
63-
// return context;
64-
// };
65-
// Supplier<MatchStrategyContext> contextSupplier3 = () -> {
66-
// MatchStrategyContext context = contextSupplier1.get();
67-
// context.getReplayList().add(new ArexMocker());
68-
// return context;
69-
// };
70-
// Supplier<MatchStrategyContext> contextSupplier4 = () -> {
71-
// MatchStrategyContext context = contextSupplier1.get();
72-
// context.getReplayList().get(0).setMethodSignatureHash(1);
73-
// context.setMockStrategy(MockStrategyEnum.STRICT_MATCH);
74-
// return context;
75-
// };
76-
//
77-
// Predicate<MatchStrategyContext> asserts1 = context -> !context.isInterrupt();
78-
// Predicate<MatchStrategyContext> asserts2 = MatchStrategyContext::isInterrupt;
79-
//
80-
// return Stream.of(
81-
// arguments(contextSupplier1.get(), asserts1),
82-
// arguments(contextSupplier2.get(), asserts2),
83-
// arguments(contextSupplier3.get(), asserts1),
84-
// arguments(contextSupplier4.get(), asserts2)
85-
// );
86-
// }
87-
//
88-
// @Test
89-
// void internalCheck() {
90-
// ArexMocker mocker = new ArexMocker();
91-
// mocker.setTargetRequest(new Mocker.Target());
92-
// assertFalse(accurateMatchStrategy.internalCheck(new MatchStrategyContext(mocker, null, null)));
93-
// }
94-
//}
1+
package io.arex.inst.runtime.match;
2+
3+
import io.arex.agent.bootstrap.model.ArexMocker;
4+
import io.arex.agent.bootstrap.model.MockCategoryType;
5+
import io.arex.agent.bootstrap.model.MockStrategyEnum;
6+
import io.arex.agent.bootstrap.model.Mocker;
7+
import io.arex.inst.runtime.match.strategy.AccurateMatchStrategy;
8+
import io.arex.inst.runtime.util.MockUtils;
9+
import org.junit.jupiter.api.AfterAll;
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.MethodSource;
15+
import org.mockito.Mockito;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.function.Predicate;
20+
import java.util.function.Supplier;
21+
import java.util.stream.Stream;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
import static org.junit.jupiter.params.provider.Arguments.arguments;
25+
26+
class AccurateMatchStrategyTest {
27+
28+
static AccurateMatchStrategy accurateMatchStrategy;
29+
30+
@BeforeAll
31+
static void setUp() {
32+
accurateMatchStrategy = new AccurateMatchStrategy();
33+
Mockito.mockStatic(MockUtils.class);
34+
}
35+
36+
@AfterAll
37+
static void tearDown() {
38+
accurateMatchStrategy = null;
39+
Mockito.clearAllCaches();
40+
}
41+
42+
@ParameterizedTest
43+
@MethodSource("processCase")
44+
void process(MatchStrategyContext context, Predicate<MatchStrategyContext> asserts) {
45+
accurateMatchStrategy.process(context);
46+
asserts.test(context);
47+
}
48+
49+
static Stream<Arguments> processCase() {
50+
Supplier<MatchStrategyContext> contextSupplier1 = () -> {
51+
ArexMocker mocker = new ArexMocker(MockCategoryType.DYNAMIC_CLASS);
52+
mocker.setOperationName("mock");
53+
mocker.setTargetRequest(new Mocker.Target());
54+
mocker.getTargetRequest().setBody("mock");
55+
mocker.setTargetResponse(new Mocker.Target());
56+
mocker.setAccurateMatchKey(MatchKeyFactory.INSTANCE.getAccurateMatchKey(mocker));
57+
List<Mocker> mergeReplayList = new ArrayList<>();
58+
mergeReplayList.add(mocker);
59+
MatchStrategyContext context = new MatchStrategyContext(mocker, MockStrategyEnum.FIND_LAST);
60+
context.setRecordList(mergeReplayList);
61+
return context;
62+
};
63+
Supplier<MatchStrategyContext> contextSupplier2 = () -> {
64+
MatchStrategyContext context = contextSupplier1.get();
65+
context.getRecordList().get(0).setMatched(true);
66+
context.setMockStrategy(MockStrategyEnum.STRICT_MATCH);
67+
return context;
68+
};
69+
Supplier<MatchStrategyContext> contextSupplier3 = () -> {
70+
MatchStrategyContext context = contextSupplier1.get();
71+
context.getRecordList().add(new ArexMocker());
72+
return context;
73+
};
74+
Supplier<MatchStrategyContext> contextSupplier4 = () -> {
75+
MatchStrategyContext context = contextSupplier1.get();
76+
context.getRecordList().get(0).setFuzzyMatchKey(1);
77+
context.setMockStrategy(MockStrategyEnum.STRICT_MATCH);
78+
return context;
79+
};
80+
Supplier<MatchStrategyContext> multiMatchResult = () -> {
81+
MatchStrategyContext context = contextSupplier1.get();
82+
ArexMocker mocker = new ArexMocker(MockCategoryType.DYNAMIC_CLASS);
83+
mocker.setOperationName("mock");
84+
mocker.setTargetRequest(new Mocker.Target());
85+
mocker.getTargetRequest().setBody("mock");
86+
mocker.setTargetResponse(new Mocker.Target());
87+
mocker.setAccurateMatchKey(MatchKeyFactory.INSTANCE.getAccurateMatchKey(mocker));
88+
context.getRecordList().add(mocker);
89+
return context;
90+
};
91+
92+
Predicate<MatchStrategyContext> asserts1 = context -> !context.isInterrupt();
93+
Predicate<MatchStrategyContext> asserts2 = MatchStrategyContext::isInterrupt;
94+
95+
return Stream.of(
96+
arguments(contextSupplier1.get(), asserts1),
97+
arguments(contextSupplier2.get(), asserts2),
98+
arguments(contextSupplier3.get(), asserts1),
99+
arguments(contextSupplier4.get(), asserts2),
100+
arguments(multiMatchResult.get(), asserts2)
101+
);
102+
}
103+
104+
@Test
105+
void internalCheck() {
106+
ArexMocker mocker = new ArexMocker();
107+
mocker.setTargetRequest(new Mocker.Target());
108+
assertFalse(accurateMatchStrategy.internalCheck(new MatchStrategyContext(mocker, null)));
109+
}
110+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,94 @@
11
package io.arex.inst.runtime.match;
22

3+
import io.arex.agent.bootstrap.model.ArexMocker;
4+
import io.arex.agent.bootstrap.model.MockCategoryType;
5+
import io.arex.agent.bootstrap.model.MockStrategyEnum;
6+
import io.arex.agent.bootstrap.model.Mocker;
7+
import io.arex.inst.runtime.config.Config;
38
import io.arex.inst.runtime.match.strategy.EigenMatchStrategy;
9+
import io.arex.inst.runtime.model.CompareConfigurationEntity;
410
import org.junit.jupiter.api.AfterAll;
511
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.MethodSource;
15+
import org.mockito.Mockito;
16+
17+
import java.util.ArrayList;
18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
import java.util.List;
21+
import java.util.function.Predicate;
22+
import java.util.function.Supplier;
23+
import java.util.stream.Stream;
24+
25+
import static org.junit.jupiter.params.provider.Arguments.arguments;
626

727
class EigenMatchStrategyTest {
828
static EigenMatchStrategy eigenMatchStrategy;
29+
static Config config;
930

1031
@BeforeAll
1132
static void setUp() {
1233
eigenMatchStrategy = new EigenMatchStrategy();
34+
Mockito.mockStatic(Config.class);
35+
config = Mockito.mock(Config.class);
36+
Mockito.when(Config.get()).thenReturn(config);
1337
}
1438

1539
@AfterAll
1640
static void tearDown() {
1741
eigenMatchStrategy = null;
42+
config = null;
43+
Mockito.clearAllCaches();
44+
}
45+
46+
@ParameterizedTest
47+
@MethodSource("processCase")
48+
void process(Runnable mocker, MatchStrategyContext context, Predicate<MatchStrategyContext> asserts) {
49+
mocker.run();
50+
eigenMatchStrategy.process(context);
51+
asserts.test(context);
52+
}
53+
54+
static Stream<Arguments> processCase() {
55+
Runnable emptyMocker = () -> {};
56+
57+
Runnable mockerCompareConfig = () -> {
58+
CompareConfigurationEntity compareConfig = new CompareConfigurationEntity();
59+
CompareConfigurationEntity.ConfigComparisonExclusionsEntity exclusion = new CompareConfigurationEntity.ConfigComparisonExclusionsEntity();
60+
exclusion.setCategoryType(MockCategoryType.DYNAMIC_CLASS.getName());
61+
exclusion.setOperationName("mock");
62+
exclusion.setExclusionList(new HashSet<>(new ArrayList<>()));
63+
compareConfig.setComparisonExclusions(Collections.singletonList(exclusion));
64+
Mockito.when(config.getCompareConfiguration()).thenReturn(compareConfig);
65+
};
66+
67+
Supplier<MatchStrategyContext> contextSupplier1 = () -> {
68+
ArexMocker mocker = new ArexMocker(MockCategoryType.DYNAMIC_CLASS);
69+
mocker.setOperationName("mock");
70+
mocker.setTargetRequest(new Mocker.Target());
71+
mocker.getTargetRequest().setBody("mock");
72+
mocker.setTargetResponse(new Mocker.Target());
73+
mocker.setAccurateMatchKey(MatchKeyFactory.INSTANCE.getAccurateMatchKey(mocker));
74+
List<Mocker> mergeReplayList = new ArrayList<>();
75+
mergeReplayList.add(mocker);
76+
MatchStrategyContext context = new MatchStrategyContext(mocker, MockStrategyEnum.FIND_LAST);
77+
context.setRecordList(mergeReplayList);
78+
return context;
79+
};
80+
Supplier<MatchStrategyContext> contextSupplier2 = () -> {
81+
MatchStrategyContext context = contextSupplier1.get();
82+
context.getRecordList().get(0).setMatched(true);
83+
return context;
84+
};
85+
86+
Predicate<MatchStrategyContext> asserts1 = context -> !context.isInterrupt();
87+
Predicate<MatchStrategyContext> asserts2 = MatchStrategyContext::isInterrupt;
88+
89+
return Stream.of(
90+
arguments(emptyMocker, contextSupplier1.get(), asserts1),
91+
arguments(mockerCompareConfig, contextSupplier2.get(), asserts2)
92+
);
1893
}
1994
}

0 commit comments

Comments
 (0)