|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.BeforeAll; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | + |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +public class Issue669Test { |
| 16 | + private static JsonSchema schema; |
| 17 | + |
| 18 | + @BeforeAll |
| 19 | + static void init() { |
| 20 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); |
| 21 | + String schemaPath = "/draft7/issue669.json"; |
| 22 | + InputStream schemaInputStream = Issue669Test.class.getResourceAsStream(schemaPath); |
| 23 | + schema = factory.getSchema(schemaInputStream); |
| 24 | + } |
| 25 | + |
| 26 | + public static Stream<Arguments> validTimeZoneOffsets() { |
| 27 | + String json1 = "{\"testTime\":\"08:27:53-05:00\"}"; // America/New_York |
| 28 | + String json2 = "{\"testTime\":\"08:27:53-04:00\"}"; // America/New_York (DST) |
| 29 | + String json3 = "{\"testTime\":\"08:27:53-03:30\"}"; // America/St_Johns |
| 30 | + String json4 = "{\"testTime\":\"08:27:53-02:30\"}"; // America/St_Johns (DST) |
| 31 | + String json5 = "{\"testTime\":\"08:27:53+02:00\"}"; // Africa/Cairo |
| 32 | + String json6 = "{\"testTime\":\"08:27:53+03:30\"}"; // Asia/Tehran |
| 33 | + String json7 = "{\"testTime\":\"08:27:53+04:30\"}"; // Asia/Tehran (DST) |
| 34 | + String json8 = "{\"testTime\":\"08:27:53+04:00\"}"; // Asia/Dubai |
| 35 | + String json9 = "{\"testTime\":\"08:27:53+05:00\"}"; // Indian/Maldives |
| 36 | + String json10 = "{\"testTime\":\"08:27:53+05:30\"}"; // Asia/Kolkata |
| 37 | + String json11 = "{\"testTime\":\"08:27:53+10:00\"}"; // Australia/Sydney |
| 38 | + String json12 = "{\"testTime\":\"08:27:53+11:00\"}"; // Australia/Sydney (DST) |
| 39 | + String json13 = "{\"testTime\":\"08:27:53+14:00\"}"; // Pacific/Kiritimati |
| 40 | + String json14 = "{\"testTime\":\"18:45:32.123-05:00\"}"; // America/New_York |
| 41 | + String json15 = "{\"testTime\":\"18:45:32.123456-05:00\"}"; // America/New_York |
| 42 | + String json16 = "{\"testTime\":\"08:27:53Z\"}"; // UTC |
| 43 | + String json17 = "{\"testTime\":\"08:27:53+00:00\"}"; // UTC |
| 44 | + |
| 45 | + return Stream.of( |
| 46 | + Arguments.of(json1), |
| 47 | + Arguments.of(json2), |
| 48 | + Arguments.of(json3), |
| 49 | + Arguments.of(json4), |
| 50 | + Arguments.of(json5), |
| 51 | + Arguments.of(json6), |
| 52 | + Arguments.of(json7), |
| 53 | + Arguments.of(json8), |
| 54 | + Arguments.of(json9), |
| 55 | + Arguments.of(json10), |
| 56 | + Arguments.of(json11), |
| 57 | + Arguments.of(json12), |
| 58 | + Arguments.of(json13), |
| 59 | + Arguments.of(json14), |
| 60 | + Arguments.of(json15), |
| 61 | + Arguments.of(json16), |
| 62 | + Arguments.of(json17) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Confirms that valid time zone offsets do not result in a JSON validation error. |
| 68 | + * |
| 69 | + * @param jsonObject a sample JSON payload to test |
| 70 | + */ |
| 71 | + @ParameterizedTest |
| 72 | + @MethodSource("validTimeZoneOffsets") |
| 73 | + void testValidTimeZoneOffsets(String jsonObject) throws JsonProcessingException { |
| 74 | + Set<ValidationMessage> errors = schema.validate(new ObjectMapper().readTree(jsonObject)); |
| 75 | + Assertions.assertTrue(errors.isEmpty()); |
| 76 | + } |
| 77 | + |
| 78 | + public static Stream<Arguments> invalidTimeRepresentations() { |
| 79 | + |
| 80 | + // Invalid JSON payload: seconds missing from time |
| 81 | + String json1 = "{\"testTime\":\"11:23Z\"}"; |
| 82 | + // Invalid JSON payload: Text instead of date-time value |
| 83 | + String json2 = "{\"testTime\":\"Orlando\"}"; |
| 84 | + // Invalid JSON payload: A time zone offset of +23:00 is not valid |
| 85 | + String json3 = "{\"testTime\":\"08:27:53+23:00\"}"; |
| 86 | + // Invalid JSON payload: A time zone offset of -23:00 is not valid |
| 87 | + String json4 = "{\"testTime\":\"08:27:53-23:00\"}"; |
| 88 | + |
| 89 | + return Stream.of( |
| 90 | + Arguments.of(json1), |
| 91 | + Arguments.of(json2), |
| 92 | + Arguments.of(json3), |
| 93 | + Arguments.of(json4) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Confirms that invalid time representations result in one or more a JSON validation errors. |
| 99 | + * |
| 100 | + * @param jsonObject a sample JSON payload to test |
| 101 | + */ |
| 102 | + @ParameterizedTest |
| 103 | + @MethodSource("invalidTimeRepresentations") |
| 104 | + void testInvalidTimeRepresentations(String jsonObject) throws JsonProcessingException { |
| 105 | + Set<ValidationMessage> errors = schema.validate(new ObjectMapper().readTree(jsonObject)); |
| 106 | + Assertions.assertFalse(errors.isEmpty()); |
| 107 | + } |
| 108 | +} |
0 commit comments