diff --git a/src/main/java/cz/cvut/eshop/storage/ItemStock.java b/src/main/java/cz/cvut/eshop/storage/ItemStock.java index 8756f138b8a37a34517a9f4d04f8423e4e2c88ba..98dd5e34b4adcd4ca3c49f868e3e40aa7b3276c5 100644 --- a/src/main/java/cz/cvut/eshop/storage/ItemStock.java +++ b/src/main/java/cz/cvut/eshop/storage/ItemStock.java @@ -9,7 +9,8 @@ public class ItemStock { private Item refItem; private int count; - ItemStock(Item refItem) { + public ItemStock(Item refItem) { + if(refItem==null) throw new IllegalArgumentException("Null refItem is not allowed!"); this.refItem = refItem; count = 0; } @@ -18,20 +19,22 @@ public class ItemStock { public String toString() { return "STOCK OF ITEM: "+refItem.toString()+" PIECES IN STORAGE: "+count; } - - void IncreaseItemCount(int x) { + + public void IncreaseItemCount(int x) { + if(x<0) throw new IllegalArgumentException("Increasing by negative number is not allowed!"); count += x; } - - void decreaseItemCount(int x) { + + public void decreaseItemCount(int x) { + if(x<0) throw new IllegalArgumentException("Decreasing by negative number is not allowed!"); count -= x; } - int getCount() { + public int getCount() { return count; } - Item getItem() { + public Item getItem() { return refItem; } } \ No newline at end of file diff --git a/src/main/java/cz/cvut/eshop/storage/Storage.java b/src/main/java/cz/cvut/eshop/storage/Storage.java index b155b26f80abcb194275c05dcdbb0667ce8d7ac7..7ae813a7878a570b21cd484fa8a1a1c54b166e8e 100644 --- a/src/main/java/cz/cvut/eshop/storage/Storage.java +++ b/src/main/java/cz/cvut/eshop/storage/Storage.java @@ -11,6 +11,7 @@ public class Storage { private HashMap<Integer, ItemStock> stock; public Storage(HashMap<Integer, ItemStock> stock) { + if(stock==null) throw new IllegalArgumentException("Null stock is not allowed!"); this.stock = stock; } @@ -33,20 +34,19 @@ public class Storage { } } - /** * Insert N pieces of item to the storage * @param item item to insert * @param count N - count of inserted item */ public void insertItems(Item item, int count) { + if(item==null) throw new IllegalArgumentException("Null item is not allowed!"); if (!stock.containsKey(item.getID())) { stock.put(item.getID(), new ItemStock(item)); } ItemStock e = stock.get(item.getID()); e.IncreaseItemCount(count); } - /** @@ -56,6 +56,7 @@ public class Storage { * @throws NoItemInStorage */ public void removeItems(Item item, int count) throws NoItemInStorage { + if(item==null) throw new IllegalArgumentException("Null item is not allowed!"); if (stock.containsKey(item.getID())) { ItemStock e = stock.get(item.getID()); if (e.getCount() >= count) { diff --git a/src/test/java/storage/ItemStockTest.java b/src/test/java/storage/ItemStockTest.java new file mode 100644 index 0000000000000000000000000000000000000000..022601487892978c19e5f7c05e1a2f6761d79851 --- /dev/null +++ b/src/test/java/storage/ItemStockTest.java @@ -0,0 +1,98 @@ +package storage; + +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.StandardItem; +import cz.cvut.eshop.storage.ItemStock; +import org.junit.*; + + +import static junit.framework.TestCase.*; + +public class ItemStockTest { + + @BeforeClass + public static void beforeAll() { + System.out.println("Starting tests for ItemStockTest"); + } + + @AfterClass + public static void afterAll() { + System.out.println("Finished"); + } + + @Test + public void constructor_NotNullItem() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + assertNotNull(is.getItem()); + } + + @Test + public void constructor_NotNullItem_ZeroCount() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + assertEquals(0, is.getCount()); + } + + @Test(expected = IllegalArgumentException.class) + public void constructor_NullItem() { + Item i = null; + ItemStock is = new ItemStock(i); + } + + @Test + public void toStringTest() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + String result = is.toString(); + String expected = "STOCK OF ITEM: " + i.toString() + " PIECES IN STORAGE: " + 0; + assertEquals(expected, result); + } + + @Test + public void IncreaseItemCount_PositiveX() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + int before = is.getCount(); + int increment = 1; + is.IncreaseItemCount(increment); + int after = is.getCount(); + + assertEquals(before + increment, after); + } + + @Test(expected = IllegalArgumentException.class) + public void IncreaseItemCount_NegativeX() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + int increment = -1; + is.IncreaseItemCount(increment); + } + + @Test + public void DecreaseItemCount_PositiveX() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + int before = is.getCount(); + int decrement = 1; + is.decreaseItemCount(decrement); + int after = is.getCount(); + + assertEquals(before - decrement, after); + } + + @Test(expected = IllegalArgumentException.class) + public void DecreaseItemCount_NegativeX() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + int decrement = -1; + is.decreaseItemCount(decrement); + } +} diff --git a/src/test/java/storage/StorageTest.java b/src/test/java/storage/StorageTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9d1599b88beb3908973ce8c88cd6054c4f5cf861 --- /dev/null +++ b/src/test/java/storage/StorageTest.java @@ -0,0 +1,132 @@ +package storage; + +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.StandardItem; +import cz.cvut.eshop.storage.ItemStock; +import cz.cvut.eshop.storage.Storage; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.HashMap; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; + +public class StorageTest { + + @BeforeClass + public static void beforeAll() { + System.out.println("Starting tests for StorageTest"); + } + + @AfterClass + public static void afterAll() { + System.out.println("Finished"); + } + + + @Test(expected = IllegalArgumentException.class) + public void constructor_WithArgument_NullStock() { + Storage s = new Storage(null); + } + + @Test + public void constructor_Default() { + Storage s = new Storage(); + assertNotNull(s); + } + + @Test + public void constructor_WithArgument() { + HashMap<Integer, ItemStock> stock = new HashMap<>(); + Storage s = new Storage(stock); + + assertNotNull(s); + } + + @Test + public void printListOfStoredItems_EmptyStock() { + Storage s = new Storage(); + s.printListOfStoredItems(); + } + + @Test + public void printListOfStoredItems_FilledStock() { + Item i = new StandardItem(1, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + HashMap<Integer, ItemStock> stock = new HashMap<>(); + stock.put(1, is); + + Storage s = new Storage(stock); + s.printListOfStoredItems(); + } + + @Test(expected = IllegalArgumentException.class) + public void insertItems_NullItem() { + Item i = null; + Storage s = new Storage(); + + s.insertItems(i, 1); + } + + @Test(expected = IllegalArgumentException.class) + public void insertItems_NegativeCount() { + int id = 1; + + Item i = new StandardItem(id, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + HashMap<Integer, ItemStock> stock = new HashMap<>(); + stock.put(id, is); + + Storage s = new Storage(stock); + + int increment = -1; + s.insertItems(i, increment); + } + + @Test + public void insertItems_PositiveCount_NewItem() { + int id = 1; + + Item i = new StandardItem(id, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + HashMap<Integer, ItemStock> stock = new HashMap<>(); + stock.put(id, is); + + Storage s = new Storage(stock); + + int before = stock.get(id).getCount(); + int increment = 1; + s.insertItems(i, increment); + + int after = stock.get(id).getCount(); + + assertEquals(after, before+increment); + } + + @Test + public void insertItems_PositiveCount_ExistingItem() { + int id = 1; + + Item i = new StandardItem(id, "item1", 150, "category1", 150); + ItemStock is = new ItemStock(i); + + HashMap<Integer, ItemStock> stock = new HashMap<>(); + stock.put(id, is); + + Storage s = new Storage(stock); + int increment = 1; + s.insertItems(i, increment); + + int before = stock.get(id).getCount(); + increment = 1; + s.insertItems(i, increment); + int after = stock.get(id).getCount(); + + assertEquals(after, before+increment); + } +} \ No newline at end of file