Skip to content

Commit

Permalink
Merge pull request #27 from CAUSOLDOUTMEN/feature/26-refactor-userser…
Browse files Browse the repository at this point in the history
…vicetest

✅ Refactor: UserServiceTest Mock으로 리팩토링 (#26)
  • Loading branch information
synoti21 authored Oct 15, 2023
2 parents 7595a46 + 2e8afd8 commit f3c862c
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import net.bytebuddy.implementation.bind.annotation.Empty;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/diareat/diareat/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public class User {
private List<FavoriteFood> favoriteFoods = new ArrayList<>();

// 생성 메서드
public static User createUser(String name, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) {
public static User createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) {
User user = new User();
user.name = name;
user.image = image;
user.keyCode = keyCode;
user.height = height;
user.weight = weight;
Expand All @@ -68,4 +69,8 @@ public void updateUser(String name, int height, int weight, int age) {
public void updateBaseNutrition(BaseNutrition baseNutrition) {
this.baseNutrition = baseNutrition;
}

public void setId(Long id) { // 테스트코드용 메서드
this.id = id;
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/diareat/diareat/user/dto/CreateUserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
public class CreateUserDto {

private String name;
private String image;
private String keyCode;
private int gender;
private int height;
private int weight;
private int age;

public static CreateUserDto of(String name, String keyCode, int gender, int height, int weight, int age){
return new CreateUserDto(name, keyCode, gender, height, weight, age);
public static CreateUserDto of(String name, String image, String keyCode, int gender, int height, int weight, int age){
return new CreateUserDto(name, image, keyCode, gender, height, weight, age);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -29,7 +30,7 @@ public Long saveUser(CreateUserDto createUserDto) {
// BaseNutrition baseNutrition = BaseNutrition.createNutrition(createUserDto.getGender(), createUserDto.getAge(), createUserDto.getHeight(), createUserDto.getWeight());
if (userRepository.existsByName(createUserDto.getName()))
throw new UserException(ResponseCode.USER_ALREADY_EXIST);
User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition);
User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition);
return userRepository.save(user).getId();
}

Expand Down Expand Up @@ -68,6 +69,7 @@ public void updateBaseNutrition(UpdateUserNutritionDto updateUserNutritionDto) {
User user = getUserById(updateUserNutritionDto.getUserId());
BaseNutrition baseNutrition = BaseNutrition.createNutrition(updateUserNutritionDto.getCalorie(), updateUserNutritionDto.getCarbohydrate(), updateUserNutritionDto.getProtein(), updateUserNutritionDto.getFat());
user.updateBaseNutrition(baseNutrition);
userRepository.save(user);
}

// 회원 탈퇴
Expand All @@ -81,7 +83,8 @@ public void deleteUser(Long userId) {
@Transactional(readOnly = true)
public List<ResponseSearchUserDto> searchUser(Long hostId, String name) {
validateUser(hostId);
List<User> users = userRepository.findAllByNameContaining(name);
List<User> users = new ArrayList<>(userRepository.findAllByNameContaining(name));
users.removeIf(user -> user.getId().equals(hostId)); // 검색 결과에서 자기 자신은 제외 (removeIf 메서드는 ArrayList에만 존재)
return users.stream()
.map(user -> ResponseSearchUserDto.of(user.getId(), user.getName(), user.getImage(), followRepository.existsByFromUserAndToUser(hostId, user.getId()))).collect(Collectors.toList());
}
Expand All @@ -103,7 +106,7 @@ public void unfollowUser(Long userId, Long unfollowId) {
validateUser(userId);
validateUser(unfollowId);
// 이미 팔로우 취소한 경우
if (followRepository.existsByFromUserAndToUser(userId, unfollowId))
if (!followRepository.existsByFromUserAndToUser(userId, unfollowId))
throw new UserException(ResponseCode.UNFOLLOWED_ALREADY);
followRepository.delete(Follow.makeFollow(userId, unfollowId));
}
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/com/diareat/diareat/service/FoodServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setUp() {
void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리스트 불러오기
// given
BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1,180, 80,18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1,180, 80,18));

//when
Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood",testBaseNutrition));
Expand All @@ -69,7 +69,7 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리
void testUpdateFood() {
//given
BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition));

//when
Expand All @@ -90,7 +90,7 @@ void testUpdateFood() {
void testDeleteFood() {
//given
BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser","testImage", "tessPassword", 1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition));

//when
Expand All @@ -103,7 +103,7 @@ void testDeleteFood() {
void testSaveAndGetFavoriteFood() {
//given
BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition));

//when
Expand All @@ -119,7 +119,7 @@ void testSaveAndGetFavoriteFood() {
void testUpdateFavoriteFood() {
//given
BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition));
Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition));

Expand All @@ -142,7 +142,7 @@ void testUpdateFavoriteFood() {
void testDeleteFavoriteFood() {
//given
BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition));
Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition));

Expand All @@ -156,7 +156,7 @@ void testDeleteFavoriteFood() {
void testNutritionSumByDate(){
//given
BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword",1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword",1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood", testFoodNutrition));
Food food = foodRepository.getReferenceById(foodId);

Expand All @@ -177,15 +177,15 @@ void testNutritionSumByDate(){
void testNutritionSumByWeekAndMonth(){
//given
BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(100,150,200,250);
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword",1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword",1, 180, 80, 18));
Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood", testFoodNutrition));

}

@Test
void getBest3FoodTest() {
// given
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1, 180, 80, 18));
foodService.saveFood(CreateFoodDto.of(userId, "Food1", BaseNutrition.createNutrition(100, 100 ,10, 1)));
foodService.saveFood(CreateFoodDto.of(userId, "Food2", BaseNutrition.createNutrition(100, 100 ,8, 2)));
foodService.saveFood(CreateFoodDto.of(userId, "Food3", BaseNutrition.createNutrition(100, 100 ,6, 3)));
Expand All @@ -207,7 +207,7 @@ void getBest3FoodTest() {
@Test
void getWorst3FoodsTest() {
// given
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1, 180, 80, 18));
Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1, 180, 80, 18));
foodService.saveFood(CreateFoodDto.of(userId, "Food1", BaseNutrition.createNutrition(100, 50 ,10, 1)));
foodService.saveFood(CreateFoodDto.of(userId, "Food2", BaseNutrition.createNutrition(100, 100 ,8, 20)));
foodService.saveFood(CreateFoodDto.of(userId, "Food3", BaseNutrition.createNutrition(100, 80 ,6, 7)));
Expand Down
Loading

0 comments on commit f3c862c

Please sign in to comment.