Commit 0a11a14f authored by Deepak's avatar Deepak

Added postcode validator

parent fc259652
......@@ -5,8 +5,11 @@ import java.net.URL;
import org.codehaus.plexus.util.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.postcode.io.PostCodeDetails;
import com.postcode.io.json.JsonFetcher;
......@@ -97,4 +100,8 @@ public class Postcode {
this.json = json;
}
public static boolean validatePostcode(String postcode) throws JSONException, UnirestException {
return Unirest.get("https://api.postcodes.io/postcodes/".concat(postcode).concat("/validate")).asJson()
.getBody().getObject().getBoolean("result");
}
}
......@@ -3,6 +3,9 @@ package com.postcode.io.initializers;
import java.net.URL;
import java.util.List;
import org.json.JSONException;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.postcode.io.initializers.ReverseGeocoding.Reverse;
/**
......@@ -88,4 +91,8 @@ public class PostcodeLookup {
return new ReverseGeocoding(outcode);
}
public static boolean validate(String postcode) throws JSONException, UnirestException {
return Postcode.validatePostcode(postcode);
}
}
package com.postcode.io.initializers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
......@@ -99,7 +100,13 @@ public class PostcodeLookupTest {
.getBody().getObject(),
PostcodeLookup.randomPostcode("bs347np").asjson(), JSONCompareMode.LENIENT);
assertTrue(PostcodeLookup.randomPostcode().asjson().has("result"));
assertEquals(200,PostcodeLookup.randomPostcode().asjson().getInt("status"));
assertEquals(200, PostcodeLookup.randomPostcode().asjson().getInt("status"));
}
@Test
public void testPostcodeValidator() throws JSONException, UnirestException {
assertTrue(PostcodeLookup.validate("ST42EU"));
assertFalse(PostcodeLookup.validate("ST4"));
}
}
\ No newline at end of file
package com.postcode.io.initializers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.json.JSONException;
import org.junit.Test;
import com.mashape.unirest.http.exceptions.UnirestException;
public class PostcodeTest {
@Test
......@@ -21,4 +26,10 @@ public class PostcodeTest {
}
}
@Test
public void testPostcodeValidator() throws JSONException, UnirestException {
assertTrue(Postcode.validatePostcode("ST42EU"));
assertFalse(Postcode.validatePostcode("ST4"));
}
}
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