diff --git a/src/test/java/com/siftscience/AddItemToCartEventTest.java b/src/test/java/com/siftscience/AddItemToCartEventTest.java index 002fb16..6cec72a 100644 --- a/src/test/java/com/siftscience/AddItemToCartEventTest.java +++ b/src/test/java/com/siftscience/AddItemToCartEventTest.java @@ -76,8 +76,8 @@ public void testAddItemToCart() throws Exception { // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/AddPromotionEventTest.java b/src/test/java/com/siftscience/AddPromotionEventTest.java index fd921c5..b1c3fdd 100644 --- a/src/test/java/com/siftscience/AddPromotionEventTest.java +++ b/src/test/java/com/siftscience/AddPromotionEventTest.java @@ -79,8 +79,8 @@ public void testAddPromotion() throws Exception { // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/ContentStatusEventTest.java b/src/test/java/com/siftscience/ContentStatusEventTest.java index f5f4eb4..431d5db 100644 --- a/src/test/java/com/siftscience/ContentStatusEventTest.java +++ b/src/test/java/com/siftscience/ContentStatusEventTest.java @@ -63,8 +63,8 @@ public void testContentStatus() throws Exception { // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/CreateOrderEventTest.java b/src/test/java/com/siftscience/CreateOrderEventTest.java index a067432..c83f3bf 100644 --- a/src/test/java/com/siftscience/CreateOrderEventTest.java +++ b/src/test/java/com/siftscience/CreateOrderEventTest.java @@ -1,11 +1,13 @@ package com.siftscience; +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; import static java.net.HttpURLConnection.HTTP_OK; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import com.siftscience.exception.InvalidFieldException; import com.siftscience.model.Booking; import com.siftscience.model.CreateOrderFieldSet; import com.siftscience.model.DigitalOrder; @@ -220,14 +222,346 @@ public void testCreateOrderEventWithBookings() throws JSONException, IOException // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); server.shutdown(); } + @Test + public void testCreateOrderEventWithBookingsShippingCarrierIsNull() + throws IOException, InterruptedException { + + // The expected JSON payload of the request. + String expectedRequestBody = "{\n" + + " \"$type\" : \"$create_order\",\n" + + " \"$api_key\" : \"YOUR_API_KEY\",\n" + + " \"$user_id\" : \"billy_jones_301\",\n" + + "\n" + + " \"$session_id\" : \"gigtleqddo84l8cm15qe4il\",\n" + + " \"$order_id\" : \"ORDER-28168441\",\n" + + " \"$user_email\" : \"bill@gmail.com\",\n" + + " \"$amount\" : 115940000,\n" + + " \"$currency_code\" : \"USD\",\n" + + " \"$billing_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$payment_methods\" : [\n" + + " {\n" + + " \"$payment_type\" : \"$credit_card\",\n" + + " \"$payment_gateway\" : \"$braintree\",\n" + + " \"$card_bin\" : \"542486\",\n" + + " \"$card_last4\" : \"4444\"\n" + + " }\n" + + " ],\n" + + " \"$shipping_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$expedited_shipping\" : true,\n" + + " \"$shipping_method\" : \"$physical\",\n" + + " \"$shipping_carrier\" : null,\n" + + " \"$shipping_tracking_numbers\" : [\"track-1\", \"track-2\"],\n" + + " \"$bookings\": [\n" + + " {\n" + + " \"$booking_type\": \"$flight\",\n" + + " \"$title\": \"SFO - LAS, 2 Adults\",\n" + + " \"$start_time\": 12038412903,\n" + + " \"$end_time\": 12048412903,\n" + + " \"$guests\": [\n" + + " {\n" + + " \"$name\": \"John Doe\",\n" + + " \"$birth_date\": \"1985-01-19\",\n" + + " \"$loyalty_program\": \"skymiles\",\n" + + " \"$loyalty_program_id\": \"PSOV34DF\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$email\": \"jdoe@domain.com\"\n" + + " },\n" + + " {\n" + + " \"$name\": \"Jane Doe\"\n" + + " }\n" + + " ],\n" + + " \"$segments\": [\n" + + " {\n" + + " \"$departure_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$arrival_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6041\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$start_time\": 2190121220,\n" + + " \"$end_time\": 2290122129,\n" + + " \"$vessel_number\": \"LH454\",\n" + + " \"$fare_class\": \"Premium Economy\",\n" + + " \"$departure_airport_code\": \"SFO\",\n" + + " \"$arrival_airport_code\": \"LAS\"\n" + + " }\n" + + " ],\n" + + " \"$price\": 49900000,\n" + + " \"$currency_code\": \"USD\",\n" + + " \"$quantity\": 1,\n" + + " \"$tags\": [\n" + + " \"team-123\",\n" + + " \"region-123\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + "\n" + + "}"; + + // Start a new mock server and enqueue a mock response. + MockWebServer server = new MockWebServer(); + MockResponse response = new MockResponse(); + response.setResponseCode(HTTP_BAD_REQUEST); + response.setBody("{\n" + + " \"status\" : 53,\n" + + " \"error_message\" : \"Invalid field value(s) for fields: $.$shipping_carrier, $.$shipping_tracking_numbers. Please check the documentation for valid field values.\",\n" + + " \"time\" : 1327604222,\n" + + " \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" + + "}"); + server.enqueue(response); + server.start(); + + // Create a new client and link it to the mock server. + SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID", + new OkHttpClient.Builder() + .addInterceptor(OkHttpUtils.urlRewritingInterceptor(server)) + .build()); + + // Build the request body. + // Payment methods. + List paymentMethodList = new ArrayList<>(); + paymentMethodList.add(TestUtils.samplePaymentMethod1()); + + // Bookings + List bookingList = new ArrayList<>(); + bookingList.add(TestUtils.sampleBooking()); + + // Build and execute the request against the mock server. + EventRequest request = client.buildRequest( + new CreateOrderFieldSet() + .setUserId("billy_jones_301") + .setSessionId("gigtleqddo84l8cm15qe4il") + .setOrderId("ORDER-28168441") + .setUserEmail("bill@gmail.com") + .setAmount(115940000L) + .setCurrencyCode("USD") + .setBillingAddress(TestUtils.sampleAddress2()) + .setPaymentMethods(paymentMethodList) + .setShippingAddress(TestUtils.sampleAddress2()) + .setExpeditedShipping(true) + .setShippingMethod("$physical") + .setShippingCarrier(null) + .setShippingTrackingNumbers(Arrays.asList("track-1", "track-2")) + .setBookings(bookingList)); + + Assert.assertThrows(InvalidFieldException.class, request::send); + + // Verify the request. + RecordedRequest request1 = server.takeRequest(); + Assert.assertEquals("POST", request1.getMethod()); + Assert.assertEquals("/v205/events", request1.getPath()); + + server.shutdown(); + } + + @Test + public void testCreateOrderEventWithBookingsShippingTrackingNumberIsNull() + throws IOException, InterruptedException { + + // The expected JSON payload of the request. + String expectedRequestBody = "{\n" + + " \"$type\" : \"$create_order\",\n" + + " \"$api_key\" : \"YOUR_API_KEY\",\n" + + " \"$user_id\" : \"billy_jones_301\",\n" + + "\n" + + " \"$session_id\" : \"gigtleqddo84l8cm15qe4il\",\n" + + " \"$order_id\" : \"ORDER-28168441\",\n" + + " \"$user_email\" : \"bill@gmail.com\",\n" + + " \"$amount\" : 115940000,\n" + + " \"$currency_code\" : \"USD\",\n" + + " \"$billing_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$payment_methods\" : [\n" + + " {\n" + + " \"$payment_type\" : \"$credit_card\",\n" + + " \"$payment_gateway\" : \"$braintree\",\n" + + " \"$card_bin\" : \"542486\",\n" + + " \"$card_last4\" : \"4444\"\n" + + " }\n" + + " ],\n" + + " \"$shipping_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$expedited_shipping\" : true,\n" + + " \"$shipping_method\" : \"$physical\",\n" + + " \"$shipping_carrier\" : \"The Best Carrier\",\n" + + " \"$shipping_tracking_numbers\" : [null],\n" + + " \"$bookings\": [\n" + + " {\n" + + " \"$booking_type\": \"$flight\",\n" + + " \"$title\": \"SFO - LAS, 2 Adults\",\n" + + " \"$start_time\": 12038412903,\n" + + " \"$end_time\": 12048412903,\n" + + " \"$guests\": [\n" + + " {\n" + + " \"$name\": \"John Doe\",\n" + + " \"$birth_date\": \"1985-01-19\",\n" + + " \"$loyalty_program\": \"skymiles\",\n" + + " \"$loyalty_program_id\": \"PSOV34DF\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$email\": \"jdoe@domain.com\"\n" + + " },\n" + + " {\n" + + " \"$name\": \"Jane Doe\"\n" + + " }\n" + + " ],\n" + + " \"$segments\": [\n" + + " {\n" + + " \"$departure_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$arrival_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6041\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$start_time\": 2190121220,\n" + + " \"$end_time\": 2290122129,\n" + + " \"$vessel_number\": \"LH454\",\n" + + " \"$fare_class\": \"Premium Economy\",\n" + + " \"$departure_airport_code\": \"SFO\",\n" + + " \"$arrival_airport_code\": \"LAS\"\n" + + " }\n" + + " ],\n" + + " \"$price\": 49900000,\n" + + " \"$currency_code\": \"USD\",\n" + + " \"$quantity\": 1,\n" + + " \"$tags\": [\n" + + " \"team-123\",\n" + + " \"region-123\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + "\n" + + "}"; + + // Start a new mock server and enqueue a mock response. + MockWebServer server = new MockWebServer(); + MockResponse response = new MockResponse(); + response.setResponseCode(HTTP_BAD_REQUEST); + response.setBody("{\n" + + " \"status\" : 53,\n" + + " \"error_message\" : \"Invalid field value(s) for fields: $.$shipping_tracking_numbers[0]. Please check the documentation for valid field values.\",\n" + + " \"time\" : 1327604222,\n" + + " \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" + + "}"); + server.enqueue(response); + server.start(); + + // Create a new client and link it to the mock server. + SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID", + new OkHttpClient.Builder() + .addInterceptor(OkHttpUtils.urlRewritingInterceptor(server)) + .build()); + + // Build the request body. + // Payment methods. + List paymentMethodList = new ArrayList<>(); + paymentMethodList.add(TestUtils.samplePaymentMethod1()); + + // Bookings + List bookingList = new ArrayList<>(); + bookingList.add(TestUtils.sampleBooking()); + + // Build and execute the request against the mock server. + EventRequest request = client.buildRequest( + new CreateOrderFieldSet() + .setUserId("billy_jones_301") + .setSessionId("gigtleqddo84l8cm15qe4il") + .setOrderId("ORDER-28168441") + .setUserEmail("bill@gmail.com") + .setAmount(115940000L) + .setCurrencyCode("USD") + .setBillingAddress(TestUtils.sampleAddress2()) + .setPaymentMethods(paymentMethodList) + .setShippingAddress(TestUtils.sampleAddress2()) + .setExpeditedShipping(true) + .setShippingMethod("$physical") + .setShippingCarrier("The Best Carrier") + .setShippingTrackingNumbers(Arrays.asList((String) null)) + .setBookings(bookingList)); + + Assert.assertThrows(InvalidFieldException.class, request::send); + + // Verify the request. + RecordedRequest request1 = server.takeRequest(); + Assert.assertEquals("POST", request1.getMethod()); + Assert.assertEquals("/v205/events", request1.getPath()); + + server.shutdown(); + } + @Test public void testCreateOrderEventWithItems() throws JSONException, IOException, InterruptedException { @@ -393,8 +727,8 @@ public void testCreateOrderEventWithItems() throws JSONException, IOException, // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); @@ -521,8 +855,8 @@ public void testCreateOrderEventwithSiteBrandOrderedFromFields() throws // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/FlagContentEventTest.java b/src/test/java/com/siftscience/FlagContentEventTest.java index 300db0c..b09ca73 100644 --- a/src/test/java/com/siftscience/FlagContentEventTest.java +++ b/src/test/java/com/siftscience/FlagContentEventTest.java @@ -66,8 +66,8 @@ public void testFlagContent() throws Exception { // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/RemoveItemFromCartEventTest.java b/src/test/java/com/siftscience/RemoveItemFromCartEventTest.java index c2fd457..5b294ea 100644 --- a/src/test/java/com/siftscience/RemoveItemFromCartEventTest.java +++ b/src/test/java/com/siftscience/RemoveItemFromCartEventTest.java @@ -75,8 +75,8 @@ public void testRemoveItemFromCart() throws Exception { // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/UpdateOrderEventTest.java b/src/test/java/com/siftscience/UpdateOrderEventTest.java index f8e34ae..fea32d2 100644 --- a/src/test/java/com/siftscience/UpdateOrderEventTest.java +++ b/src/test/java/com/siftscience/UpdateOrderEventTest.java @@ -1,11 +1,13 @@ package com.siftscience; +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; import static java.net.HttpURLConnection.HTTP_OK; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import com.siftscience.exception.InvalidFieldException; import com.siftscience.model.Booking; import com.siftscience.model.DigitalOrder; import com.siftscience.model.EventResponseBody; @@ -219,14 +221,346 @@ public void testUpdateOrderEventWithBookings() throws JSONException, IOException // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); server.shutdown(); } + @Test + public void testUpdateOrderEventWithBookingsShippingCarrierIsNull() throws IOException, + InterruptedException { + + // The expected JSON payload of the request. + String expectedRequestBody = "{\n" + + " \"$type\" : \"$update_order\",\n" + + " \"$api_key\" : \"YOUR_API_KEY\",\n" + + " \"$user_id\" : \"billy_jones_301\",\n" + + "\n" + + " \"$session_id\" : \"gigtleqddo84l8cm15qe4il\",\n" + + " \"$order_id\" : \"ORDER-28168441\",\n" + + " \"$user_email\" : \"bill@gmail.com\",\n" + + " \"$amount\" : 115940000,\n" + + " \"$currency_code\" : \"USD\",\n" + + " \"$billing_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$payment_methods\" : [\n" + + " {\n" + + " \"$payment_type\" : \"$credit_card\",\n" + + " \"$payment_gateway\" : \"$braintree\",\n" + + " \"$card_bin\" : \"542486\",\n" + + " \"$card_last4\" : \"4444\"\n" + + " }\n" + + " ],\n" + + " \"$shipping_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$expedited_shipping\" : true,\n" + + " \"$shipping_method\" : \"$physical\",\n" + + " \"$shipping_carrier\" : null,\n" + + " \"$shipping_tracking_numbers\" : [\"track-1\", \"track-2\"],\n" + + " \"$bookings\": [\n" + + " {\n" + + " \"$booking_type\": \"$flight\",\n" + + " \"$title\": \"SFO - LAS, 2 Adults\",\n" + + " \"$start_time\": 12038412903,\n" + + " \"$end_time\": 12048412903,\n" + + " \"$guests\": [\n" + + " {\n" + + " \"$name\": \"John Doe\",\n" + + " \"$birth_date\": \"1985-01-19\",\n" + + " \"$loyalty_program\": \"skymiles\",\n" + + " \"$loyalty_program_id\": \"PSOV34DF\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$email\": \"jdoe@domain.com\"\n" + + " },\n" + + " {\n" + + " \"$name\": \"Jane Doe\"\n" + + " }\n" + + " ],\n" + + " \"$segments\": [\n" + + " {\n" + + " \"$departure_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$arrival_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6041\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$start_time\": 2190121220,\n" + + " \"$end_time\": 2290122129,\n" + + " \"$vessel_number\": \"LH454\",\n" + + " \"$fare_class\": \"Premium Economy\",\n" + + " \"$departure_airport_code\": \"SFO\",\n" + + " \"$arrival_airport_code\": \"LAS\"\n" + + " }\n" + + " ],\n" + + " \"$price\": 49900000,\n" + + " \"$currency_code\": \"USD\",\n" + + " \"$quantity\": 1,\n" + + " \"$tags\": [\n" + + " \"team-123\",\n" + + " \"region-123\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + "\n" + + "}"; + + // Start a new mock server and enqueue a mock response. + MockWebServer server = new MockWebServer(); + MockResponse response = new MockResponse(); + response.setResponseCode(HTTP_BAD_REQUEST); + response.setBody("{\n" + + " \"status\" : 53,\n" + + " \"error_message\" : \"Invalid field value(s) for fields: $.$shipping_carrier, $.$shipping_tracking_numbers. Please check the documentation for valid field values.\",\n" + + " \"time\" : 1327604222,\n" + + " \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" + + "}"); + server.enqueue(response); + server.start(); + + // Create a new client and link it to the mock server. + SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID", + new OkHttpClient.Builder() + .addInterceptor(OkHttpUtils.urlRewritingInterceptor(server)) + .build()); + + // Build the request body. + // Payment methods. + List paymentMethodList = new ArrayList<>(); + paymentMethodList.add(TestUtils.samplePaymentMethod1()); + + // Bookings + List bookingList = new ArrayList<>(); + bookingList.add(TestUtils.sampleBooking()); + + // Build and execute the request against the mock server. + SiftRequest request = client.buildRequest( + new UpdateOrderFieldSet() + .setUserId("billy_jones_301") + .setSessionId("gigtleqddo84l8cm15qe4il") + .setOrderId("ORDER-28168441") + .setUserEmail("bill@gmail.com") + .setAmount(115940000L) + .setCurrencyCode("USD") + .setBillingAddress(TestUtils.sampleAddress2()) + .setPaymentMethods(paymentMethodList) + .setShippingAddress(TestUtils.sampleAddress2()) + .setExpeditedShipping(true) + .setShippingMethod("$physical") + .setShippingCarrier(null) + .setShippingTrackingNumbers(Arrays.asList("track-1", "track-2")) + .setBookings(bookingList)); + + Assert.assertThrows(InvalidFieldException.class, request::send); + + // Verify the request. + RecordedRequest request1 = server.takeRequest(); + Assert.assertEquals("POST", request1.getMethod()); + Assert.assertEquals("/v205/events", request1.getPath()); + + server.shutdown(); + } + + @Test + public void testUpdateOrderEventWithBookingsShippingTrackingNumberIsNull() throws IOException, + InterruptedException { + + // The expected JSON payload of the request. + String expectedRequestBody = "{\n" + + " \"$type\" : \"$update_order\",\n" + + " \"$api_key\" : \"YOUR_API_KEY\",\n" + + " \"$user_id\" : \"billy_jones_301\",\n" + + "\n" + + " \"$session_id\" : \"gigtleqddo84l8cm15qe4il\",\n" + + " \"$order_id\" : \"ORDER-28168441\",\n" + + " \"$user_email\" : \"bill@gmail.com\",\n" + + " \"$amount\" : 115940000,\n" + + " \"$currency_code\" : \"USD\",\n" + + " \"$billing_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$payment_methods\" : [\n" + + " {\n" + + " \"$payment_type\" : \"$credit_card\",\n" + + " \"$payment_gateway\" : \"$braintree\",\n" + + " \"$card_bin\" : \"542486\",\n" + + " \"$card_last4\" : \"4444\"\n" + + " }\n" + + " ],\n" + + " \"$shipping_address\" : {\n" + + " \"$name\" : \"Bill Jones\",\n" + + " \"$phone\" : \"1-415-555-6041\",\n" + + " \"$address_1\" : \"2100 Main Street\",\n" + + " \"$address_2\" : \"Apt 3B\",\n" + + " \"$city\" : \"New London\",\n" + + " \"$region\" : \"New Hampshire\",\n" + + " \"$country\" : \"US\",\n" + + " \"$zipcode\" : \"03257\"\n" + + " },\n" + + " \"$expedited_shipping\" : true,\n" + + " \"$shipping_method\" : \"$physical\",\n" + + " \"$shipping_carrier\" : \"The Best Carrier\",\n" + + " \"$shipping_tracking_numbers\" : [null],\n" + + " \"$bookings\": [\n" + + " {\n" + + " \"$booking_type\": \"$flight\",\n" + + " \"$title\": \"SFO - LAS, 2 Adults\",\n" + + " \"$start_time\": 12038412903,\n" + + " \"$end_time\": 12048412903,\n" + + " \"$guests\": [\n" + + " {\n" + + " \"$name\": \"John Doe\",\n" + + " \"$birth_date\": \"1985-01-19\",\n" + + " \"$loyalty_program\": \"skymiles\",\n" + + " \"$loyalty_program_id\": \"PSOV34DF\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$email\": \"jdoe@domain.com\"\n" + + " },\n" + + " {\n" + + " \"$name\": \"Jane Doe\"\n" + + " }\n" + + " ],\n" + + " \"$segments\": [\n" + + " {\n" + + " \"$departure_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6040\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$arrival_address\":\n" + + " {\n" + + " \"$name\": \"Bill Jones\",\n" + + " \"$phone\": \"1-415-555-6041\",\n" + + " \"$address_1\": \"2100 Main Street\",\n" + + " \"$address_2\": \"Apt 3B\",\n" + + " \"$city\": \"New London\",\n" + + " \"$region\": \"New Hampshire\",\n" + + " \"$country\": \"US\",\n" + + " \"$zipcode\": \"03257\"\n" + + " },\n" + + " \"$start_time\": 2190121220,\n" + + " \"$end_time\": 2290122129,\n" + + " \"$vessel_number\": \"LH454\",\n" + + " \"$fare_class\": \"Premium Economy\",\n" + + " \"$departure_airport_code\": \"SFO\",\n" + + " \"$arrival_airport_code\": \"LAS\"\n" + + " }\n" + + " ],\n" + + " \"$price\": 49900000,\n" + + " \"$currency_code\": \"USD\",\n" + + " \"$quantity\": 1,\n" + + " \"$tags\": [\n" + + " \"team-123\",\n" + + " \"region-123\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + "\n" + + "}"; + + // Start a new mock server and enqueue a mock response. + MockWebServer server = new MockWebServer(); + MockResponse response = new MockResponse(); + response.setResponseCode(HTTP_BAD_REQUEST); + response.setBody("{\n" + + " \"status\" : 53,\n" + + " \"error_message\" : \"Invalid field value(s) for fields: $.$shipping_tracking_numbers[0]. Please check the documentation for valid field values.\",\n" + + " \"time\" : 1327604222,\n" + + " \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" + + "}"); + server.enqueue(response); + server.start(); + + // Create a new client and link it to the mock server. + SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID", + new OkHttpClient.Builder() + .addInterceptor(OkHttpUtils.urlRewritingInterceptor(server)) + .build()); + + // Build the request body. + // Payment methods. + List paymentMethodList = new ArrayList<>(); + paymentMethodList.add(TestUtils.samplePaymentMethod1()); + + // Bookings + List bookingList = new ArrayList<>(); + bookingList.add(TestUtils.sampleBooking()); + + // Build and execute the request against the mock server. + SiftRequest request = client.buildRequest( + new UpdateOrderFieldSet() + .setUserId("billy_jones_301") + .setSessionId("gigtleqddo84l8cm15qe4il") + .setOrderId("ORDER-28168441") + .setUserEmail("bill@gmail.com") + .setAmount(115940000L) + .setCurrencyCode("USD") + .setBillingAddress(TestUtils.sampleAddress2()) + .setPaymentMethods(paymentMethodList) + .setShippingAddress(TestUtils.sampleAddress2()) + .setExpeditedShipping(true) + .setShippingMethod("$physical") + .setShippingCarrier("The Best Carrier") + .setShippingTrackingNumbers(Arrays.asList((String) null)) + .setBookings(bookingList)); + + Assert.assertThrows(InvalidFieldException.class, request::send); + + // Verify the request. + RecordedRequest request1 = server.takeRequest(); + Assert.assertEquals("POST", request1.getMethod()); + Assert.assertEquals("/v205/events", request1.getPath()); + + server.shutdown(); + } + @Test public void testUpdateOrderEventWithItems() throws JSONException, IOException, InterruptedException { @@ -390,8 +724,8 @@ public void testUpdateOrderEventWithItems() throws JSONException, IOException, I // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); @@ -516,8 +850,8 @@ public void testUpdateOrderEventWithSiteBrandOrderedFromFields() // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); diff --git a/src/test/java/com/siftscience/UpdatePasswordEventTest.java b/src/test/java/com/siftscience/UpdatePasswordEventTest.java index a7f18a0..92609e9 100644 --- a/src/test/java/com/siftscience/UpdatePasswordEventTest.java +++ b/src/test/java/com/siftscience/UpdatePasswordEventTest.java @@ -86,8 +86,8 @@ public void testUpdatePassword() throws Exception { // Verify the response. Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode()); + Assert.assertNotNull(siftResponse.getBody()); Assert.assertEquals(0, (int) siftResponse.getBody().getStatus()); - Assert.assertNotNull(response.getBody()); JSONAssert.assertEquals(response.getBody().readUtf8(), siftResponse.getBody().toJson(), true); server.shutdown();