From 7aa1a6df6b0f5720ec6192bf143b0fe595aed557 Mon Sep 17 00:00:00 2001 From: Rijn Buve Date: Wed, 23 Nov 2016 09:51:14 +0100 Subject: [PATCH] Added /json methods, fixed Javascript --- deployment/pom.xml | 2 +- example/index.html | 76 ++++++------- pom.xml | 2 +- service/pom.xml | 2 +- .../com/mapcode/services/MapcodeResource.java | 93 ++++++++++++++++ .../com/mapcode/services/RootResource.java | 10 ++ .../implementation/MapcodeResourceImpl.java | 103 ++++++++++++++++++ .../implementation/RootResourceImpl.java | 37 ++++--- .../mapcode/services/ApiAlphabetsTest.java | 18 ++- .../com/mapcode/services/ApiCodesTest.java | 19 +++- .../com/mapcode/services/ApiOthersTest.java | 44 +++++--- .../mapcode/services/ApiTerritoriesTest.java | 56 ++++++---- 12 files changed, 368 insertions(+), 94 deletions(-) diff --git a/deployment/pom.xml b/deployment/pom.xml index 41c4612..c3d68ab 100644 --- a/deployment/pom.xml +++ b/deployment/pom.xml @@ -22,7 +22,7 @@ com.mapcode mapcode-rest-service - 2.4.1.2 + 2.4.1.3 deployment diff --git a/example/index.html b/example/index.html index 9e1f3c6..1680d9d 100644 --- a/example/index.html +++ b/example/index.html @@ -37,7 +37,7 @@

Sample HTML/Javascript using the mapcode REST API

You can have a look at the source code of this page to see how the REST API is called, or you can try it out by simply entering some information in the dialog boxes and pressing the conversion/request buttons. -

+

Copyright (C) 2016, Stichting Mapcode Foundation, http://www.mapcode.com
@@ -63,10 +63,10 @@

REST API Version

@@ -127,7 +127,7 @@

REST API Version

@@ -244,7 +244,7 @@

Convert a coordinate (latitude, longitude) to mapcode(s)

var url = apiBaseURL + "/coords/" + mapcode + "?context=" + context + "&allowLog=true&client=web"; /* Update page. */ - document.getElementById("resultDecode").innerHTML = 'Waiting...'; + document.getElementById("resultDecode").innerHTML = "Waiting...<" + "/font>"; document.getElementById("requestDecode").innerHTML = createRequestLine(url); /* Asynchronously request to decode mapcode, call decodeResponseReady when done. */ @@ -253,7 +253,7 @@

Convert a coordinate (latitude, longitude) to mapcode(s)

if (httpRequest.readyState == 4) { decodeResponseReady(httpRequest.responseText, httpRequest.status); } - } + }; httpRequest.open("GET", url, true); httpRequest.setRequestHeader('Accept', 'application/json'); httpRequest.send(); @@ -267,10 +267,10 @@

Convert a coordinate (latitude, longitude) to mapcode(s)

