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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
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<String, Object> 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());
}
}
|