Browse Source

Revert "Revert "compute result worked and then stopped working""

This reverts commit 7b432ab76b347e642d6f7eea972d9f26fb0800ea.
Trinh Tong 7 years ago
parent
commit
70a730c026

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java View File

38
         long voteCount = StreamSupport.stream(allVotes.spliterator(), false).count();
38
         long voteCount = StreamSupport.stream(allVotes.spliterator(), false).count();
39
         HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
39
         HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
40
 
40
 
41
+        // Ok now it's grabbing all the votes, BUT the option ID is null??
42
+//        {
43
+//            "totalVotes": 3,
44
+//                "results": [
45
+//            {
46
+//                "optionId": null,
47
+//                    "count": 3
48
+//            }
49
+//    ]
50
+//        }
51
+
41
         for (Vote v: allVotes) {
52
         for (Vote v: allVotes) {
42
             Long optionID = (v.getOption().getId());
53
             Long optionID = (v.getOption().getId());
43
             OptionCount temp;
54
             OptionCount temp;
44
             if (!optionCountsMap.keySet().contains(optionID)) {
55
             if (!optionCountsMap.keySet().contains(optionID)) {
45
                 temp = new OptionCount();
56
                 temp = new OptionCount();
57
+                temp.setOptionId(optionID);
46
                 temp.setCount(1);
58
                 temp.setCount(1);
47
                 optionCountsMap.put(optionID,temp);
59
                 optionCountsMap.put(optionID,temp);
48
             } else {
60
             } else {

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java View File

9
             "FROM Option o, Vote v " +
9
             "FROM Option o, Vote v " +
10
             "WHERE o.POLL_ID = ?1 " +
10
             "WHERE o.POLL_ID = ?1 " +
11
             "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
11
             "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
12
-    Iterable<Vote> findVotesByPoll(Long pollId);
13
-
12
+    public Iterable<Vote> findVotesByPoll(Long pollId);
14
 }
13
 }
14
+

+ 20
- 18
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java View File

118
      postURLtemplate URL */
118
      postURLtemplate URL */
119
     public void testCreatePollSuccess() throws Exception {
119
     public void testCreatePollSuccess() throws Exception {
120
         Poll testPoll2 = new Poll();
120
         Poll testPoll2 = new Poll();
121
-//        String expected = "http://localhost/polls/2/";
121
+        String expected = "http://localhost/polls/2/";
122
 
122
 
123
-//        HttpHeaders responseHeader =  new HttpHeaders();
124
-//        responseHeader.setLocation(URI.create(expected));
123
+        HttpHeaders responseHeader =  new HttpHeaders();
124
+        responseHeader.setLocation(URI.create(expected));
125
 
125
 
126
         given(pollController.createPoll(testPoll2)).willReturn(null);
126
         given(pollController.createPoll(testPoll2)).willReturn(null);
127
-//        doNothing().when(pollController).createPoll(testPoll2);
128
-
129
-//        MockHttpServletResponse response = mvc.perform(post("/polls/2")
130
-//                .content(asJsonString(testPoll2))
131
-//                .contentType(MediaType.APPLICATION_JSON)
132
-//                .accept(MediaType.APPLICATION_JSON))
133
-//                .andReturn().getResponse();
134
 
127
 
135
-        mvc.perform(post("/polls/{id}",2)
128
+        MockHttpServletResponse response = mvc.perform(post("/polls/2")
129
+                .content(asJsonString(testPoll2))
136
                 .contentType(MediaType.APPLICATION_JSON)
130
                 .contentType(MediaType.APPLICATION_JSON)
137
-                .contentType(asJsonString(testPoll2)))
138
-                .andExpect(status().isCreated())
139
-                .andExpect(header().string("location", containsString("/polls/2")));
131
+                .accept(MediaType.APPLICATION_JSON))
132
+                .andReturn().getResponse();
140
 
133
 
141
-        verify(pollController, times(1)).createPoll(testPoll2);
134
+        String header = response.getHeader("Location");
142
 
135
 
143
-//        String header = response.getHeader("Location");
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")));
144
 //
147
 //
145
-//        Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
146
-//        Assert.assertEquals(expected, header);
148
+//        verify(pollController, times(1)).createPoll(testPoll2);
147
     }
149
     }
148
 
150
 
149
     @Test
151
     @Test

+ 0
- 15
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java View File

140
 
140
 
141
         verify(voteController, times(1)).createVote(pollId, testVote2);
141
         verify(voteController, times(1)).createVote(pollId, testVote2);
142
     }
142
     }
143
-//
144
-//    @Test
145
-//    public void test_create_user_fail_404_not_found() throws Exception {
146
-//        User user = new User("username exists");
147
-//
148
-//        when(userService.exists(user)).thenReturn(true);
149
-//
150
-//        mockMvc.perform(
151
-//                post("/users")
152
-//                        .contentType(MediaType.APPLICATION_JSON)
153
-//                        .content(asJsonString(user)))
154
-//                .andExpect(status().isConflict());
155
-//
156
-//        verify(userService, times(1)).exists(user);
157
-//        verifyNoMoreInteractions(userService);
158
     }
143
     }