Skip to content

Commit

Permalink
test/#353: CreateFoodRequest Valid test
Browse files Browse the repository at this point in the history
  • Loading branch information
LJH098 committed Jul 4, 2024
1 parent 30970b8 commit bce1e05
Show file tree
Hide file tree
Showing 3 changed files with 311 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gaebaljip.exceed.food.adapter.in;

import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -18,8 +20,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;

@RestController
@SecurityRequirement(name = "access-token")
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
package com.gaebaljip.exceed.food;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.test.web.servlet.ResultActions;

import com.gaebaljip.exceed.common.IntegrationTest;
import com.gaebaljip.exceed.common.ValidationMessage;
import com.gaebaljip.exceed.common.WithMockUser;
import com.gaebaljip.exceed.dto.request.CreateFoodRequest;

class CreateFoodIntegrationTest extends IntegrationTest {

@Test
@DisplayName("음식 추가 : 성공")
@WithMockUser
void createFood() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

String responseBody = resultActions.andReturn().getResponse().getContentAsString();

// then
resultActions.andExpect(status().isOk());
}

@Test
@DisplayName("음식 추가 : 실패 - 이름이 없는 경우")
@WithMockUser
void createFoodFail() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("이름을 " + ValidationMessage.NOT_BLANK));
}

@Test
@DisplayName("음식 추가 : 실패 - 칼로리가 없는 경우")
@WithMockUser
void createFoodFail2() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("칼로리를 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 탄수화물이 없는 경우")
@WithMockUser
void createFoodFail3() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("탄수화물을 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 식이섬유가 없는 경우")
@WithMockUser
void createFoodFail4() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("식이섬유를 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 지방이 없는 경우")
@WithMockUser
void createFoodFail5() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("지방을 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 단백질이 없는 경우")
@WithMockUser
void createFoodFail6() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.servingSize(1.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("단백질을 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 1회 제공량이 없는 경우")
@WithMockUser
void createFoodFail7() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.sodium(10.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("1회 제공량을 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 당이 없는 경우")
@WithMockUser
void createFoodFail8() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sodium(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("당을 " + ValidationMessage.NOT_NULL));
}

@Test
@DisplayName("음식 추가 : 실패 - 나트륨이 없는 경우")
@WithMockUser
void createFoodFail9() throws Exception {
// given
CreateFoodRequest request =
CreateFoodRequest.builder()
.name("민초마라탕")
.calorie(1000.0)
.carbohydrate(100.0)
.dietaryFiber(10.0)
.fat(10.0)
.protein(10.0)
.servingSize(1.0)
.sugars(10.0)
.build();
// when
ResultActions resultActions =
mockMvc.perform(
RestDocumentationRequestBuilders.post("/v1/food")
.content(om.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON));

// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.error.reason").value("나트륨을 " + ValidationMessage.NOT_NULL));
}
}

This file was deleted.

0 comments on commit bce1e05

Please sign in to comment.