Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
postcodes-io-java
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
James Coyle
postcodes-io-java
Commits
241120f6
Commit
241120f6
authored
Aug 13, 2016
by
Deepak Prabhakar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added PostcodeLookupBuilder, JsonFetcher & it's test
parent
7a1c7d3b
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
376 additions
and
0 deletions
+376
-0
.classpath
.classpath
+27
-0
.gitignore
.gitignore
+1
-0
.project
.project
+28
-0
.settings/org.eclipse.core.resources.prefs
.settings/org.eclipse.core.resources.prefs
+4
-0
.settings/org.eclipse.jdt.core.prefs
.settings/org.eclipse.jdt.core.prefs
+5
-0
.settings/org.eclipse.m2e.core.prefs
.settings/org.eclipse.m2e.core.prefs
+4
-0
pom.xml
pom.xml
+54
-0
src/main/java/com/postcode/io/initializers/PostCode.java
src/main/java/com/postcode/io/initializers/PostCode.java
+10
-0
src/main/java/com/postcode/io/initializers/PostcodeLookup.java
...ain/java/com/postcode/io/initializers/PostcodeLookup.java
+56
-0
src/main/java/com/postcode/io/json/JsonFetcher.java
src/main/java/com/postcode/io/json/JsonFetcher.java
+61
-0
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
...java/com/postcode/io/initializers/PostcodeLookupTest.java
+22
-0
src/test/java/com/postcode/io/json/JsonFetcherTest.java
src/test/java/com/postcode/io/json/JsonFetcherTest.java
+69
-0
src/test/resources/postcodeLookup.json
src/test/resources/postcodeLookup.json
+35
-0
No files found.
.classpath
0 → 100644
View file @
241120f6
<?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
kind=
"src"
path=
"src/test/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>
.gitignore
View file @
241120f6
...
...
@@ -10,3 +10,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/
.project
0 → 100644
View file @
241120f6
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>
postcodes-io-java
</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>
org.eclipse.jdt.core.javabuilder
</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>
org.sonarlint.eclipse.core.sonarlintBuilder
</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>
org.eclipse.m2e.core.maven2Builder
</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>
org.eclipse.jdt.core.javanature
</nature>
<nature>
org.eclipse.m2e.core.maven2Nature
</nature>
</natures>
</projectDescription>
.settings/org.eclipse.core.resources.prefs
0 → 100644
View file @
241120f6
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
.settings/org.eclipse.jdt.core.prefs
0 → 100644
View file @
241120f6
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
.settings/org.eclipse.m2e.core.prefs
0 → 100644
View file @
241120f6
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
pom.xml
0 → 100644
View file @
241120f6
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.postcode
</groupId>
<artifactId>
postcodes-io-java
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
postcodes-io-java
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<build>
<finalName>
postcodes-io-java
</finalName>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
<version>
1.7.21
</version>
</dependency>
<dependency>
<groupId>
log4j
</groupId>
<artifactId>
log4j
</artifactId>
<version>
1.2.17
</version>
</dependency>
<dependency>
<groupId>
org.json
</groupId>
<artifactId>
json
</artifactId>
<version>
20160212
</version>
</dependency>
</dependencies>
</project>
src/main/java/com/postcode/io/initializers/PostCode.java
0 → 100644
View file @
241120f6
package
com.postcode.io.initializers
;
/**
*
* @author Deepak
*
*/
public
class
PostCode
{
}
src/main/java/com/postcode/io/initializers/PostcodeLookup.java
0 → 100644
View file @
241120f6
package
com.postcode.io.initializers
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
/**
* Lookup a postcode
*
* @author Deepak
*
*/
public
class
PostcodeLookup
{
private
static
final
String
LOOKUP_URL
=
"http://api.postcodes.io/postcodes/"
;
private
String
postcode
;
private
String
url
;
private
PostcodeLookup
(
URL
url
,
String
postcode
)
{
this
.
url
=
url
.
toString
();
this
.
postcode
=
postcode
;
}
public
String
getPostcode
()
{
return
postcode
;
}
public
String
getUrl
()
{
return
url
;
}
/**
* {@link PostcodeLookup} {@link Builder} Class
*
* @author Deepak
*
*/
public
static
class
Builder
{
private
String
postcode
;
public
Builder
()
{
}
public
PostcodeLookup
build
()
throws
MalformedURLException
{
return
new
PostcodeLookup
(
new
URL
(
LOOKUP_URL
.
toString
().
concat
(
postcode
)),
postcode
);
}
public
Builder
postcode
(
String
postcode
)
{
this
.
postcode
=
postcode
;
return
this
;
}
}
}
src/main/java/com/postcode/io/json/JsonFetcher.java
0 → 100644
View file @
241120f6
package
com.postcode.io.json
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.util.zip.GZIPInputStream
;
import
org.json.JSONObject
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
*
* Fetch Json From URL
*
* @author Deepak
*
*/
public
class
JsonFetcher
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
JsonFetcher
.
class
);
/**
* @param urlString
* @return
*/
public
static
JSONObject
urlToJson
(
URL
urlString
)
{
StringBuilder
sb
=
null
;
URL
url
;
URLConnection
urlCon
;
try
{
url
=
urlString
;
urlCon
=
url
.
openConnection
();
BufferedReader
in
;
if
(
urlCon
.
getHeaderField
(
"Content-Encoding"
)
!=
null
&&
urlCon
.
getHeaderField
(
"Content-Encoding"
).
equals
(
"gzip"
))
{
LOGGER
.
info
(
"reading data from URL as GZIP Stream"
);
in
=
new
BufferedReader
(
new
InputStreamReader
(
new
GZIPInputStream
(
urlCon
.
getInputStream
())));
}
else
{
LOGGER
.
info
(
"reading data from URL as InputStream"
);
in
=
new
BufferedReader
(
new
InputStreamReader
(
urlCon
.
getInputStream
()));
}
String
inputLine
;
sb
=
new
StringBuilder
();
while
((
inputLine
=
in
.
readLine
())
!=
null
)
{
sb
.
append
(
inputLine
);
}
in
.
close
();
}
catch
(
IOException
e
)
{
LOGGER
.
info
(
"Exception while reading JSON from URL - {}"
,
e
);
}
if
(
sb
!=
null
)
{
return
new
JSONObject
(
sb
.
toString
());
}
else
{
return
new
JSONObject
(
""
);
}
}
}
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
0 → 100644
View file @
241120f6
package
com.postcode.io.initializers
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
java.net.MalformedURLException
;
import
org.junit.Test
;
/**
* @author Deepak
*
*/
public
class
PostcodeLookupTest
{
@Test
public
void
testPostcodeLookupBuilder
()
throws
MalformedURLException
{
PostcodeLookup
postcodeLookup
=
new
PostcodeLookup
.
Builder
().
postcode
(
"bs347np"
).
build
();
assertEquals
(
postcodeLookup
.
getPostcode
(),
"bs347np"
);
assertEquals
(
postcodeLookup
.
getUrl
(),
"http://api.postcodes.io/postcodes/bs347np"
);
}
}
\ No newline at end of file
src/test/java/com/postcode/io/json/JsonFetcherTest.java
0 → 100644
View file @
241120f6
package
com.postcode.io.json
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.io.File
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
org.json.JSONObject
;
import
org.junit.Test
;
/**
* @author Deepak
*
*/
public
class
JsonFetcherTest
{
@Test
public
void
testByURL
()
throws
MalformedURLException
{
JSONObject
json
=
JsonFetcher
.
urlToJson
(
new
URL
(
"http://api.postcodes.io/postcodes/bs347np"
));
test
(
json
);
}
@Test
public
void
testByFile
()
throws
MalformedURLException
{
JSONObject
json
=
JsonFetcher
.
urlToJson
(
new
File
(
System
.
getProperty
(
"user.dir"
).
concat
(
"/src/test/resources/postcodeLookup.json"
))
.
toURI
().
toURL
());
test
(
json
);
}
public
void
test
(
JSONObject
json
)
{
assertTrue
(
json
.
has
(
"status"
));
assertTrue
(
json
.
has
(
"result"
));
JSONObject
result
=
json
.
getJSONObject
(
"result"
);
assertEquals
(
"BS34 7NP"
,
(
result
.
getString
(
"postcode"
)));
assertEquals
(
1
,
(
result
.
getInt
(
"quality"
)));
assertEquals
(
360605
,
(
result
.
getInt
(
"eastings"
)));
assertEquals
(
178655
,
(
result
.
getInt
(
"northings"
)));
assertEquals
(
"England"
,
(
result
.
getString
(
"country"
)));
assertEquals
(
"South West"
,
(
result
.
getString
(
"nhs_ha"
)));
assertEquals
(-
2.56899282995555
,
result
.
getDouble
(
"longitude"
),
1
);
assertEquals
(
51.5054493186496
,
result
.
getDouble
(
"latitude"
),
2
);
assertEquals
(
"Filton and Bradley Stoke"
,
(
result
.
getString
(
"parliamentary_constituency"
)));
assertEquals
(
"South West"
,
(
result
.
getString
(
"european_electoral_region"
)));
assertEquals
(
"South Gloucestershire"
,
(
result
.
getString
(
"primary_care_trust"
)));
assertEquals
(
"South West"
,
(
result
.
getString
(
"region"
)));
assertEquals
(
"South Gloucestershire 018D"
,
(
result
.
getString
(
"lsoa"
)));
assertEquals
(
"South Gloucestershire 018"
,
(
result
.
getString
(
"msoa"
)));
assertEquals
(
"7NP"
,
(
result
.
getString
(
"incode"
)));
assertEquals
(
"BS34"
,
(
result
.
getString
(
"outcode"
)));
assertEquals
(
"South Gloucestershire"
,
(
result
.
getString
(
"admin_district"
)));
assertEquals
(
"Filton"
,
(
result
.
getString
(
"parish"
)));
assertEquals
(
JSONObject
.
NULL
,
result
.
get
(
"admin_county"
));
assertEquals
(
"Filton"
,
(
result
.
getString
(
"admin_ward"
)));
assertEquals
(
"NHS South Gloucestershire"
,
(
result
.
getString
(
"ccg"
)));
assertEquals
(
"Bath and North East Somerset, North Somerset and South Gloucestershire"
,
(
result
.
getString
(
"nuts"
)));
JSONObject
codes
=
result
.
getJSONObject
(
"codes"
);
assertEquals
(
"E06000025"
,
(
codes
.
getString
(
"admin_district"
)));
assertEquals
(
"E99999999"
,
(
codes
.
getString
(
"admin_county"
)));
assertEquals
(
"E05002055"
,
(
codes
.
getString
(
"admin_ward"
)));
assertEquals
(
"E04001052"
,
(
codes
.
getString
(
"parish"
)));
assertEquals
(
"E38000155"
,
(
codes
.
getString
(
"ccg"
)));
assertEquals
(
"UKK12"
,
(
codes
.
getString
(
"nuts"
)));
}
}
src/test/resources/postcodeLookup.json
0 → 100644
View file @
241120f6
{
"status"
:
200
,
"result"
:
{
"postcode"
:
"BS34 7NP"
,
"quality"
:
1
,
"eastings"
:
360605
,
"northings"
:
178655
,
"country"
:
"England"
,
"nhs_ha"
:
"South West"
,
"longitude"
:
-2.56899282995555
,
"latitude"
:
51.5054493186496
,
"parliamentary_constituency"
:
"Filton and Bradley Stoke"
,
"european_electoral_region"
:
"South West"
,
"primary_care_trust"
:
"South Gloucestershire"
,
"region"
:
"South West"
,
"lsoa"
:
"South Gloucestershire 018D"
,
"msoa"
:
"South Gloucestershire 018"
,
"incode"
:
"7NP"
,
"outcode"
:
"BS34"
,
"admin_district"
:
"South Gloucestershire"
,
"parish"
:
"Filton"
,
"admin_county"
:
null
,
"admin_ward"
:
"Filton"
,
"ccg"
:
"NHS South Gloucestershire"
,
"nuts"
:
"Bath and North East Somerset, North Somerset and South Gloucestershire"
,
"codes"
:
{
"admin_district"
:
"E06000025"
,
"admin_county"
:
"E99999999"
,
"admin_ward"
:
"E05002055"
,
"parish"
:
"E04001052"
,
"ccg"
:
"E38000155"
,
"nuts"
:
"UKK12"
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment