|
| 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 | +} |
0 commit comments