Repository: joelittlejohn/jsonschema2pojo
Pull request #705 (2017-03-17 07:48:55)

org.jsonschema2pojo.ContentResolverTest require internet connection (…
An attempt to resolve the issue #704 without adding of external dependencies

s13o: Updated
wiremock added, become more elegant :)


Patch:
diff --git a/jsonschema2pojo-core/pom.xml b/jsonschema2pojo-core/pom.xml
index a2cdc219..cdb6ec1d 100644
--- a/jsonschema2pojo-core/pom.xml
+++ b/jsonschema2pojo-core/pom.xml
@@ -77,6 +77,20 @@
             <groupId>org.mockito</groupId>
             <artifactId>mockito-all</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.github.tomakehurst</groupId>
+            <artifactId>wiremock</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.3</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Annotator.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Annotator.java
index 3caa2a3b..04f00407 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 384f60a2..6f880224 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<JType, JType> {
      * Constructs a new media rule.
      * </p>
      *
-     * @param ruleFactory the rule factory that created this rule.
      * @since 0.4.2
      */
     protected MediaRule() {
diff --git a/jsonschema2pojo-gradle-plugin/pom.xml b/jsonschema2pojo-gradle-plugin/pom.xml
index 93f0325f..51209309 100644
--- a/jsonschema2pojo-gradle-plugin/pom.xml
+++ b/jsonschema2pojo-gradle-plugin/pom.xml
@@ -24,7 +24,7 @@
         <dependency>
             <groupId>org.codehaus.groovy</groupId>
             <artifactId>groovy-all</artifactId>
-            <version>1.8.6</version>
+            <version>${groovy.version}</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/pom.xml b/pom.xml
index 2a457ed2..82d769e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -51,6 +51,7 @@
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <groovy.version>1.8.6</groovy.version>
         <gradle.version>1.6</gradle.version>
         <gson.version>2.5</gson.version>
         <moshi.version>1.1.0</moshi.version>
@@ -350,6 +351,32 @@
                 <version>4.11</version>
                 <scope>test</scope>
             </dependency>
+            <dependency>
+                <groupId>org.slf4j</groupId>
+                <artifactId>slf4j-simple</artifactId>
+                <version>1.7.24</version>
+                <scope>test</scope>
+            </dependency>
+            <dependency>
+                <groupId>com.github.tomakehurst</groupId>
+                <artifactId>wiremock</artifactId>
+                <version>2.5.1</version>
+                <scope>test</scope>
+                <exclusions>
+                    <exclusion>
+                        <artifactId>slf4j-api</artifactId>
+                        <groupId>org.slf4j</groupId>
+                    </exclusion>
+                    <exclusion>
+                        <artifactId>jackson-core</artifactId>
+                        <groupId>com.fasterxml.jackson.core</groupId>
+                    </exclusion>
+                    <exclusion>
+                        <artifactId>jackson-annotations</artifactId>
+                        <groupId>com.fasterxml.jackson.core</groupId>
+                    </exclusion>
+                </exclusions>
+            </dependency>
             <dependency>
                 <groupId>org.apache.ant</groupId>
                 <artifactId>ant</artifactId>


Test patch:
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 00000000..09cd2694
--- /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 90e40d2e..657a2d44 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.IOException;
 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 class ContentResolverTest {
         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 00000000..4a31ea1e
--- /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 00000000..21010bd2
--- /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 00000000..c615376a
--- /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