Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] refactoring methods replaceCryPlaceholder and replaceSmilingPlace #2832

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
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}
Expand Down Expand Up @@ -133,6 +140,64 @@ void replaceCryPlaceholder() throws JsonMappingException, JsonProcessingExceptio
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ public static Set<String> matchCryPlaceholderField(JsonElement jsonElement) {
.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;
}

/**
* json parameter replacement
*
Expand All @@ -237,69 +260,43 @@ public static JsonElement replaceCryPlaceholder(JsonElement jsonElement, Map<Str
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();
Iterator<JsonElement> iterator = jsonArray.iterator();
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++;
}
}
Expand Down Expand Up @@ -342,34 +339,19 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map
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();
Expand Down
Loading