ソースを参照

more testing, changed controller methods

Trinh Tong 6 年 前
コミット
e345aef405

+ 26
- 6
src/main/java/com/example/bakerylab/controllers/BakerController.java ファイルの表示

@@ -10,11 +10,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
10 10
 import org.springframework.web.bind.annotation.RequestMethod;
11 11
 import org.springframework.web.bind.annotation.RestController;
12 12
 
13
+import javax.validation.constraints.Null;
14
+
13 15
 @RestController
14 16
 public class BakerController {
15 17
 
18
+    private final BakerRepository bakerRepository;
19
+
16 20
     @Autowired
17
-    private BakerRepository bakerRepository;
21
+    public BakerController(BakerRepository bakerRepository) {
22
+        this.bakerRepository = bakerRepository;
23
+    }
18 24
 
19 25
     @RequestMapping(value = "/bakers", method = RequestMethod.GET)
20 26
     public ResponseEntity<Iterable<Baker>> index() {
@@ -23,7 +29,7 @@ public class BakerController {
23 29
 
24 30
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.GET)
25 31
     public ResponseEntity<Baker> show(@PathVariable Long id) {
26
-        return new ResponseEntity<>(this.bakerRepository.findById(id).get(), HttpStatus.OK);
32
+        return verfiyBaker(id);
27 33
     }
28 34
 
29 35
     @RequestMapping(value = "/bakers", method = RequestMethod.POST)
@@ -33,17 +39,31 @@ public class BakerController {
33 39
 
34 40
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.PUT)
35 41
     public ResponseEntity<Baker> update(@PathVariable Long id, Baker baker) {
36
-        Baker foundBaker = bakerRepository.findById(id).get();
37 42
 
38
-        foundBaker.setName(baker.getName());
39
-        foundBaker.setSpecialty(baker.getSpecialty());
43
+        if (verfiyBaker(id).getStatusCode().value() != HttpStatus.NOT_FOUND.value()) {
44
+            Baker foundBaker = bakerRepository.findById(id).get();
45
+            foundBaker.setName(baker.getName());
46
+            foundBaker.setSpecialty(baker.getSpecialty());
40 47
 
41
-        return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
48
+            return new ResponseEntity<>(this.bakerRepository.save(foundBaker), HttpStatus.OK);
49
+        } else {
50
+            return verfiyBaker(id);
51
+        }
42 52
     }
43 53
 
44 54
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
45 55
     public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
56
+        verfiyBaker(id);
46 57
         this.bakerRepository.delete(bakerRepository.findById(id).get());
47 58
         return new ResponseEntity<>(true, HttpStatus.OK);
48 59
     }
60
+
61
+    public ResponseEntity<Baker> verfiyBaker(Long id) {
62
+        try {
63
+            bakerRepository.findById(id).get().getId();
64
+        } catch (NullPointerException npe) {
65
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
66
+        }
67
+        return new ResponseEntity<>(bakerRepository.findById(id).get(), HttpStatus.OK);
68
+    }
49 69
 }

+ 5
- 1
src/main/java/com/example/bakerylab/controllers/MuffinController.java ファイルの表示

@@ -13,8 +13,12 @@ import org.springframework.web.bind.annotation.RestController;
13 13
 @RestController
