Trinh Tong 6 anos atrás
pai
commit
1afbd7787d

+ 1
- 1
src/main/java/com/example/bakerylab/BakerylabApplication.java Ver arquivo

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

+ 6
- 3
src/main/java/com/example/bakerylab/controllers/BakerController.java Ver arquivo

53
 
53
 
54
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
54
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
55
     public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
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
     public ResponseEntity<Baker> verfiyBaker(Long id) {
64
     public ResponseEntity<Baker> verfiyBaker(Long id) {

+ 3
- 0
src/main/java/com/example/bakerylab/models/Baker.java Ver arquivo

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

+ 3
- 0
src/main/java/com/example/bakerylab/models/Muffin.java Ver arquivo

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

+ 16
- 6
src/test/java/com/example/bakerylab/controllers/BakerControllerTest.java Ver arquivo

22
 
22
 
23
 import static com.example.bakerylab.JsonUtil.asJsonString;
23
 import static com.example.bakerylab.JsonUtil.asJsonString;
24
 import static org.mockito.BDDMockito.given;
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
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29
 
27
 
30
 
28
 
55
                 .build();
53
                 .build();
56
 
54
 
57
         froilan = new Baker("Froilan", "Master Baker", "Croissants");
55
         froilan = new Baker("Froilan", "Master Baker", "Croissants");
56
+        froilan.setId(1L);
58
     }
57
     }
59
 
58
 
60
     // ================= Create Baker ================= //
59
     // ================= Create Baker ================= //
157
 
156
 
158
     // ================= Delete Baker ================= //
157
     // ================= Delete Baker ================= //
159
 
158
 
160
-
161
     @Test
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
     @Test
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
 }