Browse Source

json util class

Trinh Tong 5 years ago
parent
commit
833a1668f4

+ 1
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java View File

@@ -38,8 +38,7 @@ public class ComputeResultController {
38 38
         long voteCount = StreamSupport.stream(allVotes.spliterator(), false).count();
39 39
         HashMap<Long, OptionCount> optionCountsMap = new HashMap<>();
40 40
 
41
-        for (Vote v: allVotes
42
-             ) {
41
+        for (Vote v: allVotes) {
43 42
             Long optionID = (v.getOption().getId());
44 43
             if (!optionCountsMap.keySet().contains(optionID)) {
45 44
                 optionCountsMap.put(optionID, new OptionCount());

+ 14
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/JsonTestUtilities.java View File

@@ -0,0 +1,14 @@
1
+package io.zipcoder.tc_spring_poll_application;
2
+
3
+import com.fasterxml.jackson.databind.ObjectMapper;
4
+
5
+public class JsonTestUtilities {
6
+    public static String asJsonString(final Object obj) {
7
+        try {
8
+            final ObjectMapper mapper = new ObjectMapper();
9
+            return mapper.writeValueAsString(obj);
10
+        } catch (Exception e) {
11
+            throw new RuntimeException(e);
12
+        }
13
+    }
14
+}

+ 28
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java View File

@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
14 14
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15 15
 import org.springframework.boot.test.context.SpringBootTest;
16 16
 import org.springframework.boot.test.json.JacksonTester;
17
+import org.springframework.http.HttpHeaders;
17 18
 import org.springframework.http.HttpStatus;
18 19
 import org.springframework.http.MediaType;
19 20
 import org.springframework.mock.web.MockHttpServletResponse;
@@ -21,10 +22,14 @@ import org.springframework.test.context.junit4.SpringRunner;
21 22
 import org.springframework.test.web.servlet.MockMvc;
22 23
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
23 24
 
25
+import java.net.URI;
24 26
 import java.util.Arrays;
25 27
 
28
+import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
26 29
 import static org.mockito.BDDMockito.given;
30
+import static org.mockito.Matchers.endsWith;
27 31
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
28 33
 
29 34
 @SpringBootTest
30 35
 @RunWith(SpringRunner.class)
@@ -103,4 +108,27 @@ public class PollControllerTest {
103 108
         Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
104 109
         Assert.assertTrue(response.getContentAsString().isEmpty());
105 110
     }
111
+
112
+    @Test /* This one is failing */
113
+    public void testCreatePollSuccess() throws Exception {
114
+        Poll testPoll2 = new Poll();
115
+        String expected = "http://localhost:8080/polls/2";
116
+
117
+        HttpHeaders responseHeader =  new HttpHeaders();
118
+        responseHeader.setLocation(URI.create(expected));
119
+
120
+        given(pollController.createPoll(testPoll2)).willReturn(null);
121
+
122
+        MockHttpServletResponse response = mvc.perform(post("/polls")
123
+                .content(asJsonString(testPoll2))
124
+                .contentType(MediaType.APPLICATION_JSON)
125
+                .accept(MediaType.APPLICATION_JSON))
126
+                .andReturn().getResponse();
127
+
128
+        String header = response.getHeader("Location");
129
+
130
+        Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
131
+        Assert.assertEquals(expected, header);
132
+    }
133
+
106 134
 }