|
@@ -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
|
}
|