From acac4aba2f7876c557b466ae6578c7012419679b Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 3 Mar 2025 17:06:15 -0500 Subject: Added IOException tests --- .../controller/CupboardControllerTest.java | 87 ++++++++++++++++++++-- 1 file changed, 80 insertions(+), 7 deletions(-) (limited to 'ufund-api/src/test/java/com') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java index 04ce41d..c9b0c5e 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java @@ -1,16 +1,18 @@ package com.ufund.api.ufundapi.controller; -import com.ufund.api.ufundapi.model.Need; -import com.ufund.api.ufundapi.persistence.CupboardFileDao; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.http.HttpStatus; - import java.io.IOException; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.springframework.http.HttpStatus; + +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardFileDao; public class CupboardControllerTest { private CupboardController cupboardController; @@ -33,6 +35,26 @@ public class CupboardControllerTest { assertEquals(need, res.getBody()); } + @Test + public void createNeedBadMaxGoal() throws IOException { + var need = new Need("Name", 1, -100, Need.GoalType.MONETARY); + when(mockCupboardDAO.createNeed(need)).thenReturn(need); + + var res = cupboardController.createNeed(need); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } + + @Test + public void createNeedIOException() throws IOException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardDAO.createNeed(need)).thenThrow(new IOException()); + + var res = cupboardController.createNeed(need); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } + @Test public void getNeeds() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); @@ -44,6 +66,16 @@ public class CupboardControllerTest { assertArrayEquals(new Need[]{need}, res.getBody()); } + @Test + public void getNeedsIOException() { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardDAO.getNeeds()).thenThrow(new IOException()); + + var res = cupboardController.getNeeds(); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } + @Test public void getNeedsEmpty() { when(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{}); @@ -65,6 +97,16 @@ public class CupboardControllerTest { assertArrayEquals(new Need[]{need}, res.getBody()); } + @Test + public void searchNeedsIOException() { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardDAO.findNeeds("Na")).thenThrow(new IOException()); + + var res = cupboardController.searchNeeds("Na"); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } + @Test public void searchNeedsEmpty() { when(mockCupboardDAO.findNeeds("Na")).thenReturn(new Need[]{}); @@ -86,6 +128,16 @@ public class CupboardControllerTest { assertEquals(need, res.getBody()); } + @Test + public void getNeed() { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardDAO.getNeed(need.getId())).thenThrow(new IOException()) + + var res = cupboardController.getNeed(need.getId()); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } + @Test public void getNeedFail() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); @@ -108,6 +160,16 @@ public class CupboardControllerTest { assertEquals(need, res.getBody()); } + @Test + public void updateNeedsIOException() throws IOException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardDAO.updateNeed(need)).thenThrow(new IOException()); + + var res = cupboardController.updateNeed(need); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } + @Test public void deleteNeed() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); @@ -128,4 +190,15 @@ public class CupboardControllerTest { assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); } + + @Test + public void deleteNeedIOException() throws IOException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardDAO.getNeed(1)).thenReturn(need); + when(mockCupboardDAO.deleteNeed(1)).thenThrow(new IOException()); + + var res = cupboardController.deleteNeed(1); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } } -- cgit v1.2.3 From c80ffa3e32bb2cf119e1088ef0121fb2087eb107 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 3 Mar 2025 17:06:40 -0500 Subject: Added maxGoal and setName tests --- .../com/ufund/api/ufundapi/model/NeedTest.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'ufund-api/src/test/java/com') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java index 50b5cf2..ffcd808 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java @@ -59,6 +59,7 @@ public class NeedTest { assertEquals(need.getCurrent(), current); } + @Test public void testFilterAttributes() { @@ -75,4 +76,35 @@ public class NeedTest { assertEquals(need.getFilterAttributes(), filterAttributes); } + @Test + public void testSetMaxGoal() { + + String name = "Jellyfish"; + int id = 0; + double maxGoal = 100.00; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, id, maxGoal, type); + + double newGoal = 200.00; + need.setMaxGoal(newGoal); + + + assertEquals(newGoal, need.getMaxGoal()); + } + + @Test + public void testSetName() { + + String name = "Jellyfish"; + int id = 0; + double maxGoal = 100.00; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, id, maxGoal, type); + + String newName = "TESTINGFUN"; + need.setName(newName); + + assertEquals(newName, need.getName()); + } + } -- cgit v1.2.3 From 3e4864cd69cfebffc9d06e9e7645ed019a99be73 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 3 Mar 2025 17:06:54 -0500 Subject: Added verifyPassword and RemeoveBasketNeed tests --- .../com/ufund/api/ufundapi/model/UserTest.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'ufund-api/src/test/java/com') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java index 8b8dd99..716fbfd 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java @@ -45,4 +45,32 @@ public class UserTest { } + @Test + public void testRemoveBasketNeed() { + + String expectedName = "Bob"; + + User user = new User(expectedName); + Need need = new Need("Test", 0, 100, Need.GoalType.MONETARY); + Need need2 = new Need("Test2", 0, 100, Need.GoalType.MONETARY); + + user.addToBasket(need); + user.removeBasketNeed(need); + user.addToBasket(need2); + + assertEquals(need2, user.getBasketNeeds()[0]); + + } + + @Test + public void testVerifyPassword() { + + String expectedName = "Bob"; + + User user = new User(expectedName); + + assertEquals(false, user.verifyPassword(expectedName)); + + } + } -- cgit v1.2.3 From 4a6c06f669a62c84ed59c52397e2b0632e8f7f5d Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 3 Mar 2025 17:08:34 -0500 Subject: Fixed semicolon error --- .../com/ufund/api/ufundapi/controller/CupboardControllerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ufund-api/src/test/java/com') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java index c9b0c5e..3702581 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java @@ -129,9 +129,9 @@ public class CupboardControllerTest { } @Test - public void getNeed() { + public void getNeedIOException() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.getNeed(need.getId())).thenThrow(new IOException()) + when(mockCupboardDAO.getNeed(need.getId())).thenThrow(new IOException()); var res = cupboardController.getNeed(need.getId()); -- cgit v1.2.3 From 9db38f2874b16fa345f4dde660d99465c8dcb1c2 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 3 Mar 2025 17:10:06 -0500 Subject: Fixed httpstatus in assert statement --- .../java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api/src/test/java/com') diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java index 3702581..839c518 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java @@ -42,7 +42,7 @@ public class CupboardControllerTest { var res = cupboardController.createNeed(need); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); } @Test -- cgit v1.2.3