Browse Source

setup voteController test

Trinh Tong 5 years ago
parent
commit
92aad5c621

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

@@ -10,6 +10,7 @@ import org.springframework.http.ResponseEntity;
10 10
 import org.springframework.web.bind.annotation.*;
11 11
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 12
 
13
+import javax.validation.constraints.Null;
13 14
 import java.net.URI;
14 15
 
15 16
 @RestController
@@ -50,12 +51,18 @@ public class PollController {
50 51
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
51 52
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52 53
         // Save the entity
54
+        verifyPoll(poll);
53 55
         Poll p = pollRepository.save(poll);
54 56
         return new ResponseEntity<>(HttpStatus.OK);
55 57
     }
56 58
 
57 59
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
58 60
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        try {
62
+            pollRepository.findOne(pollId);
63
+        } catch (NullPointerException nullpointer) {
64
+            throw new ResourceNotFoundException("Poll Id was not found.");
65
+        }
59 66
         pollRepository.delete(pollId);
60 67
         return new ResponseEntity<>(HttpStatus.OK);
61 68
     }

+ 60
- 6
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java View File

@@ -27,9 +27,7 @@ import java.util.Arrays;
27 27
 
28 28
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
29 29
 import static org.mockito.BDDMockito.given;
30
-import static org.mockito.Matchers.endsWith;
31
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
30
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
33 31
 
34 32
 @SpringBootTest
35 33
 @RunWith(SpringRunner.class)
@@ -109,17 +107,18 @@ public class PollControllerTest {
109 107
         Assert.assertTrue(response.getContentAsString().isEmpty());
110 108
     }
111 109
 
