Skip to content

Commit

Permalink
Merge pull request #30 from CAUSOLDOUTMEN/feature/28-feat-usercontrol…
Browse files Browse the repository at this point in the history
…lertest

✅ Feat: UserControllerTest 구현 (#28)
  • Loading branch information
synoti21 authored Oct 15, 2023
2 parents f3c862c + ce4428e commit a0ba351
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ApiResponse<Long> saveUser(CreateUserDto createUserDto) {

// 회원 기본정보 조회
@Operation(summary = "[프로필] 회원 기본정보 조회", description = "회원 기본정보를 조회합니다.")
@GetMapping("{userId}/info/simple/")
@GetMapping("{userId}/info/simple")
public ApiResponse<ResponseSimpleUserDto> getSimpleUserInfo(@PathVariable Long userId) {
return ApiResponse.success(userService.getSimpleUserInfo(userId), ResponseCode.USER_CREATE_SUCCESS.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ResponseSearchUserDto {
private Long userId;
private String name;
private String image;
private boolean isFollow; // 유저가 이미 팔로우한 유저인지 확인
private boolean follow; // 유저가 이미 팔로우한 유저인지 확인

public static ResponseSearchUserDto of(Long userId, String name, String image, boolean isFollow) {
return new ResponseSearchUserDto(userId, name, image, isFollow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public void updateUserInfo(UpdateUserDto updateUserDto) {
user.updateUser(updateUserDto.getName(), updateUserDto.getHeight(), updateUserDto.getWeight(), updateUserDto.getAge());
}

// 회원 기준영양소 조회
// 회원 기준섭취량 조회
@Transactional(readOnly = true)
public ResponseUserNutritionDto getUserNutrition(Long userId) {
User user = getUserById(userId);
return ResponseUserNutritionDto.from(user);
}

// 회원 기준영양소 직접 수정
// 회원 기준섭취량 직접 수정
@Transactional
public void updateBaseNutrition(UpdateUserNutritionDto updateUserNutritionDto) {
User user = getUserById(updateUserNutritionDto.getUserId());
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/com/diareat/diareat/util/api/ApiBody.java

This file was deleted.

13 changes: 5 additions & 8 deletions src/main/java/com/diareat/diareat/util/api/ApiHeader.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.diareat.diareat.util.api;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ApiHeader {

private int code;
private String message;

public ApiHeader(int code, String message) {
this.code = code;
this.message = message;
}

public ApiHeader() {
}

public int getCode() {
return code;
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/diareat/diareat/util/api/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
public class ApiResponse<T> {

private ApiHeader header;
private ApiBody body;
private T data;
private String msg;

private static final int SUCCESS = 200;

public ApiResponse(ApiHeader header, ApiBody body) {
this.header = header;
this.body = body;
}

public ApiResponse(ApiHeader header) {
private ApiResponse(ApiHeader header, T data, String msg) {
this.header = header;
this.data = data;
this.msg = msg;
}

public static <T> ApiResponse<T> success(T data, String message) {
return new ApiResponse<T>(new ApiHeader(SUCCESS, "SUCCESS"), new ApiBody(data, message));
return new ApiResponse<T>(new ApiHeader(SUCCESS, "SUCCESS"), data, message);
}

public static <T> ApiResponse<T> fail(ResponseCode responseCode) {
return new ApiResponse(new ApiHeader(responseCode.getHttpStatusCode(), responseCode.getMessage()), new ApiBody(null, responseCode.getMessage()));
return new ApiResponse<T>(new ApiHeader(responseCode.getHttpStatusCode(), responseCode.getMessage()), null, responseCode.getMessage());
}
}
222 changes: 222 additions & 0 deletions src/test/java/com/diareat/diareat/controller/UserControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package com.diareat.diareat.controller;

import com.diareat.diareat.user.controller.UserController;
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.dto.*;
import com.diareat.diareat.user.service.UserService;
import com.diareat.diareat.util.api.ApiResponse;
import com.diareat.diareat.util.api.ResponseCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@WebMvcTest(controllers = UserController.class)
class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@MockBean
private UserService userService;

private final Long testUserId = 1L;
private final ObjectMapper mapper = new ObjectMapper();
private final User testUser = User.createUser("test", "test","test", 180, 70, 0, 20, BaseNutrition.createNutrition(2000, 300, 80, 80));

@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
testUser.setId(testUserId);
when(userService.getSimpleUserInfo(testUserId)).thenReturn(ResponseSimpleUserDto.of(testUser.getName(), testUser.getImage(), 100.0));
when(userService.getUserInfo(testUserId)).thenReturn(ResponseUserDto.from(testUser));
}

@DisplayName("회원정보 저장")
@Test
@WithMockUser("test")
void testSaveUser() throws Exception {
// Given
ApiResponse<Long> expectedResponse = ApiResponse.success(testUserId, ResponseCode.USER_CREATE_SUCCESS.getMessage());
String json = "{\"name\":\"test\",\"image\":\"test\",\"keyCode\":\"test\",\"gender\":0,\"height\":180,\"weight\":70,\"age\":20}";
when(userService.saveUser(any(CreateUserDto.class))).thenReturn(testUserId);

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.post("/api/user/save")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.msg").value(expectedResponse.getMsg()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data").value(expectedResponse.getData()));
}


@DisplayName("회원 기본정보 조회")
@Test
@WithMockUser("test")
void testGetSimpleUserInfo() throws Exception {
// Given
ApiResponse<ResponseSimpleUserDto> expectedResponse = ApiResponse.success(
ResponseSimpleUserDto.of(testUser.getName(), testUser.getImage(), 100.0), ResponseCode.USER_CREATE_SUCCESS.getMessage());

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.get("/api/user/1/info/simple", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.name").value(expectedResponse.getData().getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.image").value(expectedResponse.getData().getImage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.nutritionScore").value(expectedResponse.getData().getNutritionScore()));
}

@DisplayName("회원정보 조회")
@Test
@WithMockUser("test")
void getUserInfo() throws Exception {
// Given
ApiResponse<ResponseUserDto> expectedResponse = ApiResponse.success(
ResponseUserDto.from(testUser), ResponseCode.USER_CREATE_SUCCESS.getMessage());

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.get("/api/user/1/info", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.name").value(expectedResponse.getData().getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.height").value(expectedResponse.getData().getHeight()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.weight").value(expectedResponse.getData().getWeight()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.age").value(expectedResponse.getData().getAge()));
}

@DisplayName("회원정보 수정")
@Test
@WithMockUser("test")
void updateUser() throws Exception {
// Given
ApiResponse<Void> expectedResponse = ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());
UpdateUserDto user = UpdateUserDto.of(testUserId, "test2", 170, 80, 21, true);
String json = mapper.writeValueAsString(user);

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.put("/api/user/update")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.msg").value(expectedResponse.getMsg()));
}

@DisplayName("회원 기준섭취량 조회")
@Test
@WithMockUser("test")
void getUserNutrition() throws Exception {
// Given
when(userService.getUserNutrition(testUserId)).thenReturn(ResponseUserNutritionDto.of(2000, 300, 300, 80));
ApiResponse<ResponseUserNutritionDto> expectedResponse = ApiResponse.success(
ResponseUserNutritionDto.of(2000, 300, 300, 80), ResponseCode.USER_READ_SUCCESS.getMessage());

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.get("/api/user/1/nutrition", 1L)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.calorie").value(expectedResponse.getData().getCalorie()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.protein").value(expectedResponse.getData().getProtein()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.fat").value(expectedResponse.getData().getFat()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data.carbohydrate").value(expectedResponse.getData().getCarbohydrate()));
}

@DisplayName("회원 기준섭취량 직접 수정")
@Test
@WithMockUser("test")
void updateUserNutrition() throws Exception {
// Given
ApiResponse<Void> expectedResponse = ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());
UpdateUserNutritionDto nutrition = UpdateUserNutritionDto.of(testUserId, 2000, 300, 300, 80);
String json = mapper.writeValueAsString(nutrition);

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.put("/api/user/1/nutrition")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.msg").value(expectedResponse.getMsg()));
}

@DisplayName("회원의 친구 검색 결과 조회")
@Test
@WithMockUser("test")
void searchUser() throws Exception {
// Given
when(userService.searchUser(testUserId, "test")).thenReturn(List.of(ResponseSearchUserDto.of(2L, "test2", "test2", true)));
ApiResponse<List<ResponseSearchUserDto>> expectedResponse = ApiResponse.success(
List.of(ResponseSearchUserDto.of(2L, "test2", "test2", true)), ResponseCode.USER_SEARCH_SUCCESS.getMessage());

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.get("/api/user/1/search/test").param("name", "test")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].userId").value(2L))
.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].name").value(expectedResponse.getData().get(0).getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].image").value(expectedResponse.getData().get(0).getImage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].follow").value(expectedResponse.getData().get(0).isFollow()));
}

@DisplayName("회원이 특정 회원 팔로우 및 팔로우 취소")
@Test
@WithMockUser("test")
void followUser() throws Exception {
// Given
ApiResponse<Void> expectedResponse = ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());

// When & Then
mockMvc.perform( MockMvcRequestBuilders
.post("/api/user/1/follow/2")
//.delete("/api/user/1/follow/2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.msg").value(expectedResponse.getMsg()));
}
}
Loading

0 comments on commit a0ba351

Please sign in to comment.