@Test public void testUpdateStatusToAddedRemovesFromCart() { groceryCartService.addToShoppingList("Ham", 32); int id = groceryCartService.getShoppingList().get(0).getId(); groceryCartService.updateStatus(id, CartStatus.ADDED); assertEquals( "Cart should have exactly one item that was added", 1, groceryCartService.getCart().size()); groceryCartService.updateStatus(id, CartStatus.PURCHASED); assertEquals( "Cart should have no items since only item was purchased", 0, groceryCartService.getCart().size()); }
@Test public void testUpdateStatusNullStatus() { try { groceryCartService.updateStatus(123, null); fail("Service allowed an item to be set to null status or did not fail-fast"); } catch (IllegalArgumentException expected) { // This is expected we want to fail fast. } }
@Test public void testGetCartMultipleItems() { groceryCartService.addToShoppingList("Ham", 32); groceryCartService.addToShoppingList("Eggs", 21); for (GroceryItem gi : groceryCartService.getShoppingList()) { groceryCartService.updateStatus(gi.getId(), CartStatus.ADDED); } assertEquals("Cart should have exactly two items", 2, groceryCartService.getCart().size()); }
@Test public void testUpdateStatusToPurchased() { groceryCartService.addToShoppingList("Ham", 32); List<GroceryItem> items = groceryCartService.getShoppingList(); int id = items.get(0).getId(); groceryCartService.updateStatus(id, CartStatus.PURCHASED); assertEquals( "Update did not set status to expected result", CartStatus.PURCHASED, groceryCartService.getItemPurchaseHistory().get(0).getGroceryItem().getStatus()); }
@Test public void testUpdateStatusToNotFound() { groceryCartService.addToShoppingList("Ham", 32); List<GroceryItem> items = groceryCartService.getShoppingList(); int id = items.get(0).getId(); groceryCartService.updateStatus(id, CartStatus.NOT_FOUND); assertEquals( "Update did not set status to expected result", CartStatus.NOT_FOUND, groceryCartService.getShoppingList().get(0).getStatus()); }
@SuppressWarnings("null") @Test public void testUpdateStatusNullItemId() { try { Integer i = null; groceryCartService.updateStatus(i, CartStatus.ADDED); fail("Service allowed null item id to be updated or did not fail-fast"); } catch (NullPointerException expected) { // This is expected we want to fail fast. } }
@Test public void testGetItemPurchaseHistorySingle() { groceryCartService.addToShoppingList("Ham", 32); int id = groceryCartService.getShoppingList().get(0).getId(); groceryCartService.updateStatus(id, CartStatus.PURCHASED); List<PurchaseRecord> purchases = groceryCartService.getItemPurchaseHistory(); assertEquals("Purchase history should have exactly one item", 1, purchases.size()); assertEquals( "Item name in purchase history record should match the item purchased", "Ham", purchases.get(0).getGroceryItem().getName()); }
@Test public void testGetCartSingleItem() { groceryCartService.addToShoppingList("Ham", 32); groceryCartService.updateStatus( groceryCartService.getShoppingList().get(0).getId(), CartStatus.ADDED); List<GroceryItem> items = groceryCartService.getCart(); assertEquals("Cart should have exactly one item that was added", 1, items.size()); assertEquals("Item in cart does not have correct name", "Ham", items.get(0).getName()); assertEquals("Item in cart does not have correct isle", 32, items.get(0).getIsle()); assertEquals( "Item in cart does not have correct status", CartStatus.ADDED, items.get(0).getStatus()); }
@Test public void testGetItemPurchaseHistoryMultiple() { groceryCartService.addToShoppingList("Ham", 32); groceryCartService.addToShoppingList("Eggs", 32); groceryCartService.addToShoppingList("Milk", 23); groceryCartService.addToShoppingList("Bacon", 12); for (GroceryItem gi : groceryCartService.getShoppingList()) { groceryCartService.updateStatus(gi.getId(), CartStatus.PURCHASED); } List<PurchaseRecord> purchases = groceryCartService.getItemPurchaseHistory(); assertEquals("Purchase history should have exactly four items", 4, purchases.size()); }
@Test public void testUpdateStatusToAddedRemovesItemFromShoppingList() { groceryCartService.addToShoppingList("Ham", 32); assertEquals( "Shopping list should not be empty after adding an item", 1, groceryCartService.getShoppingList().size()); int id = groceryCartService.getShoppingList().get(0).getId(); groceryCartService.updateStatus(id, CartStatus.ADDED); assertEquals( "Shopping list should be empty after only item was added to cart", 0, groceryCartService.getShoppingList().size()); }
@Test public void testUpdateStatusNonExistingItem() { boolean result = groceryCartService.updateStatus(123, CartStatus.NOT_FOUND); assertFalse("Tried to update a bogus item and no business layer error occurred", result); }