From 1ccc2115a798bb1699a44eeb7c56c4aa78991180 Mon Sep 17 00:00:00 2001 From: s13o Date: Fri, 17 Mar 2017 15:32:54 +0200 Subject: [PATCH] Allow ContentResolverTest to run without an internet connection (#705) --- jsonschema2pojo-core/pom.xml | 14 ++++ .../java/org/jsonschema2pojo/Annotator.java | 6 +- .../org/jsonschema2pojo/rules/MediaRule.java | 1 - .../ContentResolverNetworkTest.java | 76 +++++++++++++++++++ .../jsonschema2pojo/ContentResolverTest.java | 27 ++----- .../resources/wiremock/mappings/address.json | 13 ++++ .../wiremock/mappings/address404.json | 13 ++++ .../wiremock/mappings/address500.json | 13 ++++ jsonschema2pojo-gradle-plugin/pom.xml | 2 +- pom.xml | 27 +++++++ 10 files changed, 163 insertions(+), 29 deletions(-) create mode 100644 jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverNetworkTest.java create mode 100644 jsonschema2pojo-core/src/test/resources/wiremock/mappings/address.json create mode 100644 jsonschema2pojo-core/src/test/resources/wiremock/mappings/address404.json create mode 100644 jsonschema2pojo-core/src/test/resources/wiremock/mappings/address500.json diff --git a/jsonschema2pojo-core/pom.xml b/jsonschema2pojo-core/pom.xml index a2cdc2195..cdb6ec1d4 100644 --- a/jsonschema2pojo-core/pom.xml +++ b/jsonschema2pojo-core/pom.xml @@ -77,6 +77,20 @@ org.mockito mockito-all + + com.github.tomakehurst + wiremock + + + org.slf4j + slf4j-simple + + + org.apache.httpcomponents + httpclient + 4.5.3 + test + diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Annotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Annotator.java index 3caa2a3b7..04f004076 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Annotator.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Annotator.java @@ -166,14 +166,10 @@ public interface Annotator { * * @param field * the field that contains data that will be serialized - * @param clazz - * the owner of the field (class to which the field belongs) - * @param propertyName - * the name of the JSON property that this field represents * @param propertyNode * the schema node defining this property */ - void dateField(JFieldVar field, JsonNode node); + void dateField(JFieldVar field, JsonNode propertyNode); void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName); } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MediaRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MediaRule.java index 384f60a29..6f880224a 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MediaRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/MediaRule.java @@ -41,7 +41,6 @@ public class MediaRule implements Rule { * Constructs a new media rule. *

* - * @param ruleFactory the rule factory that created this rule. * @since 0.4.2 */ protected MediaRule() { diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverNetworkTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverNetworkTest.java new file mode 100644 index 000000000..09cd26948 --- /dev/null +++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverNetworkTest.java @@ -0,0 +1,76 @@ +/* + * Copyright © 2010-2014 Nokia + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jsonschema2pojo; + +import com.fasterxml.jackson.databind.JsonNode; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import java.net.URI; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +/** + * @author {@link "https://github.com/s13o" "s13o"} + * @since 3/17/2017 + */ +public class ContentResolverNetworkTest { + + private static final String ADDRESS = "localhost"; + + @Rule + public WireMockRule server = new WireMockRule( + options().dynamicPort().bindAddress(ADDRESS).usingFilesUnderClasspath("wiremock") + ); + + @Before + public void before() throws Exception { + server.start(); + } + + @After + public void after() throws Exception { + server.stop(); + } + + private ContentResolver resolver = new ContentResolver(); + + @Test(expected=IllegalArgumentException.class) + public void brokenLinkCausesIllegalArgumentException() { + URI brokenHttpUri = URI.create("http://" + ADDRESS + ":" + server.port() + "/address404.json"); + resolver.resolve(brokenHttpUri); + } + + @Test(expected=IllegalArgumentException.class) + public void serverErrorCausesIllegalArgumentException() { + URI brokenHttpUri = URI.create("http://" + ADDRESS + ":" + server.port() + "/address500.json"); + resolver.resolve(brokenHttpUri); + } + + @Test + public void httpLinkIsResolvedToContent() { + URI httpUri = URI.create("http://" + ADDRESS + ":" + server.port() + "/address.json"); + JsonNode uriContent = resolver.resolve(httpUri); + assertThat(uriContent.path("description").asText().length(), is(greaterThan(0))); + } + +} diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverTest.java index 90e40d2e6..657a2d44b 100644 --- a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverTest.java +++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/ContentResolverTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright © 2010-2014 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,7 +16,8 @@ package org.jsonschema2pojo; -import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.JsonNode; +import org.junit.Test; import java.io.File; import java.io.FileOutputStream; @@ -24,14 +25,12 @@ import java.io.OutputStream; import java.net.URI; -import org.junit.Test; - -import com.fasterxml.jackson.databind.JsonNode; import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; public class ContentResolverTest { - private ContentResolver resolver = new ContentResolver(); + private ContentResolver resolver = new ContentResolver(); @Test(expected=IllegalArgumentException.class) public void wrongProtocolCausesIllegalArgumentException() { @@ -39,22 +38,6 @@ public void wrongProtocolCausesIllegalArgumentException() { URI uriWithUnrecognisedProtocol = URI.create("foobar://schema/address.json"); resolver.resolve(uriWithUnrecognisedProtocol); } - - @Test(expected=IllegalArgumentException.class) - public void brokenLinkCausesIllegalArgumentException() { - - URI brokenHttpUri = URI.create("http://json-schema.org/address123123213"); - resolver.resolve(brokenHttpUri); - } - - @Test - public void httpLinkIsResolvedToContent() { - - URI httpUri = URI.create("http://json-schema.org/address"); - JsonNode uriContent = resolver.resolve(httpUri); - - assertThat(uriContent.path("description").asText().length(), is(greaterThan(0))); - } @Test public void fileLinkIsResolvedToContent() throws IOException { diff --git a/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address.json b/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address.json new file mode 100644 index 000000000..4a31ea1ea --- /dev/null +++ b/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address.json @@ -0,0 +1,13 @@ +{ + "request": { + "method": "GET", + "url": "/address.json" + }, + "response": { + "status": 200, + "body": "{\n\"description\" : \"An Address following the convention of http://microformats.org/wiki/hcard\",\n\"type\" : \"object\",\n\"properties\" : {\n\"post-office-box\" : { \"type\" : \"string\" },\n\"extended-address\" : { \"type\" : \"string\" },\n\"street-address\" : { \"type\":\"string\" },\n\"locality\" : { \"type\" : \"string\", \"required\" : true },\n\"region\" : { \"type\" : \"string\", \"required\" : true },\n\"postal-code\" : { \"type\" : \"string\" },\n\"country-name\" : { \"type\" : \"string\", \"required\" : true }\n},\n\"dependencies\" : {\n\"post-office-box\" : \"street-address\",\n\"extended-address\" : \"street-address\",\n\"street-address\" : \"region\",\n\"locality\" : \"region\",\n\"region\" : \"country-name\"}\n}", + "headers": { + "Content-Type": "application/json" + } + } +} \ No newline at end of file diff --git a/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address404.json b/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address404.json new file mode 100644 index 000000000..21010bd28 --- /dev/null +++ b/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address404.json @@ -0,0 +1,13 @@ +{ + "request": { + "method": "GET", + "url": "/address404.json" + }, + "response": { + "status": 404, + "body": "File not found", + "headers": { + "Content-Type": "plain/test" + } + } +} \ No newline at end of file diff --git a/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address500.json b/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address500.json new file mode 100644 index 000000000..c615376ab --- /dev/null +++ b/jsonschema2pojo-core/src/test/resources/wiremock/mappings/address500.json @@ -0,0 +1,13 @@ +{ + "request": { + "method": "GET", + "url": "/address500.json" + }, + "response": { + "status": 500, + "body": "Server error", + "headers": { + "Content-Type": "plain/test" + } + } +} \ No newline at end of file diff --git a/jsonschema2pojo-gradle-plugin/pom.xml b/jsonschema2pojo-gradle-plugin/pom.xml index 93f0325f0..51209309b 100644 --- a/jsonschema2pojo-gradle-plugin/pom.xml +++ b/jsonschema2pojo-gradle-plugin/pom.xml @@ -24,7 +24,7 @@ org.codehaus.groovy groovy-all - 1.8.6 + ${groovy.version} provided diff --git a/pom.xml b/pom.xml index 2a457ed24..82d769e8c 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ UTF-8 UTF-8 + 1.8.6 1.6 2.5 1.1.0 @@ -350,6 +351,32 @@ 4.11 test + + org.slf4j + slf4j-simple + 1.7.24 + test + + + com.github.tomakehurst + wiremock + 2.5.1 + test + + + slf4j-api + org.slf4j + + + jackson-core + com.fasterxml.jackson.core + + + jackson-annotations + com.fasterxml.jackson.core + + + org.apache.ant ant