|
|
@@ -0,0 +1,106 @@
|
|
|
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.HttpStatus;
|
|
|
18
|
+import org.springframework.http.MediaType;
|
|
|
19
|
+import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
20
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
21
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
22
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
23
|
+
|
|
|
24
|
+import java.util.Arrays;
|
|
|
25
|
+
|
|
|
26
|
+import static org.mockito.BDDMockito.given;
|
|
|
27
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
28
|
+
|
|
|
29
|
+@SpringBootTest
|
|
|
30
|
+@RunWith(SpringRunner.class)
|
|
|
31
|
+@AutoConfigureMockMvc
|
|
|
32
|
+public class PollControllerTest {
|
|
|
33
|
+ @Autowired
|
|
|
34
|
+ private MockMvc mvc;
|
|
|
35
|
+ private Poll testPoll;
|
|
|
36
|
+ private Iterable<Poll> polls;
|
|
|
37
|
+
|
|
|
38
|
+ @Mock
|
|
|
39
|
+ private PollRepository pollRepository;
|
|
|
40
|
+
|
|
|
41
|
+ @InjectMocks
|
|
|
42
|
+ private PollController pollController;
|
|
|
43
|
+
|
|
|
44
|
+ private JacksonTester<Poll> pollJacksonTester;
|
|
|
45
|
+
|
|
|
46
|
+ @Before
|
|
|
47
|
+ public void setUp() {
|
|
|
48
|
+
|
|
|
49
|
+ pollController = new PollController(pollRepository);
|
|
|
50
|
+ JacksonTester.initFields(this, new ObjectMapper());
|
|
|
51
|
+
|
|
|
52
|
+ mvc = MockMvcBuilders.standaloneSetup(pollController)
|
|
|
53
|
+ .setControllerAdvice( new ResourceNotFoundException())
|
|
|
54
|
+ .build();
|
|
|
55
|
+
|
|
|
56
|
+ testPoll = new Poll();
|
|
|
57
|
+ pollController.createPoll(testPoll);
|
|
|
58
|
+
|
|
|
59
|
+ Poll[] pollArr = {testPoll};
|
|
|
60
|
+ polls = Arrays.asList(pollArr);
|
|
|
61
|
+
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ @Test
|
|
|
65
|
+ public void testGetPollByIdSuccess() throws Exception {
|
|
|
66
|
+
|
|
|
67
|
+ given(pollRepository.findOne(1L))
|
|
|
68
|
+ .willReturn(testPoll);
|
|
|
69
|
+
|
|
|
70
|
+ MockHttpServletResponse response = mvc.perform(get("/polls/1")
|
|
|
71
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
72
|
+ .andReturn().getResponse();
|
|
|
73
|
+
|
|
|
74
|
+ Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
|
|
75
|
+ Assert.assertEquals(response.getContentAsString(), testPoll.toString());
|
|
|
76
|
+ }
|
|
|
77
|
+
|
|
|
78
|
+ @Test
|
|
|
79
|
+ public void testGetAllPollsSuccess() throws Exception {
|
|
|
80
|
+
|
|
|
81
|
+ given(pollRepository.findAll())
|
|
|
82
|
+ .willReturn(polls);
|
|
|
83
|
+
|
|
|
84
|
+ MockHttpServletResponse response = mvc.perform(get("/polls")
|
|
|
85
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
86
|
+ .andReturn().getResponse();
|
|
|
87
|
+
|
|
|
88
|
+ Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
|
|
|
89
|
+ Assert.assertEquals(response.getContentAsString(), polls.toString());
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ @Test
|
|
|
93
|
+ public void testGetPollByIdFailException() throws Exception {
|
|
|
94
|
+
|
|
|
95
|
+ given(pollRepository.findOne(200L))
|
|
|
96
|
+ .willThrow(new ResourceNotFoundException());
|
|
|
97
|
+
|
|
|
98
|
+ MockHttpServletResponse response = mvc.perform(
|
|
|
99
|
+ get("/polls/200")
|
|
|
100
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
|
101
|
+ .andReturn().getResponse();
|
|
|
102
|
+
|
|
|
103
|
+ Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
|
|
|
104
|
+ Assert.assertTrue(response.getContentAsString().isEmpty());
|
|
|
105
|
+ }
|
|
|
106
|
+}
|