-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update controller to use ParameterObject As used in the metadata endpoint we want Swagger to be able to interpret and automatically generate pagination parameters like page, size, and sort. * Add error handling for incorrect sort value * Add test for error handling * Fix workflow to only release main branch to maven central * Update CHANGELOG.md * Add unit test for BeeekeeperExceptionHandler Testing the request response and the errorResponses * Implement builder pattern for ErrorResponse --------- Co-authored-by: Hamza Jugon <[email protected]> Co-authored-by: Jay Green-Stevens <[email protected]>
- Loading branch information
1 parent
eabe909
commit 17084bd
Showing
8 changed files
with
181 additions
and
10 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
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
55 changes: 55 additions & 0 deletions
55
...per-api/src/main/java/com/expediagroup/beekeeper/api/error/BeekeeperExceptionHandler.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,55 @@ | ||
/** | ||
* Copyright (C) 2019-2024 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.beekeeper.api.error; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.data.mapping.PropertyReferenceException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@RestControllerAdvice | ||
public class BeekeeperExceptionHandler { | ||
|
||
/** | ||
* Handles invalid sort parameters. | ||
* | ||
* @param exception the exception is thrown when an invalid property is referenced | ||
* @param request the HTTP request | ||
* @return a ResponseEntity containing the error response | ||
*/ | ||
|
||
@ExceptionHandler(PropertyReferenceException.class) | ||
public ResponseEntity<ErrorResponse> handlePropertyReferenceException( | ||
PropertyReferenceException exception, HttpServletRequest request) { | ||
|
||
ErrorResponse errorResponse = ErrorResponse.builder() | ||
.timestamp(LocalDateTime.now().toString()) | ||
.status(HttpStatus.BAD_REQUEST.value()) | ||
.error(HttpStatus.BAD_REQUEST.getReasonPhrase()) | ||
.message(exception.getMessage()) | ||
.path(request.getRequestURI()) | ||
.build(); | ||
|
||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
beekeeper-api/src/main/java/com/expediagroup/beekeeper/api/error/ErrorResponse.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,30 @@ | ||
/** | ||
* Copyright (C) 2019-2024 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expediagroup.beekeeper.api.error; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ErrorResponse { | ||
private final String timestamp; | ||
private final int status; | ||
private final String error; | ||
private final String message; | ||
private final String path; | ||
} |
62 changes: 62 additions & 0 deletions
62
...api/src/test/java/com/expediagroup/beekeeper/api/error/BeekeeperExceptionHandlerTest.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,62 @@ | ||
/** | ||
* Copyright (C) 2019-2023 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.expediagroup.beekeeper.api.error; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.data.mapping.PropertyReferenceException; | ||
import org.springframework.data.mapping.PropertyPath; | ||
import org.springframework.data.util.ClassTypeInformation; | ||
import org.springframework.data.util.TypeInformation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.expediagroup.beekeeper.api.error.BeekeeperExceptionHandler; | ||
import com.expediagroup.beekeeper.api.error.ErrorResponse; | ||
import com.expediagroup.beekeeper.core.model.HousekeepingPath; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class BeekeeperExceptionHandlerTest { | ||
|
||
private final BeekeeperExceptionHandler exceptionHandler = new BeekeeperExceptionHandler(); | ||
|
||
@Test | ||
public void handlePropertyReferenceException_ShouldReturnBadRequest() { | ||
String propertyName = "string"; | ||
TypeInformation<?> typeInformation = ClassTypeInformation.from(HousekeepingPath.class); | ||
List<PropertyPath> baseProperty = Collections.emptyList(); | ||
PropertyReferenceException exception = new PropertyReferenceException(propertyName, typeInformation, baseProperty); | ||
|
||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.setRequestURI("/api/v1/database/testDb/table/testTable/unreferenced-paths"); | ||
|
||
ResponseEntity<ErrorResponse> response = exceptionHandler.handlePropertyReferenceException(exception, request); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||
|
||
ErrorResponse errorResponse = response.getBody(); | ||
assertThat(errorResponse).isNotNull(); | ||
assertThat(errorResponse.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); | ||
assertThat(errorResponse.getError()).isEqualTo("Bad Request"); | ||
assertThat(errorResponse.getMessage()).isEqualTo(exception.getMessage()); | ||
assertThat(errorResponse.getPath()).isEqualTo("/api/v1/database/testDb/table/testTable/unreferenced-paths"); | ||
assertThat(errorResponse.getTimestamp()).isNotNull(); | ||
} | ||
} |
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