From 0f19f2705010b97134aa07b6698c897b3a25b7a5 Mon Sep 17 00:00:00 2001
From: Andreas <andreksv@ntnu.no>
Date: Sun, 19 Mar 2023 17:17:32 +0100
Subject: [PATCH] Added more tests to method getItemsByCategory

---
 .../demo/data/Economics/ItemRegister.java     |   8 +-
 .../demo/data/Economics/ItemRegisterTest.java | 116 +++++++++---------
 2 files changed, 63 insertions(+), 61 deletions(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
index 78fbccf3..21355162 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegister.java
@@ -73,18 +73,18 @@ public class ItemRegister<T extends Item>{
     public <S> List<T> getItemsByCategory(S category) {
         //Checks if category is an instance of IncomeCategory
         if(category instanceof IncomeCategory) {
-            ArrayList<Income> castedIncome = (ArrayList<Income>) items;
             try {
-                return (ArrayList<T>) castedIncome.stream().filter(anIncome -> anIncome.getCategory().compareTo((IncomeCategory) category) == 0).collect(Collectors.toList());
+                ArrayList<Income> castedIncome = (ArrayList<Income>) items;
+                return (ArrayList<T>) castedIncome.stream().filter(income -> income.getCategory().compareTo((IncomeCategory) category) == 0).collect(Collectors.toList());
             } catch (ClassCastException ex) {
                 throw new ClassCastException("Can´t use IncomeCategory for Expense´s: " + ex.getMessage());
             }
         }
         //Checks category is s an instance of ExpenseCategory
         else if(category instanceof ExpenseCategory){
-            ArrayList<Expense> castedExpenses = (ArrayList<Expense>) items;
             try {
-                return (ArrayList<T>) castedExpenses.stream().filter(anExpense -> anExpense.getCategory().compareTo((ExpenseCategory) category) == 0).collect(Collectors.toList());
+                ArrayList<Expense> castedExpenses = (ArrayList<Expense>) items;
+                return (ArrayList<T>) castedExpenses.stream().filter(expense -> expense.getCategory().compareTo((ExpenseCategory) category) == 0).collect(Collectors.toList());
             } catch (ClassCastException ex){
                 throw new ClassCastException("Can´t use ExpenseCategory for Income: " + ex.getMessage());
             }
diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java
index 8752fbef..34569ad6 100644
--- a/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java
+++ b/src/test/java/no/ntnu/idatt1002/demo/data/Economics/ItemRegisterTest.java
@@ -6,29 +6,23 @@ import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 import java.util.ArrayList;
+import java.util.List;
 
 import static org.junit.jupiter.api.Assertions.*;
 
 public class ItemRegisterTest {
-    ItemRegister<Income> incomeRegister;
-    ItemRegister<Expense> expenseRegister;
+    ItemRegister<Income> incomeRegister = new ItemRegister<>();
+    ItemRegister<Expense> expenseRegister = new ItemRegister<>();
 
     @Nested
     @DisplayName("Test ItemRegister when using Income")
     class incomeRegisterTests {
-        @BeforeEach
-        public void createItemOverView() {
-            incomeRegister = new ItemRegister();
-        }
-
         @Test
         @DisplayName("addItem method throws exception when it should")
         void addItemThrows() {
             Income income = new Income("description", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
-            assertThrows(IllegalArgumentException.class, () -> {
-                incomeRegister.addItem(income);
-                incomeRegister.addItem(income);
-            });
+            incomeRegister.addItem(income);
+            assertThrows(IllegalArgumentException.class, () -> incomeRegister.addItem(income));
         }
 
         @Test
@@ -36,56 +30,53 @@ public class ItemRegisterTest {
         void addItemDoesNotThrow() {
             Income income1 = new Income("description", 59.9f, false, IncomeCategory.GIFT, "03.03.23");
             Income income2 = new Income("anotherDescription", 6.5f, true, IncomeCategory.SALARY, "02.03.23");
-            assertDoesNotThrow(() -> {
-                incomeRegister.addItem(income1);
-                incomeRegister.addItem(income2);
-            });
+            assertDoesNotThrow(() -> incomeRegister.addItem(income1));
+            assertDoesNotThrow(() -> incomeRegister.addItem(income2));
         }
 
-        @Test
-        @DisplayName("getTotalSum method gives correct amount")
-        void getTotalSumCorrectAmount() {
-            Income income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
-            Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21");
-            Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23");
+        Income income1;
+        Income income2;
+        Income income3;
+        @BeforeEach
+        void addIncomeToRegister(){
+            income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
+            income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21");
+            income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23");
             incomeRegister.addItem(income1);
             incomeRegister.addItem(income2);
             incomeRegister.addItem(income3);
+        }
 
+        @Test
+        @DisplayName("getTotalSum method gives correct amount")
+        void getTotalSumCorrectAmount() {
             double totalIncome = 59.9f + 62.4f + 9.81f;
             assertEquals(Math.round(incomeRegister.getTotalSum()), Math.round(totalIncome));
         }
 
         @Test
-        void getIncomeBasedOnCategoryGivesCorrectIncome() {
-            Income income1 = new Income("description1", 59.9f, false, IncomeCategory.SALARY, "03.03.23");
-            Income income2 = new Income("description2", 62.4f, true, IncomeCategory.GIFT, "01.02.21");
-            Income income3 = new Income("description3", 9.81f, false, IncomeCategory.SALARY, "05.07.23");
-
-            incomeRegister.addItem(income1);
-            incomeRegister.addItem(income2);
-            incomeRegister.addItem(income3);
-            ArrayList<Income> incomeSalary = incomeRegister.getItemsBasedOnCategory(IncomeCategory.SALARY);
+        @DisplayName("getItemsByCategory gives expected Income back")
+        void getIncomeByCategoryGivesCorrectIncome() {
+            List<Income> incomeSalary = incomeRegister.getItemsByCategory(IncomeCategory.SALARY);
             assertTrue(incomeSalary.contains(income1) && incomeSalary.contains(income3));
         }
+
+        @Test
+        @DisplayName("getItemsByCategory throws exception when it takes in ExpenseCategory")
+        void getIncomeByExpenseCategoryThrowsException(){
+            assertThrows(ClassCastException.class, () -> incomeRegister.getItemsByCategory(ExpenseCategory.CLOTHES));
+        }
     }
 
     @Nested
     @DisplayName("Test ItemRegister when using Expense")
     class expenseRegisterTests {
-        @BeforeEach
-        public void createItemOverView() {
-            expenseRegister = new ItemRegister();
-        }
-
         @Test
         @DisplayName("addItem method throws exception when it should")
         void addItemThrows() {
             Expense expense = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
-            assertThrows(IllegalArgumentException.class, () -> {
-                expenseRegister.addItem(expense);
-                expenseRegister.addItem(expense);
-            });
+            expenseRegister.addItem(expense);
+            assertThrows(IllegalArgumentException.class, () -> expenseRegister.addItem(expense));
         }
 
         @Test
@@ -93,37 +84,48 @@ public class ItemRegisterTest {
         void addItemDoesNotThrow() {
             Expense expense1 = new Expense("description", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
             Expense expense2 = new Expense("anotherDescription", 6.5f, true, ExpenseCategory.BOOKS, "02.03.23");
-            assertDoesNotThrow(() -> {
-                expenseRegister.addItem(expense1);
-                expenseRegister.addItem(expense2);
-            });
+            assertDoesNotThrow(() -> expenseRegister.addItem(expense1));
+            assertDoesNotThrow(() -> expenseRegister.addItem(expense2));
         }
 
-        @Test
-        @DisplayName("getTotalSum method gives correct amount")
-        void getTotalSumCorrectAmount(){
-            Expense expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
-            Expense expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21");
-            Expense expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23");
+        Expense expense1;
+        Expense expense2;
+        Expense expense3;
+        @BeforeEach
+        void addExpensesToRegister(){
+            expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
+            expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21");
+            expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23");
 
             expenseRegister.addItem(expense1);
             expenseRegister.addItem(expense2);
             expenseRegister.addItem(expense3);
+        }
+
+        @Test
+        @DisplayName("getTotalSum method gives correct amount")
+        void getTotalSumCorrectAmount(){
             double totalIncome = 59.9f + 62.4f + 9.81f;
             assertEquals(Math.round(expenseRegister.getTotalSum()), Math.round(totalIncome));
         }
 
         @Test
+        @DisplayName("getItemsByCategory gives expected Expenses back")
         void getIncomeBasedOnCategoryGivesCorrectIncome() {
-            Expense expense1 = new Expense("description1", 59.9f, false, ExpenseCategory.CLOTHES, "03.03.23");
-            Expense expense2 = new Expense("description2", 62.4f, true, ExpenseCategory.FOOD, "01.02.21");
-            Expense expense3 = new Expense("description3", 9.81f, false, ExpenseCategory.CLOTHES, "05.07.23");
-
-            expenseRegister.addItem(expense1);
-            expenseRegister.addItem(expense2);
-            expenseRegister.addItem(expense3);
-            ArrayList<Expense> expenseSalary = expenseRegister.getItemsBasedOnCategory(ExpenseCategory.CLOTHES);
+            List<Expense> expenseSalary = expenseRegister.getItemsByCategory(ExpenseCategory.CLOTHES);
             assertTrue(expenseSalary.contains(expense1) && expenseSalary.contains(expense3));
         }
+
+        @Test
+        @DisplayName("getItemsByCategory throws exception when it takes in IncomeCategory")
+        void getIncomeByExpenseCategoryThrowsException(){
+            assertThrows(ClassCastException.class, () -> expenseRegister.getItemsByCategory(IncomeCategory.SALARY));
+        }
+    }
+
+    @Test
+    @DisplayName("getItemsByCategory throws exception when IncomeCategory or ExpenseCategory is not used")
+    void getItemsByCategoryThrowsExceptionWhenIncomeCategoryOrExpenseCategoryIsNotUsed(){
+        assertThrows(IllegalArgumentException.class, () -> incomeRegister.getItemsByCategory("Not IncomeCategory"));
     }
 }
-- 
GitLab