Skip to content

Commit aa2c43f

Browse files
authored
Implement an object that verifies JSON keys from JsonObjects (#62)
* Implement class ValidKey and unit tests for it * Implement .asJsonObject() method for ValidKey and unit tests for it * Replace assertKey methods with the ValidKey class - All tests pass * Add ValidKey to ContainerAppConsoleLogsType * Add ValidKey to CCType * Add ValidKey objects to AppServiceConsoleLogsType * Do not use star import on LogicAppWorkflowRuntimeType * Clarify ValidKeyTest's test names * Test cases for other JSON ValueTypes for ValidKey - Changes the logic of ValidKey.asString to only allow returns of ValueType.STRING * Make fields private for ValidKey * Change Exception messages to be more accurate for ValidKey * Rename ValidKey to ValidStringKey * Make ValidKey an interface which supports Java Generics * Implement ValidBooleanKey and ValidIntegerKey - Only used in tests * Add Javadoc to describe the ValidKey * Remove unused import in ValidIntegerKeyTest * Update Javadoc for ValidKey * Declare variables as ValidKey<> instead of classes that implement that interface
1 parent f82bcaf commit aa2c43f

21 files changed

Lines changed: 1212 additions & 280 deletions

src/main/java/com/teragrep/nlf_01/types/ADFActivityRunType.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
import com.teragrep.nlf_01.util.ASCIIString;
5252
import com.teragrep.nlf_01.util.MD5Hash;
5353
import com.teragrep.nlf_01.util.ResourceId;
54+
import com.teragrep.nlf_01.util.ValidKey;
55+
import com.teragrep.nlf_01.util.ValidStringKey;
5456
import com.teragrep.nlf_01.util.ValidRFC5424AppName;
5557
import com.teragrep.nlf_01.util.ValidRFC5424Hostname;
5658
import com.teragrep.nlf_01.util.ValidRFC5424Timestamp;
5759
import com.teragrep.rlo_14.Facility;
5860
import com.teragrep.rlo_14.SDElement;
5961
import com.teragrep.rlo_14.Severity;
6062
import jakarta.json.JsonObject;
61-
import jakarta.json.JsonValue;
6263
import java.time.Instant;
6364
import java.util.HashSet;
6465
import java.util.Set;
@@ -74,17 +75,6 @@ public ADFActivityRunType(final ParsedEvent parsedEvent, final String realHostna
7475
this.realHostname = realHostname;
7576
}
7677

77-
private void assertKey(final JsonObject obj, final String key, final JsonValue.ValueType type)
78-
throws PluginException {
79-
if (!obj.containsKey(key)) {
80-
throw new PluginException(new IllegalArgumentException("Key " + key + " does not exist"));
81-
}
82-
83-
if (!obj.get(key).getValueType().equals(type)) {
84-
throw new PluginException(new IllegalArgumentException("Key " + key + " is not of type " + type));
85-
}
86-
}
87-
8878
@Override
8979
public Severity severity() {
9080
return Severity.NOTICE;
@@ -99,32 +89,25 @@ public Facility facility() {
9989
public String hostname() throws PluginException {
10090
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
10191

102-
assertKey(record, "_ResourceId", JsonValue.ValueType.STRING);
103-
final String resourceId = record.getString("_ResourceId");
92+
final ValidKey<String> validKey = new ValidStringKey(record, "_ResourceId");
10493

10594
return new ValidRFC5424Hostname(
106-
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
95+
"md5-".concat(new MD5Hash(validKey.value()).md5().concat("-").concat(new ASCIIString(new ResourceId(validKey.value()).resourceName()).withNonAsciiCharsRemoved()))
10796
).hostnameWithInvalidCharsRemoved();
108-
10997
}
11098

11199
@Override
112100
public String appName() throws PluginException {
113101
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
114102

115-
assertKey(record, "PipelineName", JsonValue.ValueType.STRING);
116-
117-
return new ValidRFC5424AppName(record.getString("PipelineName")).appName();
118-
103+
return new ValidRFC5424AppName(new ValidStringKey(record, "PipelineName").value()).appName();
119104
}
120105

121106
@Override
122107
public long timestamp() throws PluginException {
123108
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
124109

125-
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
126-
127-
return new ValidRFC5424Timestamp(record.getString("TimeGenerated")).validTimestamp();
110+
return new ValidRFC5424Timestamp(new ValidStringKey(record, "TimeGenerated").value()).validTimestamp();
128111
}
129112

130113
@Override

src/main/java/com/teragrep/nlf_01/types/ADFPipelineRunType.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
import com.teragrep.nlf_01.util.ASCIIString;
5252
import com.teragrep.nlf_01.util.MD5Hash;
5353
import com.teragrep.nlf_01.util.ResourceId;
54+
import com.teragrep.nlf_01.util.ValidKey;
55+
import com.teragrep.nlf_01.util.ValidStringKey;
5456
import com.teragrep.nlf_01.util.ValidRFC5424AppName;
5557
import com.teragrep.nlf_01.util.ValidRFC5424Hostname;
5658
import com.teragrep.nlf_01.util.ValidRFC5424Timestamp;
5759
import com.teragrep.rlo_14.Facility;
5860
import com.teragrep.rlo_14.SDElement;
5961
import com.teragrep.rlo_14.Severity;
6062
import jakarta.json.JsonObject;
61-
import jakarta.json.JsonValue;
6263
import java.time.Instant;
6364
import java.util.HashSet;
6465
import java.util.Set;
@@ -74,17 +75,6 @@ public ADFPipelineRunType(final ParsedEvent parsedEvent, final String realHostna
7475
this.realHostname = realHostname;
7576
}
7677

77-
private void assertKey(final JsonObject obj, final String key, final JsonValue.ValueType type)
78-
throws PluginException {
79-
if (!obj.containsKey(key)) {
80-
throw new PluginException(new IllegalArgumentException("Key " + key + " does not exist"));
81-
}
82-
83-
if (!obj.get(key).getValueType().equals(type)) {
84-
throw new PluginException(new IllegalArgumentException("Key " + key + " is not of type " + type));
85-
}
86-
}
87-
8878
@Override
8979
public Severity severity() {
9080
return Severity.NOTICE;
@@ -99,32 +89,27 @@ public Facility facility() {
9989
public String hostname() throws PluginException {
10090
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
10191

102-
assertKey(record, "_ResourceId", JsonValue.ValueType.STRING);
103-
final String resourceId = record.getString("_ResourceId");
92+
final ValidKey<String> validKey = new ValidStringKey(record, "_ResourceId");
10493

10594
return new ValidRFC5424Hostname(
106-
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
95+
"md5-".concat(new MD5Hash(validKey.value()).md5().concat("-").concat(new ASCIIString(new ResourceId(validKey.value()).resourceName()).withNonAsciiCharsRemoved()))
10796
).hostnameWithInvalidCharsRemoved();
108-
10997
}
11098

11199
@Override
112100
public String appName() throws PluginException {
113101
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
114102

115-
assertKey(record, "PipelineName", JsonValue.ValueType.STRING);
116-
117-
return new ValidRFC5424AppName(record.getString("PipelineName")).appName();
118-
103+
return new ValidRFC5424AppName(
104+
new ASCIIString(new ValidStringKey(record, "PipelineName").value()).withNonAsciiCharsRemoved()
105+
).appName();
119106
}
120107

121108
@Override
122109
public long timestamp() throws PluginException {
123110
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
124111

125-
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
126-
127-
return new ValidRFC5424Timestamp(record.getString("TimeGenerated")).validTimestamp();
112+
return new ValidRFC5424Timestamp(new ValidStringKey(record, "TimeGenerated").value()).validTimestamp();
128113
}
129114

130115
@Override

src/main/java/com/teragrep/nlf_01/types/AppInsightType.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import com.teragrep.rlo_14.SDElement;
5454
import com.teragrep.rlo_14.Severity;
5555
import jakarta.json.JsonObject;
56-
import jakarta.json.JsonValue;
5756

5857
import java.time.Instant;
5958
import java.util.HashSet;
@@ -70,17 +69,6 @@ public AppInsightType(final ParsedEvent parsedEvent, final String realHostname)
7069
this.realHostname = realHostname;
7170
}
7271

73-
private void assertKey(final JsonObject obj, final String key, final JsonValue.ValueType type)
74-
throws PluginException {
75-
if (!obj.containsKey(key)) {
76-
throw new PluginException(new IllegalArgumentException("Key " + key + " does not exist"));
77-
}
78-
79-
if (!obj.get(key).getValueType().equals(type)) {
80-
throw new PluginException(new IllegalArgumentException("Key " + key + " is not of type " + type));
81-
}
82-
}
83-
8472
@Override
8573
public Severity severity() {
8674
return Severity.NOTICE;
@@ -95,11 +83,10 @@ public Facility facility() {
9583
public String hostname() throws PluginException {
9684
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
9785

98-
assertKey(record, "_ResourceId", JsonValue.ValueType.STRING);
99-
final String resourceId = record.getString("_ResourceId");
86+
final ValidKey<String> validKey = new ValidStringKey(record, "_ResourceId");
10087

10188
return new ValidRFC5424Hostname(
102-
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
89+
"md5-".concat(new MD5Hash(validKey.value()).md5().concat("-").concat(new ASCIIString(new ResourceId(validKey.value()).resourceName()).withNonAsciiCharsRemoved()))
10390
).hostnameWithInvalidCharsRemoved();
10491

10592
}
@@ -108,19 +95,16 @@ public String hostname() throws PluginException {
10895
public String appName() throws PluginException {
10996
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
11097

111-
assertKey(record, "AppRoleName", JsonValue.ValueType.STRING);
112-
113-
return new ValidRFC5424AppName(record.getString("AppRoleName")).appName();
114-
98+
return new ValidRFC5424AppName(
99+
new ASCIIString(new ValidStringKey(record, "AppRoleName").value()).withNonAsciiCharsRemoved()
100+
).appName();
115101
}
116102

117103
@Override
118104
public long timestamp() throws PluginException {
119105
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
120106

121-
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
122-
123-
return new ValidRFC5424Timestamp(record.getString("TimeGenerated")).validTimestamp();
107+
return new ValidRFC5424Timestamp(new ValidStringKey(record, "TimeGenerated").value()).validTimestamp();
124108
}
125109

126110
@Override

src/main/java/com/teragrep/nlf_01/types/AppServiceConsoleLogsType.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
import com.teragrep.nlf_01.util.ASCIIString;
5252
import com.teragrep.nlf_01.util.MD5Hash;
5353
import com.teragrep.nlf_01.util.ResourceId;
54+
import com.teragrep.nlf_01.util.ValidKey;
55+
import com.teragrep.nlf_01.util.ValidStringKey;
5456
import com.teragrep.nlf_01.util.ValidRFC5424AppName;
5557
import com.teragrep.nlf_01.util.ValidRFC5424Hostname;
5658
import com.teragrep.nlf_01.util.ValidRFC5424Timestamp;
5759
import com.teragrep.rlo_14.Facility;
5860
import com.teragrep.rlo_14.SDElement;
5961
import com.teragrep.rlo_14.Severity;
6062
import jakarta.json.JsonObject;
61-
import jakarta.json.JsonValue;
6263
import java.time.Instant;
6364
import java.util.HashSet;
6465
import java.util.Set;
@@ -74,17 +75,6 @@ public AppServiceConsoleLogsType(final ParsedEvent parsedEvent, final String rea
7475
this.realHostname = realHostname;
7576
}
7677

77-
private void assertKey(final JsonObject obj, final String key, final JsonValue.ValueType type)
78-
throws PluginException {
79-
if (!obj.containsKey(key)) {
80-
throw new PluginException(new IllegalArgumentException("Key " + key + " does not exist"));
81-
}
82-
83-
if (!obj.get(key).getValueType().equals(type)) {
84-
throw new PluginException(new IllegalArgumentException("Key " + key + " is not of type " + type));
85-
}
86-
}
87-
8878
@Override
8979
public Severity severity() {
9080
return Severity.NOTICE;
@@ -99,30 +89,25 @@ public Facility facility() {
9989
public String hostname() throws PluginException {
10090
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
10191

102-
assertKey(record, "_ResourceId", JsonValue.ValueType.STRING);
103-
final String resourceId = record.getString("_ResourceId");
92+
final ValidKey<String> validKey = new ValidStringKey(record, "_ResourceId");
10493

10594
return new ValidRFC5424Hostname(
106-
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
95+
"md5-".concat(new MD5Hash(validKey.value()).md5().concat("-").concat(new ASCIIString(new ResourceId(validKey.value()).resourceName()).withNonAsciiCharsRemoved()))
10796
).hostnameWithInvalidCharsRemoved();
10897
}
10998

11099
@Override
111100
public String appName() throws PluginException {
112101
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
113102

114-
assertKey(record, "Type", JsonValue.ValueType.STRING);
115-
116-
return new ValidRFC5424AppName(record.getString("Type")).appName();
103+
return new ValidRFC5424AppName(new ValidStringKey(record, "Type").value()).appName();
117104
}
118105

119106
@Override
120107
public long timestamp() throws PluginException {
121108
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
122109

123-
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
124-
125-
return new ValidRFC5424Timestamp(record.getString("TimeGenerated")).validTimestamp();
110+
return new ValidRFC5424Timestamp(new ValidStringKey(record, "TimeGenerated").value()).validTimestamp();
126111
}
127112

128113
@Override

src/main/java/com/teragrep/nlf_01/types/CCType.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@
5151
import com.teragrep.nlf_01.util.ASCIIString;
5252
import com.teragrep.nlf_01.util.MD5Hash;
5353
import com.teragrep.nlf_01.util.ResourceId;
54+
import com.teragrep.nlf_01.util.ValidJsonObjectKey;
55+
import com.teragrep.nlf_01.util.ValidKey;
56+
import com.teragrep.nlf_01.util.ValidStringKey;
5457
import com.teragrep.nlf_01.util.ValidRFC5424AppName;
5558
import com.teragrep.nlf_01.util.ValidRFC5424Hostname;
5659
import com.teragrep.nlf_01.util.ValidRFC5424Timestamp;
5760
import com.teragrep.rlo_14.Facility;
5861
import com.teragrep.rlo_14.SDElement;
5962
import com.teragrep.rlo_14.Severity;
6063
import jakarta.json.JsonObject;
61-
import jakarta.json.JsonValue;
6264
import java.time.Instant;
6365
import java.util.HashSet;
6466
import java.util.Set;
@@ -85,17 +87,6 @@ private CCType(final ParsedEvent parsedEvent, final String realHostname, final P
8587
this.appNamePattern = appNamePattern;
8688
}
8789

88-
private void assertKey(final JsonObject obj, final String key, final JsonValue.ValueType type)
89-
throws PluginException {
90-
if (!obj.containsKey(key)) {
91-
throw new PluginException(new IllegalArgumentException("Key " + key + " does not exist"));
92-
}
93-
94-
if (!obj.get(key).getValueType().equals(type)) {
95-
throw new PluginException(new IllegalArgumentException("Key " + key + " is not of type " + type));
96-
}
97-
}
98-
9990
@Override
10091
public Severity severity() throws PluginException {
10192
return Severity.NOTICE;
@@ -110,22 +101,23 @@ public Facility facility() throws PluginException {
110101
public String hostname() throws PluginException {
111102
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
112103

113-
assertKey(record, "_Internal_WorkspaceResourceId", JsonValue.ValueType.STRING);
114-
final String resourceId = record.getString("_Internal_WorkspaceResourceId");
104+
final ValidKey<String> validKey = new ValidStringKey(record, "_Internal_WorkspaceResourceId");
115105

116106
return new ValidRFC5424Hostname(
117-
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
107+
"md5-".concat(new MD5Hash(validKey.value()).md5().concat("-").concat(new ASCIIString(new ResourceId(validKey.value()).resourceName()).withNonAsciiCharsRemoved()))
118108
).hostnameWithInvalidCharsRemoved();
119109
}
120110

121111
@Override
122112
public String appName() throws PluginException {
123113
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
124114

125-
assertKey(record, "data", JsonValue.ValueType.OBJECT);
126-
final JsonObject data = record.getJsonObject("data");
127-
assertKey(data, "resourceName", JsonValue.ValueType.STRING);
128-
final String resourceName = data.getString("resourceName");
115+
final ValidKey<JsonObject> validData = new ValidJsonObjectKey(record, "data");
116+
117+
final JsonObject data = validData.value();
118+
final ValidKey<String> validResourceName = new ValidStringKey(data, "resourceName");
119+
120+
final String resourceName = validResourceName.value();
129121

130122
final Matcher matcher = appNamePattern.matcher(resourceName);
131123
if (!matcher.find()) {
@@ -142,10 +134,8 @@ public String appName() throws PluginException {
142134
@Override
143135
public long timestamp() throws PluginException {
144136
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
145-
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
146-
final String time = record.getString("TimeGenerated");
147137

148-
return new ValidRFC5424Timestamp(time).validTimestamp();
138+
return new ValidRFC5424Timestamp(new ValidStringKey(record, "TimeGenerated").value()).validTimestamp();
149139
}
150140

151141
@Override

0 commit comments

Comments
 (0)