package com.ufund.api.ufundapi.controller; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.http.HttpStatus; import java.io.IOException; import java.util.HashMap; import java.util.Map; import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.ufund.api.ufundapi.service.CupboardService; public class CupboardControllerTest { private CupboardController cupboardController; private CupboardService mockCupboardService; @BeforeEach public void setupCupboardDAO() { mockCupboardService = mock(CupboardService.class); cupboardController = new CupboardController(mockCupboardService); } @Test public void createNeed() throws IOException, CupboardService.DuplicateKeyException { String name = "Test"; int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; var need = new Need(name, type, maxGoal); when(mockCupboardService.createNeed(name, maxGoal, type)).thenReturn(need); Map needMap = Map.ofEntries( entry("id", need.getId()), entry("need", need) ); var res = cupboardController.createNeed(needMap); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); } @Test public void getNeeds() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); var res = cupboardController.getNeeds(); assertEquals(HttpStatus.OK, res.getStatusCode()); assertArrayEquals(new Need[]{need}, res.getBody()); } @Test public void getNeedsEmpty() { when(mockCupboardService.getNeeds()).thenReturn(new Need[]{}); var res = cupboardController.getNeeds(); assertEquals(HttpStatus.OK, res.getStatusCode()); assertArrayEquals(new Need[]{}, res.getBody()); } @Test public void searchNeeds() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.findNeeds("Na")).thenReturn(new Need[]{need}); var res = cupboardController.searchNeeds("Na"); assertEquals(HttpStatus.OK, res.getStatusCode()); assertArrayEquals(new Need[]{need}, res.getBody()); } @Test public void searchNeedsEmpty() { when(mockCupboardService.findNeeds("Na")).thenReturn(new Need[]{}); var res = cupboardController.searchNeeds("Na"); assertEquals(HttpStatus.OK, res.getStatusCode()); assertArrayEquals(new Need[]{}, res.getBody()); } @Test public void getNeed() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeed(need.getId())).thenReturn(need); var res = cupboardController.getNeed(need.getId()); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); } @Test public void getNeedFail() { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeed(need.getId())).thenReturn(null); var res = cupboardController.getNeed(need.getId()); assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); assertNull(res.getBody()); } @Test public void updateNeeds() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.updateNeed(need)).thenReturn(need); var res = cupboardController.updateNeed(need); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); } @Test public void deleteNeed() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenReturn(true); var res = cupboardController.deleteNeed(1); assertEquals(HttpStatus.OK, res.getStatusCode()); } @Test public void deleteNeedFail() throws IOException { when(mockCupboardService.getNeed(1)).thenReturn(null); when(mockCupboardService.deleteNeed(1)).thenReturn(false); var res = cupboardController.deleteNeed(1); assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); } }