|
| 1 | +package io.github.simbo1905.json.schema; |
| 2 | + |
| 3 | +import jdk.sandbox.java.util.json.Json; |
| 4 | +import org.assertj.core.api.Assertions; |
| 5 | +import org.junit.jupiter.api.DynamicTest; |
| 6 | +import org.junit.jupiter.api.TestFactory; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URISyntaxException; |
| 10 | +import java.net.URL; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Objects; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration tests: validate OpenRPC documents using a minimal embedded meta-schema. |
| 22 | + * |
| 23 | + * Resources: |
| 24 | + * - Schema: src/test/resources/openrpc/schema.json |
| 25 | + * - Examples: src/test/resources/openrpc/examples/*.json |
| 26 | + * Files containing "-bad-" are intentionally invalid and must fail validation. |
| 27 | + */ |
| 28 | +public class OpenRPCSchemaValidationIT { |
| 29 | + |
| 30 | + private static String readResource(String name) throws IOException { |
| 31 | + try { |
| 32 | + URL url = Objects.requireNonNull(OpenRPCSchemaValidationIT.class.getClassLoader().getResource(name), name); |
| 33 | + return Files.readString(Path.of(url.toURI()), StandardCharsets.UTF_8); |
| 34 | + } catch (URISyntaxException e) { |
| 35 | + throw new IOException(e); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @TestFactory |
| 40 | + Stream<DynamicTest> validateOpenRPCExamples() throws Exception { |
| 41 | + // Compile the minimal OpenRPC schema (self-contained, no remote $ref) |
| 42 | + String schemaJson = readResource("openrpc/schema.json"); |
| 43 | + JsonSchema schema = JsonSchema.compile(Json.parse(schemaJson)); |
| 44 | + |
| 45 | + // Discover example files |
| 46 | + URL dirUrl = Objects.requireNonNull(getClass().getClassLoader().getResource("openrpc/examples"), |
| 47 | + "missing openrpc examples directory"); |
| 48 | + Path dir = Path.of(dirUrl.toURI()); |
| 49 | + |
| 50 | + try (Stream<Path> files = Files.list(dir)) { |
| 51 | + List<Path> jsons = files |
| 52 | + .filter(p -> p.getFileName().toString().endsWith(".json")) |
| 53 | + .sorted() |
| 54 | + .toList(); |
| 55 | + |
| 56 | + assertThat(jsons).isNotEmpty(); |
| 57 | + |
| 58 | + return jsons.stream().map(path -> DynamicTest.dynamicTest(path.getFileName().toString(), () -> { |
| 59 | + String doc = Files.readString(path, StandardCharsets.UTF_8); |
| 60 | + boolean expectedValid = !path.getFileName().toString().contains("-bad-"); |
| 61 | + boolean actualValid = schema.validate(Json.parse(doc)).valid(); |
| 62 | + Assertions.assertThat(actualValid) |
| 63 | + .as("validation of %s", path.getFileName()) |
| 64 | + .isEqualTo(expectedValid); |
| 65 | + })); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
0 commit comments