Skip to content

Commit

Permalink
fix: map properties error (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
YongwuHe authored Oct 28, 2024
1 parent b367c39 commit 9d6488f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public ArexMocker(MockCategoryType categoryType) {
this.categoryType = categoryType;
this.appId = System.getProperty(ConfigConstants.SERVICE_NAME);
this.recordVersion = System.getProperty(ConfigConstants.AGENT_VERSION);
this.tags = (Map<String, String>) System.getProperties().getOrDefault(ConfigConstants.MOCKER_TAGS, Collections.emptyMap());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ private static void indent(StringBuilder sb, int indent) {
}
}

public static String mapToString(Map<String, String> map) {
if (MapUtils.isEmpty(map)) {
return StringUtil.EMPTY;
}
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, String> entry : map.entrySet()) {
stringBuilder.append(entry.getKey())
.append("=")
.append(entry.getValue())
.append(";");
}
return stringBuilder.toString();
}

public static Map<String, String> asMap(String content) {
if (isEmpty(content)) {
return Collections.emptyMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,12 @@ void splitByLastSeparator() {
actualResult = StringUtil.splitByLastSeparator(val, ',');
assertArrayEquals(new String[] {"x,,y", "z"}, actualResult);
}

@Test
void mapToString() {
Map<String, String> map = new HashMap<>();
map.put("a", "b");
map.put("c", "d");
assertEquals("a=b;c=d;", StringUtil.mapToString(map));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static Config get() {
private final String recordVersion;
private final Set<String> includeServiceOperations;
private final Set<String> coveragePackages = new ConcurrentHashSet<>();
private final Map<String, String> mockerTags;

Config(boolean enableDebug, String serviceName, List<DynamicClassEntity> dynamicClassList,
Map<String, String> properties,
Expand All @@ -60,6 +61,7 @@ public static Config get() {
this.recordRate = recordRate;
this.recordVersion = properties.get("arex.agent.version");
this.includeServiceOperations = StringUtil.splitToSet(properties.get("includeServiceOperations"), SEPARATOR);
this.mockerTags = StringUtil.asMap(System.getProperty(ConfigConstants.MOCKER_TAGS));
buildCoveragePackages(properties);
buildDynamicClassInfo();
}
Expand Down Expand Up @@ -107,6 +109,10 @@ private void buildDynamicClassInfo() {
this.dynamicAbstractClassList = list.toArray(StringUtil.EMPTY_STRING_ARRAY);
}

public Map<String, String> getMockerTags() {
return mockerTags;
}

public Set<String> getCoveragePackages() {
return coveragePackages;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static ArexMocker createNettyProvider(String pattern) {

public static ArexMocker create(MockCategoryType categoryType, String operationName) {
ArexMocker mocker = new ArexMocker(categoryType);
mocker.setTags(Config.get().getMockerTags());
long createTime = System.currentTimeMillis();
ArexContext context = ContextManager.currentContext();
if (context != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import io.arex.agent.bootstrap.constants.ConfigConstants;
import io.arex.agent.bootstrap.util.ConcurrentHashSet;
import io.arex.agent.bootstrap.util.MapUtils;
import io.arex.agent.bootstrap.util.StringUtil;
import io.arex.inst.runtime.context.RecordLimiter;
import io.arex.inst.runtime.listener.EventProcessor;
import io.arex.inst.runtime.model.ArexConstants;
import io.arex.inst.runtime.model.DynamicClassEntity;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

Expand All @@ -17,9 +20,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -147,4 +150,18 @@ void buildCoveragePackages() {
System.clearProperty(ArexConstants.SPRING_SCAN_PACKAGES);

}

@Test
void arexMockerTags() {
ConfigBuilder configBuilder = ConfigBuilder.create("mock");
configBuilder.build();
assertTrue(MapUtils.isEmpty(Config.get().getMockerTags()));
Map<String, String> tags = new HashMap<>();
tags.put("key1", "value1");
tags.put("key2", "value2");
System.setProperty(ConfigConstants.MOCKER_TAGS, StringUtil.mapToString(tags));
configBuilder.build();
assertEquals("value1", Config.get().getMockerTags().get("key1"));
assertEquals("value2", Config.get().getMockerTags().get("key2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ void match() {
Mockito.when(MockUtils.methodRequestTypeHash(requestMocker)).thenReturn(2);
assertNull(ReplayMatcher.match(requestMocker, MockStrategyEnum.FIND_LAST));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ static Stream<Arguments> recordRemainCase() {
arguments(contextSupplier2.get(), asserts2)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ static Stream<Arguments> replayAllMockerCase() {
arguments(mocker2)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.arex.foundation.util.NetUtils;
import io.arex.agent.bootstrap.util.StringUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -136,7 +135,7 @@ public Map<String, String> getSystemProperties() {
map.put(key, value);
buildTags(mockerTags, key, value);
}
properties.put(ConfigConstants.MOCKER_TAGS, Collections.unmodifiableMap(mockerTags));
System.setProperty(ConfigConstants.MOCKER_TAGS, StringUtil.mapToString(mockerTags));
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import feign.Response;
import io.arex.agent.bootstrap.model.MockResult;
import io.arex.inst.httpclient.common.HttpClientExtractor;
import io.arex.inst.runtime.config.ConfigBuilder;
import io.arex.inst.runtime.context.ContextManager;
import io.arex.inst.runtime.context.RepeatedCollectManager;
import io.arex.inst.runtime.util.IgnoreUtils;
Expand All @@ -24,6 +25,7 @@ static void setUp() {
Mockito.mockStatic(IgnoreUtils.class);
Mockito.mockStatic(ContextManager.class);
Mockito.mockStatic(RepeatedCollectManager.class);
ConfigBuilder.create("test").build();
}

@AfterAll
Expand Down

0 comments on commit 9d6488f

Please sign in to comment.