/* Parse the response into a coordinate. */ var result = JSON.parse(response); - document.getElementById("resultDecode").innerHTML = "Coordinate:
(" + result.latDeg + ", " + result.lonDeg + ")"; + document.getElementById("resultDecode").innerHTML = "Coordinate:<" + "/u><" + "/b>
(" + result.latDeg + ", " + result.lonDeg + ")<" + "/tt>"; } else { - document.getElementById("resultDecode").innerHTML = "Error (status = " + status + ')'; + document.getElementById("resultDecode").innerHTML = "Error<" + "/b> (status = " + status + ")"; } } @@ -305,12 +305,12 @@

Convert a mapcode to a coordinate (latitude, longitude)

@@ -383,7 +383,7 @@

Get territory information

diff --git a/pom.xml b/pom.xml index b5c21e3..2605448 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ mapcode-rest-service pom - 2.4.1.2 + 2.4.1.3 Mapcode REST API Web Service diff --git a/service/pom.xml b/service/pom.xml index f3b4ad8..15a6310 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -22,7 +22,7 @@ com.mapcode mapcode-rest-service - 2.4.1.2 + 2.4.1.3 service diff --git a/service/src/main/java/com/mapcode/services/MapcodeResource.java b/service/src/main/java/com/mapcode/services/MapcodeResource.java index 2d6eb33..7a9f1ba 100644 --- a/service/src/main/java/com/mapcode/services/MapcodeResource.java +++ b/service/src/main/java/com/mapcode/services/MapcodeResource.java @@ -85,6 +85,12 @@ void convertLatLonToMapcode( void convertLatLonToMapcodeXml( @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/codes") + void convertLatLonToMapcodeJson( + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Convert a lat/lon to one or more mapcodes. All possible mapcodes are returned. * @@ -134,6 +140,21 @@ void convertLatLonToMapcodeXml( @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/codes/{" + PARAM_LAT_DEG + "},{" + PARAM_LON_DEG + '}') + void convertLatLonToMapcodeJson( + @PathParam(PARAM_LAT_DEG) double paramLatDeg, + @PathParam(PARAM_LON_DEG) double paramLonDeg, + @QueryParam(PARAM_PRECISION) @DefaultValue("0") int paramPrecision, + @QueryParam(PARAM_TERRITORY) @Nullable String paramTerritory, + @QueryParam(PARAM_CONTEXT) @Nullable String paramContextMustBeNull, + @QueryParam(PARAM_ALPHABET) @Nullable String paramAlphabet, + @QueryParam(PARAM_INCLUDE) @DefaultValue("") @Nonnull String paramInclude, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Convert a lat/lon to one or more mapcodes. * @@ -187,6 +208,22 @@ void convertLatLonToMapcodeXml( @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramDebug, @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/codes/{" + PARAM_LAT_DEG + "},{" + PARAM_LON_DEG + "}/{" + PARAM_TYPE + '}') + void convertLatLonToMapcodeJson( + @PathParam(PARAM_LAT_DEG) double paramLatDeg, + @PathParam(PARAM_LON_DEG) double paramLonDeg, + @PathParam(PARAM_TYPE) @Nullable String paramType, + @QueryParam(PARAM_PRECISION) @DefaultValue("0") int paramPrecision, + @QueryParam(PARAM_TERRITORY) @Nullable String paramTerritory, + @QueryParam(PARAM_CONTEXT) @Nullable String paramContextMustBeNull, + @QueryParam(PARAM_ALPHABET) @Nullable String paramAlphabet, + @QueryParam(PARAM_INCLUDE) @DefaultValue("") @Nonnull String paramInclude, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramDebug, + @Suspended @Nonnull AsyncResponse response) throws ApiException; + // Unsupported operation. @GET @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @@ -200,6 +237,12 @@ void convertMapcodeToLatLon( void convertMapcodeToLatLonXml( @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/coords") + void convertMapcodeToLatLonJson( + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Convert a mapcode into a lat/lon pair. * @@ -234,6 +277,17 @@ void convertMapcodeToLatLonXml( @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramDebug, @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/coords/{" + PARAM_CODE + '}') + void convertMapcodeToLatLonJson( + @PathParam(PARAM_CODE) @Nonnull String paramCode, + @QueryParam(PARAM_CONTEXT) @Nullable String paramContext, + @QueryParam(PARAM_TERRITORY) @Nullable String paramTerritoryMustBeNull, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramDebug, + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Get a list of all valid territory codes. * @@ -264,6 +318,16 @@ void getTerritoriesXml( @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/territories") + void getTerritoriesJson( + @QueryParam(PARAM_OFFSET) @DefaultValue(DEFAULT_OFFSET) int offset, + @QueryParam(PARAM_COUNT) @DefaultValue(DEFAULT_COUNT) int count, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Get info for a specific territory. * @@ -294,6 +358,16 @@ void getTerritoryXml( @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/territories/{" + PARAM_TERRITORY + '}') + void getTerritoryJson( + @PathParam(PARAM_TERRITORY) @Nonnull String paramTerritory, + @QueryParam(PARAM_CONTEXT) @Nullable String paramContext, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Get a list of all valid alphabet codes. * @@ -324,6 +398,16 @@ void getAlphabetsXml( @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, @Suspended @Nonnull AsyncResponse response) throws ApiException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/alphabets") + void getAlphabetsJson( + @QueryParam(PARAM_OFFSET) @DefaultValue(DEFAULT_OFFSET) int offset, + @QueryParam(PARAM_COUNT) @DefaultValue(DEFAULT_COUNT) int count, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, + @Suspended @Nonnull AsyncResponse response) throws ApiException; + /** * Get info for a specific alphabet. * @@ -350,4 +434,13 @@ void getAlphabetXml( @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, @Suspended @Nonnull AsyncResponse response) throws ApiException; + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("json/alphabets/{" + PARAM_ALPHABET + '}') + void getAlphabetJson( + @PathParam(PARAM_ALPHABET) @Nonnull String paramAlphabet, + @QueryParam(PARAM_CLIENT) @DefaultValue("") @Nonnull String paramClient, + @QueryParam(PARAM_ALLOW_LOG) @DefaultValue("true") @Nonnull String paramAllowLog, + @Suspended @Nonnull AsyncResponse response) throws ApiException; } diff --git a/service/src/main/java/com/mapcode/services/RootResource.java b/service/src/main/java/com/mapcode/services/RootResource.java index 016f29b..c1f5b01 100644 --- a/service/src/main/java/com/mapcode/services/RootResource.java +++ b/service/src/main/java/com/mapcode/services/RootResource.java @@ -53,6 +53,11 @@ public interface RootResource { @Produces(MediaType.APPLICATION_XML) void getVersionXml(@Suspended @Nonnull AsyncResponse response); + @Path("json/version") + @GET + @Produces(MediaType.APPLICATION_JSON) + void getVersionJson(@Suspended @Nonnull AsyncResponse response); + /** * This method returns whether the service is operational or not (status code 200 is OK). * @@ -68,6 +73,11 @@ public interface RootResource { @GET void getStatusXml(@Suspended @Nonnull AsyncResponse response); + @Path("json/status") + @Produces(MediaType.APPLICATION_JSON) + @GET + void getStatusJson(@Suspended @Nonnull AsyncResponse response); + /** * This method returns system metrics. * diff --git a/service/src/main/java/com/mapcode/services/implementation/MapcodeResourceImpl.java b/service/src/main/java/com/mapcode/services/implementation/MapcodeResourceImpl.java index cfd9524..beed089 100644 --- a/service/src/main/java/com/mapcode/services/implementation/MapcodeResourceImpl.java +++ b/service/src/main/java/com/mapcode/services/implementation/MapcodeResourceImpl.java @@ -109,6 +109,12 @@ public void convertLatLonToMapcodeXml( convertLatLonToMapcode(response); } + @Override + public void convertLatLonToMapcodeJson( + @Suspended @Nonnull final AsyncResponse response) throws ApiInvalidFormatException { + convertLatLonToMapcode(response); + } + @Override public void convertLatLonToMapcode( final double paramLatDeg, @@ -142,6 +148,23 @@ public void convertLatLonToMapcodeXml( paramAlphabet, paramInclude, paramClient, paramAllowLog, response); } + @Override + public void convertLatLonToMapcodeJson( + final double paramLatDeg, + final double paramLonDeg, + final int paramPrecision, + @Nullable final String paramTerritory, + @Nullable final String paramContextMustBeNull, + @Nullable final String paramAlphabet, + @Nonnull final String paramInclude, + @Nonnull final String paramClient, + @Nonnull final String paramAllowLog, + @Suspended @Nonnull final AsyncResponse response) + throws ApiInvalidFormatException { + convertLatLonToMapcode(paramLatDeg, paramLonDeg, paramPrecision, paramTerritory, paramContextMustBeNull, + paramAlphabet, paramInclude, paramClient, paramAllowLog, response); + } + @Override public void convertLatLonToMapcode( final double paramLatDeg, @@ -369,6 +392,24 @@ public void convertLatLonToMapcodeXml( paramAlphabet, paramInclude, paramClient, paramDebug, response); } + @Override + public void convertLatLonToMapcodeJson( + final double paramLatDeg, + final double paramLonDeg, + @Nullable final String paramType, + final int paramPrecision, + @Nullable final String paramTerritory, + @Nullable final String paramContextMustBeNull, + @Nullable final String paramAlphabet, + @Nonnull final String paramInclude, + @Nonnull final String paramClient, + @Nonnull final String paramDebug, + @Suspended @Nonnull final AsyncResponse response) + throws ApiInvalidFormatException { + convertLatLonToMapcode(paramLatDeg, paramLonDeg, paramType, paramPrecision, paramTerritory, paramContextMustBeNull, + paramAlphabet, paramInclude, paramClient, paramDebug, response); + } + @Override public void convertMapcodeToLatLon( @Nonnull final AsyncResponse response) throws ApiNotFoundException, ApiInvalidFormatException { @@ -386,6 +427,13 @@ public void convertMapcodeToLatLonXml( convertMapcodeToLatLon(response); } + @Override + public void convertMapcodeToLatLonJson( + @Suspended @Nonnull final AsyncResponse response) + throws ApiNotFoundException, ApiInvalidFormatException { + convertMapcodeToLatLon(response); + } + @Override public void convertMapcodeToLatLon( @Nonnull final String paramCode, @@ -467,6 +515,18 @@ public void convertMapcodeToLatLonXml( convertMapcodeToLatLon(paramCode, paramContext, paramTerritoryMustBeNull, paramClient, paramDebug, response); } + @Override + public void convertMapcodeToLatLonJson( + @Nonnull final String paramCode, + @Nullable final String paramContext, + @Nullable final String paramTerritoryMustBeNull, + @Nonnull final String paramClient, + @Nonnull final String paramDebug, + @Suspended @Nonnull final AsyncResponse response) + throws ApiNotFoundException, ApiInvalidFormatException { + convertMapcodeToLatLon(paramCode, paramContext, paramTerritoryMustBeNull, paramClient, paramDebug, response); + } + @Override public void getTerritories( final int offset, @@ -513,6 +573,17 @@ public void getTerritoriesXml( getTerritories(offset, count, paramClient, paramAllowLog, response); } + @Override + public void getTerritoriesJson( + final int offset, + final int count, + @Nonnull final String paramClient, + @Nonnull final String paramAllowLog, + @Suspended @Nonnull final AsyncResponse response) + throws ApiIntegerOutOfRangeException { + getTerritories(offset, count, paramClient, paramAllowLog, response); + } + @Override public void getTerritory( @Nonnull final String paramTerritory, @@ -568,6 +639,17 @@ public void getTerritoryXml( getTerritory(paramTerritory, paramContext, paramClient, paramAllowLog, response); } + @Override + public void getTerritoryJson( + @Nonnull final String paramTerritory, + @Nullable final String paramContext, + @Nonnull final String paramClient, + @Nonnull final String paramAllowLog, + @Suspended @Nonnull final AsyncResponse response) + throws ApiInvalidFormatException { + getTerritory(paramTerritory, paramContext, paramClient, paramAllowLog, response); + } + @Override public void getAlphabets( final int offset, @@ -614,6 +696,17 @@ public void getAlphabetsXml( getAlphabets(offset, count, paramClient, paramAllowLog, response); } + @Override + public void getAlphabetsJson( + final int offset, + final int count, + @Nonnull final String paramClient, + @Nonnull final String paramAllowLog, + @Suspended @Nonnull final AsyncResponse response) + throws ApiIntegerOutOfRangeException { + getAlphabets(offset, count, paramClient, paramAllowLog, response); + } + @Override public void getAlphabet( @Nonnull final String paramAlphabet, @@ -658,6 +751,16 @@ public void getAlphabetXml( getAlphabet(paramAlphabet, paramClient, paramAllowLog, response); } + @Override + public void getAlphabetJson( + @Nonnull final String paramAlphabet, + @Nonnull final String paramClient, + @Nonnull final String paramAllowLog, + @Suspended @Nonnull final AsyncResponse response) + throws ApiInvalidFormatException { + getAlphabet(paramAlphabet, paramClient, paramAllowLog, response); + } + @Nonnull private static Territory resolveTerritory(@Nonnull final String paramTerritory, @Nullable final String paramParent) { Territory parentTerritory; diff --git a/service/src/main/java/com/mapcode/services/implementation/RootResourceImpl.java b/service/src/main/java/com/mapcode/services/implementation/RootResourceImpl.java index 2b53fe8..19b11b0 100644 --- a/service/src/main/java/com/mapcode/services/implementation/RootResourceImpl.java +++ b/service/src/main/java/com/mapcode/services/implementation/RootResourceImpl.java @@ -142,20 +142,21 @@ public class RootResourceImpl implements RootResource { "The default response type is JSON, if no \"Accept:\" header is specified.\n\n" + - "Alternatively, to retrieve XML responses if no \"Accept:\" header is specified, you can add \"/xml\"\n" + - "in the URL, directly after \"/mapcode\".\n\n" + + "Note that some browsers (such as FireFox) may silently insert an 'Accept:' header when using a\n" + + "XMLHttpRequest in Javascript. This may lead to returning JSON in some browsers and XML in others.\n" + + "It is advised to explicitly set the appropriate header in Javascript in such cases.\n\n" + - "So, the following methods are supported as well and return XML by default:\n\n" + + "Alternatively, to retrieve XML or JSON responses if no \"Accept:\" header is specified, you can add\n" + + "\"/xml\" or \"/json\" in the URL, directly after \"/mapcode\".\n\n" + - " GET /mapcode/xml/version\n" + - " GET /mapcode/xml/status\n" + - " GET /mapcode/xml/codes\n" + - " GET /mapcode/xml/coords\n" + - " GET /mapcode/xml/territories\n" + - " GET /mapcode/xml/alphabets\n\n" + + "So, the following methods are supported as well and return XML or JSON by default:\n\n" + - "We recommend to use the method without \"/xml: and use the \"Accept:\" header to specify the\n" + - "response type, however.\n"; + " GET /mapcode/xml/version GET /mapcode/json/version\n" + + " GET /mapcode/xml/status GET /mapcode/json/status\n" + + " GET /mapcode/xml/codes GET /mapcode/json/codes\n" + + " GET /mapcode/xml/coords GET /mapcode/json/coords\n" + + " GET /mapcode/xml/territories GET /mapcode/json/territories\n" + + " GET /mapcode/xml/alphabets GET /mapcode/json/alphabets\n"; private final MavenProperties mavenProperties; private final SystemMetrics metrics; @@ -199,7 +200,12 @@ public void getVersion(@Suspended @Nonnull final AsyncResponse response) { } @Override - public void getVersionXml(@Suspended @Nonnull AsyncResponse response) { + public void getVersionXml(@Suspended @Nonnull final AsyncResponse response) { + getVersion(response); + } + + @Override + public void getVersionJson(@Suspended @Nonnull final AsyncResponse response) { getVersion(response); } @@ -211,7 +217,12 @@ public void getStatus(@Suspended @Nonnull final AsyncResponse response) { } @Override - public void getStatusXml(@Suspended @Nonnull AsyncResponse response) { + public void getStatusXml(@Suspended @Nonnull final AsyncResponse response) { + getStatus(response); + } + + @Override + public void getStatusJson(@Suspended @Nonnull final AsyncResponse response) { getStatus(response); } diff --git a/service/src/test/java/com/mapcode/services/ApiAlphabetsTest.java b/service/src/test/java/com/mapcode/services/ApiAlphabetsTest.java index 21999ca..3165d8e 100644 --- a/service/src/test/java/com/mapcode/services/ApiAlphabetsTest.java +++ b/service/src/test/java/com/mapcode/services/ApiAlphabetsTest.java @@ -208,16 +208,17 @@ public void checkAlphabetJsonError() { } @Test - public void checkAlphabetXml() { + public void checkAlphabetXmlJson() { LOG.info("checkAlphabetXml"); - final String expected = "GREEK"; + final String expectedXml = "GREEK"; + final String expectedJson = "{\"name\":\"GREEK\"}"; Response response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/alphabets/greek")). request(). accept(MediaType.APPLICATION_XML_TYPE).get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(expected, + Assert.assertEquals(expectedXml, response.readEntity(String.class)); response = new ResteasyClientBuilder().build(). @@ -226,7 +227,16 @@ public void checkAlphabetXml() { get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(expected, + Assert.assertEquals(expectedXml, + response.readEntity(String.class)); + + response = new ResteasyClientBuilder().build(). + target(server.url("/mapcode/json/alphabets/greek")). + request(). + get(); + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(expectedJson, response.readEntity(String.class)); } diff --git a/service/src/test/java/com/mapcode/services/ApiCodesTest.java b/service/src/test/java/com/mapcode/services/ApiCodesTest.java index 849f1b3..467e977 100644 --- a/service/src/test/java/com/mapcode/services/ApiCodesTest.java +++ b/service/src/test/java/com/mapcode/services/ApiCodesTest.java @@ -186,15 +186,17 @@ public void checkCodesXml() { } @Test - public void checkCodesPrecision0Json() { - LOG.info("checkCodesPrecision0Json"); + public void checkCodesPrecision0XmlJson() { + LOG.info("checkCodesPrecision0XmlJson"); + final String expectedXml = "JL0.KPLUXVJ0L6.9PNQJL0.KPLUXR8RN.07ZLUXSQB.NR3BELR8RN.07ZBEL0L46.LG9DEUR8RN.07ZFRAVJ0L6.9PNQ"; + final String expectedJson = "{\"local\":{\"mapcode\":\"JL0.KP\",\"territory\":\"LUX\"},\"international\":{\"mapcode\":\"VJ0L6.9PNQ\"},\"mapcodes\":[{\"mapcode\":\"JL0.KP\",\"territory\":\"LUX\"},{\"mapcode\":\"R8RN.07Z\",\"territory\":\"LUX\"},{\"mapcode\":\"SQB.NR3\",\"territory\":\"BEL\"},{\"mapcode\":\"R8RN.07Z\",\"territory\":\"BEL\"},{\"mapcode\":\"0L46.LG9\",\"territory\":\"DEU\"},{\"mapcode\":\"R8RN.07Z\",\"territory\":\"FRA\"},{\"mapcode\":\"VJ0L6.9PNQ\"}]}"; Response response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/codes/" + TEST_LATLON1 + "?precision=0")). request(). accept(MediaType.APPLICATION_JSON_TYPE).get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals("{\"local\":{\"mapcode\":\"JL0.KP\",\"territory\":\"LUX\"},\"international\":{\"mapcode\":\"VJ0L6.9PNQ\"},\"mapcodes\":[{\"mapcode\":\"JL0.KP\",\"territory\":\"LUX\"},{\"mapcode\":\"R8RN.07Z\",\"territory\":\"LUX\"},{\"mapcode\":\"SQB.NR3\",\"territory\":\"BEL\"},{\"mapcode\":\"R8RN.07Z\",\"territory\":\"BEL\"},{\"mapcode\":\"0L46.LG9\",\"territory\":\"DEU\"},{\"mapcode\":\"R8RN.07Z\",\"territory\":\"FRA\"},{\"mapcode\":\"VJ0L6.9PNQ\"}]}", + Assert.assertEquals(expectedJson, response.readEntity(String.class)); response = new ResteasyClientBuilder().build(). @@ -203,7 +205,16 @@ public void checkCodesPrecision0Json() { get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals("JL0.KPLUXVJ0L6.9PNQJL0.KPLUXR8RN.07ZLUXSQB.NR3BELR8RN.07ZBEL0L46.LG9DEUR8RN.07ZFRAVJ0L6.9PNQ", + Assert.assertEquals(expectedXml, + response.readEntity(String.class)); + + response = new ResteasyClientBuilder().build(). + target(server.url("/mapcode/json/codes/" + TEST_LATLON1 + "?precision=0")). + request(). + get(); + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(expectedJson, response.readEntity(String.class)); } diff --git a/service/src/test/java/com/mapcode/services/ApiOthersTest.java b/service/src/test/java/com/mapcode/services/ApiOthersTest.java index 591e7d3..685d9b1 100644 --- a/service/src/test/java/com/mapcode/services/ApiOthersTest.java +++ b/service/src/test/java/com/mapcode/services/ApiOthersTest.java @@ -47,23 +47,32 @@ public void stopServer() { } @Test - public void checkStatus() { - LOG.info("checkStatus"); - Response r = new ResteasyClientBuilder().build(). + public void checkStatusXmlJson() { + LOG.info("checkStatusXmlJson"); + Response request = new ResteasyClientBuilder().build(). target(server.url("/mapcode/status")). request(). get(); - Assert.assertNotNull(r); - int status = r.getStatus(); + Assert.assertNotNull(request); + int status = request.getStatus(); LOG.info("status = {}", status); Assert.assertEquals(200, status); - r = new ResteasyClientBuilder().build(). + request = new ResteasyClientBuilder().build(). target(server.url("/mapcode/xml/status")). request(). get(); - Assert.assertNotNull(r); - status = r.getStatus(); + Assert.assertNotNull(request); + status = request.getStatus(); + LOG.info("status = {}", status); + Assert.assertEquals(200, status); + + request = new ResteasyClientBuilder().build(). + target(server.url("/mapcode/json/status")). + request(). + get(); + Assert.assertNotNull(request); + status = request.getStatus(); LOG.info("status = {}", status); Assert.assertEquals(200, status); } @@ -100,16 +109,17 @@ public void checkVersionJson() { } @Test - public void checkVersionXml() { - LOG.info("checkVersionXml"); - final String expected = "1.0"; + public void checkVersionXmlJson() { + LOG.info("checkVersionXmlJson"); + final String expectedXml = "1.0"; + final String expectedJson = "{\"version\":\"1.0\"}"; Response response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/version")). request(). accept(MediaType.APPLICATION_XML_TYPE).get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(expected, response.readEntity(String.class)); + Assert.assertEquals(expectedXml, response.readEntity(String.class)); response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/xml/version")). @@ -117,7 +127,15 @@ public void checkVersionXml() { get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(expected, response.readEntity(String.class)); + Assert.assertEquals(expectedXml, response.readEntity(String.class)); + + response = new ResteasyClientBuilder().build(). + target(server.url("/mapcode/json/version")). + request(). + get(); + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(expectedJson, response.readEntity(String.class)); } @Test diff --git a/service/src/test/java/com/mapcode/services/ApiTerritoriesTest.java b/service/src/test/java/com/mapcode/services/ApiTerritoriesTest.java index 2b698bc..6bb24e6 100644 --- a/service/src/test/java/com/mapcode/services/ApiTerritoriesTest.java +++ b/service/src/test/java/com/mapcode/services/ApiTerritoriesTest.java @@ -64,10 +64,12 @@ public void checkTerritories1Json() { } @Test - public void checkTerritories1Xml() { - LOG.info("checkTerritories1Xml"); - final String expected1 = "533USAUSAUSAUSAUSUnited States of AmericaAmericaROMAN533USAUSAUSAUSAUSUnited States of AmericaAmericaROMAN 500); String sub1 = r.substring(0, 500); String sub2 = r.substring(r.length() - 500, r.length()); - Assert.assertEquals(expected1, - sub1); - Assert.assertEquals(expected2, - sub2); + Assert.assertEquals(expectedXml1, sub1); + Assert.assertEquals(expectedXml2, sub2); response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/xml/territories")). @@ -93,10 +93,21 @@ public void checkTerritories1Xml() { Assert.assertTrue(r.length() > 500); sub1 = r.substring(0, 500); sub2 = r.substring(r.length() - 500, r.length()); - Assert.assertEquals(expected1, - sub1); - Assert.assertEquals(expected2, - sub2); + Assert.assertEquals(expectedXml1, sub1); + Assert.assertEquals(expectedXml2, sub2); + + response = new ResteasyClientBuilder().build(). + target(server.url("/mapcode/json/territories")). + request(). + get(); + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + r = response.readEntity(String.class); + Assert.assertTrue(r.length() > 500); + sub1 = r.substring(0, 500); + sub2 = r.substring(r.length() - 500, r.length()); + Assert.assertEquals(expectedJson1, sub1); + Assert.assertEquals(expectedJson2, sub2); } @Test @@ -214,17 +225,17 @@ public void checkTerritoryJson3() { } @Test - public void checkTerritoryXml1() { - LOG.info("checkTerritoryXml1"); - final String expected = "NLDNLDNLDNetherlandsROMAN"; + public void checkTerritoryXmlJson1() { + LOG.info("checkTerritoryXmlJson1"); + final String expectedXml = "NLDNLDNLDNetherlandsROMAN"; + final String expectedJson = "{\"alphaCode\":\"NLD\",\"alphaCodeMinimalUnambiguous\":\"NLD\",\"alphaCodeMinimal\":\"NLD\",\"fullName\":\"Netherlands\",\"alphabets\":[{\"name\":\"ROMAN\"}]}"; Response response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/territories/nld")). request(). accept(MediaType.APPLICATION_XML_TYPE).get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(expected, - response.readEntity(String.class)); + Assert.assertEquals(expectedXml, response.readEntity(String.class)); response = new ResteasyClientBuilder().build(). target(server.url("/mapcode/xml/territories/nld")). @@ -232,8 +243,15 @@ public void checkTerritoryXml1() { get(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(expected, - response.readEntity(String.class)); + Assert.assertEquals(expectedXml, response.readEntity(String.class)); + + response = new ResteasyClientBuilder().build(). + target(server.url("/mapcode/json/territories/nld")). + request(). + get(); + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals(expectedJson, response.readEntity(String.class)); } @Test