|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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.format; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.EnumSource; |
| 25 | +import com.networknt.schema.InputFormat; |
| 26 | +import com.networknt.schema.JsonSchema; |
| 27 | +import com.networknt.schema.JsonSchemaFactory; |
| 28 | +import com.networknt.schema.SchemaValidatorsConfig; |
| 29 | +import com.networknt.schema.SpecVersion.VersionFlag; |
| 30 | +import com.networknt.schema.ValidationMessage; |
| 31 | + |
| 32 | +class TimeFormatTest { |
| 33 | + |
| 34 | + enum ValidTimeFormatInput { |
| 35 | + Z_OFFSET_LEAP_SECOND("23:59:60Z"), |
| 36 | + POSITIVE_OFFSET_LEAP_SECOND("07:59:60+08:00"), |
| 37 | + NEGATIVE_OFFSET_LEAP_SECOND("15:59:60-08:00"), |
| 38 | + Z_OFFSET_MIN_TIME("00:00:00Z"), |
| 39 | + Z_OFFSET("23:59:59Z"), |
| 40 | + POSITIVE_OFFSET_ZERO("17:00:00+00:00"), |
| 41 | + POSITIVE_OFFSET_MAX("17:00:00+23:59"), |
| 42 | + NEGATIVE_OFFSET_ZERO("17:00:00-00:00"), |
| 43 | + NEGATIVE_OFFSET_ONE_DAY("17:00:00-07:00"), |
| 44 | + NEGATIVE_OFFSET_MAX("17:00:00-23:59"); |
| 45 | + |
| 46 | + String format; |
| 47 | + |
| 48 | + ValidTimeFormatInput(String format) { |
| 49 | + this.format = format; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @EnumSource(ValidTimeFormatInput.class) |
| 55 | + void validTimeShouldPass(ValidTimeFormatInput input) { |
| 56 | + String schemaData = "{\r\n" |
| 57 | + + " \"format\": \"time\"\r\n" |
| 58 | + + "}"; |
| 59 | + |
| 60 | + String inputData = "\""+input.format+"\""; |
| 61 | + |
| 62 | + SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build(); |
| 63 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 64 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 65 | + assertTrue(messages.isEmpty()); |
| 66 | + } |
| 67 | + |
| 68 | + enum InvalidTimeFormatInput { |
| 69 | + NEGATIVE_OFFSET_INVALID_LEAP_SECOND("23:59:60-07:00"), |
| 70 | + Z_OFFSET_EXCEED_LEAP_SECOND("23:59:61Z"), |
| 71 | + Z_OFFSET_EXCEED_TIME("24:00:00Z"), |
| 72 | + POSITIVE_OFFSET_EXCEED_MAX("17:00:00+24:00"), |
| 73 | + NEGATIVE_OFFSET_EXCEED_MAX("17:00:00-24:00"); |
| 74 | + |
| 75 | + String format; |
| 76 | + |
| 77 | + InvalidTimeFormatInput(String format) { |
| 78 | + this.format = format; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @EnumSource(InvalidTimeFormatInput.class) |
| 84 | + void invalidTimeShouldFail(InvalidTimeFormatInput input) { |
| 85 | + String schemaData = "{\r\n" |
| 86 | + + " \"format\": \"time\"\r\n" |
| 87 | + + "}"; |
| 88 | + |
| 89 | + String inputData = "\""+input.format+"\""; |
| 90 | + |
| 91 | + SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build(); |
| 92 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); |
| 93 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 94 | + assertFalse(messages.isEmpty()); |
| 95 | + } |
| 96 | +} |
0 commit comments