aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
blob: 6ef6710b0b6d253d8777f3df36750fc376fb21f3 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.ufund.api.ufundapi.controller;

import java.io.IOException;
import java.util.Map;
import static java.util.Map.entry;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.springframework.http.HttpStatus;

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.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, 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("name", "Test"),
                entry("maxGoal", 100.0),
                entry("type", "MONETARY")
        );

        var res = cupboardController.createNeed(needMap);

        assertEquals(HttpStatus.OK, res.getStatusCode());
        assertEquals(need, res.getBody());
    }

    @Test
    public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException {
        when(mockCupboardService.createNeed("Name", -100, Need.GoalType.MONETARY)).thenThrow(new IllegalArgumentException());

        Map<String, Object> needMap = Map.ofEntries(
                entry("name", "Name"),
                entry("maxGoal", -100.0),
                entry("type", "MONETARY"));

        var res = cupboardController.createNeed(needMap);

        assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode());
    }

    @Test
    public void createNeedIOException() throws IOException, DuplicateKeyException {
        when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new IOException());

        Map<String, Object> needMap = Map.ofEntries(
                entry("name", "Name"),
                entry("maxGoal", 100.0),
                entry("type", "MONETARY"));

        var res = cupboardController.createNeed(needMap);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
    }

    @Test
    public void getNeeds() throws IOException {
        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 getNeedsIOException() throws IOException {
        when(mockCupboardService.getNeeds()).thenThrow(new IOException());

        var res = cupboardController.getNeeds();

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
    }

    @Test
    public void getNeedsEmpty() throws IOException {
        when(mockCupboardService.getNeeds()).thenReturn(new Need[]{});

        var res = cupboardController.getNeeds();

        assertEquals(HttpStatus.OK, res.getStatusCode());
        assertArrayEquals(new Need[]{}, res.getBody());
    }

    @Test
    public void searchNeeds() throws IOException {
        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
        when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need});

        var res = cupboardController.searchNeeds("Na");

        assertEquals(HttpStatus.OK, res.getStatusCode());
        assertArrayEquals(new Need[]{need}, res.getBody());
    }

    @Test
    public void searchNeedsIOException() throws IOException {
        when(mockCupboardService.searchNeeds("Na")).thenThrow(new IOException());

        var res = cupboardController.searchNeeds("Na");

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
    }

    @Test
    public void searchNeedsEmpty() throws IOException {
        when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{});

        var res = cupboardController.searchNeeds("Na");

        assertEquals(HttpStatus.OK, res.getStatusCode());
        assertArrayEquals(new Need[]{}, res.getBody());
    }

    @Test
    public void getNeed() throws IOException {
        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 getNeedIOException() throws IOException {
        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
        when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException());

        var res = cupboardController.getNeed(need.getId());

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
    }

    @Test
    public void getNeedFail() throws IOException {
        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, 1)).thenReturn(need);

        var res = cupboardController.updateNeed(need, 1);

        assertEquals(HttpStatus.OK, res.getStatusCode());
        assertEquals(need, res.getBody());
    }

    @Test
    public void updateNeedsIOException() throws IOException {
        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
        when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException());

        var res = cupboardController.updateNeed(need, 1);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
    }

    @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());
    }

    @Test
    public void deleteNeedIOException() throws IOException {
        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
        when(mockCupboardService.getNeed(1)).thenReturn(need);
        when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException());

        var res = cupboardController.deleteNeed(1);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
    }
}