|
| 1 | +/* |
| 2 | + * Copyright (C) 2011 Everit Kft. (http://www.everit.org) |
| 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 org.everit.json.schema; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.FileNotFoundException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.UncheckedIOException; |
| 23 | +import java.net.URISyntaxException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.Optional; |
| 29 | + |
| 30 | +import org.eclipse.jetty.server.Server; |
| 31 | +import org.eclipse.jetty.servlet.ServletHandler; |
| 32 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 33 | +import org.everit.json.schema.loader.SchemaLoader; |
| 34 | +import org.json.JSONException; |
| 35 | +import org.json.JSONObject; |
| 36 | +import org.json.JSONTokener; |
| 37 | +import org.junit.Assert; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.Parameterized; |
| 41 | +import org.junit.runners.Parameterized.Parameters; |
| 42 | + |
| 43 | +@RunWith(Parameterized.class) |
| 44 | +public class IssueTest { |
| 45 | + |
| 46 | + @Parameters |
| 47 | + public static List<Object[]> params() { |
| 48 | + List<Object[]> rval = new ArrayList<>(); |
| 49 | + try { |
| 50 | + File issuesDir = new File( |
| 51 | + IssueTest.class.getResource("/org/everit/json/schema/issues").toURI()); |
| 52 | + for (File issue : issuesDir.listFiles()) { |
| 53 | + rval.add(new Object[] { issue }); |
| 54 | + } |
| 55 | + } catch (URISyntaxException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + return rval; |
| 59 | + } |
| 60 | + |
| 61 | + private final File issueDir; |
| 62 | + |
| 63 | + private Server server; |
| 64 | + |
| 65 | + public IssueTest(final File issueDir) { |
| 66 | + this.issueDir = Objects.requireNonNull(issueDir, "issueDir cannot be null"); |
| 67 | + } |
| 68 | + |
| 69 | + private Optional<File> fileByName(final String fileName) { |
| 70 | + return Arrays.stream(issueDir.listFiles()) |
| 71 | + .filter(file -> file.getName().equals(fileName)) |
| 72 | + .findFirst(); |
| 73 | + } |
| 74 | + |
| 75 | + private void initJetty(final File documentRoot) { |
| 76 | + server = new Server(1234); |
| 77 | + ServletHandler handler = new ServletHandler(); |
| 78 | + server.setHandler(handler); |
| 79 | + handler.addServletWithMapping(new ServletHolder(new IssueServlet(documentRoot)), "/*"); |
| 80 | + try { |
| 81 | + server.start(); |
| 82 | + } catch (Exception e) { |
| 83 | + throw new RuntimeException(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private Schema loadSchema() { |
| 88 | + Optional<File> schemaFile = fileByName("schema.json"); |
| 89 | + try { |
| 90 | + if (schemaFile.isPresent()) { |
| 91 | + JSONObject schemaObj = new JSONObject( |
| 92 | + new JSONTokener(new FileInputStream(schemaFile.get()))); |
| 93 | + return SchemaLoader.load(schemaObj); |
| 94 | + } |
| 95 | + throw new RuntimeException(issueDir.getCanonicalPath() + "/schema.json is not found"); |
| 96 | + } catch (IOException e) { |
| 97 | + throw new UncheckedIOException(e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void stopJetty() { |
| 102 | + if (server != null) { |
| 103 | + try { |
| 104 | + server.stop(); |
| 105 | + } catch (Exception e) { |
| 106 | + throw new RuntimeException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + server = null; |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void test() { |
| 114 | + fileByName("remotes").ifPresent(this::initJetty); |
| 115 | + Schema schema = loadSchema(); |
| 116 | + fileByName("subject-valid.json").ifPresent(file -> validate(file, schema, true)); |
| 117 | + fileByName("subject-invalid.json").ifPresent(file -> validate(file, schema, false)); |
| 118 | + stopJetty(); |
| 119 | + } |
| 120 | + |
| 121 | + private void validate(final File file, final Schema schema, final boolean shouldBeValid) { |
| 122 | + ValidationException thrown = null; |
| 123 | + try { |
| 124 | + JSONObject subject = new JSONObject(new JSONTokener(new FileInputStream(file))); |
| 125 | + try { |
| 126 | + schema.validate(subject); |
| 127 | + } catch (ValidationException e) { |
| 128 | + thrown = e; |
| 129 | + } |
| 130 | + } catch (JSONException e) { |
| 131 | + throw new RuntimeException("failed to parse subject json file", e); |
| 132 | + } catch (FileNotFoundException e) { |
| 133 | + throw new UncheckedIOException(e); |
| 134 | + } |
| 135 | + if (shouldBeValid && thrown != null) { |
| 136 | + Assert.fail("validation failed with: " + thrown); |
| 137 | + } |
| 138 | + if (!shouldBeValid && thrown == null) { |
| 139 | + Assert.fail("did not throw ValidationException for invalid subject"); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments