|
| 1 | +/* |
| 2 | + * Copyright 2021, 2023 Fabian Steeg, hbz |
| 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.metafacture.json; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.both; |
| 19 | +import static org.hamcrest.CoreMatchers.containsString; |
| 20 | +import static org.junit.Assert.assertThat; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.request; |
| 23 | + |
| 24 | +import java.io.BufferedReader; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.net.MalformedURLException; |
| 28 | +import java.net.URL; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.function.Function; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Rule; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.Parameterized; |
| 41 | +import org.metafacture.framework.MetafactureException; |
| 42 | +import org.metafacture.framework.ObjectReceiver; |
| 43 | +import org.mockito.InOrder; |
| 44 | +import org.mockito.Mock; |
| 45 | +import org.mockito.Mockito; |
| 46 | +import org.mockito.MockitoAnnotations; |
| 47 | + |
| 48 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 49 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 50 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 51 | + |
| 52 | + |
| 53 | +/** |
| 54 | + * Tests for {@link JsonValidator}. |
| 55 | + * |
| 56 | + * @author Fabian Steeg |
| 57 | + * |
| 58 | + */ |
| 59 | +@RunWith(Parameterized.class) |
| 60 | +public final class JsonValidatorTest { |
| 61 | + |
| 62 | + private static final String MAIN_SCHEMA = "/schemas/schema.json"; |
| 63 | + private static final String ID_SCHEMA = "/schemas/id.json"; |
| 64 | + private static final String JSON_VALID = "{\"id\":\"http://example.org/\"}"; |
| 65 | + private static final String JSON_INVALID_MISSING_REQUIRED = "{}"; |
| 66 | + private static final String JSON_INVALID_URI_FORMAT= "{\"id\":\"example.org/\"}"; |
| 67 | + private static final String JSON_INVALID_DUPLICATE_KEY = "{\"id\":\"val\",\"id\":\"val\"}"; |
| 68 | + private static final String JSON_INVALID_SYNTAX_ERROR = "{\"id1\":\"val\",\"id2\":\"val\""; |
| 69 | + |
| 70 | + private JsonValidator validator; |
| 71 | + |
| 72 | + @Mock |
| 73 | + private ObjectReceiver<String> receiver; |
| 74 | + private InOrder inOrder; |
| 75 | + private Function<Object, String> schemaLocationGetter; |
| 76 | + |
| 77 | + @Rule |
| 78 | + public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig() |
| 79 | + .jettyAcceptors(Runtime.getRuntime().availableProcessors()).dynamicPort()); |
| 80 | + |
| 81 | + @Parameterized.Parameters(name = "{index}") |
| 82 | + public static Collection<Object[]> siteMaps() { |
| 83 | + return Arrays.asList((Object[][]) (new Function[][] { // |
| 84 | + // Pass the schema to each test as path on classpath, file url, and http url: |
| 85 | + { (Object rule) -> MAIN_SCHEMA }, |
| 86 | + { (Object rule) -> JsonValidatorTest.class.getResource(MAIN_SCHEMA).toString() }, |
| 87 | + { (Object rule) -> ((WireMockRule) rule).baseUrl() + MAIN_SCHEMA } })); |
| 88 | + } |
| 89 | + |
| 90 | + public JsonValidatorTest(Function<Object, String> schemaLocationGetter) { |
| 91 | + this.schemaLocationGetter = schemaLocationGetter; |
| 92 | + } |
| 93 | + |
| 94 | + @Before |
| 95 | + public void setup() throws IOException { |
| 96 | + MockitoAnnotations.initMocks(this); |
| 97 | + wireMock(MAIN_SCHEMA, ID_SCHEMA); |
| 98 | + String schemaLocation = schemaLocationGetter.apply(wireMockRule); |
| 99 | + validator = new JsonValidator(schemaLocation); |
| 100 | + validator.setReceiver(receiver); |
| 101 | + inOrder = Mockito.inOrder(receiver); |
| 102 | + } |
| 103 | + |
| 104 | + private void wireMock(final String... schemaLocations) throws IOException { |
| 105 | + for (String schemaLocation : schemaLocations) { |
| 106 | + stubFor(request("GET", WireMock.urlEqualTo(schemaLocation)).willReturn( |
| 107 | + WireMock.ok().withBody(readToString(getClass().getResource(schemaLocation))) |
| 108 | + .withHeader("Content-type", "application/json"))); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private String readToString(final URL url) throws IOException { |
| 113 | + return new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) |
| 114 | + .lines().collect(Collectors.joining("\n")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void callWireMockSchema() throws MalformedURLException, IOException { |
| 119 | + final String schemaContent = readToString(new URL(wireMockRule.baseUrl() + MAIN_SCHEMA)); |
| 120 | + assertThat(schemaContent, both(containsString("$schema")).and(containsString("$ref"))); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testShouldValidate() { |
| 125 | + validator.process(JSON_VALID); |
| 126 | + inOrder.verify(receiver, Mockito.calls(1)).process(JSON_VALID); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testShouldInvalidateMissingRequired() { |
| 131 | + validator.process(JSON_INVALID_MISSING_REQUIRED); |
| 132 | + inOrder.verifyNoMoreInteractions(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testShouldInvalidateUriFormat() { |
| 137 | + validator.process(JSON_INVALID_URI_FORMAT); |
| 138 | + inOrder.verifyNoMoreInteractions(); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testShouldInvalidateDuplicateKey() { |
| 143 | + validator.process(JSON_INVALID_DUPLICATE_KEY); |
| 144 | + inOrder.verifyNoMoreInteractions(); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testShouldInvalidateSyntaxError() { |
| 149 | + validator.process(JSON_INVALID_SYNTAX_ERROR); |
| 150 | + inOrder.verifyNoMoreInteractions(); |
| 151 | + } |
| 152 | + |
| 153 | + @Test(expected = MetafactureException.class) |
| 154 | + public void testShouldCatchMissingSchemaFile() { |
| 155 | + new JsonValidator("").process("{}"); |
| 156 | + } |
| 157 | + |
| 158 | + @Test(expected = MetafactureException.class) |
| 159 | + public void testShouldCatchMissingValidOutputFile() { |
| 160 | + validator.setWriteValid(""); |
| 161 | + validator.process(JSON_INVALID_MISSING_REQUIRED); |
| 162 | + } |
| 163 | + |
| 164 | + @Test(expected = MetafactureException.class) |
| 165 | + public void testShouldCatchMissingInvalidOutputFile() { |
| 166 | + validator.setWriteInvalid(""); |
| 167 | + validator.process(JSON_INVALID_MISSING_REQUIRED); |
| 168 | + } |
| 169 | + |
| 170 | + @After |
| 171 | + public void cleanup() { |
| 172 | + validator.closeStream(); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments