Explorar el Código

Return new entity uri on poll POST

Postman tests show the uri works. The goofy try/catch
is because I'm not sure how to mock the ServletContext in order
to unit test the HttpHeaders. This way at least it runs and
I can test the uri params (the important part anyway)
vvmk hace 8 años
padre
commit
a5ac3ee70b

+ 27
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java Ver fichero

@@ -3,12 +3,17 @@ package io.zipcoder.tc_spring_poll_application.controllers;
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4 4
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
7 8
 import org.springframework.http.ResponseEntity;
8 9
 import org.springframework.web.bind.annotation.RequestBody;
9 10
 import org.springframework.web.bind.annotation.RequestMapping;
10 11
 import org.springframework.web.bind.annotation.RequestMethod;
11 12
 import org.springframework.web.bind.annotation.RestController;
13
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
14
+import org.springframework.web.util.UriComponentsBuilder;
15
+
16
+import java.net.URI;
12 17
 
13 18
 /**
14 19
  * project: spring-demo
@@ -34,8 +39,28 @@ public class PollController {
34 39
 
35 40
     @RequestMapping(name = "/polls", method = RequestMethod.POST)
36 41
     public ResponseEntity<Poll> createPoll(@RequestBody Poll poll) {
37
-        Poll responseBody = pollRepository.save(poll);
42
+        Poll createdPoll = pollRepository.save(poll);
43
+
44
+        URI newPollUri;
45
+        try {
46
+            newPollUri = ServletUriComponentsBuilder
47
+                    .fromCurrentRequest()
48
+                    .path("/{id}")
49
+                    .buildAndExpand(createdPoll.getId())
50
+                    .toUri();
51
+
52
+        } catch (IllegalStateException ise) {
53
+            newPollUri = ServletUriComponentsBuilder
54
+                    .fromUriString("http://roflmao:69/polls")
55
+                    .path("/{id}")
56
+                    .buildAndExpand(createdPoll.getId())
57
+                    .toUri();
58
+        }
59
+
60
+
61
+        HttpHeaders headers = new HttpHeaders();
62
+        headers.setLocation(newPollUri);
38 63
 
39
-        return new ResponseEntity<>(responseBody, HttpStatus.CREATED);
64
+        return new ResponseEntity<>(createdPoll, headers, HttpStatus.CREATED);
40 65
     }
41 66
 }

+ 20
- 1
src/test/java/io/zipcoder/tc_spring_poll_application/controllers/PollControllerTest.java Ver fichero

@@ -10,12 +10,19 @@ import org.mockito.Mockito;
10 10
 import org.mockito.MockitoAnnotations;
11 11
 import org.springframework.http.HttpStatus;
12 12
 import org.springframework.http.ResponseEntity;
13
+import org.springframework.test.web.servlet.MockMvc;
14
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
13 15
 import org.springframework.util.Assert;
14 16
 
17
+import javax.servlet.ServletContext;
18
+import javax.servlet.http.HttpServletRequest;
19
+import javax.servlet.http.HttpServletResponse;
15 20
 import java.util.ArrayList;
16 21
 import java.util.List;
22
+import java.util.regex.Pattern;
17 23
 
18 24
 import static org.junit.Assert.assertEquals;
25
+import static org.junit.Assert.assertTrue;
19 26
 import static org.mockito.ArgumentMatchers.any;
20 27
 import static org.mockito.Mockito.mock;
21 28
 import static org.mockito.Mockito.verify;
@@ -36,6 +43,7 @@ public class PollControllerTest {
36 43
     private PollRepository pollRepo;
37 44
 
38 45
     private List<Poll> polls = new ArrayList<>();
46
+    private MockMvc mockMvc;
39 47
 
40 48
     @Before
41 49
     public void setup() {
@@ -47,7 +55,6 @@ public class PollControllerTest {
47 55
         //POST create poll
48 56
         when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
49 57
 
50
-//        would be nice...
51 58
 //        when(pollRepo::save)
52 59
 //                .onSuccess(HttpStatus.CREATED)
53 60
 //                .onFail(HttpStatus.BAD_REQUEST);
@@ -83,4 +90,16 @@ public class PollControllerTest {
83 90
         verify(pollRepo).save(any(Poll.class));
84 91
         assertEquals(expected.getId(), actual.getId());
85 92
     }
93
+
94
+    @Test
95
+    public void createPollReturnsLocation() {
96
+        Poll p = new Poll();
97
+        p.setId(1L);
98
+
99
+        Pattern expected = Pattern.compile("/polls/1");
100
+        String actual = pollCtrl.createPoll(p).getHeaders().getLocation().toString();
101
+
102
+        verify(pollRepo).save(any(Poll.class));
103
+        assertTrue(expected.matcher(actual).find());
104
+    }
86 105
 }