소스 검색

GET one poll by pollId

vvmk 8 년 전
부모
커밋
dff4fa02f0

+ 10
- 7
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java 파일 보기

@@ -6,14 +6,11 @@ import org.springframework.beans.factory.annotation.Autowired;
6 6
 import org.springframework.http.HttpHeaders;
7 7
 import org.springframework.http.HttpStatus;
8 8
 import org.springframework.http.ResponseEntity;
9
-import org.springframework.web.bind.annotation.RequestBody;
10
-import org.springframework.web.bind.annotation.RequestMapping;
11
-import org.springframework.web.bind.annotation.RequestMethod;
12
-import org.springframework.web.bind.annotation.RestController;
9
+import org.springframework.web.bind.annotation.*;
13 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
14
-import org.springframework.web.util.UriComponentsBuilder;
15 11
 
16 12
 import java.net.URI;
13
+import java.util.Optional;
17 14
 
18 15
 /**
19 16
  * project: spring-demo
@@ -21,6 +18,7 @@ import java.net.URI;
21 18
  * author: https://github.com/vvmk
22 19
  * date: 4/5/18
23 20
  */
21
+
24 22
 @RestController
25 23
 public class PollController {
26 24
 
@@ -57,10 +55,15 @@ public class PollController {
57 55
                     .toUri();
58 56
         }
59 57
 
60
-
61 58
         HttpHeaders headers = new HttpHeaders();
62 59
         headers.setLocation(newPollUri);
63 60
 
64 61
         return new ResponseEntity<>(createdPoll, headers, HttpStatus.CREATED);
65 62
     }
66
-}
63
+
64
+    @RequestMapping(name = "/polls/{pollId}", method = RequestMethod.GET)
65
+    public ResponseEntity<Poll> getPoll(@PathVariable Long pollId) {
66
+        Optional<Poll> poll = pollRepository.findById(pollId);
67
+        return new ResponseEntity<>(poll.orElse(new Poll()), HttpStatus.OK);
68
+    }
69
+}

+ 15
- 10
src/test/java/io/zipcoder/tc_spring_poll_application/controllers/PollControllerTest.java 파일 보기

@@ -6,27 +6,20 @@ import org.junit.Before;
6 6
 import org.junit.Test;
7 7
 import org.mockito.InjectMocks;
8 8
 import org.mockito.Mock;
9
-import org.mockito.Mockito;
10 9
 import org.mockito.MockitoAnnotations;
11 10
 import org.springframework.http.HttpStatus;
12
-import org.springframework.http.ResponseEntity;
13
-import org.springframework.test.web.servlet.MockMvc;
14
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15 11
 import org.springframework.util.Assert;
16 12
 
17
-import javax.servlet.ServletContext;
18
-import javax.servlet.http.HttpServletRequest;
19
-import javax.servlet.http.HttpServletResponse;
20 13
 import java.util.ArrayList;
21 14
 import java.util.List;
15
+import java.util.Optional;
22 16
 import java.util.regex.Pattern;
23 17
 
24 18
 import static org.junit.Assert.assertEquals;
25 19
 import static org.junit.Assert.assertTrue;
26 20
 import static org.mockito.ArgumentMatchers.any;
27
-import static org.mockito.Mockito.mock;
28
-import static org.mockito.Mockito.verify;
29
-import static org.mockito.Mockito.when;
21
+import static org.mockito.ArgumentMatchers.anyLong;
22
+import static org.mockito.Mockito.*;
30 23
 
31 24
 /**
32 25
  * project: spring-demo
@@ -51,6 +44,9 @@ public class PollControllerTest {
51 44
         // GET all polls
52 45
         when(pollRepo.findAll()).thenReturn(polls);
53 46
 
47
+        // GET one poll
48
+        when(pollRepo.findById(anyLong())).thenReturn(Optional.of(new Poll()));
49
+
54 50
         //POST create poll
55 51
         when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
56 52
 
@@ -101,4 +97,13 @@ public class PollControllerTest {
101 97
         verify(pollRepo).save(any(Poll.class));
102 98
         assertTrue(expected.matcher(actual).find());
103 99
     }
100
+
101
+    @Test
102
+    public void getPollReturnsPoll() {
103
+        HttpStatus expected = HttpStatus.OK;
104
+        HttpStatus actual = pollCtrl.getPoll(anyLong()).getStatusCode();
105
+
106
+        verify(pollRepo).findById(anyLong());
107
+        assertEquals(expected, actual);
108
+    }
104 109
 }