Selaa lähdekoodia

Create ResourceNotFoundException and verifyPolls

vvmk 8 vuotta sitten
vanhempi
commit
e19520cca4

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

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controllers;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpHeaders;
@@ -81,4 +82,9 @@ public class PollController {
81 82
         pollRepository.deleteById(pollId);
82 83
         return new ResponseEntity<>(null, HttpStatus.OK);
83 84
     }
85
+
86
+    protected void verifyPoll(Long pollId) throws ResourceNotFoundException {
87
+        if (!pollRepository.existsById(pollId))
88
+            throw new ResourceNotFoundException();
89
+    }
84 90
 }

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Näytä tiedosto

@@ -0,0 +1,25 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.web.bind.annotation.ResponseStatus;
5
+
6
+/**
7
+ * project: spring-demo
8
+ * package: io.zipcoder.tc_spring_poll_application.exception
9
+ * author: https://github.com/vvmk
10
+ * date: 4/7/18
11
+ */
12
+@ResponseStatus(HttpStatus.NOT_FOUND)
13
+public class ResourceNotFoundException extends RuntimeException {
14
+    public ResourceNotFoundException() {
15
+        super();
16
+    }
17
+
18
+    public ResourceNotFoundException(String message) {
19
+        super(message);
20
+    }
21
+
22
+    public ResourceNotFoundException(String message, Throwable throwable) {
23
+        super(message, throwable);
24
+    }
25
+}

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

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controllers;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 6
 import org.junit.Before;
6 7
 import org.junit.Test;
@@ -15,6 +16,7 @@ import java.util.List;
15 16
 import java.util.Optional;
16 17
 import java.util.regex.Pattern;
17 18
 
19
+import static junit.framework.TestCase.fail;
18 20
 import static org.junit.Assert.assertEquals;
19 21
 import static org.junit.Assert.assertTrue;
20 22
 import static org.mockito.ArgumentMatchers.any;
@@ -51,6 +53,9 @@ public class PollControllerTest {
51 53
         // PUT update poll
52 54
         when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
53 55
 
56
+        when(pollRepo.existsById(10L)).thenReturn(true);
57
+        when(pollRepo.existsById(999L)).thenReturn(false);
58
+
54 59
 //        when(pollRepo::save)
55 60
 //                .onSuccess(HttpStatus.CREATED)
56 61
 //                .onFail(HttpStatus.BAD_REQUEST);
@@ -125,4 +130,21 @@ public class PollControllerTest {
125 130
         verify(pollRepo).deleteById(anyLong());
126 131
         assertEquals(expected, actual);
127 132
     }
133
+
134
+    @Test
135
+    public void testVerifyPoll() {
136
+
137
+        Long exists = 10L;
138
+
139
+        try {
140
+            pollCtrl.verifyPoll(exists);
141
+        } catch (ResourceNotFoundException rnfe) {
142
+            fail();
143
+        }
144
+    }
145
+
146
+    @Test(expected = ResourceNotFoundException.class)
147
+    public void testVerifyPollNotFound() {
148
+        pollCtrl.verifyPoll(999L);
149
+    }
128 150
 }