Skip to content

Commit

Permalink
Merge pull request #397 from JNU-econovation/feat/#396
Browse files Browse the repository at this point in the history
[BE/feat&test]  식사 등록 API 유효성 검증 및 비즈니스 로직 검증 수정
  • Loading branch information
hwangdaesun authored Jul 17, 2024
2 parents ab38246 + fe4cca8 commit 4465370
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.List;

import com.gaebaljip.exceed.application.domain.meal.MealType;
import com.gaebaljip.exceed.common.exception.meal.InvalidGException;
import com.gaebaljip.exceed.common.exception.meal.InvalidMultipleAndGException;
import com.gaebaljip.exceed.common.exception.meal.InvalidMultipleException;
import com.gaebaljip.exceed.dto.EatMealFoodDTO;
import com.gaebaljip.exceed.dto.request.EatMealRequest;

Expand All @@ -12,6 +15,34 @@
public record EatMealCommand(
List<EatMealFoodDTO> eatMealFoodDTOS, Long memberId, MealType mealType) {

public EatMealCommand {
eatMealFoodDTOS.forEach(
dto -> {
if ((dto.multiple() != null && dto.g() != null)
|| (dto.multiple() == null && dto.g() == null)) {
throw InvalidMultipleAndGException.EXCEPTION;
}
if (dto.multiple() != null) {
validateMultiple(dto.multiple());
}
if (dto.g() != null) {
validateG(dto.g());
}
});
}

private void validateMultiple(double multiple) {
if (multiple <= 0 || multiple > 100) {
throw InvalidMultipleException.EXECPTION;
}
}

private void validateG(int g) {
if (g <= 0) {
throw InvalidGException.EXCEPTION;
}
}

public static EatMealCommand of(EatMealRequest request, Long memberId) {
return EatMealCommand.builder()
.eatMealFoodDTOS(request.eatMealFoodDTOS())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import com.gaebaljip.exceed.application.port.out.meal.MealFoodPort;
import com.gaebaljip.exceed.application.port.out.meal.MealPort;
import com.gaebaljip.exceed.application.port.out.member.MemberPort;
import com.gaebaljip.exceed.common.exception.meal.InvalidGException;
import com.gaebaljip.exceed.common.exception.meal.InvalidMultipleAndGException;
import com.gaebaljip.exceed.common.exception.meal.InvalidMultipleException;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -47,7 +45,6 @@ public class EatMealService implements EatMealUsecase {
@Override
@Transactional
public Long execute(EatMealCommand command) {
validateGAndMultiple(command);
List<FoodEntity> foodEntities =
foodPort.queryAllEntities(
command.eatMealFoodDTOS().stream()
Expand All @@ -62,34 +59,4 @@ public Long execute(EatMealCommand command) {
foodEntities, mealEntity, command.eatMealFoodDTOS()));
return mealEntity.getId();
}

private void validateGAndMultiple(EatMealCommand command) {
command.eatMealFoodDTOS()
.forEach(
dto -> {
if ((dto.multiple() != null && dto.g() != null)
|| (dto.multiple() == null && dto.g() == null)) {
throw InvalidMultipleAndGException.EXCEPTION;
}
if (dto.multiple() != null) {
validateMultiple(dto.multiple());
}
if (dto.g() != null) {
validateG(dto.g());
}
});
}

private void validateMultiple(double multiple) {
if (multiple <= 0 || multiple > 100) {
throw InvalidMultipleException.EXECPTION;
}
}

private void validateG(int g) {
if (g <= 0) {
throw InvalidGException.EXCEPTION;
}
}
// todo EatMealCommand에 옮기기
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class ValidationMessage {
public static final String NOT_NULL = "입력해 주세요";
public static final String NOT_BLANK = "입력해 주세요";
public static final String MIN_0 = "0이상 입력해 주세요";

public static final String ENUM_SUFFIX = "는 올바르지 않은 값입니다.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.gaebaljip.exceed.common.ValidationMessage;

public class EnumValidator implements ConstraintValidator<Enum, String> {

private Enum annotation;
Expand All @@ -25,7 +27,7 @@ public boolean isValid(String value, ConstraintValidatorContext context) {
}
}
}
context.buildConstraintViolationWithTemplate(value + "는 올바르지 않은 값입니다.")
context.buildConstraintViolationWithTemplate(value + ValidationMessage.ENUM_SUFFIX)
.addConstraintViolation()
.disableDefaultConstraintViolation();
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.gaebaljip.exceed.dto;

import javax.validation.constraints.NotNull;

import com.gaebaljip.exceed.common.ValidationMessage;

import lombok.Builder;

public record EatMealFoodDTO(Long foodId, Double multiple, Integer g) {
public record EatMealFoodDTO(
@NotNull(message = "음식PK를 " + ValidationMessage.NOT_NULL) Long foodId,
Double multiple,
Integer g) {

@Builder
public EatMealFoodDTO {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import com.gaebaljip.exceed.application.domain.meal.MealType;
import com.gaebaljip.exceed.common.ValidationMessage;
import com.gaebaljip.exceed.common.annotation.Enum;
import com.gaebaljip.exceed.dto.EatMealFoodDTO;

import lombok.Builder;

public record EatMealRequest(
List<EatMealFoodDTO> eatMealFoodDTOS,
@Valid List<EatMealFoodDTO> eatMealFoodDTOS,
@Enum(enumClass = MealType.class) String mealType,
String fileName) {
@NotBlank(message = "파일명을 " + ValidationMessage.NOT_BLANK) String fileName) {
@Builder
public EatMealRequest {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;
Expand All @@ -16,6 +17,7 @@
import com.gaebaljip.exceed.application.port.in.meal.EatMealUsecase;
import com.gaebaljip.exceed.application.port.in.meal.UploadImageUsecase;
import com.gaebaljip.exceed.common.ControllerTest;
import com.gaebaljip.exceed.common.ValidationMessage;
import com.gaebaljip.exceed.common.WithMockUser;
import com.gaebaljip.exceed.dto.EatMealFoodDTO;
import com.gaebaljip.exceed.dto.request.EatMealRequest;
Expand Down Expand Up @@ -48,4 +50,67 @@ void when_eatMeal_gNotNullAndMultipleNull_expected_returnPresignedUrl() throws E
// then
resultActions.andExpect(status().isCreated());
}

@Test
@WithMockUser
void when_eatMeal_withEmptyFileName_thenBadRequest() throws Exception {
// given
EatMealFoodDTO eatMealFoodDTO = new EatMealFoodDTO(1L, null, 100);
EatMealRequest request = new EatMealRequest(List.of(eatMealFoodDTO), "LUNCH", "");

// when
ResultActions resultActions =
mockMvc.perform(
post("/v1/meal")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error.reason").value("파일명을 " + ValidationMessage.NOT_BLANK));
}

@Test
@WithMockUser
void when_eatMeal_withInvalidMealType_thenBadRequest() throws Exception {
// given
EatMealFoodDTO eatMealFoodDTO = new EatMealFoodDTO(1L, null, 100);
EatMealRequest request =
new EatMealRequest(List.of(eatMealFoodDTO), "INVALID_MEAL_TYPE", "test.jpeg");

// when
ResultActions resultActions =
mockMvc.perform(
post("/v1/meal")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions
.andExpect(status().isBadRequest())
.andExpect(
jsonPath("$.error.reason")
.value("INVALID_MEAL_TYPE" + ValidationMessage.ENUM_SUFFIX));
}

@Test
@WithMockUser
void when_eatMeal_withNullFoodId_thenBadRequest() throws Exception {
// given
EatMealFoodDTO eatMealFoodDTO = new EatMealFoodDTO(null, null, 100);
EatMealRequest request = new EatMealRequest(List.of(eatMealFoodDTO), "LUNCH", "test.jpeg");

// when
ResultActions resultActions =
mockMvc.perform(
post("/v1/meal")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error.reason").value("음식PK를 " + ValidationMessage.NOT_NULL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.gaebaljip.exceed.dto.TargetMealDTO;

@WebMvcTest(GetMealController.class)
class SpecificMealDTOControllerTest extends ControllerTest {
class SpecificMealControllerTest extends ControllerTest {

@MockBean private GetMaintainMealUsecase getMaintainMealUsecase;

Expand Down

This file was deleted.

Loading

0 comments on commit 4465370

Please sign in to comment.