|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 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 | + |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import com.networknt.schema.SpecVersion.VersionFlag; |
| 25 | + |
| 26 | +/** |
| 27 | + * Test MultipleOfValidator validator. |
| 28 | + */ |
| 29 | +public class MultipleOfValidatorTest { |
| 30 | + String schemaData = "{" + |
| 31 | + " \"type\": \"object\"," + |
| 32 | + " \"properties\": {" + |
| 33 | + " \"value1\": {" + |
| 34 | + " \"type\": \"number\"," + |
| 35 | + " \"multipleOf\": 0.01" + |
| 36 | + " }," + |
| 37 | + " \"value2\": {" + |
| 38 | + " \"type\": \"number\"," + |
| 39 | + " \"multipleOf\": 0.01" + |
| 40 | + " }," + |
| 41 | + " \"value3\": {" + |
| 42 | + " \"type\": \"number\"," + |
| 43 | + " \"multipleOf\": 0.01" + |
| 44 | + " }" + |
| 45 | + " }" + |
| 46 | + "}"; |
| 47 | + |
| 48 | + @Test |
| 49 | + void test() { |
| 50 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); |
| 51 | + JsonSchema schema = factory.getSchema(schemaData); |
| 52 | + String inputData = "{\"value1\":123.892,\"value2\":123456.2934,\"value3\":123.123}"; |
| 53 | + String validData = "{\"value1\":123.89,\"value2\":123456,\"value3\":123.010}"; |
| 54 | + |
| 55 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 56 | + assertEquals(3, messages.size()); |
| 57 | + assertEquals(3, messages.stream().filter(m -> "multipleOf".equals(m.getType())).count()); |
| 58 | + |
| 59 | + messages = schema.validate(validData, InputFormat.JSON); |
| 60 | + assertEquals(0, messages.size()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testTypeLoose() { |
| 65 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); |
| 66 | + JsonSchema schema = factory.getSchema(schemaData); |
| 67 | + |
| 68 | + String inputData = "{\"value1\":\"123.892\",\"value2\":\"123456.2934\",\"value3\":123.123}"; |
| 69 | + String validTypeLooseInputData = "{\"value1\":\"123.89\",\"value2\":\"123456.29\",\"value3\":123.12}"; |
| 70 | + |
| 71 | + // Without type loose this has 2 type and 1 multipleOf errors |
| 72 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 73 | + assertEquals(3, messages.size()); |
| 74 | + assertEquals(2, messages.stream().filter(m -> "type".equals(m.getType())).count()); |
| 75 | + assertEquals(1, messages.stream().filter(m -> "multipleOf".equals(m.getType())).count()); |
| 76 | + |
| 77 | + // 2 type errors |
| 78 | + messages = schema.validate(validTypeLooseInputData, InputFormat.JSON); |
| 79 | + assertEquals(2, messages.size()); |
| 80 | + assertEquals(2, messages.stream().filter(m -> "type".equals(m.getType())).count()); |
| 81 | + |
| 82 | + // With type loose this has 3 multipleOf errors |
| 83 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 84 | + config.setTypeLoose(true); |
| 85 | + JsonSchema typeLoose = factory.getSchema(schemaData, config); |
| 86 | + messages = typeLoose.validate(inputData, InputFormat.JSON); |
| 87 | + assertEquals(3, messages.size()); |
| 88 | + assertEquals(3, messages.stream().filter(m -> "multipleOf".equals(m.getType())).count()); |
| 89 | + |
| 90 | + // No errors |
| 91 | + messages = typeLoose.validate(validTypeLooseInputData, InputFormat.JSON); |
| 92 | + assertEquals(0, messages.size()); |
| 93 | + } |
| 94 | +} |
0 commit comments