1- package org .opendevstack .apiservice .projectusers .controller .advice ;
2- import org .junit .jupiter .api .AfterEach ;
3- import org .junit .jupiter .api .BeforeEach ;
4- import org .junit .jupiter .api .Test ;
5- import org .mockito .MockitoAnnotations ;
1+ package org .opendevstack .apiservice .projectusers .exception ;
2+
63import org .opendevstack .apiservice .projectusers .controller .ProjectUserController ;
74import org .opendevstack .apiservice .projectusers .model .AddUserToProjectRequest ;
85import org .opendevstack .apiservice .projectusers .model .ValidationErrorResponse ;
9- import org .springframework .core .MethodParameter ;
6+ import org .junit .jupiter .api .BeforeEach ;
7+ import org .junit .jupiter .api .Test ;
108import org .springframework .http .HttpStatus ;
119import org .springframework .http .ResponseEntity ;
1210import org .springframework .validation .BeanPropertyBindingResult ;
1311import org .springframework .validation .FieldError ;
1412import org .springframework .web .bind .MethodArgumentNotValidException ;
13+ import org .springframework .core .MethodParameter ;
14+
1515import java .util .List ;
16- import static org .junit .jupiter .api .Assertions .assertEquals ;
17- import static org .junit .jupiter .api .Assertions .assertFalse ;
18- import static org .junit .jupiter .api .Assertions .assertNotNull ;
19- import static org .junit .jupiter .api .Assertions .assertTrue ;
20- import static org .junit .jupiter .api .Assertions .fail ;
16+
17+ import static org .junit .jupiter .api .Assertions .*;
18+
2119/**
22- * Unit test class for the ProjectUserExceptionHandler to verify improved validation
20+ * Unit test class for the GlobalExceptionHandler to verify improved validation
2321 * error messages.
2422 */
25- class ProjectUserExceptionHandlerTest {
26- private ProjectUserExceptionHandler sut ;
27- private AutoCloseable mocks ;
23+ class GlobalExceptionHandlerTest {
24+
25+ private GlobalExceptionHandler exceptionHandler ;
26+
2827 @ BeforeEach
2928 void setUp () {
30- mocks = MockitoAnnotations .openMocks (this );
31- sut = new ProjectUserExceptionHandler ();
32- }
33- @ AfterEach
34- void tearDown () throws Exception {
35- mocks .close ();
29+ exceptionHandler = new GlobalExceptionHandler ();
3630 }
31+
3732 @ Test
38- void handle_method_argument_not_valid_exception_returns_bad_request_with_field_errors () {
39- // GIVEN
33+ void testValidationErrorHandling () {
34+ // Create a mock MethodArgumentNotValidException with validation errors
35+ // Target object representing the @RequestBody argument
4036 AddUserToProjectRequest target = new AddUserToProjectRequest ();
4137 BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult (target , "addUserToProjectRequest" );
38+
39+ // Add field errors for required fields
4240 bindingResult .addError (new FieldError ("addUserToProjectRequest" , "environment" , null , false , null , null ,
4341 "Environment cannot be blank" ));
4442 bindingResult .addError (
@@ -47,32 +45,45 @@ void handle_method_argument_not_valid_exception_returns_bad_request_with_field_e
4745 "Account cannot be blank" ));
4846 bindingResult .addError (
4947 new FieldError ("addUserToProjectRequest" , "role" , null , false , null , null , "Role cannot be null" ));
48+
49+ // Create a MethodParameter referencing the controller method's @RequestBody
50+ // parameter
5051 MethodParameter methodParameter ;
5152 try {
5253 methodParameter = new MethodParameter (
5354 ProjectUserController .class .getMethod (
5455 "triggerMembershipRequest" , String .class , AddUserToProjectRequest .class ),
55- 1 );
56+ 1 // index of AddUserToProjectRequest parameter
57+ );
5658 } catch (NoSuchMethodException e ) {
5759 fail ("Failed to reflect controller method for test: " + e .getMessage ());
58- return ;
60+ return ; // unreachable, but required for compilation
5961 }
62+
6063 MethodArgumentNotValidException exception = new MethodArgumentNotValidException (methodParameter , bindingResult );
61- // WHEN
62- ResponseEntity <ValidationErrorResponse > response = sut .handleMethodArgumentNotValidException (exception );
63- // THEN
64+
65+ // Test the exception handler
66+ ResponseEntity <ValidationErrorResponse > response = exceptionHandler
67+ .handleMethodArgumentNotValidException (exception );
68+
69+ // Verify response
6470 assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
6571 assertNotNull (response .getBody ());
72+
6673 ValidationErrorResponse errorResponse = response .getBody ();
6774 assertFalse (errorResponse .getSuccess ());
6875 assertEquals ("PROJECT_USER_ERROR" , errorResponse .getErrorCode ());
6976 assertNotNull (errorResponse .getFieldErrors ());
7077 assertEquals (4 , errorResponse .getFieldErrors ().size ());
78+
79+ // Check specific field errors
7180 List <org .opendevstack .apiservice .projectusers .model .FieldError > fieldErrors = errorResponse .getFieldErrors ();
7281 assertTrue (fieldErrors .stream ().anyMatch (error -> "environment" .equals (error .getField ())));
7382 assertTrue (fieldErrors .stream ().anyMatch (error -> "user" .equals (error .getField ())));
7483 assertTrue (fieldErrors .stream ().anyMatch (error -> "account" .equals (error .getField ())));
7584 assertTrue (fieldErrors .stream ().anyMatch (error -> "role" .equals (error .getField ())));
85+
86+ // Verify expected format is provided for each field
7687 fieldErrors .forEach (fieldError -> {
7788 assertNotNull (fieldError .getField ());
7889 assertNotNull (fieldError .getMessage ());
@@ -82,14 +93,15 @@ void handle_method_argument_not_valid_exception_returns_bad_request_with_field_e
8293 }
8394 });
8495 }
96+
8597 @ Test
86- void handle_generic_exception_returns_internal_server_error () {
87- // GIVEN
98+ void testGenericExceptionHandling () {
99+ // Test generic exception handling
88100 Exception exception = new RuntimeException ("Unexpected error" );
89- // WHEN
90- ResponseEntity <?> response = sut .handleGenericException (exception );
91- // THEN
101+
102+ ResponseEntity <?> response = exceptionHandler .handleGenericException (exception );
103+
92104 assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
93105 assertNotNull (response .getBody ());
94106 }
95- }
107+ }
0 commit comments