Skip to content

Commit 15a378b

Browse files
authored
AdditionalPropertiesOneOfFails test (#505)
* AdditionalPropertiesOneOfFails test * corrected test
1 parent a015e31 commit 15a378b

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2020 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.InputStream;
25+
import java.util.Set;
26+
27+
public class AdditionalPropertiesOneOfFailsTest {
28+
29+
private static Set<ValidationMessage> errors = null;
30+
31+
protected JsonSchema getJsonSchemaFromStreamContent(InputStream schemaContent) {
32+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
33+
return factory.getSchema(schemaContent);
34+
}
35+
36+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
37+
ObjectMapper mapper = new ObjectMapper();
38+
JsonNode node = mapper.readTree(content);
39+
return node;
40+
}
41+
42+
@BeforeEach
43+
public void withJsonSchema() {
44+
// correct processing would include the assertions in the tests
45+
if (errors == null) {
46+
String schemaPathJson = "/openapi3/AdditionalPropertiesOneOfFailsTest.json";
47+
String dataPath = "/data/AdditionalPropertiesOneOfFailsTest.json";
48+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPathJson);
49+
50+
JsonSchema schema = getJsonSchemaFromStreamContent(schemaInputStream);
51+
schema.getValidationContext().getConfig().setFailFast(false);
52+
53+
54+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
55+
try {
56+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
57+
58+
errors = schema.validate(node);
59+
60+
System.out.println("nr. of reported errors: " + errors.size());
61+
errors.stream().forEach(er -> System.out.println(er.toString()));
62+
} catch (Exception e) {
63+
System.out.println("Fail!");
64+
}
65+
66+
}
67+
}
68+
69+
@Test
70+
public void toxicIsAdditional() {
71+
Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("toxic: is not defined in the schema")).count() == 2,
72+
"property toxic is not defined on activity chemical");
73+
}
74+
75+
@Test
76+
public void chemicalCharacteristicNameIsAdditional() {
77+
78+
79+
Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("$.activities[2].chemicalCharacteristic.name: is not defined in the schema")).count() == 1,
80+
"property name is not defined in 'oneOf' the ChemicalCharacteristic component schemas");
81+
}
82+
83+
84+
@Test
85+
public void depthIsAdditional() {
86+
87+
Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("depth: is not defined in the schema")).count() == 1,
88+
"property depth is not defined on activity machine");
89+
}
90+
91+
@Test
92+
public void chemicalCharacteristicCategoryNameIsDefined() {
93+
94+
Assertions.assertFalse(errors.stream().filter(er -> er.toString().contains("$.activities[0].chemicalCharacteristic.categoryName: is not defined in the schema")).count() == 1,
95+
"property categoryName is defined in 'oneOf' the ChemicalCharacteristic component schemas ");
96+
}
97+
98+
@Test
99+
public void weightIsMissingOnlyOnce() {
100+
101+
Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("weight: is missing")).count() == 1,
102+
"property weight is required on activity machine ");
103+
}
104+
105+
@Test
106+
public void heightIsNotMissingNotOnceAndNotTwice() {
107+
108+
Assertions.assertFalse(errors.stream().filter(er -> er.toString().contains("heigth: is missing")).count() == 1,
109+
"property height is defined ");
110+
111+
}
112+
113+
@Test
114+
public void heightWrongType() {
115+
116+
Assertions.assertTrue(errors.stream().filter(er -> er.toString().contains("heigth: number found, integer expected")).count() == 1,
117+
"property height has the wrong type");
118+
119+
}
120+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"locationName": "factoryLocation",
3+
"activities": [
4+
{
5+
"activityType": "machine",
6+
"age": "(additionalProperty not allowed)",
7+
"height": 10.5
8+
},
9+
{
10+
"activityType": "chemical",
11+
"toxic": "(additionalProperty not allowed)",
12+
"chemicalCharacteristic": {
13+
"commonName": "methane",
14+
"chemicalName": "CH4"
15+
}
16+
},
17+
{
18+
"activityType": "chemical",
19+
"toxic": "(additionalProperty not allowed)",
20+
"chemicalCharacteristic": {
21+
"name": "methane",
22+
"categoryName": "gasses",
23+
"chemicalName": "CH4"
24+
}
25+
}
26+
]
27+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"locationName": {
5+
"type": "string"
6+
},
7+
"activities": {
8+
"type": "array",
9+
"items": {
10+
"oneOf": [
11+
{
12+
"type": "object",
13+
"required": [
14+
"activityType",
15+
"weight",
16+
"height"
17+
],
18+
"additionalProperties": false,
19+
"properties": {
20+
"activityType": {
21+
"enum": [
22+
"machine"
23+
]
24+
},
25+
"weight": {
26+
"type": "integer"
27+
},
28+
"height": {
29+
"type": "integer"
30+
}
31+
}
32+
},
33+
{
34+
"type": "object",
35+
"required": [
36+
"activityType",
37+
"chemicalCharacteristic"
38+
],
39+
"additionalProperties": false,
40+
"properties": {
41+
"activityType": {
42+
"enum": [
43+
"chemical"
44+
]
45+
},
46+
"chemicalCharacteristic": {
47+
"oneOf": [
48+
{
49+
"type": "object",
50+
"required": [
51+
"chemicalName"
52+
],
53+
"additionalProperties": false,
54+
"properties": {
55+
"commonName": {
56+
"type": "string"
57+
},
58+
"chemicalName": {
59+
"type": "string"
60+
}
61+
}
62+
},
63+
{
64+
"type": "object",
65+
"required": [
66+
"chemicalName"
67+
],
68+
"additionalProperties": false,
69+
"properties": {
70+
"categoryName": {
71+
"type": "string"
72+
},
73+
"chemicalName": {
74+
"type": "string"
75+
}
76+
}
77+
}
78+
]
79+
}
80+
}
81+
}
82+
]
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)