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

Closes #830 - Extend EUM server to handle non existing beacon fields as empty values #831

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,25 @@ public Beacon process(Beacon beacon) {
}

private String deriveTag(RegexDerivedTag tag, String input) {
if (input == null) {
return null;
String value = input;
if (value == null) {
if (tag.isNullAsEmpty()) {
value = "";
} else {
return null;
}
}

Pattern regex = tag.getRegex();
if (regex != null) {
try {
return regexReplaceAll(input, regex, tag.getReplacement(), tag.isKeepIfNoMatch());
return regexReplaceAll(value, regex, tag.getReplacement(), tag.isKeepIfNoMatch());
} catch (Exception ex) {
log.error("Could not derive beacon tag value <{}> of input string <{}>!", tag.getTagName(), input, tag.getReplacement(), ex);
log.error("Could not derive beacon tag value <{}> of input string <{}>!", tag.getTagName(), value, tag.getReplacement(), ex);
return null;
}
} else {
return input;
return value;
}
}

Expand Down Expand Up @@ -149,6 +154,11 @@ private static class RegexDerivedTag {
*/
boolean keepIfNoMatch;

/**
* Specify whether the input field should be considered as an empty string if it does not exists.
*/
boolean nullAsEmpty;

private static RegexDerivedTag fromSettings(String tagName, BeaconTagSettings settings) {
Pattern pattern = null;
String regex = settings.getRegex();
Expand All @@ -161,6 +171,7 @@ private static RegexDerivedTag fromSettings(String tagName, BeaconTagSettings se
.regex(pattern)
.replacement(settings.getReplacement())
.keepIfNoMatch(settings.isKeepNoMatch())
.nullAsEmpty(settings.isNullAsEmpty())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public class BeaconTagSettings {
@Builder.Default
private String replacement = "";

/**
* Specify whether the input field should be considered as an empty string if it does not exists.
*/
@Builder.Default
private boolean nullAsEmpty = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,26 @@ void nullAttribute() {
entry("value", "Hello World")
);
}

@Test
void nullAttributeAsEmpty() {
BeaconTagSettings first = BeaconTagSettings.builder().input("not-existing").regex("^$").replacement("one").nullAsEmpty(true).build();

Map<String, BeaconTagSettings> beaconTags = new LinkedHashMap<>();
beaconTags.put("first", first);

EumServerConfiguration conf = new EumServerConfiguration();
conf.setTags(new EumTagsSettings());
conf.getTags().setBeacon(beaconTags);

RegexReplacementBeaconProcessor processor = new RegexReplacementBeaconProcessor(conf);

Beacon result = processor.process(Beacon.of(ImmutableMap.of("value", "Hello World")));

assertThat(result.toMap()).containsOnly(
entry("value", "Hello World"),
entry("first", "one")
);
}
}
}