Trinh Tong преди 6 години
родител
ревизия
1afbd7787d

+ 1
- 1
src/main/java/com/example/bakerylab/BakerylabApplication.java Целия файл

@@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication;
4 4
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 5
 
6 6
 @SpringBootApplication
7
-public class BakerylabApplication {
7
+public class  BakerylabApplication {
8 8
 
9 9
     public static void main(String[] args) {
10 10
         SpringApplication.run(BakerylabApplication.class, args);

+ 6
- 3
src/main/java/com/example/bakerylab/controllers/BakerController.java Целия файл

@@ -53,9 +53,12 @@ public class BakerController {
53 53
 
54 54
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
55 55
     public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
56
-        verfiyBaker(id);
57
-        this.bakerRepository.delete(bakerRepository.findById(id).get());
58
-        return new ResponseEntity<>(true, HttpStatus.OK);
56
+//        if (verfiyBaker(id).getStatusCode().value() != HttpStatus.NOT_FOUND.value()) {
57
+            this.bakerRepository.delete(bakerRepository.findById(id).get());
58
+            return new ResponseEntity<>(true, HttpStatus.OK);
59
+//        } else {
60
+//            return new ResponseEntity<>(false, HttpStatus.NOT_FOUND);
61
+//        }
59 62
     }
60 63
 
61 64
     public ResponseEntity<Baker> verfiyBaker(Long id) {

+ 3
- 0
src/main/java/com/example/bakerylab/models/Baker.java Целия файл

@@ -18,6 +18,9 @@ public class Baker {
18 18
     @Column(name = "Specialty")
19 19
     private String specialty;
20 20
 
21
+    public Baker() {
22
+    }
23
+
21 24
     public Baker(String name, String employeeId, String specialty) {
22 25
         this.name = name;
23 26
         this.employeeId = employeeId;

+ 3
- 0
src/main/java/com/example/bakerylab/models/Muffin.java Целия файл

@@ -12,6 +12,9 @@ public class Muffin {
12 12
     @Column(name = "FLAVOR")
13 13
     private String flavor;
14 14
 
15
+    public Muffin() {
16
+    }
17
+
15 18
     public Muffin(String flavor) {
16 19
         this.flavor = flavor;
17 20
     }

+ 16
- 6
src/test/java/com/example/bakerylab/controllers/BakerControllerTest.java Целия файл

@@ -22,9 +22,7 @@ import java.util.Optional;
22 22
 
23 23
 import static com.example.bakerylab.JsonUtil.asJsonString;
24 24
 import static org.mockito.BDDMockito.given;
25
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
25
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
28 26
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29 27
 
30 28
 
@@ -55,6 +53,7 @@ public class BakerControllerTest {
55 53
                 .build();
56 54
 
57 55
         froilan = new Baker("Froilan", "Master Baker", "Croissants");
56
+        froilan.setId(1L);
58 57
     }
59 58
 
60 59
     // ================= Create Baker ================= //
@@ -157,14 +156,25 @@ public class BakerControllerTest {
157 156
 
158 157
     // ================= Delete Baker ================= //
159 158
 
160
-
161 159
     @Test
162
-    public void deleteBakerSuccess() {
160
+    public void deleteBakerSuccess() throws Exception {
161
+
162
+        given(bakerRepository.findById(froilan.getId()))
163
+                .willReturn(Optional.ofNullable(froilan));
163 164
 
165
+        mvc.perform(delete("/bakers/{id}", froilan.getId())
166
+                .accept(MediaType.APPLICATION_JSON))
167
+                .andExpect(status().isOk());
164 168
     }
165 169
 
166 170
     @Test
167
-    public void deleteBakerFail() {
171
+    public void deleteBakerFail() throws Exception {
168 172
 
173
+        given(bakerRepository.findById(froilan.getId()))
174
+                .willReturn(null);
175
+
176
+        mvc.perform(delete("bakers/{id}", froilan.getId())
177
+                .accept(MediaType.APPLICATION_JSON))
178
+                .andExpect(status().isNotFound());
169 179
     }
170 180
 }