1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CupboardControllerTest {
private CupboardController cupboardController;
private CupboardFileDao mockCupboardDAO;
@BeforeEach
public void setupCupboardDAO() {
mockCupboardDAO = mock(CupboardFileDao.class);
cupboardController = new CupboardController(mockCupboardDAO);
}
@Test
public void createNeed() 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.OK, res.getStatusCode());
assertEquals(need, res.getBody());
}
@Test
public void getNeeds() {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
when(mockCupboardDAO.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(mockCupboardDAO.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(mockCupboardDAO.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(mockCupboardDAO.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(mockCupboardDAO.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(mockCupboardDAO.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(mockCupboardDAO.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(mockCupboardDAO.getNeed(1)).thenReturn(need);
when(mockCupboardDAO.deleteNeed(1)).thenReturn(true);
var res = cupboardController.deleteNeed(1);
assertEquals(HttpStatus.OK, res.getStatusCode());
}
@Test
public void deleteNeedFail() throws IOException {
when(mockCupboardDAO.getNeed(1)).thenReturn(null);
when(mockCupboardDAO.deleteNeed(1)).thenReturn(false);
var res = cupboardController.deleteNeed(1);
assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode());
}
}
|