Skip to content

Refactor: Improve Category Management Validation and Maintainability#26

Merged
SymonMuchemi merged 4 commits intomasterfrom
chore/controller_tests
Oct 12, 2025
Merged

Refactor: Improve Category Management Validation and Maintainability#26
SymonMuchemi merged 4 commits intomasterfrom
chore/controller_tests

Conversation

@SymonMuchemi
Copy link
Copy Markdown
Owner

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:

  • Robust Validation and Error Handling:
    • DTO Validation: Implemented @NotBlank validation on the CategoryRequestDTO.name field and enabled it in CategoryController using @Valid, ensuring category names are never empty.
    • Global Error Mapping: Added dedicated handlers to GlobalExceptionHandler for validation failures (MethodArgumentNotValidException) and parameter type mismatches (MethodArgumentTypeMismatchException), providing clear 400 Bad Request responses.
  • Codebase Maintainability:
    • Centralized Mapping: Introduced CategoryResponseDTOMapper to standardize entity-to-DTO conversion, replacing scattered inline mapping logic in CategoryService.
    • Standardized Exceptions: Refactored the internal service logic to consistently throw IllegalArgumentException for empty/null category names, aligning with the new global validation handler.
  • Testing and Cleanup:
    • Comprehensive Test Coverage: Added extensive tests to CategoryControllerTest covering all CRUD operations, input validation failure cases, conflict errors, and resource not found scenarios.
    • Controller Cleanup: Removed unused imports and an unnecessary service dependency from ExpenseController.

These changes make the category endpoints more secure, predictable, and easier to evolve.

…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.
@SymonMuchemi SymonMuchemi merged commit b7db07b into master Oct 12, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant