|
|
@@ -1,9 +1,11 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
2
|
|
|
3
|
3
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.domain.Option;
|
|
4
|
5
|
import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
5
|
6
|
import io.zipcoder.tc_spring_poll_application.domain.Vote;
|
|
6
|
7
|
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
|
8
|
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
7
|
9
|
import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
|
|
8
|
10
|
import org.junit.Assert;
|
|
9
|
11
|
import org.junit.Before;
|
|
|
@@ -11,41 +13,54 @@ import org.junit.Test;
|
|
11
|
13
|
import org.junit.runner.RunWith;
|
|
12
|
14
|
import org.mockito.InjectMocks;
|
|
13
|
15
|
import org.mockito.Mock;
|
|
|
16
|
+import org.mockito.MockitoAnnotations;
|
|
14
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
15
|
18
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
|
19
|
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|
16
|
20
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
17
|
21
|
import org.springframework.boot.test.json.JacksonTester;
|
|
|
22
|
+import org.springframework.boot.test.mock.mockito.MockBean;
|
|
18
|
23
|
import org.springframework.http.HttpStatus;
|
|
19
|
24
|
import org.springframework.http.MediaType;
|
|
20
|
25
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
21
|
26
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
22
|
27
|
import org.springframework.test.web.servlet.MockMvc;
|
|
23
|
28
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
29
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
30
|
+import org.springframework.web.context.request.RequestAttributes;
|
|
|
31
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
24
|
32
|
|
|
25
|
33
|
import java.util.Arrays;
|
|
26
|
34
|
import java.util.Collections;
|
|
|
35
|
+import java.util.List;
|
|
27
|
36
|
|
|
|
37
|
+import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
|
|
28
|
38
|
import static org.mockito.BDDMockito.given;
|
|
29
|
39
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
30
|
40
|
|
|
31
|
|
-@SpringBootTest
|
|
|
41
|
+//@SpringBootTest
|
|
32
|
42
|
@RunWith(SpringRunner.class)
|
|
33
|
|
-@AutoConfigureMockMvc
|
|
|
43
|
+@WebMvcTest
|
|
34
|
44
|
public class VoteControllerTest {
|
|
35
|
|
-
|
|
36
|
45
|
@Autowired
|
|
37
|
46
|
private MockMvc mvc;
|
|
|
47
|
+
|
|
38
|
48
|
private Vote testVote;
|
|
39
|
49
|
private Iterable<Vote> votes;
|
|
40
|
|
- private Long testId;
|
|
41
|
|
-
|
|
|
50
|
+ private final Long TEST_ID = 1L;
|
|
|
51
|
+ private final Long BAD_ID = 400L;
|
|
42
|
52
|
|
|
43
|
|
- @Mock
|
|
|
53
|
+ @MockBean
|
|
44
|
54
|
private VoteRepository voteRepository;
|
|
45
|
55
|
|
|
|
56
|
+ @MockBean
|
|
|
57
|
+ private PollRepository pollRepository;
|
|
|
58
|
+
|
|
46
|
59
|
@InjectMocks
|
|
47
|
60
|
private VoteController voteController;
|
|
48
|
61
|
|
|
|
62
|
+ private Poll testPoll;
|
|
|
63
|
+
|
|
49
|
64
|
@Before
|
|
50
|
65
|
public void setUp() {
|
|
51
|
66
|
|
|
|
@@ -57,60 +72,71 @@ public class VoteControllerTest {
|
|
57
|
72
|
.build();
|
|
58
|
73
|
|
|
59
|
74
|
testVote = new Vote();
|
|
60
|
|
- testId = 1L;
|
|
61
|
|
- testVote.setId(1L);
|
|
|
75
|
+ testVote.setId(TEST_ID);
|
|
62
|
76
|
|
|
63
|
77
|
Vote[] votearr = {testVote};
|
|
64
|
78
|
votes = Arrays.asList(votearr);
|
|
65
|
79
|
|
|
|
80
|
+ testPoll = new Poll();
|
|
|
81
|
+ testPoll.setId(TEST_ID);
|
|
66
|
82
|
}
|
|
67
|
83
|
|
|
68
|
84
|
@Test
|
|
69
|
85
|
public void testGetVotePollIdSuccess() throws Exception {
|
|
70
|
86
|
|
|
71
|
|
-// given(voteRepository.findVotesByPoll(testId))
|
|
72
|
|
-// .willReturn(votes);
|
|
73
|
|
-//
|
|
74
|
|
-// MockHttpServletResponse response = mvc.perform(get("polls/1/votes")
|
|
75
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
|
76
|
|
-// .andReturn().getResponse();
|
|
77
|
|
-//
|
|
78
|
|
-// Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
|
79
|
|
-// Assert.assertEquals(response.getContentAsString(), testVote.toString());
|
|
|
87
|
+ given(voteRepository.findVotesByPoll(TEST_ID))
|
|
|
88
|
+ .willReturn(votes);
|
|
|
89
|
+
|
|
|
90
|
+ MockHttpServletResponse response = mvc.perform(get("/polls/1/votes")
|
|
|
91
|
+ .content(asJsonString(votes))
|
|
|
92
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
93
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
94
|
+ .andReturn().getResponse();
|
|
|
95
|
+
|
|
|
96
|
+ Assert.assertEquals(HttpStatus.OK.value(), response.getStatus());
|
|
|
97
|
+ Assert.assertEquals(response.getContentAsString(), votes.toString());
|
|
80
|
98
|
}
|
|
81
|
99
|
|
|
|
100
|
+// @Test(expected = ResourceNotFoundException.class)
|
|
82
|
101
|
@Test
|
|
83
|
102
|
public void testGetVoteIdFail() throws Exception {
|
|
|
103
|
+ given(voteRepository.findVotesByPoll(BAD_ID))
|
|
|
104
|
+ .willThrow(new ResourceNotFoundException());
|
|
84
|
105
|
|
|
85
|
|
- given(voteRepository.findAll())
|
|
86
|
|
- .willReturn(null);
|
|
|
106
|
+ MockHttpServletResponse response = mvc.perform(get("/polls/500/votes")
|
|
|
107
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
108
|
+ .andReturn().getResponse();
|
|
|
109
|
+
|
|
|
110
|
+ System.out.println(response.getStatus());
|
|
|
111
|
+
|
|
|
112
|
+ Assert.assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatus());
|
|
|
113
|
+ }
|
|
87
|
114
|
|
|
88
|
|
- MockHttpServletResponse response = mvc.perform(get("polls/200/votes")
|
|
|
115
|
+ @Test
|
|
|
116
|
+ public void testGetAllVotes() throws Exception {
|
|
|
117
|
+ given(voteController.getAllVotes())
|
|
|
118
|
+ .willReturn(votes);
|
|
|
119
|
+
|
|
|
120
|
+ MockHttpServletResponse response = mvc.perform(get("/polls/votes")
|
|
89
|
121
|
.accept(MediaType.APPLICATION_JSON))
|
|
90
|
122
|
.andReturn().getResponse();
|
|
91
|
123
|
|
|
92
|
|
- Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
|
|
124
|
+ Assert.assertEquals(response.getStatus(), HttpStatus.OK.value());
|
|
93
|
125
|
Assert.assertEquals(response.getContentAsString(), votes.toString());
|
|
94
|
126
|
}
|
|
95
|
127
|
|
|
96
|
|
-// @Test
|
|
97
|
|
-// public void testGetAllVotes() throws Exception {
|
|
98
|
|
-//
|
|
99
|
|
-// given(voteRepository.findOne(200L))
|
|
100
|
|
-// .willThrow(new ResourceNotFoundException());
|
|
101
|
|
-//
|
|
102
|
|
-// MockHttpServletResponse response = mvc.perform(
|
|
103
|
|
-// get("polls/votes")
|
|
104
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
|
105
|
|
-// .andReturn().getResponse();
|
|
106
|
|
-//
|
|
107
|
|
-// Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
|
|
108
|
|
-// Assert.assertTrue(response.getContentAsString().isEmpty());
|
|
109
|
|
-// }
|
|
110
|
|
-
|
|
111
|
128
|
@Test
|
|
112
|
|
- public void testGetAllVotesFail() {
|
|
|
129
|
+ public void testGetAllVotesFail() throws Exception {
|
|
|
130
|
+ given(voteController.getAllVotes())
|
|
|
131
|
+ .willThrow(new ResourceNotFoundException());
|
|
|
132
|
+
|
|
|
133
|
+ MockHttpServletResponse response = mvc.perform(
|
|
|
134
|
+ get("polls/votes")
|
|
|
135
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
136
|
+ .andReturn().getResponse();
|
|
113
|
137
|
|
|
|
138
|
+ Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
|
|
|
139
|
+ Assert.assertTrue(response.getContentAsString().isEmpty());
|
|
114
|
140
|
}
|
|
115
|
141
|
|
|
116
|
142
|
@Test
|