112
-    @Test /* This one is failing */
110
+    @Test /* This one is failing ??? mock the response better??, changed the
111
+     postURLtemplate URL */
113 112
     public void testCreatePollSuccess() throws Exception {
114 113
         Poll testPoll2 = new Poll();
115
-        String expected = "http://localhost:8080/polls/2";
114
+        String expected = "http://localhost/polls/2/";
116 115
 
117 116
         HttpHeaders responseHeader =  new HttpHeaders();
118 117
         responseHeader.setLocation(URI.create(expected));
119 118
 
120 119
         given(pollController.createPoll(testPoll2)).willReturn(null);
121 120
 
122
-        MockHttpServletResponse response = mvc.perform(post("/polls")
121
+        MockHttpServletResponse response = mvc.perform(post("/polls/2")
123 122
                 .content(asJsonString(testPoll2))
124 123
                 .contentType(MediaType.APPLICATION_JSON)
125 124
                 .accept(MediaType.APPLICATION_JSON))
@@ -131,4 +130,59 @@ public class PollControllerTest {
131 130
         Assert.assertEquals(expected, header);
132 131
     }
133 132
 
133
+    @Test
134
+    public void testUpdatePollSuccess() throws Exception {
135
+        given(pollController.updatePoll(testPoll, 1L))
136
+                .willReturn(null);
137
+
138
+        MockHttpServletResponse response = mvc.perform(put("/polls/1")
139
+                .content(asJsonString(testPoll))
140
+                .contentType(MediaType.APPLICATION_JSON)
141
+                .accept(MediaType.APPLICATION_JSON))
142
+                .andReturn().getResponse();
143
+
144
+        Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
145
+    }
146
+
147
+    @Test(expected = ResourceNotFoundException.class)
148
+    public void testUpdatePollFail() throws Exception {
149
+        given(pollController.updatePoll(testPoll, 100L))
150
+                .willThrow(new ResourceNotFoundException());
151
+
152
+        MockHttpServletResponse response = mvc.perform(put("/polls/10101")
153
+                .content(asJsonString(testPoll))
154
+                .contentType(MediaType.APPLICATION_JSON)
155
+                .accept(MediaType.APPLICATION_JSON))
156
+                .andReturn().getResponse();
157
+
158
+        Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
159
+    }
160
+
161
+    @Test
162
+    public void testDeletePollSuccess() throws Exception {
163
+        given(pollController.updatePoll(testPoll, 1L))
164
+                .willReturn(null);
165
+
166
+        MockHttpServletResponse response = mvc.perform(delete("/polls/1")
167
+                .content(asJsonString(testPoll))
168
+                .contentType(MediaType.APPLICATION_JSON)
169
+                .accept(MediaType.APPLICATION_JSON))
170
+                .andReturn().getResponse();
171
+
172
+        Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
173
+    }
174
+
175
+//    @Test(expected = ResourceNotFoundException.class)
176
+    @Test
177
+    public void testDeletePollFail() throws Exception {
178
+        given(pollController.deletePoll(400L))
179
+                .willThrow(new ResourceNotFoundException());
180
+
181
+        MockHttpServletResponse response = mvc.perform(delete("/polls/400")
182
+                .accept(MediaType.APPLICATION_JSON))
183
+                .andReturn().getResponse();
184
+
185
+        Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
186
+
187
+    }
134 188
 }

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

@@ -0,0 +1,113 @@
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.domain.Vote;
6
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
7
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
8
+import org.junit.Assert;
9
+import org.junit.Before;
10
+import org.junit.Test;
11
+import org.junit.runner.RunWith;
12
+import org.mockito.InjectMocks;
13
+import org.mockito.Mock;
14
+import org.springframework.beans.factory.annotation.Autowired;
15
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
16
+import org.springframework.boot.test.context.SpringBootTest;
17
+import org.springframework.boot.test.json.JacksonTester;
18
+import org.springframework.http.HttpHeaders;
19
+import org.springframework.http.HttpStatus;
20
+import org.springframework.http.MediaType;
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
+
28
+import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
29
+import static org.mockito.BDDMockito.given;
30
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
31
+
32
+@SpringBootTest
33
+@RunWith(SpringRunner.class)
34
+@AutoConfigureMockMvc
35
+public class VoteControllerTest {
36
+    @Autowired
37
+    private MockMvc mvc;
38
+    private Vote testVote;
39
+    private Iterable<Vote> votes;
40
+
41
+    @Mock
42
+    private VoteRepository voteRepository;
43
+
44
+    @InjectMocks
45
+    private VoteController voteController;
46
+
47
+    @Before
48
+    public void setUp() {
49
+
50
+        voteController = new VoteController(voteRepository);
51
+        JacksonTester.initFields(this, new ObjectMapper());
52
+
53
+        mvc = MockMvcBuilders.standaloneSetup(voteController)
54
+                .setControllerAdvice( new ResourceNotFoundException())
55
+                .build();
56
+
57
+        testVote = new Vote();
58
+
59
+    }
60
+
61
+    @Test
62
+    public void testGetVoteIdSuccess() throws Exception {
63
+
64
+        given(voteRepository.findOne(1L))
65
+                .willReturn(testVote);
66
+
67
+        MockHttpServletResponse response = mvc.perform(get("/polls/1")
68
+                .accept(MediaType.APPLICATION_JSON))
69
+                .andReturn().getResponse();
70
+
71
+        Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
72
+        Assert.assertEquals(response.getContentAsString(), testVote.toString());
73
+    }
74
+
75
+    @Test
76
+    public void testGetVoteIdFail() throws Exception {
77
+
78
+        given(voteRepository.findAll())
79
+                .willReturn(null);
80
+
81
+        MockHttpServletResponse response = mvc.perform(get("/polls")
82
+                .accept(MediaType.APPLICATION_JSON))
83
+                .andReturn().getResponse();
84
+
85
+        Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
86
+        Assert.assertEquals(response.getContentAsString(), votes.toString());
87
+    }
88
+
89
+    @Test
90
+    public void testGetAllVotes() throws Exception {
91
+
92
+        given(voteRepository.findOne(200L))
93
+                .willThrow(new ResourceNotFoundException());
94
+
95
+        MockHttpServletResponse response = mvc.perform(
96
+                get("/polls/200")
97
+                        .accept(MediaType.APPLICATION_JSON))
98
+                .andReturn().getResponse();
99
+
100
+        Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
101
+        Assert.assertTrue(response.getContentAsString().isEmpty());
102
+    }
103
+
104
+    @Test
105
+    public void testGetAllVotesFail() {
106
+
107
+    }
108
+
109
+    @Test
110
+    public void testCreateVoteSuccess() {
111
+
112
+    }
113
+}