Skip to content

Commit 430197a

Browse files
committed
Use MediaType class
1 parent 3dcf335 commit 430197a

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

Baloot1/src/test/java/controllers/CommoditiesControllerApiTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1717
import org.springframework.boot.test.context.SpringBootTest;
1818
import org.springframework.boot.test.mock.mockito.MockBean;
19+
import org.springframework.http.MediaType;
1920
import org.springframework.test.web.servlet.MockMvc;
2021
import service.Baloot;
2122

@@ -101,7 +102,7 @@ public void testRateCommodityApi(String rate) throws Exception {
101102
when(baloot.getCommodityById(commodity.getId())).thenReturn(commodity);
102103
Map<String, String> map = Map.of("rate", rate, "username", "user");
103104
mockMvc.perform(post("/commodities/{id}/rate", commodity.getId())
104-
.contentType("application/json")
105+
.contentType(MediaType.APPLICATION_JSON)
105106
.content(objectMapper.writeValueAsString(map)))
106107
.andExpect(status().isOk())
107108
.andExpect(content().string("rate added successfully!"));
@@ -113,7 +114,7 @@ public void testRateCommodityApiWithNonExistingCommodity() throws Exception {
113114
when(baloot.getCommodityById(anyString())).thenThrow(new NotExistentCommodity());
114115
Map<String, String> map = Map.of("rate", "5", "username", "user");
115116
mockMvc.perform(post("/commodities/{id}/rate", "1")
116-
.contentType("application/json")
117+
.contentType(MediaType.APPLICATION_JSON)
117118
.content(objectMapper.writeValueAsString(map)))
118119
.andExpect(status().isNotFound())
119120
.andExpect(content().string(NOT_EXISTENT_COMMODITY));
@@ -129,7 +130,7 @@ public void testRateCommodityApiWithWrongRate(String rate) throws Exception {
129130
when(baloot.getCommodityById(commodity.getId())).thenReturn(commodity);
130131
Map<String, String> map = Map.of("rate", rate, "username", "user");
131132
mockMvc.perform(post("/commodities/{id}/rate", commodity.getId())
132-
.contentType("application/json")
133+
.contentType(MediaType.APPLICATION_JSON)
133134
.content(objectMapper.writeValueAsString(map)))
134135
.andExpect(status().isBadRequest())
135136
.andExpect(jsonPath("$").isNotEmpty());
@@ -145,7 +146,7 @@ public void testRateCommodityApiWithWrongRateRange(String rate) throws Exception
145146
when(baloot.getCommodityById(commodity.getId())).thenReturn(commodity);
146147
Map<String, String> map = Map.of("rate", rate, "username", "user");
147148
mockMvc.perform(post("/commodities/{id}/rate", commodity.getId())
148-
.contentType("application/json")
149+
.contentType(MediaType.APPLICATION_JSON)
149150
.content(objectMapper.writeValueAsString(map)))
150151
.andExpect(status().isBadRequest())
151152
.andExpect(content().string(INVALID_RATE_RANGE));
@@ -160,7 +161,7 @@ public void testRateCommodityApiWithNoRate() throws Exception {
160161
when(baloot.getCommodityById(commodity.getId())).thenReturn(commodity);
161162
Map<String, String> map = Map.of("username", "user");
162163
mockMvc.perform(post("/commodities/{id}/rate", commodity.getId())
163-
.contentType("application/json")
164+
.contentType(MediaType.APPLICATION_JSON)
164165
.content(objectMapper.writeValueAsString(map)))
165166
.andExpect(status().isBadRequest())
166167
.andExpect(jsonPath("$").isNotEmpty());
@@ -177,7 +178,7 @@ public void testAddCommodityCommentApi() throws Exception {
177178
when(baloot.getUserById(user.getUsername())).thenReturn(user);
178179
Map<String, String> map = Map.of("username", user.getUsername(), "comment", "comment");
179180
mockMvc.perform(post("/commodities/{id}/comment", commodity.getId())
180-
.contentType("application/json")
181+
.contentType(MediaType.APPLICATION_JSON)
181182
.content(objectMapper.writeValueAsString(map)))
182183
.andExpect(status().isOk())
183184
.andExpect(content().string("comment added successfully!"));
@@ -193,7 +194,7 @@ public void testAddCommodityCommentApiWithNonExistingUser() throws Exception {
193194
when(baloot.getUserById(anyString())).thenThrow(new NotExistentUser());
194195
Map<String, String> map = Map.of("username", "user", "comment", "comment");
195196
mockMvc.perform(post("/commodities/{id}/comment", commodity.getId())
196-
.contentType("application/json")
197+
.contentType(MediaType.APPLICATION_JSON)
197198
.content(objectMapper.writeValueAsString(map)))
198199
.andExpect(status().isNotFound())
199200
.andExpect(content().string(NOT_EXISTENT_USER));
@@ -250,7 +251,7 @@ public void testSearchCommoditiesApiFilterByName() throws Exception {
250251
when(baloot.filterCommoditiesByName("name")).thenReturn(commodities);
251252
Map<String, String> map = Map.of("searchOption", "name", "searchValue", "name");
252253
mockMvc.perform(post("/commodities/search")
253-
.contentType("application/json")
254+
.contentType(MediaType.APPLICATION_JSON)
254255
.content(objectMapper.writeValueAsString(map)))
255256
.andExpect(status().isOk())
256257
.andExpect(jsonPath("$.length()").value(2))
@@ -267,7 +268,7 @@ public void testSearchCommoditiesApiFilterByNameEmptyList() throws Exception {
267268
when(baloot.filterCommoditiesByName("name1")).thenReturn(commodities);
268269
Map<String, String> map = Map.of("searchOption", "name", "searchValue", "name1");
269270
mockMvc.perform(post("/commodities/search")
270-
.contentType("application/json")
271+
.contentType(MediaType.APPLICATION_JSON)
271272
.content(objectMapper.writeValueAsString(map)))
272273
.andExpect(status().isOk())
273274
.andExpect(jsonPath("$").isEmpty());
@@ -289,7 +290,7 @@ public void testSearchCommoditiesApiFilterByCategory() throws Exception {
289290
when(baloot.filterCommoditiesByCategory("category")).thenReturn(commodities);
290291
Map<String, String> map = Map.of("searchOption", "category", "searchValue", "category");
291292
mockMvc.perform(post("/commodities/search")
292-
.contentType("application/json")
293+
.contentType(MediaType.APPLICATION_JSON)
293294
.content(objectMapper.writeValueAsString(map)))
294295
.andExpect(status().isOk())
295296
.andExpect(jsonPath("$.length()").value(2))
@@ -306,7 +307,7 @@ public void testSearchCommoditiesApiFilterByCategoryEmptyList() throws Exception
306307
when(baloot.filterCommoditiesByCategory("category1")).thenReturn(commodities);
307308
Map<String, String> map = Map.of("searchOption", "category", "searchValue", "category");
308309
mockMvc.perform(post("/commodities/search")
309-
.contentType("application/json")
310+
.contentType(MediaType.APPLICATION_JSON)
310311
.content(objectMapper.writeValueAsString(map)))
311312
.andExpect(status().isOk())
312313
.andExpect(jsonPath("$").isEmpty());
@@ -328,7 +329,7 @@ public void testSearchCommoditiesApiFilterByProvider() throws Exception {
328329
when(baloot.filterCommoditiesByProviderName("provider")).thenReturn(commodities);
329330
Map<String, String> map = Map.of("searchOption", "provider", "searchValue", "provider");
330331
mockMvc.perform(post("/commodities/search")
331-
.contentType("application/json")
332+
.contentType(MediaType.APPLICATION_JSON)
332333
.content(objectMapper.writeValueAsString(map)))
333334
.andExpect(status().isOk())
334335
.andExpect(jsonPath("$.length()").value(2))
@@ -345,7 +346,7 @@ public void testSearchCommoditiesApiFilterByProviderEmptyList() throws Exception
345346
when(baloot.filterCommoditiesByProviderName("provider1")).thenReturn(commodities);
346347
Map<String, String> map = Map.of("searchOption", "provider", "searchValue", "provider1");
347348
mockMvc.perform(post("/commodities/search")
348-
.contentType("application/json")
349+
.contentType(MediaType.APPLICATION_JSON)
349350
.content(objectMapper.writeValueAsString(map)))
350351
.andExpect(status().isOk())
351352
.andExpect(jsonPath("$").isEmpty());
@@ -356,7 +357,7 @@ public void testSearchCommoditiesApiFilterByProviderEmptyList() throws Exception
356357
public void testSearchCommoditiesApiFilterByInvalidOption() throws Exception {
357358
Map<String, String> map = Map.of("searchOption", "invalid", "searchValue", "value");
358359
mockMvc.perform(post("/commodities/search")
359-
.contentType("application/json")
360+
.contentType(MediaType.APPLICATION_JSON)
360361
.content(objectMapper.writeValueAsString(map)))
361362
.andExpect(status().isOk())
362363
.andExpect(jsonPath("$").isEmpty());

Baloot1/src/test/java/controllers/UserControllerApiTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1010
import org.springframework.boot.test.context.SpringBootTest;
1111
import org.springframework.boot.test.mock.mockito.MockBean;
12+
import org.springframework.http.MediaType;
1213
import org.springframework.test.web.servlet.MockMvc;
1314
import service.Baloot;
1415

@@ -32,7 +33,7 @@ public class UserControllerApiTest {
3233
private MockMvc mockMvc;
3334
private final ObjectMapper objectMapper = new ObjectMapper();
3435

35-
private static User getAnonymouseUser() {
36+
private static User getAnonymousUser() {
3637
return new User("username", "password", "email@mail.com", "2023-01-01", "address");
3738
}
3839

@@ -44,7 +45,7 @@ public void setUp() {
4445
@Test
4546
@DisplayName("Test getUser() with a valid id")
4647
public void testGetUserApiWithValidId() throws Exception {
47-
User user = getAnonymouseUser();
48+
User user = getAnonymousUser();
4849
when(baloot.getUserById(user.getUsername())).thenReturn(user);
4950
mockMvc.perform(get("/users/{id}", user.getUsername()))
5051
.andExpect(status().isOk())
@@ -67,11 +68,11 @@ public void testGetUserApiWithInvalidId() throws Exception {
6768
@Test
6869
@DisplayName("Test addCredit() with a valid id and credit")
6970
public void testAddCreditApiWithValidIdAndCredit() throws Exception {
70-
User user = getAnonymouseUser();
71+
User user = getAnonymousUser();
7172
Map<String, String> input = Map.of("credit", "1000");
7273
when(baloot.getUserById(user.getUsername())).thenReturn(user);
7374
mockMvc.perform(post("/users/{id}/credit", user.getUsername())
74-
.contentType("application/json")
75+
.contentType(MediaType.APPLICATION_JSON)
7576
.content(objectMapper.writeValueAsString(input)))
7677
.andExpect(status().isOk())
7778
.andExpect(content().string("credit added successfully!"));
@@ -83,7 +84,7 @@ public void testAddCreditApiWithInvalidId() throws Exception {
8384
Map<String, String> input = Map.of("credit", "1000");
8485
when(baloot.getUserById("1")).thenThrow(new NotExistentUser());
8586
mockMvc.perform(post("/users/{id}/credit", "1")
86-
.contentType("application/json")
87+
.contentType(MediaType.APPLICATION_JSON)
8788
.content(objectMapper.writeValueAsString(input)))
8889
.andExpect(status().isNotFound())
8990
.andExpect(content().string(NOT_EXISTENT_USER));
@@ -92,11 +93,11 @@ public void testAddCreditApiWithInvalidId() throws Exception {
9293
@Test
9394
@DisplayName("Test addCredit() with an invalid credit")
9495
public void testAddCreditApiWithInvalidCredit() throws Exception {
95-
User user = getAnonymouseUser();
96+
User user = getAnonymousUser();
9697
Map<String, String> input = Map.of("credit", "invalidCredit");
9798
when(baloot.getUserById(user.getUsername())).thenReturn(user);
9899
mockMvc.perform(post("/users/{id}/credit", user.getUsername())
99-
.contentType("application/json")
100+
.contentType(MediaType.APPLICATION_JSON)
100101
.content(objectMapper.writeValueAsString(input)))
101102
.andExpect(status().isBadRequest())
102103
.andExpect(content().string("Please enter a valid number for the credit amount."));
@@ -105,11 +106,11 @@ public void testAddCreditApiWithInvalidCredit() throws Exception {
105106
@Test
106107
@DisplayName("Test addCredit() with a negative credit")
107108
public void testAddCreditApiWithNegativeCredit() throws Exception {
108-
User user = getAnonymouseUser();
109+
User user = getAnonymousUser();
109110
Map<String, String> input = Map.of("credit", "-1000");
110111
when(baloot.getUserById(user.getUsername())).thenReturn(user);
111112
mockMvc.perform(post("/users/{id}/credit", user.getUsername())
112-
.contentType("application/json")
113+
.contentType(MediaType.APPLICATION_JSON)
113114
.content(objectMapper.writeValueAsString(input)))
114115
.andExpect(status().isBadRequest())
115116
.andExpect(content().string(INVALID_CREDIT_RANGE));
@@ -118,11 +119,11 @@ public void testAddCreditApiWithNegativeCredit() throws Exception {
118119
@Test
119120
@DisplayName("Test addCredit() with no credit")
120121
public void testAddCreditApiWithNoCredit() throws Exception {
121-
User user = getAnonymouseUser();
122+
User user = getAnonymousUser();
122123
Map<String, String> input = Map.of();
123124
when(baloot.getUserById(user.getUsername())).thenReturn(user);
124125
mockMvc.perform(post("/users/{id}/credit", user.getUsername())
125-
.contentType("application/json")
126+
.contentType(MediaType.APPLICATION_JSON)
126127
.content(objectMapper.writeValueAsString(input)))
127128
.andExpect(status().isBadRequest())
128129
.andExpect(content().string("Please enter a valid number for the credit amount."));

0 commit comments

Comments
 (0)