Commit fc259652 authored by Deepak's avatar Deepak

Added random postcode retriever

parent 57a473ae
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
......@@ -80,4 +80,12 @@ public class PostcodeLookup {
return new ReverseGeocoding(reverses);
}
public static ReverseGeocoding randomPostcode() {
return new ReverseGeocoding("");
}
public static ReverseGeocoding randomPostcode(String outcode) {
return new ReverseGeocoding(outcode);
}
}
......@@ -20,12 +20,18 @@ public class ReverseGeocoding {
private static final String LOOKUP_URL = "http://api.postcodes.io/postcodes?";
private static final String RANDOM_LOOKUP_URL = "https://api.postcodes.io/random/postcodes";
private static Double longitude;
private static Double latitude;
private static int limit;
private static String outcode;
private static boolean random;
private static int radius;
private static boolean wideSearch;
......@@ -37,6 +43,11 @@ public class ReverseGeocoding {
public ReverseGeocoding() {
}
public ReverseGeocoding(String outcode) {
ReverseGeocoding.outcode = outcode;
ReverseGeocoding.random = true;
}
public ReverseGeocoding(Double longitude, Double latitude) {
ReverseGeocoding.longitude = longitude;
ReverseGeocoding.latitude = latitude;
......@@ -77,6 +88,9 @@ public class ReverseGeocoding {
* @throws UnirestException
*/
public JSONObject asjson() throws IOException, UnirestException {
if (random) {
return Unirest.get(RANDOM_LOOKUP_URL).queryString("outcode", outcode).asJson().getBody().getObject();
}
String url = "";
url = url.concat("lon=").concat(String.valueOf(longitude));
url = url.concat("&lat=").concat(String.valueOf(latitude));
......
package com.postcode.io.initializers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
......@@ -62,7 +63,7 @@ public class PostcodeLookupTest {
@Test
public void testReverseGeocodings() throws MalformedURLException, UnirestException, IOException {
List<com.postcode.io.initializers.ReverseGeocoding.Reverse> reverseList = new ArrayList<>();
List<Reverse> reverseList = new ArrayList<>();
ReverseGeocoding reverseGeocoding = new ReverseGeocoding();
Reverse reverse = reverseGeocoding.new Reverse();
reverse.setLongitude(0.629834723775309);
......@@ -91,4 +92,14 @@ public class PostcodeLookupTest {
JSONCompareMode.STRICT);
}
@Test
public void testRandomPostcode() throws JSONException, UnirestException, IOException {
JSONAssert.assertEquals(
Unirest.get("https://api.postcodes.io/random/postcodes").queryString("outcode", "bs347np").asJson()
.getBody().getObject(),
PostcodeLookup.randomPostcode("bs347np").asjson(), JSONCompareMode.LENIENT);
assertTrue(PostcodeLookup.randomPostcode().asjson().has("result"));
assertEquals(200,PostcodeLookup.randomPostcode().asjson().getInt("status"));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment