Trinh Tong 7 лет назад
Родитель
Сommit
f5e6edfb61

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Просмотреть файл

@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestParam;
14 14
 import org.springframework.web.bind.annotation.RestController;
15 15
 
16 16
 import java.util.HashMap;
17
+import java.util.stream.StreamSupport;
17 18
 
18 19
 @RestController
19 20
 public class ComputeResultController {
@@ -34,12 +35,11 @@ public class ComputeResultController {
34 35
         // increase the optioncount count field accordingly for each vote
35 36
 
36 37
         // create Collection Option Counts
37
-        int voteCount = 0;
38
+        long voteCount = StreamSupport.stream(allVotes.spliterator(), false).count();
38 39
         HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
39 40
 
40 41
         for (Vote v: allVotes
41 42
              ) {
42
-            voteCount++;
43 43
             Long optionID = (v.getOption().getId());
44 44
             if (!optionCountsMap.keySet().contains(optionID)) {
45 45
                 optionCountsMap.put(optionID, new OptionCount());
@@ -51,7 +51,7 @@ public class ComputeResultController {
51 51
             }
52 52
         }
53 53
 
54
-        voteResult.setTotalVotes(voteCount);
54
+        voteResult.setTotalVotes((int)voteCount);
55 55
         voteResult.setResults(optionCountsMap.values());
56 56
 
57 57
         //TODO: Implement algorithm to count votes

+ 1
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

@@ -14,7 +14,7 @@ import java.net.URI;
14 14
 
15 15
 @RestController
16 16
 public class PollController {
17
-    PollRepository pollRepository;
17
+    private PollRepository pollRepository;
18 18
 
19 19
     @Autowired
20 20
     public PollController(PollRepository pollRepository) {

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Просмотреть файл

@@ -43,4 +43,11 @@ public class Poll {
43 43
     public void setOptions(Set<Option> options) {
44 44
         this.options = options;
45 45
     }
46
+
47
+    @Override
48
+    public String toString() {
49
+        return "{\"id\":" + this.id
50
+                + ",\"question\":" + question
51
+                + ",\"options\":" + options + "}";
52
+    }
46 53
 }

+ 106
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java Просмотреть файл

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