-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SMA-91 rest api should return 400 when data is invalid (#81)
- Loading branch information
1 parent
4a1f9d1
commit 73ad94f
Showing
6 changed files
with
138 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/sportsmatch/src/main/java/com/sportsmatch/configs/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.sportsmatch.configs; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import javax.security.sasl.AuthenticationException; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
// handle request body | ||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
public ResponseEntity<?> handleHttpMessageNotReadableException( | ||
HttpMessageNotReadableException e) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
} | ||
|
||
// handle parameter with @Valid | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<?> handleMethodArgumentNotValidException( | ||
MethodArgumentNotValidException e) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(AuthenticationException.class) | ||
public ResponseEntity<?> handleAuthenticationException(AuthenticationException e) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sportsmatch.BaseTest; | ||
import com.sportsmatch.auth.JwtService; | ||
import com.sportsmatch.dtos.PlaceDTO; | ||
import com.sportsmatch.models.Gender; | ||
import com.sportsmatch.models.Role; | ||
import com.sportsmatch.models.User; | ||
import com.sportsmatch.repositories.UserRepository; | ||
import com.sportsmatch.services.PlaceService; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
@@ -24,20 +29,20 @@ | |
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
|
||
@ExtendWith(SpringExtension.class) | ||
@AutoConfigureMockMvc | ||
@SpringBootTest | ||
class PlaceControllerTest extends BaseTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@Autowired private MockMvc mockMvc; | ||
|
||
@Autowired private ObjectMapper objectMapper; | ||
|
||
@Autowired private JwtService jwtService; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
@Autowired private UserRepository userRepository; | ||
|
||
@MockBean | ||
private PlaceService placeService; | ||
@MockBean private PlaceService placeService; | ||
|
||
PlaceDTO createPlaceDTO1() { | ||
return PlaceDTO.builder() | ||
|
@@ -67,9 +72,11 @@ void addNewPlaceShouldReturn403NotAuthenticatedUser() throws Exception { | |
.thenReturn(ResponseEntity.ok("Place added successfully")); | ||
|
||
// Perform a POST request to add a new place without authentication | ||
mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/places/add") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(placeDTO))) | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.post("/api/v1/places/add") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(placeDTO))) | ||
// Verify that the response status is 401 Forbidden | ||
.andExpect(MockMvcResultMatchers.status().isUnauthorized()); | ||
} | ||
|
@@ -84,10 +91,25 @@ void addNewPlaceShouldReturn200AndSuccessfulMessageAuthenticatedUser() throws Ex | |
when(placeService.addNewPlace(any(PlaceDTO.class))) | ||
.thenReturn(ResponseEntity.ok("Place successfully added")); | ||
|
||
// Create User | ||
User user = | ||
User.builder() | ||
.email("[email protected]") | ||
.password("password") | ||
.name("testuser") | ||
.gender(Gender.MALE) | ||
.role(Role.USER) | ||
.build(); | ||
userRepository.save(user); | ||
String token = jwtService.generateToken(user); | ||
|
||
// Perform a POST request to add a new place with authentication | ||
mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/places/add") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(placeDTO))) | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.post("/api/v1/places/add") | ||
.header("Authorization", "Bearer " + token) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(placeDTO))) | ||
// Verify that the response status is 200 OK | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
// Verify that the response content contains the expected success message | ||
|
@@ -105,16 +127,19 @@ void searchPlaces() throws Exception { | |
when(placeService.searchPlaces(any(String.class))).thenReturn(expectedPlaces); | ||
|
||
// Perform a GET request to search for places with a name parameter "test" | ||
mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/places/search") | ||
.param("name", "test") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
mockMvc | ||
.perform( | ||
MockMvcRequestBuilders.get("/api/v1/places/search") | ||
.param("name", "test") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
|
||
// Verify the response is an array | ||
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) | ||
// Verify the name of the first place in the response matches the name of the first expected place | ||
// Verify the name of the first place in the response matches the name of the first expected | ||
// place | ||
.andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Test Place Name1")) | ||
// Verify the name of the second place in the response matches the name of the second expected place | ||
// Verify the name of the second place in the response matches the name of the second | ||
// expected place | ||
.andExpect(MockMvcResultMatchers.jsonPath("$[1].name").value("Test Place Name2")); | ||
|
||
} | ||
} | ||
} |