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; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; import com.ufund.api.ufundapi.persistence.CupboardDAO; @Tag("Service-tier") public class CupboardServiceTest { private CupboardDAO mockCupboardDAO; private CupboardService cupboardService; @BeforeEach public void setupCupboardService() { mockCupboardDAO = mock(CupboardDAO.class); AuthService mockAuthService = mock(AuthService.class); cupboardService = new CupboardService(mockAuthService, mockCupboardDAO); } @Test public void testCreateNeed() throws IOException, DuplicateKeyException { // Setup String name = "Jellyfish"; String location = "Atlantis"; double maxGoal = 100; 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, location, image, maxGoal, type, urgent, description); // Analyze assertNotNull(response); assertEquals(need, response); } @Test public void testCreateNeedBadGoal() 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); // 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"; 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); 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, location, image, maxGoal, type, urgent, description)); } @Test public void testSearchNeeds() 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); 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 { // 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); 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 { // 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); // When the same id is passed in, our mock User DAO will return the User object when(mockCupboardDAO.getNeed(0)).thenReturn(need); // Invoke Need response = cupboardService.getNeed(need.getId()); // Analyze assertEquals(need, response); } @Test public void testUpdateNeed() 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); Need newNeed = new Need("Octopus", 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 Need response = cupboardService.updateNeed(newNeed, need.getId()); // Analyze assertEquals(newNeed, response); } @Test public void testDeleteNeed() 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); // When the same id is passed in, our mock User DAO will return the User object when(mockCupboardDAO.deleteNeed(0)).thenReturn(true); when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]); // Invoke boolean response = cupboardService.deleteNeed(need.getId()); Need[] responseNeeds = cupboardService.getNeeds(); // Analyze assertTrue(response); assertArrayEquals(new Need[0], responseNeeds); } }