Skip to content

Commit

Permalink
Merge pull request #40 from f-lab-edu/feature/#39
Browse files Browse the repository at this point in the history
[#39] 유효성 검사 테스트 추가
  • Loading branch information
koo995 authored Sep 1, 2024
2 parents b7d638f + 2b637ae commit 35fa8cb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public enum StatusConst {
INVALID_PRODUCT_ID(4003, "존재하지 않는 식품입니다."),
DIARY_NOT_FOUND(4004, "해당 다이어리를 찾을 수 없습니다."),
DUPLICATED_DIARY(4005, "이미 등록된 다이어리입니다."),
NOT_ALLOWED_SERVING_UNIT(4006, "허용되지 않은 서빙 단위입니다."),
VALIDATION_CHECK_FAIL(4007, "유효성 검사에 실패했습니다.");
VALIDATION_CHECK_FAIL(6001, "유효성 검사에 실패했습니다."),
NOT_ALLOWED_SERVING_UNIT(6002, "허용되지 않은 서빙 단위입니다.");

private final int statusCode;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flab.nutridiary.diary.dto;

import flab.nutridiary.commom.validation.EnumValidator;
import flab.nutridiary.diary.domain.MealType;
import flab.nutridiary.diary.domain.ProductIntakeInfo;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
Expand All @@ -13,7 +15,8 @@
public class AddDiaryRecordRequest {
@NotNull(message = "상품 ID를 입력해주세요.")
private Long productId;
// @EnumValidator(enumClass = MealType.class, message = "올바른 식사 타입을 입력해주세요.")

@EnumValidator(enumClass = MealType.class, message = "올바른 식사 타입을 입력해주세요.")
private String mealType;

@NotNull(message = "섭취량을 입력해주세요.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flab.nutridiary.diary.dto;

import flab.nutridiary.commom.validation.EnumValidator;
import flab.nutridiary.diary.domain.MealType;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
Expand All @@ -16,7 +18,7 @@ public class DiaryRegisterRequest {

private Long memberId = 1L;

// @EnumValidator(enumClass = MealType.class, message = "올바른 식사 타입을 입력해주세요.")
@EnumValidator(enumClass = MealType.class, message = "올바른 식사 타입을 입력해주세요.")
private String mealType;

@NotNull(message = "섭취량을 입력해주세요.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,26 @@ void addDiaryRecordWithInvalidServingUnit() throws Exception {
.contentType("application/json")
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.statusCode").value(4006))
.andExpect(jsonPath("$.statusCode").value(6002))
.andExpect(jsonPath("$.message").value("허용되지 않은 서빙 단위입니다."))
.andExpect(jsonPath("$.data.diaryId").doesNotExist());
}

@DisplayName("올바르지 않은 식사타입 실패 테스트")
@Test
void mealType() throws Exception {
// given
DiaryRegisterRequest diaryRegisterRequest = new DiaryRegisterRequest(savedProductId, "WRONG", valueOf(1), "gram", LocalDate.of(2024, 8, 10));

// when then
mockMvc.perform(
post("/diary/new")
.content(objectMapper.writeValueAsString(diaryRegisterRequest))
.contentType("application/json")
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.statusCode").value(6001))
.andExpect(jsonPath("$.message").value("유효성 검사에 실패했습니다."))
.andExpect(jsonPath("$.data.mealType").value("올바른 식사 타입을 입력해주세요."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,6 @@ void exception() throws Exception {
// when then
BusinessException businessException = Assertions.assertThrows(BusinessException.class, () -> diaryRegisterService.createDiary(diaryRegisterRequest));
assertThat(businessException.getMessage()).isEqualTo("허용되지 않은 서빙 단위입니다.");
assertThat(businessException.getStatusCode()).isEqualTo(4006);
assertThat(businessException.getStatusCode()).isEqualTo(6002);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void addProduct() throws Exception {
.andExpect(jsonPath("$.data.productId").exists());
}

@DisplayName("상품 등록 실패 컨트롤러 테스트.")
@DisplayName("중복된 상품 등록 실패 컨트롤러 테스트.")
@Test
void addProductEx() throws Exception {
// given
Expand Down Expand Up @@ -96,4 +96,33 @@ void addProductEx() throws Exception {
.andExpect(jsonPath("$.statusCode").value(4002))
.andExpect(jsonPath("$.message").value("이미 등록된 식품입니다."));
}

@DisplayName("유효성 체크 실패 테스트")
@Test
void validation() throws Exception {
// given
NewProductRequest newProductRequest = NewProductRequest.builder()
.productName("상품명")
.corpName("업체명")
.productDefaultServingSize(BigDecimal.TWO)
.productTotalWeightGram(BigDecimal.valueOf(-1))
.calories(BigDecimal.valueOf(120))
.carbohydrate(BigDecimal.valueOf(15.5))
.protein(BigDecimal.valueOf(-100))
.fat(BigDecimal.valueOf(5.5))
.build();

// when then
mockMvc.perform(
post("/product/new")
.content(objectMapper.writeValueAsString(newProductRequest))
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.statusCode").value(6001))
.andExpect(jsonPath("$.message").value("유효성 검사에 실패했습니다."))
.andExpect(jsonPath("$.data.productTotalWeightGram").value("총 중량은 0 이상이어야 합니다."))
.andExpect(jsonPath("$.data.protein").value("단백질은 0 이상이어야 합니다."))
.andExpect(jsonPath("$.data.productDefaultServingUnit").value("서빙 단위를 입력해주세요."));
}
}

0 comments on commit 35fa8cb

Please sign in to comment.