|
| 1 | +package json.java21.transforms; |
| 2 | + |
| 3 | +import jdk.sandbox.java.util.json.Json; |
| 4 | +import jdk.sandbox.java.util.json.JsonValue; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.Arguments; |
| 7 | +import org.junit.jupiter.params.provider.MethodSource; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.Comparator; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.logging.Logger; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +/// Executes the Microsoft json-document-transforms JSON fixtures against this implementation. |
| 21 | +/// |
| 22 | +/// Fixtures are vendored into: |
| 23 | +/// `json-java21-transforms/src/test/resources/microsoft-json-document-transforms/Inputs/` |
| 24 | +class MicrosoftJsonDocumentTransformsExamplesTest extends JsonTransformsLoggingConfig { |
| 25 | + |
| 26 | + private static final Logger LOG = Logger.getLogger(MicrosoftJsonDocumentTransformsExamplesTest.class.getName()); |
| 27 | + |
| 28 | + static Stream<Arguments> microsoftFixtures() throws IOException { |
| 29 | + final Path base = Path.of(System.getProperty("transforms.test.resources")) |
| 30 | + .resolve("microsoft-json-document-transforms") |
| 31 | + .resolve("Inputs"); |
| 32 | + |
| 33 | + if (!Files.isDirectory(base)) { |
| 34 | + throw new IllegalStateException("Missing fixture directory: " + base); |
| 35 | + } |
| 36 | + |
| 37 | + try (Stream<Path> paths = Files.walk(base)) { |
| 38 | + final var files = paths |
| 39 | + .filter(Files::isRegularFile) |
| 40 | + .filter(p -> p.toString().endsWith(".json")) |
| 41 | + .filter(p -> p.getFileName().toString().contains(".Transform")) |
| 42 | + .filter(p -> !p.toString().contains("/Skipped/")) |
| 43 | + .sorted(Comparator.comparing(Path::toString)) |
| 44 | + .toList(); |
| 45 | + |
| 46 | + return files.stream() |
| 47 | + .map(p -> Arguments.of(base.relativize(p).toString(), p)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @ParameterizedTest(name = "{0}") |
| 52 | + @MethodSource("microsoftFixtures") |
| 53 | + void microsoftFixture(String fixtureName, Path transformFile) throws Exception { |
| 54 | + LOG.info(() -> "TEST: microsoftFixture - " + fixtureName); |
| 55 | + |
| 56 | + final Path dir = transformFile.getParent(); |
| 57 | + final String transformFileName = transformFile.getFileName().toString(); |
| 58 | + |
| 59 | + final int transformIdx = transformFileName.indexOf(".Transform"); |
| 60 | + assertThat(transformIdx) |
| 61 | + .as("transform file name must contain '.Transform': %s", transformFileName) |
| 62 | + .isGreaterThan(0); |
| 63 | + |
| 64 | + final String testName = transformFileName.substring(0, transformIdx); |
| 65 | + final int dot = testName.indexOf('.'); |
| 66 | + final String category = dot >= 0 ? testName.substring(0, dot) : testName; |
| 67 | + |
| 68 | + final Path sourceFile = dir.resolve(category + ".Source.json"); |
| 69 | + final Path expectedFile = dir.resolve(testName + ".Expected.json"); |
| 70 | + |
| 71 | + assertThat(sourceFile) |
| 72 | + .as("missing source fixture for %s", fixtureName) |
| 73 | + .exists(); |
| 74 | + assertThat(expectedFile) |
| 75 | + .as("missing expected fixture for %s", fixtureName) |
| 76 | + .exists(); |
| 77 | + |
| 78 | + final JsonValue source = Json.parse(readUtf8(sourceFile)); |
| 79 | + final JsonValue transform = Json.parse(readUtf8(transformFile)); |
| 80 | + final JsonValue expected = Json.parse(readUtf8(expectedFile)); |
| 81 | + |
| 82 | + final JsonTransforms compiled = JsonTransforms.parse(transform); |
| 83 | + final JsonValue actual = compiled.apply(source); |
| 84 | + |
| 85 | + assertThat(actual) |
| 86 | + .as(() -> "Expected:\n" + Json.toDisplayString(expected, 2) + "\n\nActual:\n" + Json.toDisplayString(actual, 2)) |
| 87 | + .isEqualTo(expected); |
| 88 | + } |
| 89 | + |
| 90 | + private static String readUtf8(Path file) throws IOException { |
| 91 | + Objects.requireNonNull(file, "file must not be null"); |
| 92 | + final String text = Files.readString(file, StandardCharsets.UTF_8); |
| 93 | + // Some upstream fixtures are saved with a UTF-8 BOM; Json.parse does not accept it. |
| 94 | + return text.startsWith("\ufeff") ? text.substring(1) : text; |
| 95 | + } |
| 96 | +} |
0 commit comments