Skip to content

Commit

Permalink
♻️ add page offset validation
Browse files Browse the repository at this point in the history
  • Loading branch information
oshyun00 committed Aug 5, 2024
1 parent 224678a commit 1a80eb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.recipe.exception;

import org.springframework.http.HttpStatus;

public class InvalidParameterException extends RecipeException{

public InvalidParameterException(String message) {
super(HttpStatus.BAD_REQUEST, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.dto.RecipeStepRequest;
import net.pengcook.recipe.dto.RecipeStepResponse;
import net.pengcook.recipe.exception.InvalidParameterException;
import net.pengcook.recipe.exception.NotFoundException;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.recipe.repository.RecipeStepRepository;
Expand All @@ -49,7 +50,7 @@ public class RecipeService {
private final S3ClientService s3ClientService;

public List<MainRecipeResponse> readRecipes(PageRecipeRequest pageRecipeRequest) {
Pageable pageable = PageRequest.of(pageRecipeRequest.pageNumber(), pageRecipeRequest.pageSize());
Pageable pageable = getPageable(pageRecipeRequest.pageNumber(), pageRecipeRequest.pageSize());
List<Long> recipeIds = recipeRepository.findRecipeIds(pageable);

List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeIds);
Expand Down Expand Up @@ -105,7 +106,7 @@ public RecipeStepResponse createRecipeStep(long recipeId, RecipeStepRequest reci

public List<MainRecipeResponse> readRecipesOfCategory(RecipeOfCategoryRequest request) {
String categoryName = request.category();
Pageable pageable = PageRequest.of(request.pageNumber(), request.pageSize());
Pageable pageable = getPageable(request.pageNumber(), request.pageSize());
List<Long> recipeIds = categoryRecipeRepository.findRecipeIdsByCategoryName(categoryName, pageable);

List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeIds);
Expand Down Expand Up @@ -159,4 +160,12 @@ private List<CategoryResponse> getCategoryResponses(List<RecipeDataResponse> gro
.distinct()
.collect(Collectors.toList());
}

private Pageable getPageable(int pageNumber, int pageSize) {
long offset = (long) pageNumber * pageSize;
if (offset > Integer.MAX_VALUE) {
throw new InvalidParameterException("μ μ ˆν•˜μ§€ μ•Šμ€ νŽ˜μ΄μ§€ μ •λ³΄μž…λ‹ˆλ‹€.");
}
return PageRequest.of(pageNumber, pageSize);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.pengcook.recipe.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.util.Arrays;
Expand All @@ -16,6 +17,7 @@
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.dto.RecipeStepRequest;
import net.pengcook.recipe.dto.RecipeStepResponse;
import net.pengcook.recipe.exception.InvalidParameterException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -43,6 +45,17 @@ void readRecipes(int pageNumber, int pageSize, int expectedFirstRecipeId) {
assertThat(mainRecipeResponses.getFirst().recipeId()).isEqualTo(expectedFirstRecipeId);
}

@Test
@DisplayName("μš”μ²­λ°›μ€ νŽ˜μ΄μ§€ offset 값이 int νƒ€μž…μ˜ μ΅œλŒ“κ°’μ„ μ΄ˆκ³Όν•˜λ©΄ μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€.")
void readRecipesWhenPageOffsetIsGreaterThanIntMaxValue() {
int pageNumber = 1073741824;
int pageSize = 2;
PageRecipeRequest pageRecipeRequest = new PageRecipeRequest(pageNumber, pageSize);

assertThatThrownBy(() -> recipeService.readRecipes(pageRecipeRequest))
.isInstanceOf(InvalidParameterException.class);
}

@Test
@DisplayName("μƒˆλ‘œμš΄ λ ˆμ‹œν”Όλ₯Ό μƒμ„±ν•œλ‹€.")
void createRecipe() {
Expand Down

0 comments on commit 1a80eb3

Please sign in to comment.