diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..596b573 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +## UPS API Tools ## + +**Note:** Register for API keu here: https://www.ups.com/upsdeveloperkit + +## Instantiate ## + +```java +UpsApi upsApi = new UpsApi(environment, username, password, accessKey); +``` + +Environment (PRODUCTION, CIE) + +**Note:** CIE (Customer Integration Environment) is UPS's sandbox + +```java +UpsEnvironment environment = UpsEnvironment.CIE; +``` + +## Track Package ## + +```java +UpsResponse upsResponse = upsApi.track("7798339175"); +``` + diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..a23edb4 --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f28341e --- /dev/null +++ b/pom.xml @@ -0,0 +1,60 @@ + + 4.0.0 + + com.ljunggren + ups-api + 0.0.1-SNAPSHOT + jar + + ups-api + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.httpcomponents + httpclient + 4.5.8 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.9 + + + com.ljunggren + json-utils + 0.0.1-SNAPSHOT + + + commons-io + commons-io + 2.8.0 + + + org.projectlombok + lombok + 1.18.12 + provided + + + junit + junit + 4.13 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + diff --git a/src/main/java/com/ljunggren/ups/api/UpsApi.java b/src/main/java/com/ljunggren/ups/api/UpsApi.java new file mode 100644 index 0000000..808f65f --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/UpsApi.java @@ -0,0 +1,98 @@ +package com.ljunggren.ups.api; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.List; + +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; + +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicHeader; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.ljunggren.jsonUtils.JsonUtils; +import com.ljunggren.ups.api.tracking.response.MessageCode; +import com.ljunggren.ups.api.tracking.response.Response; + +public class UpsApi { + + private String username; + private String password; + private String accessKey; + private UpsProperties properties; + private CloseableHttpClient httpClient = HttpClients.custom().build(); + + public static final List KNOWN_REQUEST_CODES = Arrays.asList(new Integer[] { + 200, 400, 401, 404, 405, 500, 503 + }); + + // package private for unit testing + UpsApi(UpsEnvironment environment, String username, String password, String accessKey, CloseableHttpClient httpClient) { + this(environment, username, password, accessKey); + this.httpClient = httpClient; + } + + public UpsApi(UpsEnvironment environment, String username, String password, String accessKey) { + this.username = username; + this.password = password; + this.accessKey = accessKey; + this.properties = new UpsProperties(environment); + } + + public UpsResponse track(String trackingNumber) { + String url = properties.getTrackingUrl().replace("{inquiryNumber}", trackingNumber); + HttpGet get = new HttpGet(url); + Header[] headers = new Header[] { + new BasicHeader("username", username), + new BasicHeader("password", password), + new BasicHeader("accessLicenseNumber", accessKey), + new BasicHeader("content-type", "application/json"), + new BasicHeader("accept", "application/json") + }; + try { + get.setHeaders(headers); + CloseableHttpResponse httpResponse = httpClient.execute(get); + int responseCode = httpResponse.getStatusLine().getStatusCode(); + String json = getResult(httpResponse); + if (KNOWN_REQUEST_CODES.contains(responseCode)) { + UpsResponse response = parse(json, UpsResponse.class); + return response; + } + return generateErrorReponse("Tracking Error", String.format("Unknown response code %d", responseCode)); + } catch(Exception e) { + return generateErrorReponse("Tracking Error", e.getMessage()); + } + } + + private UpsResponse generateErrorReponse(String code, String message) { + UpsResponse upsResponse = new UpsResponse(); + Response response = new Response().addError(new MessageCode(code, message)); + upsResponse.setResponse(response); + return upsResponse; + } + + private T parse(String json, Class clazz) throws Exception { + try { + return JsonUtils.jsonToObject(json, clazz); + } catch (JsonProcessingException e) { + throw new Exception("Unable to parse response"); + } + } + + private static String getResult(HttpResponse response) throws UnsupportedOperationException, IOException { + BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); + StringBuilder result = new StringBuilder(); + String line = new String(); + while ((line = rd.readLine()) != null) { + result.append(line); + } + return result.toString(); + } + +} diff --git a/src/main/java/com/ljunggren/ups/api/UpsEnvironment.java b/src/main/java/com/ljunggren/ups/api/UpsEnvironment.java new file mode 100644 index 0000000..0cb2729 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/UpsEnvironment.java @@ -0,0 +1,8 @@ +package com.ljunggren.ups.api; + +public enum UpsEnvironment { + + CIE, // (customer integration environment) + PRODUCTION + +} diff --git a/src/main/java/com/ljunggren/ups/api/UpsProperties.java b/src/main/java/com/ljunggren/ups/api/UpsProperties.java new file mode 100644 index 0000000..e870747 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/UpsProperties.java @@ -0,0 +1,41 @@ +package com.ljunggren.ups.api; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class UpsProperties { + + private static Properties properties = new Properties(); + + private static final String API_TRACKING_URL = "api.tracking.url"; + + public UpsProperties(UpsEnvironment environment) { + Map fileMap = generateFileMap(); + setProperties(fileMap.get(environment)); + } + + private static Map generateFileMap() { + Map fileMap = new HashMap(); + fileMap.put(UpsEnvironment.CIE, "/properties/cie/ups.properties"); + fileMap.put(UpsEnvironment.PRODUCTION, "/properties/production/ups.properties"); + return fileMap; + } + + private static void setProperties(String file) { + try { + InputStream is = UpsProperties.class.getResourceAsStream(file); + properties.load(is); + is.close(); + } catch(IOException e) { + throw new RuntimeException("Error loading properties", e); + } + } + + public String getTrackingUrl() { + return properties.getProperty(API_TRACKING_URL); + } + +} diff --git a/src/main/java/com/ljunggren/ups/api/UpsResponse.java b/src/main/java/com/ljunggren/ups/api/UpsResponse.java new file mode 100644 index 0000000..2629faf --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/UpsResponse.java @@ -0,0 +1,25 @@ +package com.ljunggren.ups.api; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.ljunggren.ups.api.tracking.response.Response; +import com.ljunggren.ups.api.tracking.response.TrackingResponse; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class UpsResponse { + + @JsonProperty("trackResponse") + private TrackingResponse trackingResponse; + private Response response; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Activity.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Activity.java new file mode 100644 index 0000000..8eb7896 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Activity.java @@ -0,0 +1,20 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class Activity { + + private Location location; + private Status status; + private String date; + private String time; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Address.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Address.java new file mode 100644 index 0000000..a65335c --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Address.java @@ -0,0 +1,25 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Address { + + private String city; + private String stateProvince; + private String postalCode; + @JsonProperty("country") + private String countryCode; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/DeliveryDate.java b/src/main/java/com/ljunggren/ups/api/tracking/response/DeliveryDate.java new file mode 100644 index 0000000..af69e21 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/DeliveryDate.java @@ -0,0 +1,18 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class DeliveryDate { + + private String type; + private String date; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/DeliveryTime.java b/src/main/java/com/ljunggren/ups/api/tracking/response/DeliveryTime.java new file mode 100644 index 0000000..6d94b5b --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/DeliveryTime.java @@ -0,0 +1,19 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class DeliveryTime { + + private String startTime; + private String endTime; + private String type; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Location.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Location.java new file mode 100644 index 0000000..22c328f --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Location.java @@ -0,0 +1,17 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class Location { + + private Address address; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/MessageCode.java b/src/main/java/com/ljunggren/ups/api/tracking/response/MessageCode.java new file mode 100644 index 0000000..0217289 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/MessageCode.java @@ -0,0 +1,18 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class MessageCode { + + private String code; + private String message; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Package.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Package.java new file mode 100644 index 0000000..02b1aa5 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Package.java @@ -0,0 +1,25 @@ +package com.ljunggren.ups.api.tracking.response; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class Package { + + private String trackingNumber; + @JsonProperty("deliveryDate") + private List deliveryDates; + private DeliveryTime deliveryTime; + @JsonProperty("activity") + private List activities; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Response.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Response.java new file mode 100644 index 0000000..c7f5d4d --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Response.java @@ -0,0 +1,31 @@ +package com.ljunggren.ups.api.tracking.response; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Response { + + private List errors; + + public Response addError(MessageCode error) { + if (errors == null) { + errors = new ArrayList<>(); + } + errors.add(error); + return this; + } + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Shipment.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Shipment.java new file mode 100644 index 0000000..d25ed8f --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Shipment.java @@ -0,0 +1,25 @@ +package com.ljunggren.ups.api.tracking.response; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Shipment { + + @JsonProperty("package") + private List packages; + private List warnings; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/Status.java b/src/main/java/com/ljunggren/ups/api/tracking/response/Status.java new file mode 100644 index 0000000..f179ce1 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/Status.java @@ -0,0 +1,22 @@ +package com.ljunggren.ups.api.tracking.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Status { + + private String type; + private String description; + private String code; + +} diff --git a/src/main/java/com/ljunggren/ups/api/tracking/response/TrackingResponse.java b/src/main/java/com/ljunggren/ups/api/tracking/response/TrackingResponse.java new file mode 100644 index 0000000..b5965a3 --- /dev/null +++ b/src/main/java/com/ljunggren/ups/api/tracking/response/TrackingResponse.java @@ -0,0 +1,24 @@ +package com.ljunggren.ups.api.tracking.response; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class TrackingResponse { + + @JsonProperty("shipment") + private List shipments; + +} diff --git a/src/main/resources/properties/cie/ups.properties b/src/main/resources/properties/cie/ups.properties new file mode 100644 index 0000000..2f9a7a2 --- /dev/null +++ b/src/main/resources/properties/cie/ups.properties @@ -0,0 +1 @@ +api.tracking.url=https://wwwcie.ups.com/track/v1/details/{inquiryNumber} diff --git a/src/main/resources/properties/production/ups.properties b/src/main/resources/properties/production/ups.properties new file mode 100644 index 0000000..5b9d460 --- /dev/null +++ b/src/main/resources/properties/production/ups.properties @@ -0,0 +1 @@ +api.tracking.url=https://onlinetools.ups.com/track/v1/details/{inquiryNumber} diff --git a/src/test/java/com/ljunggren/ups/api/UpsApiTest.java b/src/test/java/com/ljunggren/ups/api/UpsApiTest.java new file mode 100644 index 0000000..fbaf3c2 --- /dev/null +++ b/src/test/java/com/ljunggren/ups/api/UpsApiTest.java @@ -0,0 +1,86 @@ +package com.ljunggren.ups.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class UpsApiTest { + + @Mock + private CloseableHttpClient httpClient; + + @Mock + private CloseableHttpResponse httpResponse; + + @Mock + private HttpEntity httpEntity; + + @Mock + private StatusLine statusLine; + + private String readFromResources(String file) throws IOException { + return IOUtils.toString(this.getClass().getResourceAsStream(file), "UTF-8"); + } + + private void setup(String json, int responseCode) throws ClientProtocolException, IOException { + InputStream content = new ByteArrayInputStream(json.getBytes()); + when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(statusLine.getStatusCode()).thenReturn(responseCode); + when(httpResponse.getEntity()).thenReturn(httpEntity); + when(httpEntity.getContent()).thenReturn(content); + } + + @Test + public void trackTest() throws IOException { + String json = readFromResources("/trackingResponse.json"); + setup(json, 200); + UpsEnvironment environment = UpsEnvironment.CIE; + UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey", httpClient); + UpsResponse upsResponse = upsApi.track("12345"); + assertNotNull(upsResponse.getTrackingResponse()); + assertNull(upsResponse.getResponse()); + } + + @Test + public void trackUnknownResponseCodeTest() throws ClientProtocolException, IOException { + String json = "notRealResponse"; + setup(json, 407); + UpsEnvironment environment = UpsEnvironment.CIE; + UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey", httpClient); + UpsResponse upsResponse = upsApi.track("12345"); + assertNull(upsResponse.getTrackingResponse()); + assertEquals("Unknown response code 407", upsResponse.getResponse().getErrors().get(0).getMessage()); + } + + @Test + public void trackExceptionThrownTest() throws ClientProtocolException, IOException { + String json = "notRealResponse"; + setup(json, 200); + UpsEnvironment environment = UpsEnvironment.CIE; + UpsApi upsApi = new UpsApi(environment, "username", "password", "accessKey", httpClient); + UpsResponse upsResponse = upsApi.track("12345"); + assertNull(upsResponse.getTrackingResponse()); + assertEquals("Unable to parse response", upsResponse.getResponse().getErrors().get(0).getMessage()); + } + +} \ No newline at end of file diff --git a/src/test/java/com/ljunggren/ups/api/UpsPropertiesTest.java b/src/test/java/com/ljunggren/ups/api/UpsPropertiesTest.java new file mode 100644 index 0000000..51679fe --- /dev/null +++ b/src/test/java/com/ljunggren/ups/api/UpsPropertiesTest.java @@ -0,0 +1,23 @@ +package com.ljunggren.ups.api; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class UpsPropertiesTest { + + @Test + public void getTrackingUrlCieTest() { + UpsProperties properties = new UpsProperties(UpsEnvironment.CIE); + String url = properties.getTrackingUrl(); + assertNotNull(url); + } + + @Test + public void getTrackingUrlProductionTest() { + UpsProperties properties = new UpsProperties(UpsEnvironment.PRODUCTION); + String url = properties.getTrackingUrl(); + assertNotNull(url); + } + +} diff --git a/src/test/java/com/ljunggren/ups/api/UpsResponseTest.java b/src/test/java/com/ljunggren/ups/api/UpsResponseTest.java new file mode 100644 index 0000000..6531348 --- /dev/null +++ b/src/test/java/com/ljunggren/ups/api/UpsResponseTest.java @@ -0,0 +1,42 @@ +package com.ljunggren.ups.api; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +import com.ljunggren.jsonUtils.JsonUtils; + +public class UpsResponseTest { + + private String readFromResources(String file) throws IOException { + return IOUtils.toString(this.getClass().getResourceAsStream(file), "UTF-8"); + } + + @Test + public void serializeTrackingResponseTest() throws IOException { + String json = readFromResources("/trackingResponse.json"); + UpsResponse response = JsonUtils.jsonToObject(json, UpsResponse.class); + String serializedResponse = JsonUtils.objectToJson(response); + assertTrue(JsonUtils.areEqual(json, serializedResponse)); + } + + @Test + public void serializeTrackingNotFoundResponseTest() throws IOException { + String json = readFromResources("/trackingNotFoundResponse.json"); + UpsResponse response = JsonUtils.jsonToObject(json, UpsResponse.class); + String serializedResponse = JsonUtils.objectToJson(response); + assertTrue(JsonUtils.areEqual(json, serializedResponse)); + } + + @Test + public void serializeInvalidAuthenticationResponseTest() throws IOException { + String json = readFromResources("/invalidAuthentication.json"); + UpsResponse response = JsonUtils.jsonToObject(json, UpsResponse.class); + String serializedResponse = JsonUtils.objectToJson(response); + assertTrue(JsonUtils.areEqual(json, serializedResponse)); + } + +} diff --git a/src/test/resources/invalidAuthentication.json b/src/test/resources/invalidAuthentication.json new file mode 100644 index 0000000..06be424 --- /dev/null +++ b/src/test/resources/invalidAuthentication.json @@ -0,0 +1,10 @@ +{ + "response": { + "errors": [ + { + "code": "250002", + "message": "Invalid Authentication Information." + } + ] + } +} diff --git a/src/test/resources/trackingNotFoundResponse.json b/src/test/resources/trackingNotFoundResponse.json new file mode 100644 index 0000000..1cc2ec5 --- /dev/null +++ b/src/test/resources/trackingNotFoundResponse.json @@ -0,0 +1,14 @@ +{ + "trackResponse": { + "shipment": [ + { + "warnings": [ + { + "code": "TW0001", + "message": "Tracking Information Not Found" + } + ] + } + ] + } +} diff --git a/src/test/resources/trackingResponse.json b/src/test/resources/trackingResponse.json new file mode 100644 index 0000000..9dd56ab --- /dev/null +++ b/src/test/resources/trackingResponse.json @@ -0,0 +1,232 @@ +{ + "trackResponse" : { + "shipment" : [{ + "package" : [{ + "trackingNumber" : "7798339175", + "deliveryDate" : [{ + "type" : "DEL", + "date" : "20191003" + } + ], + "deliveryTime" : { + "startTime" : "", + "endTime" : "112200", + "type" : "DEL" + }, + "activity" : [{ + "location" : { + "address" : { + "city" : "Goteborg", + "stateProvince" : "O", + "country" : "SE" + } + }, + "status" : { + "description" : "Delivered", + "code" : "012" + }, + "date" : "20191003", + "time" : "112200" + }, { + "location" : { + "address" : { + "city" : "Goteborg", + "stateProvince" : "O", + "country" : "SE" + } + }, + "status" : { + "type" : "", + "description" : "Arrived at Destination Country", + "code" : "031" + }, + "date" : "20191003", + "time" : "112128" + }, { + "location" : { + "address" : { + "city" : "Goteborg", + "stateProvince" : "O", + "country" : "SE" + } + }, + "status" : { + "type" : "", + "description" : "Documents to client's broker or consignee", + "code" : "041" + }, + "date" : "20191003", + "time" : "111800" + }, { + "location" : { + "address" : { + "city" : "Goteborg", + "stateProvince" : "O", + "country" : "SE" + } + }, + "status" : { + "type" : "", + "description" : "ETA at Port of Discharge", + "code" : "006" + }, + "date" : "20191003", + "time" : "063400" + }, { + "location" : { + "address" : { + "city" : "Goteborg", + "stateProvince" : "O", + "country" : "SE" + } + }, + "status" : { + "type" : "", + "description" : "Confirmed Arrival at Final Carrier B/L Point", + "code" : "040" + }, + "date" : "20191003", + "time" : "063400" + }, { + "location" : { + "address" : { + "city" : "Goteborg", + "stateProvince" : "O", + "country" : "SE" + } + }, + "status" : { + "type" : "", + "description" : "Confirmed Arrival", + "code" : "007" + }, + "date" : "20191003", + "time" : "063400" + }, { + "location" : { + "address" : { + "city" : "Rotterdam", + "stateProvince" : "", + "country" : "NL" + } + }, + "status" : { + "type" : "", + "description" : "Departure", + "code" : "005" + }, + "date" : "20190929", + "time" : "190000" + }, { + "location" : { + "address" : { + "city" : "New York", + "stateProvince" : "NY", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "ETD from Load Port", + "code" : "004" + }, + "date" : "20190914", + "time" : "120000" + }, { + "location" : { + "address" : { + "city" : "Newark", + "stateProvince" : "NJ", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "Documents received from shipper", + "code" : "045" + }, + "date" : "20190909", + "time" : "111915" + }, { + "location" : { + "address" : { + "city" : "Newark", + "stateProvince" : "NJ", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "Date Available to Ship", + "code" : "003" + }, + "date" : "20190909", + "time" : "111915" + }, { + "location" : { + "address" : { + "city" : "Newark", + "stateProvince" : "NJ", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "Held at customs", + "code" : "A3" + }, + "date" : "20190909", + "time" : "110101" + }, { + "location" : { + "address" : { + "city" : "Newark", + "stateProvince" : "NJ", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "Planned pickup date", + "code" : "072" + }, + "date" : "20190906", + "time" : "000100" + }, { + "location" : { + "address" : { + "city" : "Newark", + "stateProvince" : "NJ", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "Received into UPS possession", + "code" : "002" + }, + "date" : "20190906", + "time" : "000100" + }, { + "location" : { + "address" : { + "city" : "Newark", + "stateProvince" : "NJ", + "country" : "US" + } + }, + "status" : { + "type" : "", + "description" : "Booking Received from Client", + "code" : "037" + }, + "date" : "20190829", + "time" : "202645" + } + ] + } + ] + } + ] + } +} \ No newline at end of file