-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[BE/TEST] DTO Validation test
- Loading branch information
Showing
6 changed files
with
438 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
BE/exceed/src/test/java/com/gaebaljip/exceed/auth/adapter/in/AuthControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.gaebaljip.exceed.auth.adapter.in; | ||
|
||
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.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import com.gaebaljip.exceed.auth.adapter.AuthController; | ||
import com.gaebaljip.exceed.auth.application.port.AuthService; | ||
import com.gaebaljip.exceed.common.ControllerTest; | ||
import com.gaebaljip.exceed.common.ValidationMessage; | ||
import com.gaebaljip.exceed.dto.request.LoginRequest; | ||
|
||
@WebMvcTest(AuthController.class) | ||
public class AuthControllerTest extends ControllerTest { | ||
@MockBean private AuthService authService; | ||
|
||
@Test() | ||
@DisplayName("로그인 실패 - 비밀번호 형식 안 맞을 때") | ||
void login_fail() throws Exception { | ||
LoginRequest loginRequest = new LoginRequest("abcd1111!@gmail.com", "abcd"); | ||
|
||
ResultActions resultActions = | ||
mockMvc.perform( | ||
MockMvcRequestBuilders.post("/v1/auth/login") | ||
.content(om.writeValueAsString(loginRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
resultActions.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test() | ||
@DisplayName("로그인 실패 - 이메일 형식 안 맞을 때") | ||
void login_fail2() throws Exception { | ||
LoginRequest loginRequest = new LoginRequest("abcd1111gmail.com", "Abc@123"); | ||
|
||
ResultActions resultActions = | ||
mockMvc.perform( | ||
MockMvcRequestBuilders.post("/v1/auth/login") | ||
.content(om.writeValueAsString(loginRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value(ValidationMessage.INVALID_EMAIL)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
284 changes: 284 additions & 0 deletions
284
BE/exceed/src/test/java/com/gaebaljip/exceed/food/adapter/in/CreateFoodControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
package com.gaebaljip.exceed.food.adapter.in; | ||
|
||
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.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import com.gaebaljip.exceed.common.ControllerTest; | ||
import com.gaebaljip.exceed.common.ValidationMessage; | ||
import com.gaebaljip.exceed.common.WithMockUser; | ||
import com.gaebaljip.exceed.dto.request.CreateFoodRequest; | ||
import com.gaebaljip.exceed.food.application.CreateFoodService; | ||
|
||
@WebMvcTest(CreateFoodController.class) | ||
public class CreateFoodControllerTest extends ControllerTest { | ||
@MockBean private CreateFoodService createFoodService; | ||
|
||
@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)); | ||
} | ||
} |
Oops, something went wrong.