From 2a80760e36bef25d93ae638fae9804b529f8dcf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=ADma?= <tomas.klim@gmail.com> Date: Sun, 28 Oct 2018 14:39:28 +0100 Subject: [PATCH] PurchasesArchiveTest and ItemPurchaseArchiveEntry tests --- .../cvut/eshop/archive/PurchasesArchive.java | 12 ++- .../archive/ItemPurchaseArchiveEntryTest.java | 35 +++++++++ .../eshop/archive/PurchasesArchiveTest.java | 78 +++++++++++++++++++ 3 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java create mode 100644 src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java diff --git a/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java b/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java index 8bb8d9f..c31c7e9 100644 --- a/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java +++ b/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java @@ -34,10 +34,7 @@ public class PurchasesArchive { } return 0; } - - - public void putOrderToPurchasesArchive(Order order) { orderArchive.add(order); ArrayList<Item> orderItems = order.getItems(); @@ -50,6 +47,13 @@ public class PurchasesArchive { } } } - + + public HashMap<Integer, ItemPurchaseArchiveEntry> getItemPurchaseArchive() { + return itemPurchaseArchive; + } + + public ArrayList<Order> getOrderArchive() { + return orderArchive; + } } diff --git a/src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java b/src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java new file mode 100644 index 0000000..f0bdeea --- /dev/null +++ b/src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java @@ -0,0 +1,35 @@ +package cz.cvut.eshop.archive; + +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ItemPurchaseArchiveEntryTest { + @Test + public void increaseCountHowManyTimesHasBeenSold_increaseSoldCounterBy2_shouldReturn3() { + Item item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ItemPurchaseArchiveEntry itemArchive = new ItemPurchaseArchiveEntry(item); + itemArchive.increaseCountHowManyTimesHasBeenSold(2); + + assertEquals(3, itemArchive.getCountHowManyTimesHasBeenSold()); + } + + @Test + public void getRefItem_gettingThisItemFromArchive_shouldReturnCorrectItem() { + Item item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ItemPurchaseArchiveEntry itemArchive = new ItemPurchaseArchiveEntry(item); + Item correctItem = itemArchive.getRefItem(); + + assertEquals(item, correctItem); + } + + @Test + public void toString_callThisItemToString_shouldReturnCorrectText() { + Item item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ItemPurchaseArchiveEntry itemArchive = new ItemPurchaseArchiveEntry(item); + + assertEquals("ITEM "+item.toString()+" HAS BEEN SOLD "+1+" TIMES", itemArchive.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java b/src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java new file mode 100644 index 0000000..5cbe645 --- /dev/null +++ b/src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java @@ -0,0 +1,78 @@ +package cz.cvut.eshop.archive; + +import cz.cvut.eshop.shop.Order; +import cz.cvut.eshop.shop.ShoppingCart; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.*; + +public class PurchasesArchiveTest { + @Test + public void putOrderToPurchasesArchive_addOrderToArchive_shouldAddsOrderToOrderArchiveAndToPurchaseOrderArchive() { + StandardItem item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ShoppingCart cart = new ShoppingCart(); + cart.addItem(item); + Order order = new Order(cart); + + PurchasesArchive archive = new PurchasesArchive(); + archive.putOrderToPurchasesArchive(order); + + List<Order> list = new ArrayList(); + list.add(order); + Map<Integer, ItemPurchaseArchiveEntry> map = new HashMap(); + map.put(item.getID(), new ItemPurchaseArchiveEntry(item)); + + assertEquals(list, archive.getOrderArchive()); + assertEquals(map.size(), archive.getItemPurchaseArchive().size()); + assertEquals(map.get(item.getID()).toString(), archive.getItemPurchaseArchive().get(item.getID()).toString()); + } + + @Test + public void putOrderToPurchasesArchive_addSameItemToOrderTwice_shouldAddItToSameHashmap() { + StandardItem item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ShoppingCart cart = new ShoppingCart(); + cart.addItem(item); + cart.addItem(item); + Order order = new Order(cart); + + PurchasesArchive archive = new PurchasesArchive(); + archive.putOrderToPurchasesArchive(order); + + ArrayList<Order> list = new ArrayList(); + list.add(order); + HashMap<Integer, ItemPurchaseArchiveEntry> map = new HashMap(); + ItemPurchaseArchiveEntry purchaseArchiveEntry = new ItemPurchaseArchiveEntry(item); + purchaseArchiveEntry.increaseCountHowManyTimesHasBeenSold(1); + map.put(item.getID(), purchaseArchiveEntry); + + assertEquals(list, archive.getOrderArchive()); + assertEquals(map.size(), archive.getItemPurchaseArchive().size()); + assertEquals(map.get(item.getID()).toString(), archive.getItemPurchaseArchive().get(item.getID()).toString()); + } + + @Test + public void getHowManyTimesHasBeenItemSold_addSameItemToTwoCarts_shouldReturnItemHasBeenSold2Times() { + StandardItem item1 = new StandardItem(1, "pen", 10, "writing accessories", 1); + StandardItem item2 = new StandardItem(2, "pencil", 5, "writing accessories", 0); + + ShoppingCart cart1 = new ShoppingCart(); + ShoppingCart cart2 = new ShoppingCart(); + cart1.addItem(item1); + cart2.addItem(item1); + cart2.addItem(item2); + Order order1 = new Order(cart1); + Order order2 = new Order(cart2); + + PurchasesArchive archive = new PurchasesArchive(); + archive.putOrderToPurchasesArchive(order1); + archive.putOrderToPurchasesArchive(order2); + + assertEquals(2, archive.getHowManyTimesHasBeenItemSold(item1)); + } +} -- GitLab