Ver código fonte

more testing, changed controller methods

Trinh Tong 6 anos atrás
pai
commit
e345aef405

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

10
 import org.springframework.web.bind.annotation.RequestMethod;
10
 import org.springframework.web.bind.annotation.RequestMethod;
11
 import org.springframework.web.bind.annotation.RestController;
11
 import org.springframework.web.bind.annotation.RestController;
12
 
12
 
13
+import javax.validation.constraints.Null;
14
+
13
 @RestController
15
 @RestController
14
 public class BakerController {
16
 public class BakerController {
15
 
17
 
18
+    private final BakerRepository bakerRepository;
19
+
16
     @Autowired
20
     @Autowired
17
-    private BakerRepository bakerRepository;
21
+    public BakerController(BakerRepository bakerRepository) {
22
+        this.bakerRepository = bakerRepository;
23
+    }
18
 
24
 
19
     @RequestMapping(value = "/bakers", method = RequestMethod.GET)
25
     @RequestMapping(value = "/bakers", method = RequestMethod.GET)
20
     public ResponseEntity<Iterable<Baker>> index() {
26
     public ResponseEntity<Iterable<Baker>> index() {
23
 
29
 
24
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.GET)
30
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.GET)
25
     public ResponseEntity<Baker> show(@PathVariable Long id) {
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
     @RequestMapping(value = "/bakers", method = RequestMethod.POST)
35
     @RequestMapping(value = "/bakers", method = RequestMethod.POST)
33
 
39
 
34
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.PUT)
40
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.PUT)
35
     public ResponseEntity<Baker> update(@PathVariable Long id, Baker baker) {
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
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
54
     @RequestMapping(value = "/bakers/{id}", method = RequestMethod.DELETE)
45
     public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
55
     public ResponseEntity<Boolean> destroy(@PathVariable Long id) {
56
+        verfiyBaker(id);
46
         this.bakerRepository.delete(bakerRepository.findById(id).get());
57
         this.bakerRepository.delete(bakerRepository.findById(id).get());
47
         return new ResponseEntity<>(true, HttpStatus.OK);
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 Ver arquivo

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

+ 71
- 11
src/test/java/com/example/bakerylab/controllers/BakerControllerTest.java Ver arquivo

10
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12
 import org.springframework.boot.test.mock.mockito.MockBean;
12
 import org.springframework.boot.test.mock.mockito.MockBean;
13
+import org.springframework.http.HttpEntity;
14
+import org.springframework.http.HttpStatus;
13
 import org.springframework.http.MediaType;
15
 import org.springframework.http.MediaType;
14
 import org.springframework.test.context.junit4.SpringRunner;
16
 import org.springframework.test.context.junit4.SpringRunner;
15
 import org.springframework.test.web.servlet.MockMvc;
17
 import org.springframework.test.web.servlet.MockMvc;
16
-import org.springframework.test.web.servlet.MvcResult;
17
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18
 
19
 
20
+import java.util.ArrayList;
19
 import java.util.Optional;
21
 import java.util.Optional;
20
 
22
 
21
 import static com.example.bakerylab.JsonUtil.asJsonString;
23
 import static com.example.bakerylab.JsonUtil.asJsonString;
22
 import static org.mockito.BDDMockito.given;
24
 import static org.mockito.BDDMockito.given;
23
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
25
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
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.result.MockMvcResultMatchers.status;
28
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26
 
29
 
27
 
30
 
28
 @RunWith(SpringRunner.class)
31
 @RunWith(SpringRunner.class)
29
 @WebMvcTest(BakerController.class)
32
 @WebMvcTest(BakerController.class)
30
 public class BakerControllerTest {
33
 public class BakerControllerTest {
34
+
31
     // === Instance Variables === //
35
     // === Instance Variables === //
32
     private Baker froilan;
36
     private Baker froilan;
33
     private final String GOOD_URL = "/bakers";
37
     private final String GOOD_URL = "/bakers";
38
+    private final String BAD_URL = "/bakers500";
34
 
39
 
35
     @Autowired
40
     @Autowired
36
     MockMvc mvc;
41
     MockMvc mvc;
50
                 .build();
55
                 .build();
51
 
56
 
52
         froilan = new Baker("Froilan", "Master Baker", "Croissants");
57
         froilan = new Baker("Froilan", "Master Baker", "Croissants");
53
-
54
     }
58
     }
55
 
59
 
56
     // ================= Create Baker ================= //
60
     // ================= Create Baker ================= //
70
     @Test
74
     @Test
71
     public void testCreateBakerFail() throws Exception {
75
     public void testCreateBakerFail() throws Exception {
72
         Baker b = new Baker ("Seth", "froilans prepper", "muffins");
76
         Baker b = new Baker ("Seth", "froilans prepper", "muffins");
73
-        String badUrl = "/bakers500";
74
         given(bakerRepository.save(b)).willReturn(b);
77
         given(bakerRepository.save(b)).willReturn(b);
75
 
78
 
76
-        mvc.perform(post(badUrl)
79
+        mvc.perform(post(BAD_URL)
77
                 .content(asJsonString(froilan))
80
                 .content(asJsonString(froilan))
78
                 .contentType(MediaType.APPLICATION_JSON)
81
                 .contentType(MediaType.APPLICATION_JSON)
79
                 .accept(MediaType.APPLICATION_JSON))
82
                 .accept(MediaType.APPLICATION_JSON))
80
                 .andExpect(status().isNotFound());
83
                 .andExpect(status().isNotFound());
81
-
82
     }
84
     }
83
 
85
 
84
     // ================= Get Baker ================= //
86
     // ================= Get Baker ================= //
89
 
91
 
90
         given(bakerRepository.findById(bakerId)).willReturn(Optional.of(froilan));
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
                 .accept(MediaType.APPLICATION_JSON))
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
     // ================= Get Bakers ================= //
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
     // ================= Update Baker ================= //
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
     // ================= Delete Baker ================= //
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
 }