aboutsummaryrefslogtreecommitdiff
path: root/ufund-api
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java160
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java39
2 files changed, 197 insertions, 2 deletions
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 2a3c8ee..da098d0 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
@@ -81,6 +81,78 @@ public class CupboardServiceTest {
}
@Test
+ public void testCreateNeedBadPhysicalGoal() throws IOException {
+ // Setup
+ String name = "Jellyfish";
+ String location = "Atlantis";
+ double maxGoal = 1.23;
+ GoalType type = Need.GoalType.PHYSICAL;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+
+ // 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, location, image, maxGoal, type, urgent, description));
+ }
+
+ @Test
+ public void testCreateNeedBadBlankFields() throws IOException {
+ // Setup
+ String name = "";
+ String location = "Atlantis";
+ double maxGoal = 1.23;
+ GoalType type = Need.GoalType.PHYSICAL;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+
+ // 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, location, image, maxGoal, type, urgent, description));
+ }
+
+ @Test
+ public void testCreateNeedNullFields() throws IOException {
+ // Setup
+ String name = "";
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+
+ // 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, location, image, maxGoal, type, urgent, description));
+ }
+
+ @Test
public void testCreateNeedDuplicate() throws IOException {
// Setup
String name = "Jellyfish";
@@ -198,6 +270,94 @@ public class CupboardServiceTest {
}
@Test
+ public void testUpdateNeedBadGoal() throws IOException {
+ // Setup
+ String name = "Jellyfish";
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+ // goal should not be 0, should throw error
+ Need newNeed = new Need(name, image, location, 0, type, urgent, description);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed);
+
+ // Invoke and Analyze
+ assertThrows(IllegalArgumentException.class, () ->
+ cupboardService.updateNeed(newNeed, need.getId()));
+ }
+
+ @Test
+ public void testUpdateNeedBadPhysicalGoal() throws IOException {
+ // Setup
+ String name = "Jellyfish";
+ String location = "Atlantis";
+ double maxGoal = 10;
+ GoalType type = Need.GoalType.PHYSICAL;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+ // goal should be integer, should throw error
+ Need newNeed = new Need(name, image, location, 1.23, type, urgent, description);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed);
+
+ // Invoke and Analyze
+ assertThrows(IllegalArgumentException.class, () ->
+ cupboardService.updateNeed(newNeed, need.getId()));
+ }
+
+ @Test
+ public void testUpdateNeedBlankFields() throws IOException {
+ // Setup
+ String name = "Jellyfish";
+ String location = "Atlantis";
+ double maxGoal = 10;
+ GoalType type = Need.GoalType.PHYSICAL;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+ // passed in blank name, should throw error
+ Need newNeed = new Need("", image, location, maxGoal, type, urgent, description);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed);
+
+ // Invoke and Analyze
+ assertThrows(IllegalArgumentException.class, () ->
+ cupboardService.updateNeed(newNeed, need.getId()));
+ }
+
+ @Test
+ public void testUpdateNeedNotMatchingIDs() throws IOException {
+ // Setup
+ String name = "Jellyfish";
+ String location = "Atlantis";
+ double maxGoal = 10;
+ GoalType type = Need.GoalType.PHYSICAL;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+ // passed in blank name, should throw error
+ Need newNeed = new Need(name, image, location, maxGoal, type, urgent, description);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed);
+
+ // Invoke and Analyze
+ assertThrows(IllegalArgumentException.class, () ->
+ cupboardService.updateNeed(newNeed, -1));
+ }
+
+ @Test
public void testDeleteNeed() throws IOException {
// Setup
String name = "Jellyfish";
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
index 5adabf1..6234416 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
@@ -3,6 +3,7 @@ package com.ufund.api.ufundapi.service;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
@@ -75,6 +76,40 @@ public class UserServiceTest {
}
@Test
+ public void testGetUserBlank() throws IOException {
+ // Setup
+ String username = "Jelly";
+ String password = "Fish";
+ User user = User.create(username, password);
+
+ // Mock
+ when(mockUserDAO.getUser("notReal")).thenReturn(user);
+
+ // Analyze
+ assertNull(userService.getUser(username));
+ }
+
+ @Test public void testGetUserCount() throws IOException {
+ // Setup
+ String username = "Jelly";
+ String password = "Fish";
+ User user = User.create(username, password);
+
+ // Mock
+ when(mockUserDAO.getUser(username)).thenReturn(null);
+ when(mockUserDAO.addUser(any())).thenReturn(user);
+ when(mockUserDAO.getUserCount()).thenReturn(1);
+
+ // Invoke
+ mockUserDAO.addUser(user);
+ int userCount = userService.getUserCount();
+
+ // Analyze
+ assertEquals(userCount, 1);
+
+ }
+
+ @Test
public void testUpdateUser() throws IOException {
// Setup
String username = "Jelly";
@@ -118,10 +153,10 @@ public class UserServiceTest {
User user = User.create(username, password);
// Mock
- when(mockUserDAO.deleteUser(username)).thenReturn(true);
+ when(mockUserDAO.deleteUser(user.getUsername())).thenReturn(true);
// Analyze
- assertTrue(userService.deleteUser(username));
+ assertTrue(userService.deleteUser(user.getUsername()));
}
}