Pārlūkot izejas kodu

found problem, databse being weird and generating some weird polls

Trinh Tong 5 gadus atpakaļ
vecāks
revīzija
bbe292a69e

+ 16
- 16
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Parādīt failu

@@ -48,22 +48,22 @@ public class ComputeResultController {
48 48
 //    ]
49 49
 //        }
50 50
 
51
-//        for (Vote v: allVotes) {
52
-//            Long optionID = (v.getOption().getId());
53
-//            OptionCount temp;
54
-//            if (!optionCountsMap.keySet().contains(optionID)) {
55
-//                temp = new OptionCount();
56
-//                temp.setOptionId(optionID);
57
-//                temp.setCount(1);
58
-//                optionCountsMap.put(optionID,temp);
59
-//            } else {
60
-//                temp = optionCountsMap.get(optionID);
61
-//                temp.setCount(temp.getCount()+1);
62
-//                optionCountsMap.replace(optionID, temp);
63
-//            }
64
-//        }
65
-//        voteResult.setTotalVotes((int)voteCount);
66
-//        voteResult.setResults(optionCountsMap.values());
51
+        for (Vote v: allVotes) {
52
+            Long optionID = (v.getOption().getId());
53
+            OptionCount temp;
54
+            if (!optionCountsMap.keySet().contains(optionID)) {
55
+                temp = new OptionCount();
56
+                temp.setOptionId(optionID);
57
+                temp.setCount(1);
58
+                optionCountsMap.put(optionID,temp);
59
+            } else {
60
+                temp = optionCountsMap.get(optionID);
61
+                temp.setCount(temp.getCount()+1);
62
+                optionCountsMap.replace(optionID, temp);
63
+            }
64
+        }
65
+        voteResult.setTotalVotes((int)voteCount);
66
+        voteResult.setResults(optionCountsMap.values());
67 67
 
68 68
         //TODO: Implement algorithm to count votes
69 69
         return new ResponseEntity<>(voteResult, HttpStatus.OK);

+ 4
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Parādīt failu

@@ -39,7 +39,7 @@ public class PollController {
39 39
         HttpHeaders headers = new HttpHeaders();
40 40
         headers.setLocation(newPollUri);
41 41
         poll = pollRepository.save(poll);
42
-        return new ResponseEntity<>(headers, HttpStatus.CREATED);
42
+        return new ResponseEntity<>(poll, headers, HttpStatus.CREATED);
43 43
     }
44 44
 
45 45
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
@@ -64,10 +64,10 @@ public class PollController {
64 64
         return new ResponseEntity<>(HttpStatus.OK);
65 65
     }
66 66
 
67
-
68
-    @RequestMapping(method = RequestMethod.GET)
67
+//    @RequestMapping(method = RequestMethod.GET)
69 68
     public void verifyPoll(Long pollId) throws ResourceNotFoundException {
70
-        if (pollRepository.findOne(pollId) == null) {
69
+        Poll verify = pollRepository.findOne(pollId);
70
+        if (verify == null) {
71 71
             throw new ResourceNotFoundException();
72 72
         }
73 73
     }

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Parādīt failu

@@ -9,15 +9,15 @@ public class Poll {
9 9
     @Id
10 10
     @GeneratedValue
11 11
     @Column(name = "POLL_ID")
12
-    Long id;
12
+    private Long id;
13 13
 
14 14
     @Column(name = "QUESTION")
15
-    String question;
15
+    private String question;
16 16
 
17 17
     @OneToMany(cascade = CascadeType.ALL)
18 18
     @JoinColumn(name = "POLL_ID")
19 19
     @OrderBy
20
-    Set<Option> options;
20
+    private Set<Option> options;
21 21
 
22 22
 
23 23
     public Long getId() {

+ 27
- 27
src/test/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultControllerTest.java Parādīt failu

@@ -1,27 +1,27 @@
1
-//package io.zipcoder.tc_spring_poll_application.controller;
2
-//
3
-//import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
4
-//import org.junit.Before;
5
-//import org.junit.Test;
6
-//import org.springframework.boot.test.mock.mockito.MockBean;
7
-//
8
-//import static org.junit.Assert.*;
9
-//
10
-//
11
-//public class ComputeResultControllerTest {
12
-//    private ComputeResultController computeResultController;
13
-//
14
-//    @MockBean
15
-//    private VoteRepository voteRepository;
16
-//
17
-//    @Before
18
-//    public void setUp(){
19
-//        computeResultController = new ComputeResultController(voteRepository);
20
-//    }
21
-//
22
-//    @Test
23
-//    public void testComputeResults() {
24
-//        voteRepository.findVotesByPoll(1L);
25
-//        computeResultController.computeResult(1L);
26
-//    }
27
-//}
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+import org.springframework.boot.test.mock.mockito.MockBean;
7
+
8
+import static org.junit.Assert.*;
9
+
10
+
11
+public class ComputeResultControllerTest {
12
+    private ComputeResultController computeResultController;
13
+
14
+    @MockBean
15
+    private VoteRepository voteRepository;
16
+
17
+    @Before
18
+    public void setUp(){
19
+        computeResultController = new ComputeResultController(voteRepository);
20
+    }
21
+
22
+    @Test
23
+    public void testComputeResults() {
24
+        voteRepository.findVotesByPoll(1L);
25
+        computeResultController.computeResult(1L);
26
+    }
27
+}

+ 204
- 204
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java Parādīt failu

@@ -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
+}

+ 134
- 143
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java Parādīt failu

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