|
| 1 | +package io.github.jamsesso.jsonlogic; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import com.google.gson.JsonArray; |
| 6 | +import com.google.gson.JsonElement; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | +import io.github.jamsesso.jsonlogic.utils.JsonValueExtractor; |
| 10 | + |
| 11 | +import static io.github.jamsesso.jsonlogic.FixtureTests.readFixtures; |
| 12 | + |
| 13 | +public class ErrorFixtureTests { |
| 14 | + private static final List<ErrorFixture> FIXTURES = readFixtures("error-fixtures.json", ErrorFixture::fromArray); |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testAllFixtures() { |
| 18 | + JsonLogic jsonLogic = new JsonLogic(); |
| 19 | + List<TestResult> failures = new ArrayList<>(); |
| 20 | + |
| 21 | + for (ErrorFixture fixture : FIXTURES) { |
| 22 | + try { |
| 23 | + jsonLogic.apply(fixture.getJson(), fixture.getData()); |
| 24 | + failures.add(new TestResult(fixture, new JsonLogicException("Expected an exception at " + fixture.getExpectedJsonPath(), ""))); |
| 25 | + } catch (JsonLogicException e) { |
| 26 | + if (!fixture.getExpectedJsonPath().equals(e.getJsonPath()) || |
| 27 | + !fixture.getExpectedError().equals(e.getMessage())) { |
| 28 | + failures.add(new TestResult(fixture, e)); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + for (TestResult testResult : failures) { |
| 34 | + JsonLogicException exception = testResult.getException(); |
| 35 | + ErrorFixture fixture = testResult.getFixture(); |
| 36 | + |
| 37 | + System.out.printf("FAIL: %s\n\t%s\n\tExpected: %s at %s Got: \"%s\" at \"%s\"\n%n", fixture.getJson(), fixture.getData(), |
| 38 | + fixture.getExpectedError(), fixture.getExpectedJsonPath(), |
| 39 | + exception.getMessage(), exception.getJsonPath()); |
| 40 | + } |
| 41 | + |
| 42 | + Assert.assertEquals(String.format("%d/%d test failures!", failures.size(), FIXTURES.size()), 0, failures.size()); |
| 43 | + } |
| 44 | + |
| 45 | + private static class ErrorFixture { |
| 46 | + private final String json; |
| 47 | + private final Object data; |
| 48 | + private final String expectedPath; |
| 49 | + private final String expectedError; |
| 50 | + |
| 51 | + private ErrorFixture(String json, JsonElement data, String expectedPath, String expectedError) { |
| 52 | + this.json = json; |
| 53 | + this.data = JsonValueExtractor.extract(data); |
| 54 | + this.expectedPath = expectedPath; |
| 55 | + this.expectedError = expectedError; |
| 56 | + } |
| 57 | + |
| 58 | + public static ErrorFixture fromArray(JsonArray array) { |
| 59 | + return new ErrorFixture(array.get(0).toString(), array.get(1), array.get(2).getAsString(), array.get(3).getAsString()); |
| 60 | + } |
| 61 | + |
| 62 | + String getJson() { |
| 63 | + return json; |
| 64 | + } |
| 65 | + |
| 66 | + Object getData() { |
| 67 | + return data; |
| 68 | + } |
| 69 | + |
| 70 | + String getExpectedJsonPath() { |
| 71 | + return expectedPath; |
| 72 | + } |
| 73 | + |
| 74 | + String getExpectedError() { |
| 75 | + return expectedError; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static class TestResult { |
| 80 | + private final ErrorFixture fixture; |
| 81 | + private final JsonLogicException exception; |
| 82 | + |
| 83 | + private TestResult(ErrorFixture fixture, JsonLogicException exception) { |
| 84 | + this.fixture = fixture; |
| 85 | + this.exception = exception; |
| 86 | + } |
| 87 | + |
| 88 | + ErrorFixture getFixture() { |
| 89 | + return fixture; |
| 90 | + } |
| 91 | + |
| 92 | + JsonLogicException getException() { |
| 93 | + return exception; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments