From e268d4c7a2defc23cacb1ce265e7fca71fe2a689 Mon Sep 17 00:00:00 2001 From: ksonny4 <ksonny4@gmail.com> Date: Sat, 27 Oct 2018 20:02:44 +0200 Subject: [PATCH 1/5] First test of order added --- src/test/java/archive/OrderTest.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/test/java/archive/OrderTest.java diff --git a/src/test/java/archive/OrderTest.java b/src/test/java/archive/OrderTest.java new file mode 100644 index 0000000..13c0261 --- /dev/null +++ b/src/test/java/archive/OrderTest.java @@ -0,0 +1,25 @@ +package archive; + +import cz.cvut.eshop.shop.Order; +import cz.cvut.eshop.shop.ShoppingCart; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +public class OrderTest { + + + @Test + public void createOrderWithNoItemsOrderedTest() { + // setup + ShoppingCart cart = new ShoppingCart(); + Order order = new Order(cart); + // act + order.create(); + int numberOfItems = order.getItems().size(); + // assert + assertEquals(numberOfItems,0); + } + + +} -- GitLab From 2c2fb34bd5a94cfe43d6d1b69aae57a97e0be2e0 Mon Sep 17 00:00:00 2001 From: Petr Bergmann <bergmpet@fel.cvut.cz> Date: Sat, 27 Oct 2018 21:46:06 +0200 Subject: [PATCH 2/5] Update of README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1517fb7..38107a0 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/bergmpet/eShop/badges/master/pipeline.svg)](https://gitlab.fel.cvut.cz/bergmpet/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/bergmpet/eShop/badges/master/coverage.svg)](https://gitlab.fel.cvut.cz/bergmpet/eShop/commits/master) -- GitLab From fc0625029c9b0eb6c91eaba5530efa30dbfc2804 Mon Sep 17 00:00:00 2001 From: Petr Bergmann <bergmpet@fel.cvut.cz> Date: Sat, 27 Oct 2018 21:47:18 +0200 Subject: [PATCH 3/5] Tests of DiscountedItem and ShoppingCart --- src/test/java/shop/DiscountedItemTest.java | 146 +++++++++++++++++++++ src/test/java/shop/ShoppingCartTest.java | 112 ++++++++++++++++ 2 files changed, 258 insertions(+) create mode 100644 src/test/java/shop/DiscountedItemTest.java create mode 100644 src/test/java/shop/ShoppingCartTest.java diff --git a/src/test/java/shop/DiscountedItemTest.java b/src/test/java/shop/DiscountedItemTest.java new file mode 100644 index 0000000..d24361f --- /dev/null +++ b/src/test/java/shop/DiscountedItemTest.java @@ -0,0 +1,146 @@ +package shop; + +import cz.cvut.eshop.shop.DiscountedItem; +import org.junit.Test; +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class DiscountedItemTest { + @Test + public void setDiscountFrom_correctDateFormat() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + String newDateString = "15.04.2005"; + + discountedItem.setDiscountFrom(newDateString); + Date date = discountedItem.getDiscountFrom(); + + assertEquals(15, date.getDate()); + assertEquals(3, date.getMonth()); + assertEquals(105, date.getYear()); + } + + @Test + public void setDiscountFrom_wrongDateFormat() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + String newDateString = "abc"; + + discountedItem.setDiscountFrom(newDateString); + Date date = discountedItem.getDiscountFrom(); + + assertEquals(10, date.getDate()); + assertEquals(4, date.getMonth()); + assertEquals(117, date.getYear()); + } + + @Test + public void setDiscountTo_correctDateFormatString() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + String newDateString = "15.04.2020"; + + discountedItem.setDiscountTo(newDateString); + Date date = discountedItem.getDiscountTo(); + + assertEquals(15, date.getDate()); + assertEquals(3, date.getMonth()); + assertEquals(120, date.getYear()); + } + + @Test + public void setDiscountTo_wrongDateFormatString() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + String newDateString = "abcdefs"; + + discountedItem.setDiscountTo(newDateString); + Date date = discountedItem.getDiscountTo(); + + assertEquals(10, date.getDate()); + assertEquals(4, date.getMonth()); + assertEquals(118, date.getYear()); + } + + @Test + public void getDiscountedPrice_zeroDiscount() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",0, "10.05.2017", "10.05.2018"); + + float value = discountedItem.getDiscountedPrice(); + + assertEquals(1.6f, value, 0.001f); + } + + @Test + public void getDiscountedPrice_nozeroDiscount() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + + float value = discountedItem.getDiscountedPrice(); + + assertEquals(0.64f, value, 0.001f); + } + + @Test + public void parseDate_correctDateFormat() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "abcd", "10.05.2018"); + + Date date = discountedItem.getDiscountFrom(); + + assertNull(date); + } + + @Test + public void parseDate_wrongDateFormat() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + + Date date = discountedItem.getDiscountFrom(); + + assertEquals(10, date.getDate()); + assertEquals(4, date.getMonth()); + assertEquals(117, date.getYear()); + } + + @Test + public void setDiscountTo_correctDateFormatDate() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + Date newDate = new Date(120,3,15); + + discountedItem.setDiscountTo(newDate); + Date date = discountedItem.getDiscountTo(); + + assertEquals(15, date.getDate()); + assertEquals(3, date.getMonth()); + assertEquals(120, date.getYear()); + } + + @Test + public void setDiscountTo_nullDateFormatDate() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + Date newDate = null; + + discountedItem.setDiscountTo(newDate); + Date date = discountedItem.getDiscountTo(); + + assertNull(date); + } + + @Test + public void getDiscountedPrice_reflectsChangeOfDiscount() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + int newDiscount = 40; + + discountedItem.setDiscount(newDiscount); + int actual = discountedItem.getDiscount(); + float value = discountedItem.getDiscountedPrice(); + + assertEquals(0.96f, value, 0.001f); + assertEquals(newDiscount, actual); + } + + @Test + public void getOriginalPrice_priceIsCorrect() { + DiscountedItem discountedItem = new DiscountedItem(1, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + + float actual = discountedItem.getOriginalPrice(); + + assertEquals(1.6f, actual, 0.001f); + } +} diff --git a/src/test/java/shop/ShoppingCartTest.java b/src/test/java/shop/ShoppingCartTest.java new file mode 100644 index 0000000..480c97b --- /dev/null +++ b/src/test/java/shop/ShoppingCartTest.java @@ -0,0 +1,112 @@ +package shop; + +import cz.cvut.eshop.shop.DiscountedItem; +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.ShoppingCart; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ShoppingCartTest { + @Test + public void removeItem_itemDoesNotExist() { + ShoppingCart shoppingCart = new ShoppingCart(); + Item item1 = new StandardItem(1, "name", 0.28f, "category", 1); + Item item2 = new DiscountedItem(2, "name2", 2.22f, "category2", 50, new Date(1996,12,20), new Date(2020,11,15)); + shoppingCart.addItem(item1); + shoppingCart.addItem(item2); + + shoppingCart.removeItem(5); + int items = shoppingCart.getItemsCount(); + + assertEquals(2, items); + } + + @Test + public void removeItem_itemExists() { + ShoppingCart shoppingCart = new ShoppingCart(); + Item item = new StandardItem(1, "name", 0.28f, "category", 1); + shoppingCart.addItem(item); + + shoppingCart.removeItem(1); + int items = shoppingCart.getItemsCount(); + + assertEquals(0, items); + } + + @Test + public void getTotalPrice_StandardItem() { + Item item1 = new StandardItem(2, "name1", 1.28f, "category1", 1); + Item item2 = new StandardItem(1, "name2", 2.01f, "category2", 2); + ArrayList<Item> list = new ArrayList<>(); + list.add(item1); + list.add(item2); + ShoppingCart shoppingCart = new ShoppingCart(list); + + float price = shoppingCart.getTotalPrice(); + + assertEquals(3.29f, price,0.01); + } + + @Test + public void getTotalPrice_DiscountedItem() { + Item item1 = new DiscountedItem(2, "name1", 1.6f, "category1",60, "10.05.2017", "10.05.2018"); + Item item2 = new DiscountedItem(1, "name2", 2.22f, "category2", 50, new Date(1996,12,20), new Date(2020,11,15)); + ArrayList<Item> list = new ArrayList<>(); + list.add(item1); + list.add(item2); + ShoppingCart shoppingCart = new ShoppingCart(list); + + float price = shoppingCart.getTotalPrice(); + + assertEquals(1.75f, price,0.01); + } + + @Test + public void getCartItems_itemIsNull() + { + ShoppingCart shoppingCart = new ShoppingCart(null); + + ArrayList<Item> items = shoppingCart.getCartItems(); + + assertNull(items); + } + + @Test(expected = java.lang.NullPointerException.class) + public void addItem_itemIsNull() + { + ShoppingCart shoppingCart = new ShoppingCart(null); + Item item = new StandardItem(1, "name", 0.28f, "category", 1); + + shoppingCart.addItem(item); + } + + @Test(expected = java.lang.NullPointerException.class) + public void removeItem_itemIsNull() + { + ShoppingCart shoppingCart = new ShoppingCart(null); + + shoppingCart.removeItem(2); + } + + @Test(expected = java.lang.NullPointerException.class) + public void itemsCount_itemIsNull() + { + ShoppingCart shoppingCart = new ShoppingCart(null); + + shoppingCart.getItemsCount(); + } + + @Test(expected = java.lang.NullPointerException.class) + public void getTotalPrice_itemIsNull() + { + ShoppingCart shoppingCart = new ShoppingCart(null); + + shoppingCart.getItemsCount(); + } +} -- GitLab From c8af476de0210255099967739e7428dc6f0a9e6f Mon Sep 17 00:00:00 2001 From: Petr Bergmann <bergmpet@fel.cvut.cz> Date: Sat, 27 Oct 2018 22:07:54 +0200 Subject: [PATCH 4/5] Update README.md errors --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 38107a0..beb797f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ [![pipeline status](https://gitlab.fel.cvut.cz/bergmpet/eShop/badges/master/pipeline.svg)](https://gitlab.fel.cvut.cz/bergmpet/eShop/commits/master) [![coverage report](https://gitlab.fel.cvut.cz/bergmpet/eShop/badges/master/coverage.svg)](https://gitlab.fel.cvut.cz/bergmpet/eShop/commits/master) + +Some test are currently failing due to errors found in: +1) class ShoppingCart where both for-cycles (line 45 and 66) contains wrong stopping condition. Instead of "i<=0" should be "i>=0" +2) function getTotalPrice() in ShoppingCart should probably return float value instead of integer as all of the item prices are float values +3) function getDiscountedPrice() in class DiscountedItem does not divide percents by 100 so the price result of multiplication is incorrect. Possible change replacement of line 114 is: return super.getPrice()*((100 - discount)/100.0f); \ No newline at end of file -- GitLab From 1a725baef65dab03bcce5da53361b943b4b89551 Mon Sep 17 00:00:00 2001 From: ksonny4 <ksonny4@gmail.com> Date: Sat, 27 Oct 2018 22:40:25 +0200 Subject: [PATCH 5/5] Found error thanks to testGetTotalAmount() --- src/test/java/archive/OrderTest.java | 87 +++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/src/test/java/archive/OrderTest.java b/src/test/java/archive/OrderTest.java index 13c0261..0f92256 100644 --- a/src/test/java/archive/OrderTest.java +++ b/src/test/java/archive/OrderTest.java @@ -1,14 +1,17 @@ package archive; + import cz.cvut.eshop.shop.Order; import cz.cvut.eshop.shop.ShoppingCart; +import cz.cvut.eshop.shop.StandardItem; import org.junit.Test; + + import static junit.framework.TestCase.assertEquals; public class OrderTest { - @Test public void createOrderWithNoItemsOrderedTest() { // setup @@ -21,5 +24,87 @@ public class OrderTest { assertEquals(numberOfItems,0); } + @Test + public void oneItemAndDeleted() { + // setup + ShoppingCart cart = new ShoppingCart(); + int num1 = 2122; + float num2 = 5411210; + int num3 = 4324; + String str1 = "PC"; + String str2 = "PCs"; + + cart.addItem(new StandardItem(num1,str1,num2,str2,num3)); + Order order = new Order(cart, 0); + order.getItems().remove(0); + // act + // assert + assertEquals(0, order.getItems().size()); + } + + @Test + public void oneItemInserted() { + // setup + ShoppingCart cart = new ShoppingCart(); + cart.addItem(new StandardItem(79987, "Generic laptop", 20000, "laptops", 200)); + Order order = new Order(cart, 0); + // act + // assert + assertEquals(1, order.getItems().size()); + } + + @Test + public void multipleItemsInserted() { + // setup + ShoppingCart cart = new ShoppingCart(); + Order order = new Order(cart, 0); + int numberOfItems = 100; + + //act + + for (int i = 0; i < numberOfItems*2; i = i + 2) { + + int num1 = i; + int num2 = i + 1; + float num3 = (float) (num1 + num2 + 0.5); + + String str1 = (char) num1 + ""; + String str2 = (char) num1 + ""; + cart.addItem(new StandardItem(num1,str1,num3,str2,num2)); + + } + + + assertEquals(numberOfItems, order.getItems().size()); + + } + + + //TODO: metoda order.getTotalAmount() ma promennou int totalAmount, ktera scita floaty + @Test + public void testGetTotalAmount() { + // setup + ShoppingCart cart = new ShoppingCart(); + Order order = new Order(cart, 0); + int numberOfItems = 100; + float price = 0; + //act + + for (int i = 0; i < numberOfItems*2; i = i + 2) { + int num1 = i; + int num2 = i + 1; + float num3 = (float) (num1 + num2 + 0.31315); + + price += num3; + + String str1 = (char) num1 + ""; + String str2 = (char) num1 + ""; + cart.addItem(new StandardItem(num1,str1,num3,str2,num2)); + } + + + assertEquals(price, order.getTotalAmount()); + + } } -- GitLab