Skip to content
This repository was archived by the owner on Mar 30, 2020. It is now read-only.

Commit afb35ef

Browse files
chore: add integration test
1 parent d65a23e commit afb35ef

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,19 @@
5858
<maven.compiler.source>1.8</maven.compiler.source>
5959
<maven.compiler.target>1.8</maven.compiler.target>
6060

61-
<version.generator>3.2.0</version.generator>
61+
<version.generator>3.4.1</version.generator>
6262

6363
<version.javax.validation>2.0.1.Final</version.javax.validation>
64+
<version.jsonassert>1.5.0</version.jsonassert>
6465
<version.junit>4.12</version.junit>
6566
<version.junitparams>1.1.1</version.junitparams>
6667
<version.mockito>2.27.0</version.mockito>
6768
</properties>
6869

70+
<prerequisites>
71+
<maven>3.5</maven>
72+
</prerequisites>
73+
6974
<dependencies>
7075
<dependency>
7176
<groupId>com.github.victools</groupId>
@@ -98,6 +103,12 @@
98103
<version>${version.mockito}</version>
99104
<scope>test</scope>
100105
</dependency>
106+
<dependency>
107+
<groupId>org.skyscreamer</groupId>
108+
<artifactId>jsonassert</artifactId>
109+
<version>${version.jsonassert}</version>
110+
<scope>test</scope>
111+
</dependency>
101112
</dependencies>
102113

103114
<build>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2019 VicTools.
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+
17+
package com.github.victools.jsonschema.module.javax.validation;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.github.victools.jsonschema.generator.SchemaGenerator;
22+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
23+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.nio.charset.StandardCharsets;
27+
import java.util.List;
28+
import java.util.Scanner;
29+
import javax.validation.constraints.DecimalMax;
30+
import javax.validation.constraints.DecimalMin;
31+
import javax.validation.constraints.Email;
32+
import javax.validation.constraints.Max;
33+
import javax.validation.constraints.Min;
34+
import javax.validation.constraints.NotBlank;
35+
import javax.validation.constraints.NotEmpty;
36+
import javax.validation.constraints.NotNull;
37+
import javax.validation.constraints.Null;
38+
import javax.validation.constraints.Pattern;
39+
import javax.validation.constraints.Size;
40+
import org.junit.Test;
41+
import org.skyscreamer.jsonassert.JSONAssert;
42+
import org.skyscreamer.jsonassert.JSONCompareMode;
43+
44+
/**
45+
* Integration test of this module being used in a real SchemaGenerator instance.
46+
*/
47+
public class IntegrationTest {
48+
49+
/**
50+
* Test
51+
*
52+
* @throws Exception
53+
*/
54+
@Test
55+
public void testIntegration() throws Exception {
56+
// active all optional modules
57+
JavaxValidationModule module = new JavaxValidationModule(
58+
JavaxValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED,
59+
JavaxValidationOption.NOT_NULLABLE_METHOD_IS_REQUIRED,
60+
JavaxValidationOption.INCLUDE_PATTERN_EXPRESSIONS);
61+
SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(new ObjectMapper())
62+
.with(module)
63+
.build();
64+
SchemaGenerator generator = new SchemaGenerator(config);
65+
JsonNode result = generator.generateSchema(TestClass.class);
66+
67+
String rawJsonSchema = result.toString();
68+
JSONAssert.assertEquals('\n' + rawJsonSchema + '\n',
69+
loadResource("integration-test-result.json"), rawJsonSchema, JSONCompareMode.STRICT);
70+
}
71+
72+
private static String loadResource(String resourcePath) throws IOException {
73+
StringBuilder stringBuilder = new StringBuilder();
74+
try (InputStream inputStream = IntegrationTest.class
75+
.getResourceAsStream(resourcePath);
76+
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
77+
while (scanner.hasNext()) {
78+
stringBuilder.append(scanner.nextLine()).append('\n');
79+
}
80+
}
81+
String fileAsString = stringBuilder.toString();
82+
return fileAsString;
83+
84+
}
85+
86+
static class TestClass {
87+
88+
@Null
89+
Object nullObject;
90+
91+
@NotNull
92+
List<String> notNullList;
93+
@NotEmpty
94+
List<String> notEmptyList;
95+
@Size(min = 3, max = 25)
96+
List<String> sizeRangeList;
97+
98+
@NotNull
99+
@Email(regexp = ".+@.+\\..+")
100+
String notNullEmail;
101+
@NotEmpty
102+
@Pattern(regexp = "\\w+")
103+
String notEmptyPatternText;
104+
@NotBlank
105+
String notBlankText;
106+
@Size(min = 5, max = 12)
107+
String sizeRangeText;
108+
109+
@Min(7)
110+
@Max(38)
111+
int inclusiveRangeInt;
112+
113+
@DecimalMin(value = "0", inclusive = false)
114+
@DecimalMax(value = "1", inclusive = false)
115+
double exclusiveRangeDouble;
116+
}
117+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"exclusiveRangeDouble": {
5+
"type": "number",
6+
"exclusiveMinimum": 0,
7+
"exclusiveMaximum": 1
8+
},
9+
"inclusiveRangeInt": {
10+
"type": "integer",
11+
"minimum": 7,
12+
"maximum": 38
13+
},
14+
"notBlankText": {
15+
"type": "string",
16+
"minLength": 1
17+
},
18+
"notEmptyList": {
19+
"minItems": 1,
20+
"type": "array",
21+
"items": {
22+
"type": "string"
23+
}
24+
},
25+
"notEmptyPatternText": {
26+
"type": "string",
27+
"minLength": 1,
28+
"pattern": "\\w+"
29+
},
30+
"notNullEmail": {
31+
"type": "string",
32+
"format": "email",
33+
"pattern": ".+@.+\\..+"
34+
},
35+
"notNullList": {
36+
"type": "array",
37+
"items": {
38+
"type": "string"
39+
}
40+
},
41+
"nullObject": {
42+
"type": ["object", "null"]
43+
},
44+
"sizeRangeList": {
45+
"minItems": 3,
46+
"maxItems": 25,
47+
"type": ["array", "null"],
48+
"items": {
49+
"type": "string"
50+
}
51+
},
52+
"sizeRangeText": {
53+
"type": ["string", "null"],
54+
"minLength": 5,
55+
"maxLength": 12
56+
}
57+
},
58+
"required": ["notNullEmail", "notEmptyPatternText", "notBlankText", "notEmptyList", "notNullList"]
59+
}

0 commit comments

Comments
 (0)