14 14
 public class MuffinController {
15 15
 
16
+    private final MuffinRepository muffinRepository;
17
+
16 18
     @Autowired
17
-    private MuffinRepository muffinRepository;
19
+    public MuffinController(MuffinRepository muffinRepository) {
20
+        this.muffinRepository = muffinRepository;
21
+    }
18 22
 
19 23
     @RequestMapping(value = "/muffins", method = RequestMethod.GET)
20 24
     public ResponseEntity<Iterable<Muffin>> index() {

+ 71
- 11
src/test/java/com/example/bakerylab/controllers/BakerControllerTest.java ファイルの表示

@@ -10,27 +10,32 @@ import org.mockito.MockitoAnnotations;
10 10
 import org.springframework.beans.factory.annotation.Autowired;
11 11
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12 12
 import org.springframework.boot.test.mock.mockito.MockBean;
13
+import org.springframework.http.HttpEntity;
14
+import org.springframework.http.HttpStatus;
13 15
 import org.springframework.http.MediaType;
14 16
 import org.springframework.test.context.junit4.SpringRunner;
15 17
 import org.springframework.test.web.servlet.MockMvc;
16
-import org.springframework.test.web.servlet.MvcResult;
17 18
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18 19
 
20
+import java.util.ArrayList;
19 21
 import java.util.Optional;
20 22
 
21 23
 import static com.example.bakerylab.JsonUtil.asJsonString;
22 24
 import static org.mockito.BDDMockito.given;
23 25
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24 26
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
25 28
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26 29
 
27 30
 
28 31
 @RunWith(SpringRunner.class)
29 32
 @WebMvcTest(BakerController.class)
30 33
 public class BakerControllerTest {
34
+
31 35
     // === Instance Variables === //
32 36
     private Baker froilan;
33 37
     private final String GOOD_URL = "/bakers";
38
+    private final String BAD_URL = "/bakers500";
34 39
 
35 40
     @Autowired
36 41
     MockMvc mvc;
@@ -50,7 +55,6 @@ public class BakerControllerTest {
50 55
                 .build();
51 56
 
52 57
         froilan = new Baker("Froilan", "Master Baker", "Croissants");
53
-
54 58
     }
55 59
 
56 60
     // ================= Create Baker ================= //
@@ -70,15 +74,13 @@ public class BakerControllerTest {
70 74
     @Test
71 75
     public void testCreateBakerFail() throws Exception {
72 76
         Baker b = new Baker ("Seth", "froilans prepper", "muffins");
73
-        String badUrl = "/bakers500";
74 77
         given(bakerRepository.save(b)).willReturn(b);
75 78
 
76
-        mvc.perform(post(badUrl)
79
+        mvc.perform(post(BAD_URL)
77 80
                 .content(asJsonString(froilan))
78 81
                 .contentType(MediaType.APPLICATION_JSON)
79 82
                 .accept(MediaType.APPLICATION_JSON))
80 83
                 .andExpect(status().isNotFound());
81
-
82 84
     }
83 85
 
84 86
     // ================= Get Baker ================= //
@@ -89,22 +91,80 @@ public class BakerControllerTest {
89 91
 
90 92
         given(bakerRepository.findById(bakerId)).willReturn(Optional.of(froilan));
91 93
 
92
-        MvcResult response = mvc.perform(get(GOOD_URL)
94
+        mvc.perform(get(GOOD_URL)
93 95
                 .accept(MediaType.APPLICATION_JSON))
94
-                .andExpect(status().isOk())
95
-                .andReturn();
96
+                .andExpect(status().isOk());
97
+    }
98
+
99
+    @Test
100
+    public void getBakerIdFail() throws Exception {
101
+        Long bakerId = 500L;
96 102
 
97
-        System.out.println(response.getResponse().getContentAsString());
98
-//        response.getResponse().getContentAsString();
103
+        given(bakerRepository.findById(bakerId)).willReturn(null);
99 104
 
105
+        mvc.perform(get(GOOD_URL + "/{id}", bakerId)
106
+                .accept(MediaType.APPLICATION_JSON))
107
+                .andExpect(status().isNotFound());
100 108
     }
101 109
 
102
-
103 110
     // ================= Get Bakers ================= //
104 111
 
112
+    @Test
113
+    public void getAllBakersSuccess() throws Exception {
114
+        ArrayList<Baker> bakers = new ArrayList<>();
115
+        bakers.add(froilan);
116
+        given(bakerRepository.findAll()).willReturn(bakers);
117
+
118
+        mvc.perform(get(GOOD_URL))
119
+                .andExpect(status().isOk());
120
+    }
121
+
122
+    @Test
123
+    public void getAllBakersFail() throws Exception {
124
+
125
+        given(bakerRepository.findAll()).willReturn(null);
126
+
127
+        mvc.perform(get(BAD_URL))
128
+                .andExpect(status().isNotFound());
129
+    }
130
+
105 131
     // ================= Update Baker ================= //
106 132
 
133
+    @Test
134
+    public void updateBakerSuccess() throws Exception {
135
+        froilan.setId(500L);
136
+        given(bakerRepository.findById(froilan.getId())).willReturn(Optional.ofNullable(froilan));
137
+
138
+
139
+        mvc.perform(put("/bakers/{id}", froilan.getId())
140
+                .content(asJsonString(froilan))
141
+                .contentType(MediaType.APPLICATION_JSON))
142
+                .andExpect(status().isOk());
143
+    }
144
+
145
+    @Test
146
+    public void updateBakerFail() throws Exception {
147
+        froilan.setId(500L);
148
+
149
+        given(bakerRepository.findById(froilan.getId())).willReturn(null);
150
+
151
+
152
+        mvc.perform(put("/bakers/{id}", froilan.getId())
153
+                .contentType(MediaType.APPLICATION_JSON)
154
+                .content(asJsonString(froilan)))
155
+                .andExpect(status().isNotFound());
156
+    }
157
+
107 158
     // ================= Delete Baker ================= //
108 159
 
109 160
 
161
+    @Test
162
+    public void deleteBakerSuccess() {
163
+
164
+    }
165
+
166
+    @Test
167
+    public void deleteBakerFail() {
168
+
169
+    }
110 170
 }