Parcourir la source

Revert "wip"

This reverts commit f884bac620719c470cb2161f6301fb5828fe81e8.
Trinh Tong il y a 7 ans
Parent
révision
fb4ede4c27

+ 17
- 16
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Voir le fichier

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
3 4
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4 5
 import io.zipcoder.tc_spring_poll_application.dtos.OptionCount;
5 6
 import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
@@ -48,22 +49,22 @@ public class ComputeResultController {
48 49
 //    ]
49 50
 //        }
50 51
 
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());
52
+        for (Vote v: allVotes) {
53
+            Long optionID = (v.getOption().getId());
54
+            OptionCount temp;
55
+            if (!optionCountsMap.keySet().contains(optionID)) {
56
+                temp = new OptionCount();
57
+                temp.setOptionId(optionID);
58
+                temp.setCount(1);
59
+                optionCountsMap.put(optionID,temp);
60
+            } else {
61
+                temp = optionCountsMap.get(optionID);
62
+                temp.setCount(temp.getCount()+1);
63
+                optionCountsMap.replace(optionID, temp);
64
+            }
65
+        }
66
+        voteResult.setTotalVotes((int)voteCount);
67
+        voteResult.setResults(optionCountsMap.values());
67 68
 
68 69
         //TODO: Implement algorithm to count votes
69 70
         return new ResponseEntity<>(voteResult, HttpStatus.OK);

+ 1
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Voir le fichier

@@ -48,6 +48,6 @@ public class Poll {
48 48
     public String toString() {
49 49
         return "{\"id\":" + this.id
50 50
                 + ",\"question\":" + question
51
-                + ",`\"options\":" + options + "}";
51
+                + ",\"options\":" + options + "}";
52 52
     }
53 53
 }

+ 27
- 27
src/test/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultControllerTest.java Voir le fichier

@@ -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 Voir le fichier

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

+ 143
- 143
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java Voir le fichier

@@ -1,143 +1,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.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.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
+    }