-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from bounswe/backend/feature/206-formatting-r…
…esponses-/search Formatting responses for /search (dishes)
- Loading branch information
Showing
9 changed files
with
160 additions
and
112 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
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
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/com/group1/cuisines/dto/DishResponseDto.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,19 @@ | ||
package com.group1.cuisines.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DishResponseDto { | ||
private String id; | ||
private String name; | ||
private String image; | ||
private String description; | ||
private String countries; | ||
private String ingredients; | ||
private String foodTypes; | ||
private String cuisines; | ||
} |
26 changes: 26 additions & 0 deletions
26
backend/src/main/java/com/group1/cuisines/exceptions/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,26 @@ | ||
package com.group1.cuisines.exceptions; | ||
|
||
import com.group1.cuisines.dao.response.ApiResponse; | ||
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ApiResponse<String>> handleException(Exception e) { | ||
ApiResponse<String> response = new ApiResponse<>(500, "An unexpected error occurred: " + e.getMessage(), null); | ||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@ExceptionHandler(ResourceNotFoundException.class) | ||
public ResponseEntity<ApiResponse<String>> handleResourceNotFoundException(ResourceNotFoundException e) { | ||
ApiResponse<String> response = new ApiResponse<>(400, e.getMessage(), null); | ||
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); | ||
} | ||
|
||
|
||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/com/group1/cuisines/exceptions/ResourceNotFoundException.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,7 @@ | ||
package com.group1.cuisines.exceptions; | ||
|
||
public class ResourceNotFoundException extends RuntimeException { | ||
public ResourceNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
public class AuthenticationControllerTest { | ||
|
@@ -31,123 +32,65 @@ public void setUp() { | |
|
||
@Test | ||
public void shouldReturnSuccessOnValidSignin() { | ||
SignInRequest signInRequest = new SignInRequest( | ||
"testUser", | ||
"testPassword" | ||
); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = | ||
new ApiResponse<>( | ||
200, | ||
"Success", | ||
new AuthenticationTokenResponse("token") | ||
); | ||
when(authenticationService.signin(signInRequest)).thenReturn( | ||
apiResponse | ||
); | ||
ResponseEntity< | ||
ApiResponse<AuthenticationTokenResponse> | ||
> responseEntity = authenticationController.signin(signInRequest); | ||
assertEquals( | ||
200, | ||
responseEntity.getBody().getStatus(), | ||
"Status code does not match expected value on successful signin" | ||
); | ||
assertEquals( | ||
apiResponse, | ||
responseEntity.getBody(), | ||
"Response body does not match expected value on successful signin" | ||
); | ||
SignInRequest signInRequest = new SignInRequest("testUser", "testPassword"); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(200, "Success", new AuthenticationTokenResponse("token")); | ||
when(authenticationService.signin(signInRequest)).thenReturn(apiResponse); | ||
|
||
ResponseEntity<ApiResponse<AuthenticationTokenResponse>> responseEntity = authenticationController.signin(signInRequest); | ||
|
||
assertEquals(HttpStatus.OK.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on successful signin"); | ||
assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on successful signin"); | ||
} | ||
|
||
@Test | ||
public void shouldReturnFailureOnInvalidSignin() { | ||
SignInRequest signInRequest = new SignInRequest( | ||
"testUser", | ||
"wrongPassword" | ||
); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = | ||
new ApiResponse<>(401, "Failure", null); | ||
when(authenticationService.signin(signInRequest)).thenReturn( | ||
apiResponse | ||
); | ||
ResponseEntity< | ||
ApiResponse<AuthenticationTokenResponse> | ||
> responseEntity = authenticationController.signin(signInRequest); | ||
assertEquals( | ||
401, | ||
responseEntity.getBody().getStatus(), | ||
"Status code does not match expected value on failed signin" | ||
); | ||
assertEquals( | ||
apiResponse, | ||
responseEntity.getBody(), | ||
"Response body does not match expected value on failed signin" | ||
); | ||
SignInRequest signInRequest = new SignInRequest("testUser", "wrongPassword"); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(401, "Invalid email/username or password.", null); | ||
when(authenticationService.signin(signInRequest)).thenReturn(apiResponse); | ||
|
||
ResponseEntity<ApiResponse<AuthenticationTokenResponse>> responseEntity = authenticationController.signin(signInRequest); | ||
|
||
assertEquals(HttpStatus.UNAUTHORIZED.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on failed signin"); | ||
assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on failed signin"); | ||
} | ||
|
||
@Test | ||
public void shouldReturnSuccessOnValidSignup() { | ||
SignUpRequest signUpRequest = SignUpRequest.builder() | ||
.email("[email protected]") | ||
.username("newUser") | ||
.country("USA") | ||
.bio("Bio of the new user") | ||
.password("newPassword") | ||
.firstName("New") | ||
.lastName("User") | ||
.build(); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = | ||
new ApiResponse<>( | ||
200, | ||
"Success", | ||
new AuthenticationTokenResponse("token") | ||
); | ||
when(authenticationService.signup(signUpRequest)).thenReturn( | ||
apiResponse | ||
); | ||
ResponseEntity< | ||
ApiResponse<AuthenticationTokenResponse> | ||
> responseEntity = authenticationController.signup(signUpRequest); | ||
assertEquals( | ||
200, | ||
responseEntity.getBody().getStatus(), | ||
"Status code does not match expected value on successful signup" | ||
); | ||
assertEquals( | ||
apiResponse, | ||
responseEntity.getBody(), | ||
"Response body does not match expected value on successful signup" | ||
); | ||
.email("[email protected]") | ||
.username("newUser") | ||
.country("USA") | ||
.bio("Bio of the new user") | ||
.password("newPassword") | ||
.firstName("New") | ||
.lastName("User") | ||
.build(); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(201, "User registered successfully.", new AuthenticationTokenResponse("token")); | ||
when(authenticationService.signup(signUpRequest)).thenReturn(apiResponse); | ||
|
||
ResponseEntity<ApiResponse<AuthenticationTokenResponse>> responseEntity = authenticationController.signup(signUpRequest); | ||
|
||
assertEquals(HttpStatus.CREATED.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on successful signup"); | ||
assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on successful signup"); | ||
} | ||
|
||
@Test | ||
public void shouldReturnFailureOnInvalidSignup() { | ||
SignUpRequest signUpRequest = SignUpRequest.builder() | ||
.email("[email protected]") | ||
.username("newUser") | ||
.country("USA") | ||
.bio("Bio of the new user") | ||
.password("newPassword") | ||
.firstName("New") | ||
.lastName("User") | ||
.build(); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = | ||
new ApiResponse<>(409, "Failure", null); | ||
when(authenticationService.signup(signUpRequest)).thenReturn( | ||
apiResponse | ||
); | ||
ResponseEntity< | ||
ApiResponse<AuthenticationTokenResponse> | ||
> responseEntity = authenticationController.signup(signUpRequest); | ||
assertEquals( | ||
409, | ||
responseEntity.getBody().getStatus(), | ||
"Status code does not match expected value on failed signup" | ||
); | ||
assertEquals( | ||
apiResponse, | ||
responseEntity.getBody(), | ||
"Response body does not match expected value on failed signup" | ||
); | ||
.email("[email protected]") | ||
.username("newUser") | ||
.country("USA") | ||
.bio("Bio of the new user") | ||
.password("newPassword") | ||
.firstName("New") | ||
.lastName("User") | ||
.build(); | ||
ApiResponse<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(409, "Email or username already exists.", null); | ||
when(authenticationService.signup(signUpRequest)).thenReturn(apiResponse); | ||
|
||
ResponseEntity<ApiResponse<AuthenticationTokenResponse>> responseEntity = authenticationController.signup(signUpRequest); | ||
|
||
assertEquals(HttpStatus.CONFLICT.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on failed signup"); | ||
assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on failed signup"); | ||
} | ||
} |