[refactor] refactoring methods replaceCryPlaceholder and replaceSmilingPlace (#2832)

Co-authored-by: Zhang Yuxuan <1373529784@qq.com>
Co-authored-by: aias00 <rokkki@163.com>
Co-authored-by: Calvin <naruse_shinji@163.com>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
This commit is contained in:
hasimmollah
2025-01-04 19:54:05 +08:00
committed by GitHub
co-authored by Zhang Yuxuan aias00 Calvin tomsun28
parent 8ab71e672f
commit 69a21ffaa1
2 changed files with 125 additions and 78 deletions
@@ -30,9 +30,16 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Test case for {@link CollectUtil}
@@ -133,6 +140,64 @@ class CollectUtilTest {
assertEquals(JSON_MAPPER.readTree(jsonArrayTarget.toString()), JSON_MAPPER.readTree(res2.toString()));
}
static Stream<Arguments> testParamsForShouldVerifyReplaceCryPlaceholder() {
JsonObject jsonObject = new JsonObject();
String value = "^o^A1-B2.C3^o^";
String replacedField = "A1-B2.C3";
String replacedValue = "A1B2C3";
String nameKey = "name";
String messageKey = "message";
jsonObject.add(messageKey, new JsonPrimitive(value));
Map<String, Configmap> configmap = new HashMap<>();
Configmap config = Configmap.builder().key(nameKey).value(replacedValue).build();
configmap.put(replacedField, config);
JsonObject jsonObjectExpected = new JsonObject();
jsonObjectExpected.addProperty(messageKey, replacedValue);
Map<String, Configmap> configmapUnmatched = new HashMap<>();
Configmap configUnmatched = Configmap.builder().key(nameKey).value(replacedValue).build();
configmapUnmatched.put(nameKey, configUnmatched);
JsonObject jsonObjectExpectedForUnmatched = new JsonObject();
jsonObjectExpectedForUnmatched.addProperty(messageKey, value);
Map<String, Configmap> configMapSameLength = new HashMap<>();
Configmap configSameLength = Configmap.builder().key(nameKey).value(null).build();
configMapSameLength.put(replacedField, configSameLength);
JsonObject jsonObjectExpectedForSameLength = new JsonObject();
jsonObjectExpectedForSameLength.addProperty(messageKey, (String) null);
return Stream.of(
Arguments.of(jsonObject.deepCopy(), configMapSameLength, jsonObjectExpectedForSameLength),
Arguments.of(jsonObject.deepCopy(), configmap, jsonObjectExpected),
Arguments.of(jsonObject.deepCopy(), configmapUnmatched, jsonObjectExpectedForUnmatched)
);
}
@ParameterizedTest
@MethodSource("testParamsForShouldVerifyReplaceCryPlaceholder")
void shouldVerifyReplaceCryPlaceholder(JsonObject jsonObject,
Map<String, Configmap> configmap,
JsonObject jsonObjectTarget) throws JsonProcessingException {
JsonElement res1 = CollectUtil.replaceCryPlaceholder(jsonObject, configmap);
assertEquals(JSON_MAPPER.readTree(jsonObjectTarget.toString()), JSON_MAPPER.readTree(res1.toString()));
List<JsonObject> metricsList = new ArrayList<>();
metricsList.add(jsonObject);
JsonElement jsonArray = new Gson().toJsonTree(metricsList);
JsonElement res2 = CollectUtil.replaceCryPlaceholder(jsonArray, configmap);
List<JsonObject> metricsListTarget = new ArrayList<>();
metricsListTarget.add(jsonObjectTarget);
JsonElement jsonArrayTarget = new Gson().toJsonTree(metricsListTarget);
assertEquals(JSON_MAPPER.readTree(jsonArrayTarget.toString()), JSON_MAPPER.readTree(res2.toString()));
}
@Test
void replaceSmilingPlaceholder() throws JsonMappingException, JsonProcessingException {
Metrics metrics = Metrics.builder().name("^_^name^_^").build();
@@ -202,6 +202,29 @@ public final class CollectUtil {
.collect(Collectors.toSet());
}
private static String replaceSpecialCharacterIfNeeded(String value,
String replacePattern, Matcher matcher,
Map<String, Configmap> configmap) {
matcher.reset();
while (matcher.find()) {
String group = matcher.group();
String replaceField = group.replaceAll(replacePattern, "");
Configmap param = configmap.get(replaceField);
if (param == null) {
continue;
}
if (param.getValue() != null) {
value = value.replace(group, (String) param.getValue());
} else if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
}
return value;
}
/**
* replace cry placeholder to metrics
*
@@ -230,34 +253,20 @@ public final class CollectUtil {
Map.Entry<String, JsonElement> entry = iterator.next();
JsonElement element = entry.getValue();
// Replace normal VALUE value
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.find()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonObject.addProperty(entry.getKey(), value);
}
} else {
if (!element.isJsonPrimitive()) {
jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
continue;
}
// Replace normal VALUE value
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!cryingMatcher.find()) {
continue;
}
// Replace special characters
value = replaceSpecialCharacterIfNeeded(value, CRYING_PLACEHOLDER_REX, cryingMatcher, configmap);
jsonObject.addProperty(entry.getKey(), value);
}
} else if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
@@ -265,34 +274,22 @@ public final class CollectUtil {
int index = 0;
while (iterator.hasNext()) {
JsonElement element = iterator.next();
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.find()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
}
} else {
if (!element.isJsonPrimitive()) {
jsonArray.set(index, replaceCryPlaceholder(element, configmap));
index++;
continue;
}
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!cryingMatcher.find()) {
index++;
continue;
}
// Replace special characters
value = replaceSpecialCharacterIfNeeded(value, CRYING_PLACEHOLDER_REX, cryingMatcher, configmap);
jsonArray.set(index, value == null ? JsonNull.INSTANCE : new JsonPrimitive(value));
index++;
}
}
@@ -335,34 +332,19 @@ public final class CollectUtil {
continue;
}
// Replace normal VALUE value
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher smilingMatcher = SMILING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (smilingMatcher.find()) {
smilingMatcher.reset();
while (smilingMatcher.find()) {
String group = smilingMatcher.group();
String replaceField = group.replaceAll(SMILING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonObject.addProperty(entry.getKey(), value);
}
} else {
if (!element.isJsonPrimitive()) {
jsonObject.add(entry.getKey(), replaceSmilingPlaceholder(entry.getValue(), configmap));
continue;
}
// Check if there are special characters Replace
String value = element.getAsString();
Matcher smilingMatcher = SMILING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!smilingMatcher.find()) {
continue;
}
// Replace special characters
value = replaceSpecialCharacterIfNeeded(value, SMILING_PLACEHOLDER_REX, smilingMatcher, configmap);
jsonObject.addProperty(entry.getKey(), value);
}
} else if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();