From e4aff9111b59fc718edf095316013cec87132002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vil=C3=A9m=20Heinz?= <heinzvilem@gmail.com> Date: Fri, 26 Oct 2018 18:49:21 +0200 Subject: [PATCH 1/3] Storage tested (Vilem Heinz) --- .../cz/cvut/eshop/storage/StorageTest.java | 291 ++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 src/test/java/cz/cvut/eshop/storage/StorageTest.java diff --git a/src/test/java/cz/cvut/eshop/storage/StorageTest.java b/src/test/java/cz/cvut/eshop/storage/StorageTest.java new file mode 100644 index 0000000..ccfa781 --- /dev/null +++ b/src/test/java/cz/cvut/eshop/storage/StorageTest.java @@ -0,0 +1,291 @@ +package cz.cvut.eshop.storage; + +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.Order; +import cz.cvut.eshop.shop.ShoppingCart; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.*; + +import java.util.ArrayList; +import java.util.Collection; + +import static org.junit.Assert.*; + +/** + * @author Vilém Heinz + * + * Test file for Storage.java + * Testing all methods except + * constructors + * getStockEntries() - it only passes value + * printListOfStoredItems() - no good way to test + */ + +public class StorageTest { + private static Item testItem1; + private static Item testItem2; + private static Item testItem3; + + private static String category1; + private static String category2; + private static String category3; + + private Storage testStorage; + + @BeforeClass + public static void beforeAll() { + category1 = "electronics"; + category2 = "books"; + category3 = "clothes"; + + testItem1 = new StandardItem(1, "testItem1", 1.1f, + category1, 5); + testItem2 = new StandardItem(2, "testItem2", 2.2f, + category2, 10); + testItem3 = new StandardItem(3, "testItem3", -0.1f, + category3, -1); + } + + @AfterClass + public static void afterAll() { + } + + @Before + public void setUp(){ + testStorage = new Storage(); + } + + @After + public void tearDown(){ + } + + + @Test + public void insertItems() { + //act + testStorage.insertItems(testItem1, 1); + int actualCount = testStorage.getItemCount(1); + + //assert + assertEquals(1, actualCount); + } + + @Test + public void insertItems_insertItemZeroTimes() { + //act + testStorage.insertItems(testItem1, 0); + int actualCount = testStorage.getItemCount(1); + + //assert + assertEquals(0, actualCount); + } + + @Test + public void removeItems_emptyStorage() { + //act + boolean exceptionThrown = false; + try { + testStorage.removeItems(testItem1, 1); + } catch (NoItemInStorage noItemInStorage) { + exceptionThrown = true; + } + + //assert + assertTrue(exceptionThrown); + } + + @Test + public void removeItems_removeZeroTimes() { + //act + testStorage.insertItems(testItem1, 1); + int expectedCount = testStorage.getItemCount(testItem1); + try { + testStorage.removeItems(testItem1, 0); + } catch (NoItemInStorage noItemInStorage) { + fail("Exception while removing item was thrown but it should not have"); + } + + //assert + assertEquals(expectedCount, testStorage.getItemCount(testItem1)); + } + + @Test + public void removeItems_removeMoreTimesThanAvailable() { + //act + boolean exceptionThrown = false; + testStorage.insertItems(testItem1, 2); + try { + testStorage.removeItems(testItem1, 3); + } catch (NoItemInStorage noItemInStorage) { + exceptionThrown = true; + } + + + //assert + assertTrue(exceptionThrown); + } + + @Test + public void removeItems_successfullRemove() { + //act + testStorage.insertItems(testItem1, 1); + testStorage.insertItems(testItem2, 3); + testStorage.insertItems(testItem3, 0); + try { + testStorage.removeItems(testItem1, 1); + } catch (NoItemInStorage noItemInStorage) { + fail("Exception while removing item was thrown but it should not have"); + } + + //assert + assertEquals(0, testStorage.getItemCount(testItem1)); + } + + @Test + public void processOrder_empty() { + //setup + ArrayList<Item> items = new ArrayList<>(); + ShoppingCart cart = new ShoppingCart(items); + Order order = new Order(cart); + + //act + boolean processOkay = false; + try { + testStorage.processOrder(order); + processOkay = true; + } catch (NoItemInStorage noItemInStorage) { + fail("Exception while processing order was thrown but it should not have"); + } + + //assert + assertTrue(processOkay); + } + + @Test + public void processOrder_notEmpty() { + //setup + ArrayList<Item> items = new ArrayList<>(); + items.add(testItem1);items.add(testItem2);items.add(testItem3); + ShoppingCart cart = new ShoppingCart(items); + Order order = new Order(cart); + testStorage.insertItems(testItem1, 2); + testStorage.insertItems(testItem2, 2); + testStorage.insertItems(testItem3, 2); + ArrayList<Integer> expectedCount = new ArrayList<>(); + expectedCount.add(1);expectedCount.add(1);expectedCount.add(1); + + //act + try { + testStorage.processOrder(order); + } catch (NoItemInStorage noItemInStorage) { + fail("Exception while processing order was thrown but it should not have"); + } + ArrayList<Integer> actualCount = new ArrayList<>(); + actualCount.add(testStorage.getItemCount(testItem1)); + actualCount.add(testStorage.getItemCount(testItem2)); + actualCount.add(testStorage.getItemCount(testItem3)); + + //assert + assertEquals(expectedCount, actualCount); + } + + @Test + public void processOrder_orderItemNotInStorage() { + //setup + ArrayList<Item> items = new ArrayList<>(); + items.add(testItem1);items.add(testItem2);items.add(testItem3); + ShoppingCart cart = new ShoppingCart(items); + Order order = new Order(cart); + + //act + boolean exceptionThrown = false; + try { + testStorage.processOrder(order); + } catch (NoItemInStorage noItemInStorage) { + exceptionThrown = true; + } + + //assert + assertTrue(exceptionThrown); + } + + @Test + public void getItemCount_byItemExists() { + //act + testStorage.insertItems(testItem1, 1); + + //assert + assertEquals(1, testStorage.getItemCount(testItem1)); + } + + @Test + public void getItemCount_byItemNotExists() { + //act + testStorage.insertItems(testItem1, 1); + + //assert + assertEquals(0, testStorage.getItemCount(testItem2)); + } + + @Test + //Exists is covered already in insertItem + public void getItemCount_byIdNotExists() { + //act + testStorage.insertItems(testItem1, 1); + + //assert + assertEquals(0, testStorage.getItemCount(2)); + } + + @Test + public void getPriceOfWholeStock_empty() { + //assert + assertEquals(0, testStorage.getPriceOfWholeStock()); + } + + @Test + //Method getPriceOfWholeStock() returns sum of every item price of product (every item just once) + //Should have different name/javadoc + public void getPriceOfWholeStock_notEmpty() { + //act + testStorage.insertItems(testItem1, 1); + testStorage.insertItems(testItem2, 2); + testStorage.insertItems(testItem3, 3); + + //assert + assertEquals(5, testStorage.getPriceOfWholeStock()); + } + + @Test + public void getItemsOfCategorySortedByPrice_empty() { + //setup + Collection<Item> expectedCol = new ArrayList<>(); + + //act + ArrayList<Item> actualCol = (ArrayList<Item>) testStorage.getItemsOfCategorySortedByPrice(category1); + + //assert + assertEquals(expectedCol, actualCol); + } + + @Test + public void getItemsOfCategorySortedByPrice_notEmptyWithMultipleCategories() { + //setup + Item itemA = new StandardItem(4, "itemA", 1.f, category1, 2); + Item itemB = new StandardItem(5, "itemB", 2.f, category1, 2); + testStorage.insertItems(testItem2, 2); + testStorage.insertItems(itemA, 2); + testStorage.insertItems(testItem1, 2); + testStorage.insertItems(itemB, 2); + + //act + ArrayList<Item> actualCol = (ArrayList<Item>) testStorage.getItemsOfCategorySortedByPrice(category1); + Collection<Item> expectedCol = new ArrayList<>(); + expectedCol.add(itemA); + expectedCol.add(testItem1); + expectedCol.add(itemB); + + //assert + assertEquals(expectedCol, actualCol); + } +} \ No newline at end of file -- GitLab From a7dc93c39d606c0ddbb3a51a8ff0883539e189d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vil=C3=A9m=20Heinz?= <heinzvilem@gmail.com> Date: Fri, 26 Oct 2018 18:55:39 +0200 Subject: [PATCH 2/3] Test pipelinE --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1517fb7..e6ba8a7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -[![pipeline status](https://gitlab.fel.cvut.cz/frajtak/eShop/badges/master/pipeline.svg)](https://gitlab.fel.cvut.cz/frajtak/eShop/commits/master) +[![pipeline status](https://gitlab.fel.cvut.cz/heinzvil/eShop/badges/master/pipeline.svg)](https://gitlab.fel.cvut.cz/heinzvil/eShop/commits/master) -[![coverage report](https://gitlab.fel.cvut.cz/frajtak/eShop/badges/master/coverage.svg)](https://gitlab.fel.cvut.cz/frajtak/eShop/commits/master)frajtak \ No newline at end of file +[![coverage report](https://gitlab.fel.cvut.cz/heinzvil/eShop/badges/master/coverage.svg)](https://gitlab.fel.cvut.cz/heinzvil/eShop/commits/master)heinzvil \ No newline at end of file -- GitLab From a3bafa2151bfdcb4e9dd5f9e2b916d36eef8464d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vil=C3=A9m=20Heinz?= <heinzvilem@gmail.com> Date: Sun, 28 Oct 2018 20:06:55 +0100 Subject: [PATCH 3/3] Renamed function getPriceOfWholeStock because it does something else - based on that changed test --- .../java/cz/cvut/eshop/storage/Storage.java | 2 +- .../cz/cvut/eshop/storage/StorageTest.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/cz/cvut/eshop/storage/Storage.java b/src/main/java/cz/cvut/eshop/storage/Storage.java index b155b26..be4dade 100644 --- a/src/main/java/cz/cvut/eshop/storage/Storage.java +++ b/src/main/java/cz/cvut/eshop/storage/Storage.java @@ -114,7 +114,7 @@ public class Storage { * Gets total price of all items in the stock * @return price of whole stock */ - public int getPriceOfWholeStock() { + public int getPriceOfAllItems() { Collection<ItemStock> s = stock.values(); float totalPrice = 0; for (ItemStock e : s) { diff --git a/src/test/java/cz/cvut/eshop/storage/StorageTest.java b/src/test/java/cz/cvut/eshop/storage/StorageTest.java index ccfa781..fddd15b 100644 --- a/src/test/java/cz/cvut/eshop/storage/StorageTest.java +++ b/src/test/java/cz/cvut/eshop/storage/StorageTest.java @@ -228,7 +228,15 @@ public class StorageTest { } @Test - //Exists is covered already in insertItem + public void getItemCount_byIdExists() { + //act + testStorage.insertItems(testItem1, 1); + + //assert + assertEquals(1, testStorage.getItemCount(1)); + } + + @Test public void getItemCount_byIdNotExists() { //act testStorage.insertItems(testItem1, 1); @@ -238,22 +246,21 @@ public class StorageTest { } @Test - public void getPriceOfWholeStock_empty() { + public void getPriceOfAllItems_empty() { //assert - assertEquals(0, testStorage.getPriceOfWholeStock()); + assertEquals(0, testStorage.getPriceOfAllItems()); } @Test - //Method getPriceOfWholeStock() returns sum of every item price of product (every item just once) - //Should have different name/javadoc - public void getPriceOfWholeStock_notEmpty() { + //Renamed function name to what it truly does + public void getPriceOfAllItems_notEmpty() { //act testStorage.insertItems(testItem1, 1); testStorage.insertItems(testItem2, 2); testStorage.insertItems(testItem3, 3); //assert - assertEquals(5, testStorage.getPriceOfWholeStock()); + assertEquals(3, testStorage.getPriceOfAllItems()); } @Test -- GitLab