From 7d5df1efe71963f100c445f3cd0da1546e7ffb6e Mon Sep 17 00:00:00 2001
From: Gunther6070 <haydenhartman10@yahoo.com>
Date: Sun, 16 Mar 2025 13:44:35 -0400
Subject: Created all cupboardService tests

---
 .../api/ufundapi/service/CupboardServiceTest.java  | 153 ++++++++++++++++++++-
 1 file changed, 147 insertions(+), 6 deletions(-)

(limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java')

diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
index ceef215..884b8b0 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
@@ -2,8 +2,11 @@ package com.ufund.api.ufundapi.service;
 
 import java.io.IOException;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.*;
 
 import org.junit.jupiter.api.BeforeEach;
@@ -19,13 +22,11 @@ import com.ufund.api.ufundapi.persistence.CupboardDAO;
 public class CupboardServiceTest {
 
     private CupboardDAO mockCupboardDAO;
-    private AuthService mockAuthService;
     private CupboardService cupboardService;
 
     @BeforeEach
     public void setupCupboardService() {
         mockCupboardDAO = mock(CupboardDAO.class);
-        mockAuthService = mock(AuthService.class);
         cupboardService = new CupboardService(mockCupboardDAO);
 
     }
@@ -35,15 +36,12 @@ public class CupboardServiceTest {
         // Setup
         String name = "Jellyfish";
         double maxGoal = 100.00;
-        int id = 0;
         GoalType type = GoalType.MONETARY;
         Need need = new Need(name, type, maxGoal);
 
         // When the same id is passed in, our mock User DAO will return the User object
-        when(mockCupboardDAO.getNeed(id)).thenReturn(need);
         when(mockCupboardDAO.addNeed(any())).thenReturn(need);
         when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
-        
 
         // Invoke
         Need response = cupboardService.createNeed(name, maxGoal, type);
@@ -53,4 +51,147 @@ public class CupboardServiceTest {
         assertEquals(need, response);
     }
 
-}
+    @Test
+    public void testCreateNeedBadGoal() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = -100.00;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.addNeed(any())).thenReturn(need);
+        when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
+
+        // Invoke
+        // Need response = cupboardService.createNeed(name, maxGoal, type);
+
+        // Analyze
+        assertThrows(IllegalArgumentException.class, () -> {
+            cupboardService.createNeed(name, maxGoal, type);
+        });
+    }
+
+    @Test
+    public void testCreateNeedDuplicate() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = 100.00;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+        Need[] needs = { need };
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.addNeed(any())).thenReturn(need);
+        when(mockCupboardDAO.getNeeds()).thenReturn(needs);
+
+        // Invoke
+        // Need response = cupboardService.createNeed(name, maxGoal, type);
+
+        // Analyze
+        assertThrows(DuplicateKeyException.class, () -> {
+            cupboardService.createNeed(name, maxGoal, type);
+        });
+    }
+
+    @Test
+    public void testSearchNeeds() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = 100.00;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+        Need[] needs = { need };
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.getNeeds()).thenReturn(needs);
+
+        // Invoke
+        Need[] response = cupboardService.searchNeeds("Jelly");
+
+        // Analyze
+        assertEquals(need, response[0]);
+        assertEquals(need.getName(), response[0].getName());
+    }
+
+    @Test
+    public void testSearchNeedsFail() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = 100.00;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+        Need[] needs = { need };
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.getNeeds()).thenReturn(needs);
+
+        // Invoke
+        Need[] response = cupboardService.searchNeeds("Octopus");
+
+        // Analyze
+        assertArrayEquals(new Need[0], response);
+    }
+
+    @Test
+    public void testGetNeed() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = 100.00;
+        int id = 0;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.getNeed(id)).thenReturn(need);
+
+        // Invoke
+        Need response = cupboardService.getNeed(id);
+
+        // Analyze
+        assertEquals(need, response);
+    }
+
+    @Test
+    public void testUpdateNeed() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = 100.00;
+        int id = 0;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+        Need newNeed = new Need("Octopus", type, maxGoal);
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed);
+
+        // Invoke
+        Need response = cupboardService.updateNeed(newNeed, id);
+
+        // Analyze
+        assertEquals(newNeed, response);
+    }
+
+    @Test
+    public void testDeleteNeed() throws IOException, DuplicateKeyException {
+        // Setup
+        String name = "Jellyfish";
+        double maxGoal = 100.00;
+        int id = 0;
+        GoalType type = GoalType.MONETARY;
+        Need need = new Need(name, type, maxGoal);
+
+        // When the same id is passed in, our mock User DAO will return the User object
+        when(mockCupboardDAO.deleteNeed(id)).thenReturn(true);
+        when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
+
+        // Invoke
+        Boolean response = cupboardService.deleteNeed(id);
+        Need[] responseNeeds = cupboardService.getNeeds();
+
+        // Analyze
+        assertTrue(response);
+        assertArrayEquals(new Need[0], responseNeeds);
+    }
+
+}
\ No newline at end of file
-- 
cgit v1.2.3


From 34a3f63ffb9f33a4817ea6d247499df5743b816a Mon Sep 17 00:00:00 2001
From: sowgro <tpoke.ferrari@gmail.com>
Date: Sun, 16 Mar 2025 23:11:58 -0400
Subject: fix warning

---
 .../test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java')

diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
index 884b8b0..99ca23c 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
@@ -186,7 +186,7 @@ public class CupboardServiceTest {
         when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
 
         // Invoke
-        Boolean response = cupboardService.deleteNeed(id);
+        boolean response = cupboardService.deleteNeed(id);
         Need[] responseNeeds = cupboardService.getNeeds();
 
         // Analyze
-- 
cgit v1.2.3