|
@@ -5,6 +5,7 @@ import io.zipcoder.tc_spring_poll_application.domain.Option;
|
5
|
5
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
6
|
6
|
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
7
|
7
|
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
8
|
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
8
|
9
|
import org.junit.Assert;
|
9
|
10
|
import org.junit.Before;
|
10
|
11
|
import org.junit.Test;
|
|
@@ -13,8 +14,10 @@ import org.mockito.InjectMocks;
|
13
|
14
|
import org.mockito.Mock;
|
14
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
16
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
17
|
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
16
|
18
|
import org.springframework.boot.test.context.SpringBootTest;
|
17
|
19
|
import org.springframework.boot.test.json.JacksonTester;
|
|
20
|
+import org.springframework.boot.test.mock.mockito.MockBean;
|
18
|
21
|
import org.springframework.http.HttpHeaders;
|
19
|
22
|
import org.springframework.http.HttpStatus;
|
20
|
23
|
import org.springframework.http.MediaType;
|
|
@@ -22,6 +25,7 @@ import org.springframework.http.ResponseEntity;
|
22
|
25
|
import org.springframework.mock.web.MockHttpServletResponse;
|
23
|
26
|
import org.springframework.test.context.junit4.SpringRunner;
|
24
|
27
|
import org.springframework.test.web.servlet.MockMvc;
|
|
28
|
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
25
|
29
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
26
|
30
|
|
27
|
31
|
import java.util.Arrays;
|
|
@@ -31,25 +35,29 @@ import java.util.Set;
|
31
|
35
|
import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
|
32
|
36
|
import static org.mockito.BDDMockito.given;
|
33
|
37
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
38
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
34
|
39
|
|
35
|
40
|
|
36
|
|
-@SpringBootTest
|
37
|
41
|
@RunWith(SpringRunner.class)
|
38
|
|
-@AutoConfigureMockMvc
|
|
42
|
+@WebMvcTest(PollController.class)
|
39
|
43
|
public class PollControllerTest {
|
|
44
|
+
|
40
|
45
|
@Autowired
|
41
|
46
|
private MockMvc mvc;
|
|
47
|
+
|
|
48
|
+ // ==== Instance variables ==== //
|
42
|
49
|
private Poll testPoll, testPoll2;
|
43
|
50
|
private Iterable<Poll> polls;
|
44
|
51
|
private final Long POLL_ID = 1L;
|
|
52
|
+ private final Long BAD_ID = 1100L;
|
45
|
53
|
private Set<Option> options1, options2;
|
46
|
54
|
private Option option1, option2, option3, option4;
|
47
|
55
|
|
48
|
|
-
|
49
|
|
- @Mock
|
|
56
|
+ @MockBean
|
50
|
57
|
private PollRepository pollRepository;
|
51
|
58
|
|
52
|
59
|
@InjectMocks
|
|
60
|
+ @Autowired
|
53
|
61
|
private PollController pollController;
|
54
|
62
|
|
55
|
63
|
@Before
|
|
@@ -102,6 +110,8 @@ public class PollControllerTest {
|
102
|
110
|
polls = Arrays.asList(pollArr);
|
103
|
111
|
}
|
104
|
112
|
|
|
113
|
+ // =================== Get one =================== //
|
|
114
|
+
|
105
|
115
|
@Test
|
106
|
116
|
public void testGetPollByIdSuccess() throws Exception {
|
107
|
117
|
|
|
@@ -109,12 +119,15 @@ public class PollControllerTest {
|
109
|
119
|
.willReturn(testPoll);
|
110
|
120
|
|
111
|
121
|
MockHttpServletResponse response = mvc.perform(get("/polls/1")
|
|
122
|
+ .content(asJsonString(testPoll))
|
112
|
123
|
.accept(MediaType.APPLICATION_JSON))
|
113
|
124
|
.andReturn().getResponse();
|
114
|
125
|
|
115
|
126
|
Assert.assertEquals(HttpStatus.OK.value(), (response.getStatus()));
|
116
|
127
|
}
|
117
|
128
|
|
|
129
|
+ // =================== Get All =================== //
|
|
130
|
+
|
118
|
131
|
@Test
|
119
|
132
|
public void testGetAllPollsSuccess() throws Exception {
|
120
|
133
|
|
|
@@ -143,9 +156,11 @@ public class PollControllerTest {
|
143
|
156
|
Assert.assertTrue(response.getContentAsString().isEmpty());
|
144
|
157
|
}
|
145
|
158
|
|
|
159
|
+ // =================== Create Poll =================== //
|
|
160
|
+
|
146
|
161
|
@Test
|
147
|
162
|
public void testCreatePollSuccess() throws Exception {
|
148
|
|
- given(pollController.createPoll(testPoll2)).willReturn(null);
|
|
163
|
+ given(pollRepository.save(testPoll2)).willReturn(testPoll2);
|
149
|
164
|
|
150
|
165
|
MockHttpServletResponse response = mvc.perform(post("/polls/")
|
151
|
166
|
.content(asJsonString(testPoll2))
|
|
@@ -156,16 +171,18 @@ public class PollControllerTest {
|
156
|
171
|
Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
|
157
|
172
|
}
|
158
|
173
|
|
|
174
|
+ // =================== Update Poll =================== //
|
|
175
|
+
|
159
|
176
|
@Test
|
160
|
177
|
public void testUpdatePollSuccess() {
|
161
|
178
|
|
162
|
179
|
/* can't get it to work, rewriting */
|
163
|
180
|
}
|
164
|
181
|
|
165
|
|
- @Test(expected = ResourceNotFoundException.class)
|
|
182
|
+ @Test
|
166
|
183
|
public void testUpdatePollFail() throws Exception {
|
167
|
|
- given(pollController.updatePoll(testPoll, 100L))
|
168
|
|
- .willThrow(new ResourceNotFoundException());
|
|
184
|
+
|
|
185
|
+ given(pollRepository.findOne(BAD_ID)).willReturn(null);
|
169
|
186
|
|
170
|
187
|
MockHttpServletResponse response = mvc.perform(put("/polls/10101")
|
171
|
188
|
.content(asJsonString(testPoll))
|
|
@@ -176,23 +193,21 @@ public class PollControllerTest {
|
176
|
193
|
Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
|
177
|
194
|
}
|
178
|
195
|
|
|
196
|
+ // =================== Delete Poll =================== //
|
|
197
|
+
|
179
|
198
|
@Test
|
180
|
199
|
public void testDeletePollSuccess() throws Exception {
|
181
|
|
- given(pollController.deletePoll(testPoll.getId()))
|
182
|
|
- .willReturn(null);
|
|
200
|
+ given(pollRepository.findOne(1l))
|
|
201
|
+ .willReturn(new Poll());
|
183
|
202
|
|
184
|
|
- MockHttpServletResponse response = mvc.perform(delete("/polls")
|
185
|
|
- .content(asJsonString(testPoll))
|
186
|
|
- .contentType(MediaType.APPLICATION_JSON)
|
|
203
|
+ mvc.perform(delete("/polls/1")
|
187
|
204
|
.accept(MediaType.APPLICATION_JSON))
|
188
|
|
- .andReturn().getResponse();
|
189
|
|
-
|
190
|
|
- Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
|
|
205
|
+ .andExpect(status().isOk());
|
191
|
206
|
}
|
192
|
207
|
|
193
|
|
- @Test(expected = ResourceNotFoundException.class)
|
|
208
|
+ @Test
|
194
|
209
|
public void testDeletePollFail() throws Exception {
|
195
|
|
- given(pollController.deletePoll(400L))
|
|
210
|
+ given(pollRepository.findOne(BAD_ID))
|
196
|
211
|
.willThrow(new ResourceNotFoundException());
|
197
|
212
|
|
198
|
213
|
MockHttpServletResponse response = mvc.perform(delete("/polls/400")
|
|
@@ -202,4 +217,7 @@ public class PollControllerTest {
|
202
|
217
|
Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
|
203
|
218
|
|
204
|
219
|
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+ // =================== Test Exception Thrown =================== //
|
205
|
223
|
}
|