|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 the original author or authors. |
| 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 static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import com.networknt.schema.SpecVersion.VersionFlag; |
| 27 | +import com.networknt.schema.i18n.ResourceBundleMessageSource; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test for ConstValidator. |
| 31 | + */ |
| 32 | +class ConstValidatorTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void localeMessageOthers() { |
| 36 | + String schemaData = "{\r\n" |
| 37 | + + " \"const\": \"aa\"\r\n" |
| 38 | + + "}"; |
| 39 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 40 | + config.setMessageSource(new ResourceBundleMessageSource("const-messages-override", "jsv-messages")); |
| 41 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 42 | + String inputData = "\"bb\""; |
| 43 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 44 | + assertEquals("$: must be the constant value 'aa' but is 'bb'", messages.iterator().next().getMessage()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void localeMessageNumber() { |
| 49 | + String schemaData = "{\r\n" |
| 50 | + + " \"const\": 1\r\n" |
| 51 | + + "}"; |
| 52 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 53 | + config.setMessageSource(new ResourceBundleMessageSource("const-messages-override", "jsv-messages")); |
| 54 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 55 | + String inputData = "2"; |
| 56 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 57 | + assertEquals("$: must be the constant value '1' but is '2'", messages.iterator().next().getMessage()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void validOthers() { |
| 62 | + String schemaData = "{\r\n" |
| 63 | + + " \"const\": \"aa\"\r\n" |
| 64 | + + "}"; |
| 65 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 66 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 67 | + String inputData = "\"aa\""; |
| 68 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 69 | + assertTrue(messages.isEmpty()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void validNumber() { |
| 74 | + String schemaData = "{\r\n" |
| 75 | + + " \"const\": 1234.56789\r\n" |
| 76 | + + "}"; |
| 77 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 78 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 79 | + String inputData = "1234.56789"; |
| 80 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 81 | + assertTrue(messages.isEmpty()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void invalidNumber() { |
| 86 | + String schemaData = "{\r\n" |
| 87 | + + " \"const\": 1234.56789\r\n" |
| 88 | + + "}"; |
| 89 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 90 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 91 | + String inputData = "\"1234.56789\""; |
| 92 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 93 | + assertFalse(messages.isEmpty()); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments