Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit 5c1cf07

Browse files
committed
Add validation using com.networknt validator; tests pass
1 parent bb7bbbd commit 5c1cf07

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repositories {
1313
}
1414

1515
dependencies {
16-
implementation 'net.jimblackler.jsonschemafriend:core:0.11.2'
16+
implementation 'com.networknt:json-schema-validator:1.0.64'
1717
implementation 'com.fasterxml.jackson.core:jackson-databind:2.0.1'
1818
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
1919
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
11
package org.idmef;
22

3-
import net.jimblackler.jsonschemafriend.*;
43

5-
import java.net.URL;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.networknt.schema.JsonSchema;
7+
import com.networknt.schema.JsonSchemaFactory;
8+
import com.networknt.schema.SpecVersion;
9+
import com.networknt.schema.ValidationMessage;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.util.Set;
614

715
public class IDMEFValidator {
816
private static final String SCHEMA_RESOURCE_PATH = "/IDMEFv2.schema";
917

1018
/**
11-
* Constructs an empty Message.
19+
* Constructs a Validator.
1220
*/
13-
public IDMEFValidator() { }
21+
public IDMEFValidator() {
22+
}
23+
24+
private boolean validateJsonNode(JsonNode jsonNode) throws IDMEFException {
25+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
26+
InputStream is = IDMEFValidator.class.getResourceAsStream(SCHEMA_RESOURCE_PATH);
27+
if (is == null)
28+
throw new IDMEFException("cannot load schema");
29+
30+
JsonSchema schema = factory.getSchema(is);
31+
32+
Set<ValidationMessage> errors = schema.validate(jsonNode);
33+
34+
return errors.size() == 0;
35+
}
1436

1537
/**
16-
* Validate the Message content w.r.t. current IDMEF JSON schema.
38+
* Validate the object content w.r.t. current IDMEF JSON schema.
1739
*
18-
* @throws IDMEFException if the Message is not valid.
1940
*/
20-
public void validate(IDMEFObject idmefObject) throws IDMEFException {
21-
URL r = IDMEFValidator.class.getResource(SCHEMA_RESOURCE_PATH);
22-
if (r == null)
23-
throw new IDMEFException("Cannot locate schema resource");
24-
25-
Schema schema;
26-
27-
SchemaStore schemaStore = new SchemaStore();
28-
try {
29-
schema = schemaStore.loadSchema(r);
30-
} catch (GenerationException e) {
31-
throw new IDMEFException("error loading schema:" + e.getMessage());
32-
}
33-
34-
Validator validator = new Validator();
35-
36-
try {
37-
validator.validate(schema, idmefObject);
38-
} catch (ValidationException e) {
39-
throw new IDMEFException("error validating:" + e.getMessage());
40-
}
41+
public boolean validate(IDMEFObject idmefObject) throws IDMEFException {
42+
ObjectMapper objectMapper = new ObjectMapper();
43+
JsonNode jsonNode = objectMapper.valueToTree(idmefObject);
44+
45+
return validateJsonNode(jsonNode);
4146
}
4247

48+
/**
49+
* Validate the bytes w.r.t. current IDMEF JSON schema.
50+
*
51+
*/
52+
public boolean validate(byte[] json) throws IOException {
53+
ObjectMapper objectMapper = new ObjectMapper();
54+
JsonNode jsonNode = objectMapper.readTree(json);
55+
56+
return validateJsonNode(jsonNode);
57+
}
4358
}

src/test/java/TestValidate.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
1+
import org.idmef.IDMEFException;
12
import org.idmef.IDMEFObject;
23
import org.idmef.IDMEFValidator;
34
import org.junit.jupiter.api.Test;
45

6+
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
8+
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
510
import static org.junit.jupiter.api.Assertions.fail;
611

712
public class TestValidate {
813

9-
private static void validate(IDMEFObject idmefObject) {
14+
private static boolean validate(IDMEFObject idmefObject) {
15+
IDMEFValidator validator = new IDMEFValidator();
16+
17+
try {
18+
return validator.validate(idmefObject);
19+
} catch (IDMEFException e) {
20+
fail(e.getMessage());
21+
}
22+
23+
return false;
24+
}
25+
26+
private static boolean validate(byte[] json) {
1027
IDMEFValidator validator = new IDMEFValidator();
1128

1229
try {
13-
validator.validate(idmefObject);
14-
} catch (Exception e) {
30+
return validator.validate(json);
31+
} catch (IOException e) {
1532
fail(e.getMessage());
1633
}
34+
35+
return false;
1736
}
1837

1938
@Test
2039
void testValidateMessage1() {
21-
validate(Util.message1());
40+
assertTrue(validate(Util.message1()));
2241
}
2342

43+
@Test
44+
void testValidateString1() {
45+
assertTrue(validate(Util.string1().getBytes(StandardCharsets.UTF_8)));
46+
}
47+
48+
@Test
49+
void testValidateMessage2() {
50+
assertTrue(validate(Util.message2()));
51+
}
52+
53+
@Test
54+
void testValidateString2() {
55+
assertTrue(validate(Util.string2().getBytes(StandardCharsets.UTF_8)));
56+
}
2457
}

src/test/java/Util.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ static IDMEFObject message1() {
2929
analyzer.put("Data", new String[]{"Log"});
3030
analyzer.put("Method", new String[]{"Monitor"});
3131

32+
msg.put("Analyzer", analyzer);
33+
3234
return msg;
3335
}
3436

0 commit comments

Comments
 (0)