|
@@ -1,206 +1,206 @@
|
1
|
|
-//package io.zipcoder.tc_spring_poll_application.controller;
|
2
|
|
-//
|
3
|
|
-//import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
|
-//import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
5
|
|
-//import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
6
|
|
-//import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
7
|
|
-//import org.junit.Assert;
|
8
|
|
-//import org.junit.Before;
|
9
|
|
-//import org.junit.Test;
|
10
|
|
-//import org.junit.runner.RunWith;
|
11
|
|
-//import org.mockito.InjectMocks;
|
12
|
|
-//import org.mockito.Mock;
|
13
|
|
-//import org.springframework.beans.factory.annotation.Autowired;
|
14
|
|
-//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
15
|
|
-//import org.springframework.boot.test.context.SpringBootTest;
|
16
|
|
-//import org.springframework.boot.test.json.JacksonTester;
|
17
|
|
-//import org.springframework.http.HttpHeaders;
|
18
|
|
-//import org.springframework.http.HttpStatus;
|
19
|
|
-//import org.springframework.http.MediaType;
|
20
|
|
-//import org.springframework.http.ResponseEntity;
|
21
|
|
-//import org.springframework.mock.web.MockHttpServletResponse;
|
22
|
|
-//import org.springframework.test.context.junit4.SpringRunner;
|
23
|
|
-//import org.springframework.test.web.servlet.MockMvc;
|
24
|
|
-//import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
25
|
|
-//
|
26
|
|
-//import java.net.URI;
|
27
|
|
-//import java.util.Arrays;
|
28
|
|
-//
|
29
|
|
-//import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
|
30
|
|
-//import static org.hamcrest.Matchers.containsString;
|
31
|
|
-//import static org.mockito.BDDMockito.given;
|
32
|
|
-//import static org.mockito.Mockito.doNothing;
|
33
|
|
-//import static org.mockito.Mockito.times;
|
34
|
|
-//import static org.mockito.Mockito.verify;
|
35
|
|
-//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
36
|
|
-//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
37
|
|
-//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
38
|
|
-//
|
39
|
|
-//@SpringBootTest
|
40
|
|
-//@RunWith(SpringRunner.class)
|
41
|
|
-//@AutoConfigureMockMvc
|
42
|
|
-//public class PollControllerTest {
|
43
|
|
-// @Autowired
|
44
|
|
-// private MockMvc mvc;
|
45
|
|
-// private Poll testPoll;
|
46
|
|
-// private Iterable<Poll> polls;
|
47
|
|
-//
|
48
|
|
-// @Mock
|
49
|
|
-// private PollRepository pollRepository;
|
50
|
|
-//
|
51
|
|
-// @InjectMocks
|
52
|
|
-// private PollController pollController;
|
53
|
|
-//
|
54
|
|
-// private JacksonTester<Poll> pollJacksonTester;
|
55
|
|
-//
|
56
|
|
-// @Before
|
57
|
|
-// public void setUp() {
|
58
|
|
-//
|
59
|
|
-// pollController = new PollController(pollRepository);
|
60
|
|
-// JacksonTester.initFields(this, new ObjectMapper());
|
61
|
|
-//
|
62
|
|
-// mvc = MockMvcBuilders.standaloneSetup(pollController)
|
63
|
|
-// .setControllerAdvice( new ResourceNotFoundException())
|
64
|
|
-// .build();
|
65
|
|
-//
|
66
|
|
-// testPoll = new Poll();
|
67
|
|
-// pollController.createPoll(testPoll);
|
68
|
|
-//
|
69
|
|
-// Poll[] pollArr = {testPoll};
|
70
|
|
-// polls = Arrays.asList(pollArr);
|
71
|
|
-//
|
72
|
|
-// }
|
73
|
|
-//
|
74
|
|
-// @Test
|
75
|
|
-// public void testGetPollByIdSuccess() throws Exception {
|
76
|
|
-//
|
77
|
|
-// given(pollRepository.findOne(1L))
|
78
|
|
-// .willReturn(testPoll);
|
79
|
|
-//
|
80
|
|
-// MockHttpServletResponse response = mvc.perform(get("/polls/1")
|
81
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
82
|
|
-// .andReturn().getResponse();
|
83
|
|
-//
|
84
|
|
-// Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
85
|
|
-// Assert.assertEquals(response.getContentAsString(), testPoll.toString());
|
86
|
|
-// }
|
87
|
|
-//
|
88
|
|
-// @Test
|
89
|
|
-// public void testGetAllPollsSuccess() throws Exception {
|
90
|
|
-//
|
91
|
|
-// given(pollRepository.findAll())
|
92
|
|
-// .willReturn(polls);
|
93
|
|
-//
|
94
|
|
-// MockHttpServletResponse response = mvc.perform(get("/polls")
|
95
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
96
|
|
-// .andReturn().getResponse();
|
97
|
|
-//
|
98
|
|
-// Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
99
|
|
-// Assert.assertEquals(response.getContentAsString(), polls.toString());
|
100
|
|
-// }
|
101
|
|
-//
|
102
|
|
-// @Test
|
103
|
|
-// public void testGetPollByIdFailException() throws Exception {
|
104
|
|
-//
|
105
|
|
-// given(pollRepository.findOne(200L))
|
106
|
|
-// .willThrow(new ResourceNotFoundException());
|
107
|
|
-//
|
108
|
|
-// MockHttpServletResponse response = mvc.perform(
|
109
|
|
-// get("/polls/200")
|
110
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
111
|
|
-// .andReturn().getResponse();
|
112
|
|
-//
|
113
|
|
-// Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
|
114
|
|
-// Assert.assertTrue(response.getContentAsString().isEmpty());
|
115
|
|
-// }
|
116
|
|
-//
|
117
|
|
-// @Test /* This one is failing ??? mock the response better??, changed the
|
118
|
|
-// postURLtemplate URL */
|
119
|
|
-// public void testCreatePollSuccess() throws Exception {
|
120
|
|
-// Poll testPoll2 = new Poll();
|
121
|
|
-// String expected = "http://localhost/polls/2/";
|
122
|
|
-//
|
123
|
|
-// HttpHeaders responseHeader = new HttpHeaders();
|
124
|
|
-// responseHeader.setLocation(URI.create(expected));
|
125
|
|
-//
|
126
|
|
-// given(pollController.createPoll(testPoll2)).willReturn(null);
|
127
|
|
-//
|
128
|
|
-// MockHttpServletResponse response = mvc.perform(post("/polls/2")
|
129
|
|
-// .content(asJsonString(testPoll2))
|
130
|
|
-// .contentType(MediaType.APPLICATION_JSON)
|
131
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
132
|
|
-// .andReturn().getResponse();
|
133
|
|
-//
|
134
|
|
-// String header = response.getHeader("Location");
|
135
|
|
-//
|
136
|
|
-// Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
|
137
|
|
-// Assert.assertEquals(expected, header);
|
138
|
|
-//
|
139
|
|
-// // =============== Mockito Version to implement
|
140
|
|
-//
|
141
|
|
-//// doNothing().when(pollController).createPoll(testPoll2);
|
142
|
|
-//// mvc.perform(post("/polls/{id}",2)
|
143
|
|
-//// .contentType(MediaType.APPLICATION_JSON)
|
144
|
|
-//// .contentType(asJsonString(testPoll2)))
|
145
|
|
-//// .andExpect(status().isCreated())
|
146
|
|
-//// .andExpect(header().string("location", containsString("/polls/2")));
|
147
|
|
-////
|
148
|
|
-//// verify(pollController, times(1)).createPoll(testPoll2);
|
149
|
|
-// }
|
150
|
|
-//
|
151
|
|
-// @Test
|
152
|
|
-// public void testUpdatePollSuccess() throws Exception {
|
153
|
|
-// given(pollController.updatePoll(testPoll, 1L))
|
154
|
|
-// .willReturn(null);
|
155
|
|
-//
|
156
|
|
-// MockHttpServletResponse response = mvc.perform(put("/polls/1")
|
157
|
|
-// .content(asJsonString(testPoll))
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.controller;
|
|
2
|
+
|
|
3
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
5
|
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
6
|
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
7
|
+import org.junit.Assert;
|
|
8
|
+import org.junit.Before;
|
|
9
|
+import org.junit.Test;
|
|
10
|
+import org.junit.runner.RunWith;
|
|
11
|
+import org.mockito.InjectMocks;
|
|
12
|
+import org.mockito.Mock;
|
|
13
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
14
|
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
15
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
16
|
+import org.springframework.boot.test.json.JacksonTester;
|
|
17
|
+import org.springframework.http.HttpHeaders;
|
|
18
|
+import org.springframework.http.HttpStatus;
|
|
19
|
+import org.springframework.http.MediaType;
|
|
20
|
+import org.springframework.http.ResponseEntity;
|
|
21
|
+import org.springframework.mock.web.MockHttpServletResponse;
|
|
22
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
23
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
24
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
25
|
+
|
|
26
|
+import java.net.URI;
|
|
27
|
+import java.util.Arrays;
|
|
28
|
+
|
|
29
|
+import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
|
|
30
|
+import static org.hamcrest.Matchers.containsString;
|
|
31
|
+import static org.mockito.BDDMockito.given;
|
|
32
|
+import static org.mockito.Mockito.doNothing;
|
|
33
|
+import static org.mockito.Mockito.times;
|
|
34
|
+import static org.mockito.Mockito.verify;
|
|
35
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
36
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
|
37
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
38
|
+
|
|
39
|
+@SpringBootTest
|
|
40
|
+@RunWith(SpringRunner.class)
|
|
41
|
+@AutoConfigureMockMvc
|
|
42
|
+public class PollControllerTest {
|
|
43
|
+ @Autowired
|
|
44
|
+ private MockMvc mvc;
|
|
45
|
+ private Poll testPoll;
|
|
46
|
+ private Iterable<Poll> polls;
|
|
47
|
+
|
|
48
|
+ @Mock
|
|
49
|
+ private PollRepository pollRepository;
|
|
50
|
+
|
|
51
|
+ @InjectMocks
|
|
52
|
+ private PollController pollController;
|
|
53
|
+
|
|
54
|
+ private JacksonTester<Poll> pollJacksonTester;
|
|
55
|
+
|
|
56
|
+ @Before
|
|
57
|
+ public void setUp() {
|
|
58
|
+
|
|
59
|
+ pollController = new PollController(pollRepository);
|
|
60
|
+ JacksonTester.initFields(this, new ObjectMapper());
|
|
61
|
+
|
|
62
|
+ mvc = MockMvcBuilders.standaloneSetup(pollController)
|
|
63
|
+ .setControllerAdvice( new ResourceNotFoundException())
|
|
64
|
+ .build();
|
|
65
|
+
|
|
66
|
+ testPoll = new Poll();
|
|
67
|
+ pollController.createPoll(testPoll);
|
|
68
|
+
|
|
69
|
+ Poll[] pollArr = {testPoll};
|
|
70
|
+ polls = Arrays.asList(pollArr);
|
|
71
|
+
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ @Test
|
|
75
|
+ public void testGetPollByIdSuccess() throws Exception {
|
|
76
|
+
|
|
77
|
+ given(pollRepository.findOne(1L))
|
|
78
|
+ .willReturn(testPoll);
|
|
79
|
+
|
|
80
|
+ MockHttpServletResponse response = mvc.perform(get("/polls/1")
|
|
81
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
82
|
+ .andReturn().getResponse();
|
|
83
|
+
|
|
84
|
+ Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
|
85
|
+ Assert.assertEquals(response.getContentAsString(), testPoll.toString());
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ @Test
|
|
89
|
+ public void testGetAllPollsSuccess() throws Exception {
|
|
90
|
+
|
|
91
|
+ given(pollRepository.findAll())
|
|
92
|
+ .willReturn(polls);
|
|
93
|
+
|
|
94
|
+ MockHttpServletResponse response = mvc.perform(get("/polls")
|
|
95
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
96
|
+ .andReturn().getResponse();
|
|
97
|
+
|
|
98
|
+ Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
|
99
|
+ Assert.assertEquals(response.getContentAsString(), polls.toString());
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ @Test
|
|
103
|
+ public void testGetPollByIdFailException() throws Exception {
|
|
104
|
+
|
|
105
|
+ given(pollRepository.findOne(200L))
|
|
106
|
+ .willThrow(new ResourceNotFoundException());
|
|
107
|
+
|
|
108
|
+ MockHttpServletResponse response = mvc.perform(
|
|
109
|
+ get("/polls/200")
|
|
110
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
111
|
+ .andReturn().getResponse();
|
|
112
|
+
|
|
113
|
+ Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
|
|
114
|
+ Assert.assertTrue(response.getContentAsString().isEmpty());
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ @Test /* This one is failing ??? mock the response better??, changed the
|
|
118
|
+ postURLtemplate URL */
|
|
119
|
+ public void testCreatePollSuccess() throws Exception {
|
|
120
|
+ Poll testPoll2 = new Poll();
|
|
121
|
+ String expected = "http://localhost/polls/2/";
|
|
122
|
+
|
|
123
|
+ HttpHeaders responseHeader = new HttpHeaders();
|
|
124
|
+ responseHeader.setLocation(URI.create(expected));
|
|
125
|
+
|
|
126
|
+ given(pollController.createPoll(testPoll2)).willReturn(null);
|
|
127
|
+
|
|
128
|
+ MockHttpServletResponse response = mvc.perform(post("/polls/2")
|
|
129
|
+ .content(asJsonString(testPoll2))
|
|
130
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
131
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
132
|
+ .andReturn().getResponse();
|
|
133
|
+
|
|
134
|
+ String header = response.getHeader("Location");
|
|
135
|
+
|
|
136
|
+ Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
|
|
137
|
+ Assert.assertEquals(expected, header);
|
|
138
|
+
|
|
139
|
+ // =============== Mockito Version to implement
|
|
140
|
+
|
|
141
|
+// doNothing().when(pollController).createPoll(testPoll2);
|
|
142
|
+// mvc.perform(post("/polls/{id}",2)
|
158
|
143
|
// .contentType(MediaType.APPLICATION_JSON)
|
159
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
160
|
|
-// .andReturn().getResponse();
|
161
|
|
-//
|
162
|
|
-// Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
|
163
|
|
-// }
|
164
|
|
-//
|
|
144
|
+// .contentType(asJsonString(testPoll2)))
|
|
145
|
+// .andExpect(status().isCreated())
|
|
146
|
+// .andExpect(header().string("location", containsString("/polls/2")));
|
|
147
|
+//
|
|
148
|
+// verify(pollController, times(1)).createPoll(testPoll2);
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ @Test
|
|
152
|
+ public void testUpdatePollSuccess() throws Exception {
|
|
153
|
+ given(pollController.updatePoll(testPoll, 1L))
|
|
154
|
+ .willReturn(null);
|
|
155
|
+
|
|
156
|
+ MockHttpServletResponse response = mvc.perform(put("/polls/1")
|
|
157
|
+ .content(asJsonString(testPoll))
|
|
158
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
159
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
160
|
+ .andReturn().getResponse();
|
|
161
|
+
|
|
162
|
+ Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ @Test(expected = ResourceNotFoundException.class)
|
|
166
|
+ public void testUpdatePollFail() throws Exception {
|
|
167
|
+ given(pollController.updatePoll(testPoll, 100L))
|
|
168
|
+ .willThrow(new ResourceNotFoundException());
|
|
169
|
+
|
|
170
|
+ MockHttpServletResponse response = mvc.perform(put("/polls/10101")
|
|
171
|
+ .content(asJsonString(testPoll))
|
|
172
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
173
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
174
|
+ .andReturn().getResponse();
|
|
175
|
+
|
|
176
|
+ Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ @Test
|
|
180
|
+ public void testDeletePollSuccess() throws Exception {
|
|
181
|
+ given(pollController.updatePoll(testPoll, 1L))
|
|
182
|
+ .willReturn(null);
|
|
183
|
+
|
|
184
|
+ MockHttpServletResponse response = mvc.perform(delete("/polls/1")
|
|
185
|
+ .content(asJsonString(testPoll))
|
|
186
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
187
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
188
|
+ .andReturn().getResponse();
|
|
189
|
+
|
|
190
|
+ Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
|
|
191
|
+ }
|
|
192
|
+
|
165
|
193
|
// @Test(expected = ResourceNotFoundException.class)
|
166
|
|
-// public void testUpdatePollFail() throws Exception {
|
167
|
|
-// given(pollController.updatePoll(testPoll, 100L))
|
168
|
|
-// .willThrow(new ResourceNotFoundException());
|
169
|
|
-//
|
170
|
|
-// MockHttpServletResponse response = mvc.perform(put("/polls/10101")
|
171
|
|
-// .content(asJsonString(testPoll))
|
172
|
|
-// .contentType(MediaType.APPLICATION_JSON)
|
173
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
174
|
|
-// .andReturn().getResponse();
|
175
|
|
-//
|
176
|
|
-// Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
|
177
|
|
-// }
|
178
|
|
-//
|
179
|
|
-// @Test
|
180
|
|
-// public void testDeletePollSuccess() throws Exception {
|
181
|
|
-// given(pollController.updatePoll(testPoll, 1L))
|
182
|
|
-// .willReturn(null);
|
183
|
|
-//
|
184
|
|
-// MockHttpServletResponse response = mvc.perform(delete("/polls/1")
|
185
|
|
-// .content(asJsonString(testPoll))
|
186
|
|
-// .contentType(MediaType.APPLICATION_JSON)
|
187
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
188
|
|
-// .andReturn().getResponse();
|
189
|
|
-//
|
190
|
|
-// Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
|
191
|
|
-// }
|
192
|
|
-//
|
193
|
|
-//// @Test(expected = ResourceNotFoundException.class)
|
194
|
|
-// @Test
|
195
|
|
-// public void testDeletePollFail() throws Exception {
|
196
|
|
-// given(pollController.deletePoll(400L))
|
197
|
|
-// .willThrow(new ResourceNotFoundException());
|
198
|
|
-//
|
199
|
|
-// MockHttpServletResponse response = mvc.perform(delete("/polls/400")
|
200
|
|
-// .accept(MediaType.APPLICATION_JSON))
|
201
|
|
-// .andReturn().getResponse();
|
202
|
|
-//
|
203
|
|
-// Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
|
204
|
|
-//
|
205
|
|
-// }
|
206
|
|
-//}
|
|
194
|
+ @Test
|
|
195
|
+ public void testDeletePollFail() throws Exception {
|
|
196
|
+ given(pollController.deletePoll(400L))
|
|
197
|
+ .willThrow(new ResourceNotFoundException());
|
|
198
|
+
|
|
199
|
+ MockHttpServletResponse response = mvc.perform(delete("/polls/400")
|
|
200
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
201
|
+ .andReturn().getResponse();
|
|
202
|
+
|
|
203
|
+ Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
|
|
204
|
+
|
|
205
|
+ }
|
|
206
|
+}
|