void add(String name, Object object) {
+ collectorMap.put(name, object);
+ }
- /**
- *
- * Gets the data associated with a given name. Please note if you are using a
- * Collector you should wait till the validation is complete to gather all data.
- *
- * For a Collector, this method will return the collector as long as load method
- * is not called. Once the load method is called this method will return the
- * actual data collected by collector.
- *
- * @param name
- * @return
- */
- public Object get(String name) {
- Object object = collectorMap.get(name);
- if (object instanceof Collector> && (collectorLoadMap.get(name) != null)) {
- return collectorLoadMap.get(name);
- }
- return collectorMap.get(name);
- }
+ /**
+ * Gets the data associated with a given name. Please note if you are using a
+ * Collector you should wait till the validation is complete to gather all data.
+ *
+ * For a Collector, this method will return the collector as long as load method
+ * is not called. Once the load method is called this method will return the
+ * actual data collected by collector.
+ *
+ * @param name String
+ * @return Object
+ */
+ public Object get(String name) {
+ Object object = collectorMap.get(name);
+ if (object instanceof Collector> && (collectorLoadMap.get(name) != null)) {
+ return collectorLoadMap.get(name);
+ }
+ return collectorMap.get(name);
+ }
- /**
- *
- * Combines data with Collector identified by the given name.
- *
- * @param name
- * @param data
- */
- public void combineWithCollector(String name, Object data) {
- Object object = collectorMap.get(name);
- if (object instanceof Collector>) {
- Collector> collector = (Collector>) object;
- collector.combine(data);
- }
- }
+ /**
+ * Combines data with Collector identified by the given name.
+ *
+ * @param name String
+ * @param data Object
+ */
+ public void combineWithCollector(String name, Object data) {
+ Object object = collectorMap.get(name);
+ if (object instanceof Collector>) {
+ Collector> collector = (Collector>) object;
+ collector.combine(data);
+ }
+ }
- /**
- * Reset the context
- */
- void reset() {
- this.collectorMap = new HashMap();
- this.collectorLoadMap = new HashMap();
- }
+ /**
+ * Reset the context
+ */
+ void reset() {
+ this.collectorMap = new HashMap();
+ this.collectorLoadMap = new HashMap();
+ }
- /**
- * Loads data from all collectors.
- */
- void loadCollectors() {
- Set> entrySet = collectorMap.entrySet();
- for (Entry entry : entrySet) {
- if (entry.getValue() instanceof Collector>) {
- Collector> collector = (Collector>) entry.getValue();
- collectorLoadMap.put(entry.getKey(), collector.collect());
- }
- }
+ /**
+ * Loads data from all collectors.
+ */
+ void loadCollectors() {
+ Set> entrySet = collectorMap.entrySet();
+ for (Entry entry : entrySet) {
+ if (entry.getValue() instanceof Collector>) {
+ Collector> collector = (Collector>) entry.getValue();
+ collectorLoadMap.put(entry.getKey(), collector.collect());
+ }
+ }
- }
+ }
}
diff --git a/src/main/java/com/networknt/schema/ConstValidator.java b/src/main/java/com/networknt/schema/ConstValidator.java
index 33dd30caf..fd3f82552 100644
--- a/src/main/java/com/networknt/schema/ConstValidator.java
+++ b/src/main/java/com/networknt/schema/ConstValidator.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/main/java/com/networknt/schema/FalseValidator.java b/src/main/java/com/networknt/schema/FalseValidator.java
index 2acea2cec..a1bca6eb2 100644
--- a/src/main/java/com/networknt/schema/FalseValidator.java
+++ b/src/main/java/com/networknt/schema/FalseValidator.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java
index b725e5979..95a94faef 100644
--- a/src/main/java/com/networknt/schema/JsonSchema.java
+++ b/src/main/java/com/networknt/schema/JsonSchema.java
@@ -216,31 +216,31 @@ public ValidationResult validateAndCollect(JsonNode node) {
return validateAndCollect(node, node, AT_ROOT);
}
-
- /**
- *
- * This method both validates and collects the data in a CollectionContext.
- * @param jsonNode
- * @param rootNode
- * @param at
- * @return
- */
- protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode, String at) {
- try {
- // Create the collector context object.
- CollectorContext collectorContext = new CollectorContext();
- // Set the collector context in thread info, this is unique for every thread.
- ThreadInfo.set(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY, collectorContext);
- Set errors = validate(jsonNode, rootNode, at);
- // Load all the data from collectors into the context.
- collectorContext.loadCollectors();
- // Collect errors and collector context into validation result.
- ValidationResult validationResult = new ValidationResult(errors, collectorContext);
- return validationResult;
- } finally {
- ThreadInfo.remove(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY);
- }
- }
+
+ /**
+ * This method both validates and collects the data in a CollectionContext.
+ *
+ * @param jsonNode JsonNode
+ * @param rootNode JsonNode
+ * @param at String path
+ * @return ValidationResult
+ */
+ protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode, String at) {
+ try {
+ // Create the collector context object.
+ CollectorContext collectorContext = new CollectorContext();
+ // Set the collector context in thread info, this is unique for every thread.
+ ThreadInfo.set(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY, collectorContext);
+ Set errors = validate(jsonNode, rootNode, at);
+ // Load all the data from collectors into the context.
+ collectorContext.loadCollectors();
+ // Collect errors and collector context into validation result.
+ ValidationResult validationResult = new ValidationResult(errors, collectorContext);
+ return validationResult;
+ } finally {
+ ThreadInfo.remove(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY);
+ }
+ }
@Override
public String toString() {
@@ -254,9 +254,9 @@ public boolean hasRequiredValidator() {
public JsonValidator getRequiredValidator() {
return requiredValidator;
}
-
+
public Map getValidators() {
- return validators;
+ return validators;
}
}
diff --git a/src/main/java/com/networknt/schema/JsonSchemaRef.java b/src/main/java/com/networknt/schema/JsonSchemaRef.java
index 3af98d38b..df654dcaf 100644
--- a/src/main/java/com/networknt/schema/JsonSchemaRef.java
+++ b/src/main/java/com/networknt/schema/JsonSchemaRef.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/main/java/com/networknt/schema/SpecVersion.java b/src/main/java/com/networknt/schema/SpecVersion.java
index cdb70deef..d67294181 100644
--- a/src/main/java/com/networknt/schema/SpecVersion.java
+++ b/src/main/java/com/networknt/schema/SpecVersion.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import java.util.EnumSet;
diff --git a/src/main/java/com/networknt/schema/ThreadInfo.java b/src/main/java/com/networknt/schema/ThreadInfo.java
index fb543d86b..e09a86c62 100644
--- a/src/main/java/com/networknt/schema/ThreadInfo.java
+++ b/src/main/java/com/networknt/schema/ThreadInfo.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import java.util.HashMap;
diff --git a/src/main/java/com/networknt/schema/TrueValidator.java b/src/main/java/com/networknt/schema/TrueValidator.java
index 27acbcc17..54f33f21b 100644
--- a/src/main/java/com/networknt/schema/TrueValidator.java
+++ b/src/main/java/com/networknt/schema/TrueValidator.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/main/java/com/networknt/schema/ValidationResult.java b/src/main/java/com/networknt/schema/ValidationResult.java
index d35c38cdf..1b247fbfb 100644
--- a/src/main/java/com/networknt/schema/ValidationResult.java
+++ b/src/main/java/com/networknt/schema/ValidationResult.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import java.util.Set;
diff --git a/src/main/java/com/networknt/schema/ValidatorState.java b/src/main/java/com/networknt/schema/ValidatorState.java
index 798067517..8770cd6ad 100644
--- a/src/main/java/com/networknt/schema/ValidatorState.java
+++ b/src/main/java/com/networknt/schema/ValidatorState.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
public class ValidatorState {
diff --git a/src/test/java/com/networknt/schema/CollectorContextTest.java b/src/test/java/com/networknt/schema/CollectorContextTest.java
index ff9a78784..924ec8b45 100644
--- a/src/test/java/com/networknt/schema/CollectorContextTest.java
+++ b/src/test/java/com/networknt/schema/CollectorContextTest.java
@@ -1,370 +1,369 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.*;
public class CollectorContextTest {
- private static final String SAMPLE_COLLECTOR = "sampleCollectorType";
-
- private JsonSchema jsonSchema;
-
- private JsonSchema jsonSchemaForCombine;
-
-
- @Before
- public void setup() throws Exception {
- setupSchema();
- }
-
-
- @SuppressWarnings("unchecked")
- @Test
- public void testCollectorContextWithKeyword() throws Exception {
- ValidationResult validationResult = validate("{\"test-property1\":\"sample1\",\"test-property2\":\"sample2\"}");
- Assert.assertEquals(0, validationResult.getValidationMessages().size());
- List contextValues = (List) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR);
- Assert.assertEquals(0, validationResult.getValidationMessages().size());
- Assert.assertEquals(2, contextValues.size());
- Assert.assertEquals(contextValues.get(0), "actual_value_added_to_context1");
- Assert.assertEquals(contextValues.get(1), "actual_value_added_to_context2");
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testCollectorContextWithMultiplThreads() throws Exception {
-
- ValidationThread validationRunnable1 = new ValidationThread("{\"test-property1\":\"sample1\" }", "thread1");
- ValidationThread validationRunnable2 = new ValidationThread("{\"test-property1\":\"sample2\" }", "thread2");
- ValidationThread validationRunnable3 = new ValidationThread("{\"test-property1\":\"sample3\" }", "thread3");
-
- // This simulates calling the validateAndCollect method from three different
- // threads.It should be noted that all these three threads use same
- // json schema instance.Create three threads with there own Runnables.
- Thread thread1 = new Thread(validationRunnable1);
- Thread thread2 = new Thread(validationRunnable2);
- Thread thread3 = new Thread(validationRunnable3);
-
- thread1.start();
- thread2.start();
- thread3.start();
-
- thread1.join();
- thread2.join();
- thread3.join();
-
- ValidationResult validationResult1 = validationRunnable1.getValidationResult();
- ValidationResult validationResult2 = validationRunnable2.getValidationResult();
- ValidationResult validationResult3 = validationRunnable3.getValidationResult();
-
- Assert.assertEquals(0, validationResult1.getValidationMessages().size());
- Assert.assertEquals(0, validationResult2.getValidationMessages().size());
- Assert.assertEquals(0, validationResult3.getValidationMessages().size());
-
- List contextValue1 = (List) validationResult1.getCollectorContext().get(SAMPLE_COLLECTOR);
- List contextValue2 = (List) validationResult2.getCollectorContext().get(SAMPLE_COLLECTOR);
- List contextValue3 = (List) validationResult3.getCollectorContext().get(SAMPLE_COLLECTOR);
-
- Assert.assertEquals(contextValue1.get(0), "actual_value_added_to_context1");
- Assert.assertEquals(contextValue2.get(0), "actual_value_added_to_context2");
- Assert.assertEquals(contextValue3.get(0), "actual_value_added_to_context3");
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void testCollectorWithFormat() throws JsonMappingException, JsonProcessingException {
- ObjectMapper objectMapper = new ObjectMapper();
- ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper.readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }"));
- List values = (List) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR);
- Assert.assertEquals(values.size(), 4);
- }
-
- private JsonMetaSchema getJsonMetaSchema(String uri) throws Exception {
- JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(uri, JsonMetaSchema.getV201909())
- .addKeyword(new CustomKeyword()).addKeyword(new CustomKeyword1()).addFormat(new Format() {
-
- @SuppressWarnings("unchecked")
- @Override
- public boolean matches(String value) {
- CollectorContext collectorContext = CollectorContext.getInstance();
- if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
- collectorContext.add(SAMPLE_COLLECTOR, new ArrayList());
- }
- List returnList = (List) collectorContext.get(SAMPLE_COLLECTOR);
- returnList.add(value);
- return true;
- }
-
- @Override
- public String getName() {
- return "sample-format";
- }
-
- // Return null. As are just testing collection context.
- @Override
- public String getErrorMessageDescription() {
- return null;
- }
- }).build();
- return jsonMetaSchema;
- }
-
- private void setupSchema() throws Exception {
- final JsonMetaSchema metaSchema = getJsonMetaSchema(
- "https://github.com/networknt/json-schema-validator/tests/schemas/example01#");
- final JsonSchemaFactory schemaFactory = JsonSchemaFactory
- .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
- .build();
- this.jsonSchema = schemaFactory.getSchema(getSchemaString());
- this.jsonSchemaForCombine = schemaFactory.getSchema(getSchemaStringMultipleProperties());
- }
-
- private String getSchemaString() {
- return "{"
- + "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
- + "\"title\" : \"Sample test schema\",\n"
- + "\"description\" : \"Sample schema definition\","
- + "\"type\" : \"object\","
- + "\"properties\" :"
- + "{"
- + "\"test-property1\" : "
- + "{"
- + "\"title\": \"Test Property1\","
- + "\"type\": \"string\", "
- + "\"custom-keyword\":[\"x\",\"y\"]"
- + "},"
- + "\"test-property2\" : "
- + "{"
- + "\"title\": \"Test Property2\","
- + "\"type\": \"string\", "
- + "\"custom-keyword\":[\"x\",\"y\"]"
- + "}"
- + "},"
- +"\"additionalProperties\":\"false\","
- + "\"required\": [\"test-property1\"]\n"
- + "}";
- }
-
- private String getSchemaStringMultipleProperties() {
- return "{"
- + "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
- + "\"title\" : \"Sample test schema\","
- + "\"description\" : \"Sample schema definition\","
- + "\"type\" : \"object\","
- + "\"properties\" :"
- + "{"
- + "\"property1\" : "
- + "{"
- + "\"title\": \"Property1\","
- + "\"type\": \"string\", "
- + "\"custom-keyword1\":[\"x\",\"y\"],"
- + "\"format\":\"sample-format\""
- + "},"
- + "\"property2\" : "
- + "{"
- + "\"title\": \"Property2\","
- + "\"type\": \"string\", "
- + "\"custom-keyword1\":[\"x\",\"y\"]"
- + "},"
- + "\"property3\" : "
- + "{"
- + "\"title\": \"Property3\","
- + "\"type\": \"string\", "
- + "\"custom-keyword1\":[\"x\",\"y\"]"
- + "}"
- + "}"
- + "}";
- }
-
-
-
-
- private class ValidationThread implements Runnable {
-
- private String data;
-
- private String name;
-
- private ValidationResult validationResult;
-
- ValidationThread(String data,String name) {
- this.name = name;
- this.data = data;
- }
-
- @Override
- public void run() {
- try {
- this.validationResult = validate(data);
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- ValidationResult getValidationResult() {
- return this.validationResult;
- }
-
- @Override
- public String toString() {
- return "ValidationThread [data=" + data + ", name=" + name + ", validationResult=" + validationResult + "]";
- }
-
- }
-
- /**
- *
- * Our own custom keyword. In this case we don't use this keyword. It is just
- * for demonstration purpose.
- *
- */
- private class CustomKeyword implements Keyword {
- @Override
- public String getValue() {
- return "custom-keyword";
- }
-
- @Override
- public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
- ValidationContext validationContext) throws JsonSchemaException, Exception {
- if (schemaNode != null && schemaNode.isArray()) {
- return new CustomValidator();
- }
- return null;
- }
- }
-
-
- /**
- *
- * We will be collecting information/data by adding the data in the form of
- * collectors into collector context object while we are validating this node.
- * This will be helpful in cases where we don't want to revisit the entire JSON
- * document again just for gathering this kind of information.
- *
- */
- private class CustomValidator implements JsonValidator {
-
- @Override
- public Set validate(JsonNode node, JsonNode rootNode, String at) {
- // Get an instance of collector context.
- CollectorContext collectorContext = CollectorContext.getInstance();
- if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
- collectorContext.add(SAMPLE_COLLECTOR, new CustomCollector());
- }
- collectorContext.combineWithCollector(SAMPLE_COLLECTOR, node.textValue());
- return new TreeSet();
- }
-
- @Override
- public Set validate(JsonNode rootNode) {
- return validate(rootNode, rootNode, BaseJsonValidator.AT_ROOT);
- }
-
- }
-
- private class CustomCollector extends AbstractCollector> {
-
- List returnList = new ArrayList();
-
- private Map referenceMap = null;
-
- public CustomCollector() {
- referenceMap = getDatasourceMap();
- }
-
- @Override
- public List collect() {
- return returnList;
- }
-
- @Override
- public void combine(Object object) {
- returnList.add(referenceMap.get((String) object));
- }
-
- }
-
- /**
- *
- * Our own custom keyword. In this case we don't use this keyword. It is just
- * for demonstration purpose.
- *
- */
- private class CustomKeyword1 implements Keyword {
- @Override
- public String getValue() {
- return "custom-keyword1";
- }
-
- @Override
- public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
- ValidationContext validationContext) throws JsonSchemaException, Exception {
- if (schemaNode != null && schemaNode.isArray()) {
- return new CustomValidator1();
- }
- return null;
- }
- }
-
- /**
- *
- * We will be collecting information/data by adding the data in the form of
- * collectors into collector context object while we are validating this node.
- * This will be helpful in cases where we don't want to revisit the entire JSON
- * document again just for gathering this kind of information. In this test case
- * we expect this validator to be called multiple times as the associated
- * keyword has been used multiple times in JSON Schema.
- *
- */
- private class CustomValidator1 implements JsonValidator {
- @SuppressWarnings("unchecked")
- @Override
- public Set validate(JsonNode node, JsonNode rootNode, String at) {
- // Get an instance of collector context.
- CollectorContext collectorContext = CollectorContext.getInstance();
- // If collector type is not added to context add one.
- if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
- collectorContext.add(SAMPLE_COLLECTOR, new ArrayList());
- }
- List returnList = (List) collectorContext.get(SAMPLE_COLLECTOR);
- returnList.add(node.textValue());
- return new TreeSet();
- }
-
- @Override
- public Set validate(JsonNode rootNode) {
- return validate(rootNode, rootNode, BaseJsonValidator.AT_ROOT);
- }
- }
-
- private ValidationResult validate(String jsonData) throws JsonMappingException, JsonProcessingException, Exception {
- ObjectMapper objectMapper = new ObjectMapper();
- return this.jsonSchema.validateAndCollect(objectMapper.readTree(jsonData));
- }
-
- private Map getDatasourceMap() {
- Map map = new HashMap();
- map.put("sample1", "actual_value_added_to_context1");
- map.put("sample2", "actual_value_added_to_context2");
- map.put("sample3", "actual_value_added_to_context3");
- return map;
- }
+ private static final String SAMPLE_COLLECTOR = "sampleCollectorType";
+
+ private JsonSchema jsonSchema;
+
+ private JsonSchema jsonSchemaForCombine;
+
+
+ @Before
+ public void setup() throws Exception {
+ setupSchema();
+ }
+
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testCollectorContextWithKeyword() throws Exception {
+ ValidationResult validationResult = validate("{\"test-property1\":\"sample1\",\"test-property2\":\"sample2\"}");
+ Assert.assertEquals(0, validationResult.getValidationMessages().size());
+ List contextValues = (List) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR);
+ Assert.assertEquals(0, validationResult.getValidationMessages().size());
+ Assert.assertEquals(2, contextValues.size());
+ Assert.assertEquals(contextValues.get(0), "actual_value_added_to_context1");
+ Assert.assertEquals(contextValues.get(1), "actual_value_added_to_context2");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testCollectorContextWithMultiplThreads() throws Exception {
+
+ ValidationThread validationRunnable1 = new ValidationThread("{\"test-property1\":\"sample1\" }", "thread1");
+ ValidationThread validationRunnable2 = new ValidationThread("{\"test-property1\":\"sample2\" }", "thread2");
+ ValidationThread validationRunnable3 = new ValidationThread("{\"test-property1\":\"sample3\" }", "thread3");
+
+ // This simulates calling the validateAndCollect method from three different
+ // threads.It should be noted that all these three threads use same
+ // json schema instance.Create three threads with there own Runnables.
+ Thread thread1 = new Thread(validationRunnable1);
+ Thread thread2 = new Thread(validationRunnable2);
+ Thread thread3 = new Thread(validationRunnable3);
+
+ thread1.start();
+ thread2.start();
+ thread3.start();
+
+ thread1.join();
+ thread2.join();
+ thread3.join();
+
+ ValidationResult validationResult1 = validationRunnable1.getValidationResult();
+ ValidationResult validationResult2 = validationRunnable2.getValidationResult();
+ ValidationResult validationResult3 = validationRunnable3.getValidationResult();
+
+ Assert.assertEquals(0, validationResult1.getValidationMessages().size());
+ Assert.assertEquals(0, validationResult2.getValidationMessages().size());
+ Assert.assertEquals(0, validationResult3.getValidationMessages().size());
+
+ List contextValue1 = (List) validationResult1.getCollectorContext().get(SAMPLE_COLLECTOR);
+ List contextValue2 = (List) validationResult2.getCollectorContext().get(SAMPLE_COLLECTOR);
+ List contextValue3 = (List) validationResult3.getCollectorContext().get(SAMPLE_COLLECTOR);
+
+ Assert.assertEquals(contextValue1.get(0), "actual_value_added_to_context1");
+ Assert.assertEquals(contextValue2.get(0), "actual_value_added_to_context2");
+ Assert.assertEquals(contextValue3.get(0), "actual_value_added_to_context3");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testCollectorWithFormat() throws JsonMappingException, JsonProcessingException {
+ ObjectMapper objectMapper = new ObjectMapper();
+ ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper.readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }"));
+ List values = (List) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR);
+ Assert.assertEquals(values.size(), 4);
+ }
+
+ private JsonMetaSchema getJsonMetaSchema(String uri) throws Exception {
+ JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(uri, JsonMetaSchema.getV201909())
+ .addKeyword(new CustomKeyword()).addKeyword(new CustomKeyword1()).addFormat(new Format() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean matches(String value) {
+ CollectorContext collectorContext = CollectorContext.getInstance();
+ if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
+ collectorContext.add(SAMPLE_COLLECTOR, new ArrayList());
+ }
+ List returnList = (List) collectorContext.get(SAMPLE_COLLECTOR);
+ returnList.add(value);
+ return true;
+ }
+
+ @Override
+ public String getName() {
+ return "sample-format";
+ }
+
+ // Return null. As are just testing collection context.
+ @Override
+ public String getErrorMessageDescription() {
+ return null;
+ }
+ }).build();
+ return jsonMetaSchema;
+ }
+
+ private void setupSchema() throws Exception {
+ final JsonMetaSchema metaSchema = getJsonMetaSchema(
+ "https://github.com/networknt/json-schema-validator/tests/schemas/example01#");
+ final JsonSchemaFactory schemaFactory = JsonSchemaFactory
+ .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
+ .build();
+ this.jsonSchema = schemaFactory.getSchema(getSchemaString());
+ this.jsonSchemaForCombine = schemaFactory.getSchema(getSchemaStringMultipleProperties());
+ }
+
+ private String getSchemaString() {
+ return "{"
+ + "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
+ + "\"title\" : \"Sample test schema\",\n"
+ + "\"description\" : \"Sample schema definition\","
+ + "\"type\" : \"object\","
+ + "\"properties\" :"
+ + "{"
+ + "\"test-property1\" : "
+ + "{"
+ + "\"title\": \"Test Property1\","
+ + "\"type\": \"string\", "
+ + "\"custom-keyword\":[\"x\",\"y\"]"
+ + "},"
+ + "\"test-property2\" : "
+ + "{"
+ + "\"title\": \"Test Property2\","
+ + "\"type\": \"string\", "
+ + "\"custom-keyword\":[\"x\",\"y\"]"
+ + "}"
+ + "},"
+ + "\"additionalProperties\":\"false\","
+ + "\"required\": [\"test-property1\"]\n"
+ + "}";
+ }
+
+ private String getSchemaStringMultipleProperties() {
+ return "{"
+ + "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
+ + "\"title\" : \"Sample test schema\","
+ + "\"description\" : \"Sample schema definition\","
+ + "\"type\" : \"object\","
+ + "\"properties\" :"
+ + "{"
+ + "\"property1\" : "
+ + "{"
+ + "\"title\": \"Property1\","
+ + "\"type\": \"string\", "
+ + "\"custom-keyword1\":[\"x\",\"y\"],"
+ + "\"format\":\"sample-format\""
+ + "},"
+ + "\"property2\" : "
+ + "{"
+ + "\"title\": \"Property2\","
+ + "\"type\": \"string\", "
+ + "\"custom-keyword1\":[\"x\",\"y\"]"
+ + "},"
+ + "\"property3\" : "
+ + "{"
+ + "\"title\": \"Property3\","
+ + "\"type\": \"string\", "
+ + "\"custom-keyword1\":[\"x\",\"y\"]"
+ + "}"
+ + "}"
+ + "}";
+ }
+
+
+ private class ValidationThread implements Runnable {
+
+ private String data;
+
+ private String name;
+
+ private ValidationResult validationResult;
+
+ ValidationThread(String data, String name) {
+ this.name = name;
+ this.data = data;
+ }
+
+ @Override
+ public void run() {
+ try {
+ this.validationResult = validate(data);
+ } catch (JsonMappingException e) {
+ e.printStackTrace();
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ ValidationResult getValidationResult() {
+ return this.validationResult;
+ }
+
+ @Override
+ public String toString() {
+ return "ValidationThread [data=" + data + ", name=" + name + ", validationResult=" + validationResult + "]";
+ }
+
+ }
+
+ /**
+ * Our own custom keyword. In this case we don't use this keyword. It is just
+ * for demonstration purpose.
+ */
+ private class CustomKeyword implements Keyword {
+ @Override
+ public String getValue() {
+ return "custom-keyword";
+ }
+
+ @Override
+ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
+ ValidationContext validationContext) throws JsonSchemaException, Exception {
+ if (schemaNode != null && schemaNode.isArray()) {
+ return new CustomValidator();
+ }
+ return null;
+ }
+ }
+
+
+ /**
+ * We will be collecting information/data by adding the data in the form of
+ * collectors into collector context object while we are validating this node.
+ * This will be helpful in cases where we don't want to revisit the entire JSON
+ * document again just for gathering this kind of information.
+ */
+ private class CustomValidator implements JsonValidator {
+
+ @Override
+ public Set validate(JsonNode node, JsonNode rootNode, String at) {
+ // Get an instance of collector context.
+ CollectorContext collectorContext = CollectorContext.getInstance();
+ if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
+ collectorContext.add(SAMPLE_COLLECTOR, new CustomCollector());
+ }
+ collectorContext.combineWithCollector(SAMPLE_COLLECTOR, node.textValue());
+ return new TreeSet();
+ }
+
+ @Override
+ public Set validate(JsonNode rootNode) {
+ return validate(rootNode, rootNode, BaseJsonValidator.AT_ROOT);
+ }
+
+ }
+
+ private class CustomCollector extends AbstractCollector> {
+
+ List returnList = new ArrayList();
+
+ private Map referenceMap = null;
+
+ public CustomCollector() {
+ referenceMap = getDatasourceMap();
+ }
+
+ @Override
+ public List collect() {
+ return returnList;
+ }
+
+ @Override
+ public void combine(Object object) {
+ returnList.add(referenceMap.get((String) object));
+ }
+
+ }
+
+ /**
+ * Our own custom keyword. In this case we don't use this keyword. It is just
+ * for demonstration purpose.
+ */
+ private class CustomKeyword1 implements Keyword {
+ @Override
+ public String getValue() {
+ return "custom-keyword1";
+ }
+
+ @Override
+ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
+ ValidationContext validationContext) throws JsonSchemaException, Exception {
+ if (schemaNode != null && schemaNode.isArray()) {
+ return new CustomValidator1();
+ }
+ return null;
+ }
+ }
+
+ /**
+ * We will be collecting information/data by adding the data in the form of
+ * collectors into collector context object while we are validating this node.
+ * This will be helpful in cases where we don't want to revisit the entire JSON
+ * document again just for gathering this kind of information. In this test case
+ * we expect this validator to be called multiple times as the associated
+ * keyword has been used multiple times in JSON Schema.
+ */
+ private class CustomValidator1 implements JsonValidator {
+ @SuppressWarnings("unchecked")
+ @Override
+ public Set validate(JsonNode node, JsonNode rootNode, String at) {
+ // Get an instance of collector context.
+ CollectorContext collectorContext = CollectorContext.getInstance();
+ // If collector type is not added to context add one.
+ if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
+ collectorContext.add(SAMPLE_COLLECTOR, new ArrayList());
+ }
+ List returnList = (List) collectorContext.get(SAMPLE_COLLECTOR);
+ returnList.add(node.textValue());
+ return new TreeSet();
+ }
+
+ @Override
+ public Set validate(JsonNode rootNode) {
+ return validate(rootNode, rootNode, BaseJsonValidator.AT_ROOT);
+ }
+ }
+
+ private ValidationResult validate(String jsonData) throws JsonMappingException, JsonProcessingException, Exception {
+ ObjectMapper objectMapper = new ObjectMapper();
+ return this.jsonSchema.validateAndCollect(objectMapper.readTree(jsonData));
+ }
+
+ private Map getDatasourceMap() {
+ Map map = new HashMap();
+ map.put("sample1", "actual_value_added_to_context1");
+ map.put("sample2", "actual_value_added_to_context2");
+ map.put("sample3", "actual_value_added_to_context3");
+ return map;
+ }
}
diff --git a/src/test/java/com/networknt/schema/Issue255Test.java b/src/test/java/com/networknt/schema/Issue255Test.java
index 20764369c..01450b2f1 100644
--- a/src/test/java/com/networknt/schema/Issue255Test.java
+++ b/src/test/java/com/networknt/schema/Issue255Test.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/test/java/com/networknt/schema/MaximumValidatorPerfTest.java b/src/test/java/com/networknt/schema/MaximumValidatorPerfTest.java
index 4d2b7cd67..34799bd1a 100644
--- a/src/test/java/com/networknt/schema/MaximumValidatorPerfTest.java
+++ b/src/test/java/com/networknt/schema/MaximumValidatorPerfTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import org.junit.Ignore;
diff --git a/src/test/java/com/networknt/schema/SpecVersionTest.java b/src/test/java/com/networknt/schema/SpecVersionTest.java
index e1fa0db98..e597a95ba 100644
--- a/src/test/java/com/networknt/schema/SpecVersionTest.java
+++ b/src/test/java/com/networknt/schema/SpecVersionTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import org.junit.Assert;
diff --git a/src/test/java/com/networknt/schema/ThresholdMixinPerfTest.java b/src/test/java/com/networknt/schema/ThresholdMixinPerfTest.java
index 3a4a81894..2f7bafe73 100644
--- a/src/test/java/com/networknt/schema/ThresholdMixinPerfTest.java
+++ b/src/test/java/com/networknt/schema/ThresholdMixinPerfTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/test/java/com/networknt/schema/UriMappingTest.java b/src/test/java/com/networknt/schema/UriMappingTest.java
index f0800cb7b..703f36ded 100644
--- a/src/test/java/com/networknt/schema/UriMappingTest.java
+++ b/src/test/java/com/networknt/schema/UriMappingTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java b/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java
index fdb600867..3ade5ed42 100644
--- a/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java
+++ b/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/test/java/com/networknt/schema/V6JsonSchemaTest.java b/src/test/java/com/networknt/schema/V6JsonSchemaTest.java
index d75f04ec0..b3686b597 100644
--- a/src/test/java/com/networknt/schema/V6JsonSchemaTest.java
+++ b/src/test/java/com/networknt/schema/V6JsonSchemaTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
diff --git a/src/test/java/com/networknt/schema/V7JsonSchemaTest.java b/src/test/java/com/networknt/schema/V7JsonSchemaTest.java
index f69ca41fc..b9963e86a 100644
--- a/src/test/java/com/networknt/schema/V7JsonSchemaTest.java
+++ b/src/test/java/com/networknt/schema/V7JsonSchemaTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (c) 2020 Network New Technologies Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;