Skip to content

Commit

Permalink
AdditionalPropertiesOneOfFails test (#505)
Browse files Browse the repository at this point in the history
* AdditionalPropertiesOneOfFails test

* corrected test
  • Loading branch information
huubfleuren authored Feb 22, 2022
1 parent a015e31 commit 15a378b
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.Set;

public class AdditionalPropertiesOneOfFailsTest {

private static Set<ValidationMessage> errors = null;

protected JsonSchema getJsonSchemaFromStreamContent(InputStream schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
return factory.getSchema(schemaContent);
}

protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(content);
return node;
}

@BeforeEach
public void withJsonSchema() {
// correct processing would include the assertions in the tests
if (errors == null) {
String schemaPathJson = "/openapi3/AdditionalPropertiesOneOfFailsTest.json";
String dataPath = "/data/AdditionalPropertiesOneOfFailsTest.json";
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPathJson);

JsonSchema schema = getJsonSchemaFromStreamContent(schemaInputStream);
schema.getValidationContext().getConfig().setFailFast(false);


InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
try {
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);

errors = schema.validate(node);

System.out.println("nr. of reported errors: " + errors.size());
errors.stream().forEach(er -> System.out.println(er.toString()));
} catch (Exception e) {
System.out.println("Fail!");
}

}
}

@Test
public void toxicIsAdditional() {
Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("toxic: is not defined in the schema")).count() == 2,
"property toxic is not defined on activity chemical");
}

@Test
public void chemicalCharacteristicNameIsAdditional() {


Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("$.activities[2].chemicalCharacteristic.name: is not defined in the schema")).count() == 1,
"property name is not defined in 'oneOf' the ChemicalCharacteristic component schemas");
}


@Test
public void depthIsAdditional() {

Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("depth: is not defined in the schema")).count() == 1,
"property depth is not defined on activity machine");
}

@Test
public void chemicalCharacteristicCategoryNameIsDefined() {

Assertions.assertFalse(errors.stream().filter(er -> er.toString().contains("$.activities[0].chemicalCharacteristic.categoryName: is not defined in the schema")).count() == 1,
"property categoryName is defined in 'oneOf' the ChemicalCharacteristic component schemas ");
}

@Test
public void weightIsMissingOnlyOnce() {

Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("weight: is missing")).count() == 1,
"property weight is required on activity machine ");
}

@Test
public void heightIsNotMissingNotOnceAndNotTwice() {

Assertions.assertFalse(errors.stream().filter(er -> er.toString().contains("heigth: is missing")).count() == 1,
"property height is defined ");

}

@Test
public void heightWrongType() {

Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("heigth: number found, integer expected")).count() == 1,
"property height has the wrong type");

}
}
27 changes: 27 additions & 0 deletions src/test/resources/data/AdditionalPropertiesOneOfFailsTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"locationName": "factoryLocation",
"activities": [
{
"activityType": "machine",
"age": "(additionalProperty not allowed)",
"height": 10.5
},
{
"activityType": "chemical",
"toxic": "(additionalProperty not allowed)",
"chemicalCharacteristic": {
"commonName": "methane",
"chemicalName": "CH4"
}
},
{
"activityType": "chemical",
"toxic": "(additionalProperty not allowed)",
"chemicalCharacteristic": {
"name": "methane",
"categoryName": "gasses",
"chemicalName": "CH4"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"type": "object",
"properties": {
"locationName": {
"type": "string"
},
"activities": {
"type": "array",
"items": {
"oneOf": [
{
"type": "object",
"required": [
"activityType",
"weight",
"height"
],
"additionalProperties": false,
"properties": {
"activityType": {
"enum": [
"machine"
]
},
"weight": {
"type": "integer"
},
"height": {
"type": "integer"
}
}
},
{
"type": "object",
"required": [
"activityType",
"chemicalCharacteristic"
],
"additionalProperties": false,
"properties": {
"activityType": {
"enum": [
"chemical"
]
},
"chemicalCharacteristic": {
"oneOf": [
{
"type": "object",
"required": [
"chemicalName"
],
"additionalProperties": false,
"properties": {
"commonName": {
"type": "string"
},
"chemicalName": {
"type": "string"
}
}
},
{
"type": "object",
"required": [
"chemicalName"
],
"additionalProperties": false,
"properties": {
"categoryName": {
"type": "string"
},
"chemicalName": {
"type": "string"
}
}
}
]
}
}
}
]
}
}
}
}

0 comments on commit 15a378b

Please sign in to comment.