Conversation
Summary of ChangesHello @guto-alves, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the API's error handling mechanisms by ensuring that requests for non-existent lottery data or invalid lottery IDs consistently return a 404 Not Found status with clear, standardized messages. Concurrently, the associated test suite has been significantly upgraded to provide comprehensive coverage for these new error behaviors and existing functionalities, ensuring the API's robustness and predictable responses. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a great step forward in improving the API's error handling and test coverage. Refactoring the ApiRestController to return a 404 status for null results is a significant improvement, and the move to @WebMvcTest with comprehensive test cases is excellent. I've identified a few areas for improvement, including a logic bug in how getLatestResult handles missing results, which is currently masked by a test with a mismatched mock. Additionally, there's an opportunity to refactor some duplicated code for better maintainability.
| if (!lotteries.contains(loteria)) { | ||
| throw new ResourceNotFoundException(String.format(invalidLotteryMessageFormat, loteria)); | ||
| throw new ResourceNotFoundException(INVALID_LOTTERY_MESSAGE.formatted(loteria, lotteries)); | ||
| } |
There was a problem hiding this comment.
This lottery validation logic is duplicated across getResultsByLottery, getResultById, and getLatestResult. To improve maintainability and reduce code duplication, consider extracting this check into a private helper method, for example:
private void validateLottery(String loteria) {
if (!lotteries.contains(loteria)) {
throw new ResourceNotFoundException(INVALID_LOTTERY_MESSAGE.formatted(loteria, lotteries));
}
}You could then call validateLottery(loteria); at the beginning of each of these endpoint methods.
| List<String> lotteries = Arrays.asList("maismilionaria", "megasena", "lotofacil", "quina", | ||
| "lotomania", "timemania", "duplasena", "federal", "diadesorte", "supersete"); |
There was a problem hiding this comment.
The list of lotteries is hardcoded here and also in testGetLatestResultAllLotteries. To improve maintainability and ensure the test stays in sync with the available lotteries, it's better to retrieve this list dynamically from the Loteria enum using Loteria.asList().
List<String> lotteries = com.gutotech.loteriasapi.model.Loteria.asList();…ts more gracefully
This pull request refactors the
ApiRestControllerto improve error handling and message clarity, and significantly enhances the test coverage for the API endpoints. The controller now provides more informative error messages for invalid lottery IDs and missing results, and the tests have been rewritten to cover a wide range of scenarios, including edge cases and error responses.Error handling and messaging improvements
ApiRestController, using new constants for message formatting and replacing the previous message construction logic. [1] [2]getResultByIdandgetLatestResultendpoints, throwing aResourceNotFoundExceptionwith a specific message if no result is found.Testing enhancements
ApiRestControllerTeststo use@WebMvcTestand@MockBeanfor isolated controller testing, and added a comprehensive set of tests covering all main endpoints, including success, not found, empty results, invalid lottery, and error response structure.Edge case coverage