Преглед изворни кода

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

This reverts commit 7b432ab76b347e642d6f7eea972d9f26fb0800ea.
Trinh Tong пре 5 година
родитељ
комит
70a730c026

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Прегледај датотеку

@@ -38,11 +38,23 @@ public class ComputeResultController {
38 38
         long voteCount = StreamSupport.stream(allVotes.spliterator(), false).count();
39 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 52
         for (Vote v: allVotes) {
42 53
             Long optionID = (v.getOption().getId());
43 54
             OptionCount temp;
44 55
             if (!optionCountsMap.keySet().contains(optionID)) {
45 56
                 temp = new OptionCount();
57
+                temp.setOptionId(optionID);
46 58
                 temp.setCount(1);
47 59
                 optionCountsMap.put(optionID,temp);
48 60
             } else {

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Прегледај датотеку

@@ -9,6 +9,6 @@ public interface VoteRepository extends CrudRepository<Vote, Long> {
9 9
             "FROM Option o, Vote v " +
10 10
             "WHERE o.POLL_ID = ?1 " +
11 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 Прегледај датотеку

@@ -118,32 +118,34 @@ public class PollControllerTest {
118 118
      postURLtemplate URL */
119 119
     public void testCreatePollSuccess() throws Exception {
120 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 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 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 151
     @Test

+ 0
- 15
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java Прегледај датотеку

@@ -140,19 +140,4 @@ public class VoteControllerTest {
140 140
 
141 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
     }