Selaa lähdekoodia

Create new Vote

vvmk 8 vuotta sitten
vanhempi
commit
64901938ec

+ 56
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/VoteController.java Näytä tiedosto

@@ -0,0 +1,56 @@
1
+package io.zipcoder.tc_spring_poll_application.controllers;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
13
+
14
+/**
15
+ * project: spring-demo
16
+ * package: io.zipcoder.tc_spring_poll_application.controllers
17
+ * author: https://github.com/vvmk
18
+ * date: 4/6/18
19
+ */
20
+@RestController
21
+public class VoteController {
22
+    private VoteRepository voteRepository;
23
+
24
+    @Autowired
25
+    public VoteController(VoteRepository voteRepository) {
26
+        this.voteRepository = voteRepository;
27
+    }
28
+
29
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
30
+    public ResponseEntity<Vote> createVote(@PathVariable Long pollId, @RequestBody Vote vote) {
31
+        vote = voteRepository.save(vote);
32
+
33
+        //
34
+        URI newVoteUri;
35
+        try {
36
+            newVoteUri = ServletUriComponentsBuilder
37
+                    .fromCurrentRequest()
38
+                    .path("/{id}")
39
+                    .buildAndExpand(vote.getId())
40
+                    .toUri();
41
+
42
+        } catch (IllegalStateException ise) {
43
+            newVoteUri = ServletUriComponentsBuilder
44
+                    .fromUriString("http://roflmao:69/polls")
45
+                    .path("/{id}")
46
+                    .buildAndExpand(vote.getId())
47
+                    .toUri();
48
+        }
49
+
50
+        //
51
+        HttpHeaders responseHeaders = new HttpHeaders();
52
+        responseHeaders.setLocation(newVoteUri);
53
+
54
+        return new ResponseEntity<>(vote, responseHeaders, HttpStatus.CREATED);
55
+    }
56
+}

+ 49
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/controllers/VoteControllerTest.java Näytä tiedosto

@@ -0,0 +1,49 @@
1
+package io.zipcoder.tc_spring_poll_application.controllers;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+import org.mockito.InjectMocks;
8
+import org.mockito.Mock;
9
+import org.mockito.MockitoAnnotations;
10
+
11
+import static junit.framework.TestCase.assertEquals;
12
+import static org.mockito.ArgumentMatchers.any;
13
+import static org.mockito.Mockito.verify;
14
+import static org.mockito.Mockito.when;
15
+
16
+/**
17
+ * project: spring-demo
18
+ * package: io.zipcoder.tc_spring_poll_application.controllers
19
+ * author: https://github.com/vvmk
20
+ * date: 4/6/18
21
+ */
22
+public class VoteControllerTest {
23
+
24
+    @InjectMocks
25
+    private VoteController voteCtrl;
26
+
27
+    @Mock
28
+    private VoteRepository voteRepo;
29
+
30
+    @Before
31
+    public void setup() {
32
+        MockitoAnnotations.initMocks(this);
33
+
34
+        //create vote
35
+        when(voteRepo.save(any(Vote.class))).thenAnswer(inv -> inv.getArgument(0));
36
+    }
37
+
38
+    @Test
39
+    public void createVote() {
40
+        Vote v = new Vote();
41
+        long expected = 1;
42
+        v.setId(expected);
43
+
44
+        long actual = voteCtrl.createVote(1L, v).getBody().getId();
45
+
46
+        verify(voteRepo).save(any(Vote.class));
47
+        assertEquals(expected, actual);
48
+    }
49
+}