Skip to content

Commit 225732f

Browse files
committed
test(cucumber_fetch_and_save): fix the exception handling
1 parent 719ea42 commit 225732f

4 files changed

Lines changed: 4 additions & 27 deletions

File tree

src/main/java/com/xpeho/spring_boot_java_random_user/presentation/exceptions/GlobalExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ResponseEntity<ErrorResponse> handleConstraintViolationException(Constrai
2222
.findFirst()
2323
.orElse(ex.getMessage());
2424
logger.warn("Constraint violation: {}", message);
25-
return buildErrorResponse("INVALID_PARAMETER", message, HttpStatus.BAD_REQUEST);
25+
return buildErrorResponse("INVALID_PAGINATION", message, HttpStatus.BAD_REQUEST);
2626
}
2727

2828
@ExceptionHandler(InvalidPaginationException.class)

src/main/java/com/xpeho/spring_boot_java_random_user/presentation/handlers/UserHandler.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest;
77
import com.xpeho.spring_boot_java_random_user.domain.enums.Gender;
88
import com.xpeho.spring_boot_java_random_user.domain.enums.UserSource;
9-
import com.xpeho.spring_boot_java_random_user.domain.exceptions.InvalidPaginationException;
109
import com.xpeho.spring_boot_java_random_user.domain.exceptions.UserNotFoundException;
1110
import com.xpeho.spring_boot_java_random_user.domain.usecases.*;
1211
import com.xpeho.spring_boot_java_random_user.presentation.controllers.UserController;
@@ -56,12 +55,6 @@ public UserHandler(
5655

5756
@Override
5857
public ResponseEntity<UserResponseDTO> getRandomUsers(int page, int size, UserSource source) {
59-
if (page < 1) {
60-
throw new InvalidPaginationException("Page must be greater than or equal to 1. Requested: " + page);
61-
}
62-
if (size < 1 || size > 30) {
63-
throw new InvalidPaginationException("Page size must be between 1 and 30. Requested: " + size);
64-
}
6558
try {
6659
PaginatedUsers result = fetchAndSaveRandomUsersUseCase.execute(page, size, source);
6760
UserResponseDTO response = new UserResponseDTO(
@@ -72,7 +65,7 @@ public ResponseEntity<UserResponseDTO> getRandomUsers(int page, int size, UserSo
7265
);
7366
return ResponseEntity.ok(response);
7467
} catch (IOException e) {
75-
logger.error("Error fetching random users: {}", e.getMessage());
68+
logger.error("Error fetching random users", e);
7669
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
7770
}
7871
}
@@ -126,6 +119,6 @@ public void deleteUserById(int id) {
126119
}
127120

128121
private void logUserNotFound(UserNotFoundException e) {
129-
logger.warn(USER_NOT_FOUND_LOG, e.getMessage());
122+
logger.warn(USER_NOT_FOUND_LOG, e);
130123
}
131124
}

src/test/java/com/xpeho/spring_boot_java_random_user/presentation/UserHandlerTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest;
77
import com.xpeho.spring_boot_java_random_user.domain.enums.Gender;
88
import com.xpeho.spring_boot_java_random_user.domain.enums.UserSource;
9-
import com.xpeho.spring_boot_java_random_user.domain.exceptions.InvalidPaginationException;
109
import com.xpeho.spring_boot_java_random_user.domain.exceptions.UserNotFoundException;
1110
import com.xpeho.spring_boot_java_random_user.domain.usecases.*;
1211
import com.xpeho.spring_boot_java_random_user.presentation.handlers.UserHandler;
1312
import com.xpeho.spring_boot_java_random_user.presentation.dto.UserResponseDTO;
1413
import org.junit.jupiter.api.BeforeEach;
1514
import org.junit.jupiter.api.DisplayName;
1615
import org.junit.jupiter.api.Test;
17-
import org.junit.jupiter.params.ParameterizedTest;
18-
import org.junit.jupiter.params.provider.CsvSource;
1916
import org.springframework.http.HttpStatus;
2017
import org.springframework.http.ResponseEntity;
2118

@@ -82,19 +79,6 @@ void shouldReturnInternalServerErrorWhenGetRandomUsersFails() throws IOException
8279
verify(fetchAndSaveRandomUsersUseCase, times(1)).execute(page, size, UserSource.RANDOM_USER);
8380
}
8481

85-
@ParameterizedTest
86-
@CsvSource({
87-
"1, 31",
88-
"0, 10",
89-
"1, 0"
90-
})
91-
@DisplayName("Should throw InvalidPaginationException for invalid pagination inputs")
92-
void shouldThrowInvalidPaginationExceptionForInvalidPaginationInputs(int page, int size) throws IOException {
93-
assertThrows(InvalidPaginationException.class, () -> userHandler.getRandomUsers(page, size, UserSource.DUMMY));
94-
verify(fetchAndSaveRandomUsersUseCase, never()).execute(page, size, UserSource.DUMMY);
95-
}
96-
97-
9882
@Test
9983
@DisplayName("Should return 200 and user when getUserById succeeds")
10084
void shouldReturnOkWhenGetUserByIdSucceeds() {

src/test/java/com/xpeho/spring_boot_java_random_user/presentation/exceptions/GlobalExceptionHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void shouldReturnBadRequestWhenConstraintViolationException() {
3434

3535
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
3636
assertNotNull(response.getBody());
37-
assertEquals("INVALID_PARAMETER", response.getBody().error());
37+
assertEquals("INVALID_PAGINATION", response.getBody().error());
3838
assertEquals(400, response.getBody().status());
3939
assertTrue(response.getBody().message().contains("must be less than or equal to 30"));
4040
}

0 commit comments

Comments
 (0)