Refactor: Improve Category Management Validation and Maintainability#26
Merged
SymonMuchemi merged 4 commits intomasterfrom Oct 12, 2025
Merged
Refactor: Improve Category Management Validation and Maintainability#26SymonMuchemi merged 4 commits intomasterfrom
SymonMuchemi merged 4 commits intomasterfrom
Conversation
…r method argument type mismatches - Removed the unused CategoryService dependency from ExpenseController. - Added a new exception handler in GlobalExceptionHandler to manage MethodArgumentTypeMismatchException, providing a structured response for invalid parameter types. - Introduced a new test class CategoryControllerTest to cover various scenarios for category management, including creation, retrieval, updating, and deletion of categories, along with handling of conflicts and invalid inputs.
- Added validation to CategoryRequestDTO to ensure the category name is not blank using @notblank annotation. - Updated createCategory method in CategoryController to validate incoming requests. - Modified createNewCategory method in CategoryService to throw IllegalArgumentException for empty category names instead of ResourceConflictException. - Implemented a global exception handler for validation errors, returning detailed error messages for bad requests. - Removed outdated test case for invalid input in CategoryControllerTest to streamline tests.
This commit introduces a new test class, ExpenseControllerTest, which includes unit tests for all major endpoints of the ExpenseController. The tests cover the creation, retrieval, updating, and deletion of expenses, ensuring that the controller behaves as expected. Each test method is annotated with descriptive display names to clarify the purpose of the tests, and mock services are utilized to simulate the behavior of the ExpenseService and CategoryService. This enhancement improves the test coverage and reliability of the expense management functionality.
- Added a new mapper class, CategoryResponseDTOMapper, to streamline the conversion of Category entities to CategoryResponseDTOs. - Refactored the createNewCategory, getAllCategories, findById, and updateCategory methods in CategoryService to utilize the new mapper for improved code readability and maintainability. - Removed redundant checks in createNewCategory method. - Updated CategoryServiceTest to reflect the change in exception handling during category creation. - Cleaned up imports in ExpenseControllerTest by removing unused imports.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request significantly enhances the robustness of category management by implementing robust input validation, improving global error handling, and refactoring the codebase for better maintainability and testability.
Key Improvements:
@NotBlankvalidation on theCategoryRequestDTO.namefield and enabled it inCategoryControllerusing@Valid, ensuring category names are never empty.GlobalExceptionHandlerfor validation failures (MethodArgumentNotValidException) and parameter type mismatches (MethodArgumentTypeMismatchException), providing clear400 Bad Requestresponses.CategoryResponseDTOMapperto standardize entity-to-DTO conversion, replacing scattered inline mapping logic inCategoryService.IllegalArgumentExceptionfor empty/null category names, aligning with the new global validation handler.CategoryControllerTestcovering all CRUD operations, input validation failure cases, conflict errors, and resource not found scenarios.ExpenseController.These changes make the category endpoints more secure, predictable, and easier to evolve.