11package fun .trackmoney .config .exception ;
22
3+ import fun .trackmoney .auth .exception .LoginException ;
34import fun .trackmoney .user .exception .EmailAlreadyExistsException ;
5+ import fun .trackmoney .user .exception .EmailNotFoundException ;
46import fun .trackmoney .user .exception .PasswordNotValid ;
57import fun .trackmoney .utils .CustomFieldError ;
68import fun .trackmoney .utils .response .ApiResponse ;
1113import org .springframework .validation .FieldError ;
1214import org .springframework .web .bind .MethodArgumentNotValidException ;
1315import java .util .List ;
14-
1516import static org .junit .jupiter .api .Assertions .*;
1617import static org .mockito .Mockito .mock ;
1718import static org .mockito .Mockito .when ;
@@ -29,10 +30,10 @@ void passwordNotValidShouldReturnBadRequestWithCorrectErrors() {
2930 new CustomFieldError ("password" , "must be at least 8 characters long" )
3031 );
3132 PasswordNotValid exception = new PasswordNotValid (errors );
32- ResponseEntity <ApiResponse <List <String >>> response = restExceptionHandler .passwordNotValid (exception );
33+ ResponseEntity <ApiResponse <List <CustomFieldError >>> response = restExceptionHandler .passwordNotValid (exception );
3334
3435 assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
35- ApiResponse <List <String >> apiResponse = response .getBody ();
36+ ApiResponse <List <CustomFieldError >> apiResponse = response .getBody ();
3637
3738 assertNotNull (apiResponse );
3839 assertFalse (apiResponse .isSuccess ());
@@ -48,10 +49,10 @@ void passwordNotValidShouldReturnBadRequestWithCorrectErrors() {
4849 @ Test
4950 void handleEmailAlreadyExistsShouldReturnConflictWithError () {
5051 EmailAlreadyExistsException exception = new EmailAlreadyExistsException ("This email is already in use." );
51- ResponseEntity <ApiResponse <Object >> response = restExceptionHandler .handleEmailAlreadyExists (exception );
52+ ResponseEntity <ApiResponse <List < CustomFieldError > >> response = restExceptionHandler .handleEmailAlreadyExists (exception );
5253
5354 assertEquals (HttpStatus .CONFLICT , response .getStatusCode ());
54- ApiResponse <Object > apiResponse = response .getBody ();
55+ ApiResponse <List < CustomFieldError > > apiResponse = response .getBody ();
5556 assertNotNull (apiResponse );
5657 assertFalse (apiResponse .isSuccess ());
5758 assertEquals ("This email is already in use." , apiResponse .getMessage ());
@@ -98,4 +99,40 @@ void handleValidationExceptions_shouldReturnBadRequestWithDetailedFieldErrors()
9899 .anyMatch (error -> error .getField ().equals ("object" ) && error .getMessage ().equals ("Global error" )),
99100 "Global validation error is missing or incorrect" );
100101 }
102+
103+ @ Test
104+ void handleEmailNotFound_shouldReturnBadRequestWithErrorMessage () {
105+ List <CustomFieldError > errors = List .of (
106+ new CustomFieldError ("user" , "Email not found!" )
107+ );
108+ EmailNotFoundException exception = new EmailNotFoundException (errors );
109+ ResponseEntity <ApiResponse <List <CustomFieldError >>> response = restExceptionHandler .emailNotFound (exception );
110+
111+ assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
112+ ApiResponse <List <CustomFieldError >> apiResponse = response .getBody ();
113+
114+ assertNotNull (apiResponse );
115+ assertFalse (apiResponse .isSuccess ());
116+ assertEquals ("Email not found!" , apiResponse .getMessage ());
117+ assertNotNull (apiResponse .getErrors ());
118+ assertEquals (errors .size (), apiResponse .getErrors ().size ());
119+ }
120+
121+ @ Test
122+ void handleLoginException_shouldReturnBadRequestWithPasswordError () {
123+ List <CustomFieldError > errors = List .of (
124+ new CustomFieldError ("Password" , "Password is incorrect." )
125+ );
126+ LoginException exception = new LoginException (errors );
127+ ResponseEntity <ApiResponse <List <CustomFieldError >>> response = restExceptionHandler .loginException (exception );
128+
129+ assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
130+ ApiResponse <List <CustomFieldError >> apiResponse = response .getBody ();
131+
132+ assertNotNull (apiResponse );
133+ assertFalse (apiResponse .isSuccess ());
134+ assertEquals ("Password is incorrect." , apiResponse .getErrors ().get (0 ).getMessage ());
135+ assertNotNull (apiResponse .getErrors ());
136+ assertEquals (errors .size (), apiResponse .getErrors ().size ());
137+ }
101138}
0 commit comments