Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow orders to report multiple line items #35

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"));
}
}