Skip to content

Commit

Permalink
Merge 1d1e32e into bd14cba
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon6193 committed Aug 22, 2019
2 parents bd14cba + 1d1e32e commit 5c50573
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ public Void postOrder(Order order, String applicationId, String sourceToken,
requestBody.put("customer_order_id", order.getCustomerOrderId());
requestBody.put("advertising_id", advertisingId);

JSONArray lineItemsJson = new JSONArray();

for (Order.LineItem lineItem : order.getLineItems()) {
JSONArray lineItemsJson = new JSONArray();
JSONObject lineItemJson = new JSONObject();

List<String> lineItemCategory = lineItem.getCategory();
Expand Down Expand Up @@ -185,9 +186,10 @@ public Void postOrder(Order order, String applicationId, String sourceToken,
lineItemJson.put("sku", lineItem.getSku());

lineItemsJson.put(lineItemJson);
requestBody.put("line_items", lineItemsJson);
}

requestBody.put("line_items", lineItemsJson);

Order.Customer customer = order.getCustomer();
if (customer != null) {
JSONObject customerJson = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -228,7 +229,7 @@ public void postOrder_validateOrder() throws Exception {
assertEquals(customerOrderId, requestBody.getString("customer_order_id"));
assertEquals(advertisingId, requestBody.getString("advertising_id"));

assertNull(requestBody.optJSONArray("line_items"));
assertEquals(0, requestBody.optJSONArray("line_items").length());
assertNull(requestBody.optJSONObject("customer"));
}

Expand Down Expand Up @@ -377,4 +378,48 @@ public void postOrder_invalidEmail_verifyPassthrough() throws Exception {
JSONObject customerJson = requestBody.getJSONObject("customer");
assertEquals(customerEmail, customerJson.getString("email_sha256"));
}

@Test
public void postOrder_multipleLineItems_validateMultipleLineItems() throws Exception {
List<Order.LineItem> lineItems = new ArrayList<>();

String lineItemOneId = "valid_line_item_one_id";
long lineItemOneTotal = 100;

String lineItemTwoId = "valid_line_item_two_id";
long lineItemTwoTotal = 200;

Order.LineItem lineItemOne = new Order.LineItem.Builder(lineItemOneId, lineItemOneTotal)
.build();

Order.LineItem lineItemTwo = new Order.LineItem.Builder(lineItemTwoId, lineItemTwoTotal)
.build();

lineItems.add(lineItemOne);
lineItems.add(lineItemTwo);

Order order = new Order.Builder("123", new Date(), lineItems)
.build();

buttonApi.postOrder(order, "valid_application_id",
"valid_source_token", "valid_advertising_id");

ArgumentCaptor<ApiRequest> argumentCaptor = ArgumentCaptor.forClass(ApiRequest.class);
verify(connectionManager).executeRequest(argumentCaptor.capture());
ApiRequest apiRequest = argumentCaptor.getValue();

JSONObject requestBody = apiRequest.getBody();
JSONArray lineItemsJsonArray = requestBody.getJSONArray("line_items");
assertEquals(2, lineItemsJsonArray.length());

JSONObject lineItemOneJson = lineItemsJsonArray.getJSONObject(0);

assertEquals(lineItemOneId, lineItemOneJson.getString("identifier"));
assertEquals(lineItemOneTotal, lineItemOneJson.getLong("total"));

JSONObject lineItemTwoJson = lineItemsJsonArray.getJSONObject(1);

assertEquals(lineItemTwoId, lineItemTwoJson.getString("identifier"));
assertEquals(lineItemTwoTotal, lineItemTwoJson.getLong("total"));
}
}

0 comments on commit 5c50573

Please sign in to comment.