Skip to content

Commit aa22e8d

Browse files
authored
Merge pull request #1734 from ctrimble/feature_network-sourcePath-test
Adds an integration test for network sourcePaths.
2 parents 6a8aff3 + 6d87411 commit aa22e8d

File tree

4 files changed

+99
-10
lines changed

4 files changed

+99
-10
lines changed

jsonschema2pojo-integration-tests/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@
210210
<version>2.0.4</version>
211211
<scope>test</scope>
212212
</dependency>
213+
<dependency>
214+
<groupId>com.github.tomakehurst</groupId>
215+
<artifactId>wiremock-jre8</artifactId>
216+
<scope>test</scope>
217+
</dependency>
218+
<dependency>
219+
<groupId>org.slf4j</groupId>
220+
<artifactId>slf4j-simple</artifactId>
221+
<scope>test</scope>
222+
</dependency>
223+
<dependency>
224+
<groupId>com.fasterxml.jackson.core</groupId>
225+
<artifactId>jackson-annotations</artifactId>
226+
<scope>test</scope>
227+
</dependency>
213228
</dependencies>
214229

215230
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright © 2010-2020 Nokia
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+
17+
package org.jsonschema2pojo.integration;
18+
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.hamcrest.Matchers.*;
21+
22+
import java.lang.reflect.Method;
23+
import java.net.URL;
24+
import java.nio.charset.StandardCharsets;
25+
26+
import org.apache.commons.io.IOUtils;
27+
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.RegisterExtension;
30+
31+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
32+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
33+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
34+
35+
@WireMockTest
36+
public class NetworkSchemaIT {
37+
38+
@RegisterExtension
39+
public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();
40+
41+
@Test
42+
public void networkSchema(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
43+
44+
URL schemaUrl = this.getClass().getResource("/schema/network/schema.json");
45+
String schemaBody = IOUtils.toString(schemaUrl, StandardCharsets.UTF_8);
46+
47+
stubFor(get("/schema.json").willReturn(ok(schemaBody)));
48+
49+
URL baseHttpUrl = new URL(wmRuntimeInfo.getHttpBaseUrl());
50+
URL schemaHttpUrl = new URL(baseHttpUrl, "/schema.json");
51+
52+
ClassLoader resultsClassLoader = schemaRule.generateAndCompile(schemaHttpUrl, "com.example");
53+
54+
Class<?> generatedType = resultsClassLoader.loadClass("com.example.Schema");
55+
56+
Method method = generatedType.getMethod("getField");
57+
58+
assertThat(method, is(notNullValue()));
59+
}
60+
61+
}

jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,21 @@ public static void generate(final URL schema, final String targetPackage, final
9090

9191
try {
9292
@SuppressWarnings("serial")
93-
Jsonschema2PojoMojo pluginMojo = new TestableJsonschema2PojoMojo().configure(new HashMap<String, Object>() {
94-
{
95-
put("sourceDirectory", URLUtil.getFileFromURL(schema).getPath());
96-
put("outputDirectory", outputDirectory);
97-
put("project", getMockProject());
98-
put("targetPackage", targetPackage);
99-
putAll(configValues);
100-
}
101-
});
102-
93+
Jsonschema2PojoMojo pluginMojo = new TestableJsonschema2PojoMojo()
94+
.configure(new HashMap<String, Object>() {
95+
{
96+
if( !schema.toExternalForm().startsWith("http://") ) {
97+
put("sourceDirectory", URLUtil.getFileFromURL(schema).getPath());
98+
} else {
99+
put("sourcePaths", new String[] {schema.toExternalForm()});
100+
}
101+
put("outputDirectory", outputDirectory);
102+
put("project", getMockProject());
103+
put("targetPackage", targetPackage);
104+
putAll(configValues);
105+
}
106+
});
107+
103108
pluginMojo.execute();
104109
} catch (MojoExecutionException | DependencyResolutionRequiredException e) {
105110
throw new RuntimeException(e);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"field": {
5+
"type": "string"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)