From c870e14e1734fb072c6030a65d6712ef7a39a44c Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 22:15:46 +0900 Subject: [PATCH 01/15] :recycle: refactor: testSaveAndGetFood() mocking (#29) --- .../com/diareat/diareat/food/domain/Food.java | 3 +- .../diareat/service/FoodServiceTest.java | 375 +++++++++--------- 2 files changed, 194 insertions(+), 184 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/domain/Food.java b/src/main/java/com/diareat/diareat/food/domain/Food.java index 851c5e5..8d9f9e2 100644 --- a/src/main/java/com/diareat/diareat/food/domain/Food.java +++ b/src/main/java/com/diareat/diareat/food/domain/Food.java @@ -5,7 +5,6 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import org.springframework.data.annotation.CreatedDate; import javax.persistence.*; import java.time.LocalDate; @@ -56,4 +55,6 @@ public void updateFood(String name, BaseNutrition baseNutrition) { public boolean isFavorite() { return this.favoriteFood != null; } + + public void setId(long id) {this.id = id;} } diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 651f7b3..652e35f 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -1,6 +1,5 @@ package com.diareat.diareat.service; -import com.diareat.diareat.food.domain.FavoriteFood; import com.diareat.diareat.food.domain.Food; import com.diareat.diareat.food.dto.*; import com.diareat.diareat.food.repository.FavoriteFoodRepository; @@ -11,218 +10,228 @@ import com.diareat.diareat.user.dto.CreateUserDto; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.user.service.UserService; -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.context.SpringBootTest; -import org.springframework.transaction.annotation.Transactional; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalDate; import java.util.List; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; -@SpringBootTest -@Transactional +@ExtendWith(MockitoExtension.class) class FoodServiceTest { - @Autowired + @InjectMocks FoodService foodService; - @Autowired + @InjectMocks UserService userService; - @Autowired + @Mock FoodRepository foodRepository; - @Autowired + @Mock FavoriteFoodRepository favoriteFoodRepository; - @Autowired + @Mock UserRepository userRepository; - @BeforeEach - public void setUp() { - userRepository.deleteAll(); - foodRepository.deleteAll(); - favoriteFoodRepository.deleteAll(); - } - + @DisplayName("음식 정보 저장") @Test void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리스트 불러오기 // given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword", 1,180, 80,18)); - - //when - Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood",testBaseNutrition)); - Food testFood = foodRepository.getReferenceById(foodId); - - List responseFoodDtoList = foodService.getFoodListByDate(userId, testFood.getDate()); - - assertNotNull(responseFoodDtoList); - assertEquals("testFood",responseFoodDtoList.get(0).getName()); - } - - @Test - void testUpdateFood() { - //given - BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); - Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); - - //when - BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); - foodService.updateFood(UpdateFoodDto.of(foodId, "testChangedFood", testChangedBaseNutrition)); - - Food changedFood = foodRepository.getReferenceById(foodId); - - assertNotNull(changedFood); - assertEquals("testChangedFood", changedFood.getName()); - assertEquals(2,changedFood.getBaseNutrition().getKcal()); - assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); - assertEquals(4,changedFood.getBaseNutrition().getProtein()); - assertEquals(5,changedFood.getBaseNutrition().getFat()); - } - - @Test - void testDeleteFood() { - //given - BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser","testImage", "tessPassword", 1, 180, 80, 18)); - Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); - - //when - foodService.deleteFood(foodId); - - assertNull(foodRepository.findById(foodId).orElse(null)); - } - - @Test - void testSaveAndGetFavoriteFood() { - //given - BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); - Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); - - //when - Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); - - List responseFavoriteFoodDtoList = foodService.getFavoriteFoodList(userId); - - assertNotNull(responseFavoriteFoodDtoList); - assertEquals("testFood",responseFavoriteFoodDtoList.get(0).getName()); - } - - @Test - void testUpdateFavoriteFood() { - //given - BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - 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)); + CreateUserDto createUserDto = CreateUserDto.of("testUser", "testImage","testPassword", 1,180, 80,18); + User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), testBaseNutrition); + user.setId(1L); + CreateFoodDto createFoodDto = CreateFoodDto.of(user.getId(), "testFood", testBaseNutrition); + Food food = Food.createFood("testFood", user, testBaseNutrition); + food.setId(2L); - //when - BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); - foodService.updateFavoriteFood(UpdateFavoriteFoodDto.of(favoriteFoodId, "testChangedFood", testChangedBaseNutrition)); - - FavoriteFood changedFood = favoriteFoodRepository.getReferenceById(favoriteFoodId); - - assertNotNull(changedFood); - assertEquals("testChangedFood", changedFood.getName()); - assertEquals(2,changedFood.getBaseNutrition().getKcal()); - assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); - assertEquals(4,changedFood.getBaseNutrition().getProtein()); - assertEquals(5,changedFood.getBaseNutrition().getFat()); - } - - @Test - void testDeleteFavoriteFood() { - //given - BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - 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)); + given(userRepository.findById(any(Long.class))).willReturn(Optional.of(user)); + given(foodRepository.save(any(Food.class))).willReturn(food); + given(foodRepository.findAllByUserIdAndDate(any(Long.class), any(LocalDate.class))).willReturn(List.of(food)); //when - foodService.deleteFavoriteFood(favoriteFoodId); + Long foodId = foodService.saveFood(createFoodDto); - assertNull(favoriteFoodRepository.findById(favoriteFoodId).orElse(null)); - } + List responseFoodDtoList = foodService.getFoodListByDate(user.getId(), food.getDate()); - @Test - void testNutritionSumByDate(){ - //given - BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); - 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); - - //when - ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByDate(userId,food.getDate()); - assertEquals(1400, responseNutritionSumByDateDto.getTotalKcal()); - assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); - assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); - assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); - - assertEquals(Math.round((((double)1400 / (double)2000) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioKcal()); - assertEquals(Math.round((((double)150 / (double)300) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); - assertEquals(Math.round((((double)200 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioProtein()); - assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); - } - - @Test - void testNutritionSumByWeekAndMonth(){ - //given - BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(100,150,200,250); - 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", "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))); - foodService.saveFood(CreateFoodDto.of(userId, "Food4", BaseNutrition.createNutrition(100, 100 ,4, 4))); - foodService.saveFood(CreateFoodDto.of(userId, "Food5", BaseNutrition.createNutrition(100, 100 ,2, 5))); - - - // when - ResponseFoodRankDto response = foodService.getBestFoodByWeek(userId); - List top3Foods = response.getRankFoodList(); - - // then - assertEquals(3, top3Foods.size()); - assertEquals("Food1", top3Foods.get(0).getName()); - assertEquals("Food2", top3Foods.get(1).getName()); - assertEquals("Food3", top3Foods.get(2).getName()); + assertEquals(2L, foodId); + assertEquals("testFood",responseFoodDtoList.get(0).getName()); + verify(foodRepository, times(1)).save(any(Food.class)); } - @Test - void getWorst3FoodsTest() { - // given - 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))); - foodService.saveFood(CreateFoodDto.of(userId, "Food4", BaseNutrition.createNutrition(100, 100 ,4, 5))); - foodService.saveFood(CreateFoodDto.of(userId, "Food5", BaseNutrition.createNutrition(100, 90 ,2, 6))); - - - // when - ResponseFoodRankDto response = foodService.getWorstFoodByWeek(userId); - List top3Foods = response.getRankFoodList(); - - // then - assertEquals(3, top3Foods.size()); - assertEquals("Food2", top3Foods.get(0).getName()); - assertEquals("Food4", top3Foods.get(1).getName()); - assertEquals("Food5", top3Foods.get(2).getName()); - } +// @Test +// void testUpdateFood() { +// //given +// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); +// Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); +// Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); +// +// //when +// BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); +// foodService.updateFood(UpdateFoodDto.of(foodId, "testChangedFood", testChangedBaseNutrition)); +// +// Food changedFood = foodRepository.getReferenceById(foodId); +// +// assertNotNull(changedFood); +// assertEquals("testChangedFood", changedFood.getName()); +// assertEquals(2,changedFood.getBaseNutrition().getKcal()); +// assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); +// assertEquals(4,changedFood.getBaseNutrition().getProtein()); +// assertEquals(5,changedFood.getBaseNutrition().getFat()); +// } +// +// @Test +// void testDeleteFood() { +// //given +// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); +// Long userId = userService.saveUser(CreateUserDto.of("testUser","testImage", "tessPassword", 1, 180, 80, 18)); +// Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); +// +// //when +// foodService.deleteFood(foodId); +// +// assertNull(foodRepository.findById(foodId).orElse(null)); +// } +// +// @Test +// void testSaveAndGetFavoriteFood() { +// //given +// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); +// Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); +// Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); +// +// //when +// Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); +// +// List responseFavoriteFoodDtoList = foodService.getFavoriteFoodList(userId); +// +// assertNotNull(responseFavoriteFoodDtoList); +// assertEquals("testFood",responseFavoriteFoodDtoList.get(0).getName()); +// } +// +// @Test +// void testUpdateFavoriteFood() { +// //given +// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); +// 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)); +// +// +// //when +// BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); +// foodService.updateFavoriteFood(UpdateFavoriteFoodDto.of(favoriteFoodId, "testChangedFood", testChangedBaseNutrition)); +// +// FavoriteFood changedFood = favoriteFoodRepository.getReferenceById(favoriteFoodId); +// +// assertNotNull(changedFood); +// assertEquals("testChangedFood", changedFood.getName()); +// assertEquals(2,changedFood.getBaseNutrition().getKcal()); +// assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); +// assertEquals(4,changedFood.getBaseNutrition().getProtein()); +// assertEquals(5,changedFood.getBaseNutrition().getFat()); +// } +// +// @Test +// void testDeleteFavoriteFood() { +// //given +// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); +// 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)); +// +// //when +// foodService.deleteFavoriteFood(favoriteFoodId); +// +// assertNull(favoriteFoodRepository.findById(favoriteFoodId).orElse(null)); +// } +// +// @Test +// void testNutritionSumByDate(){ +// //given +// BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); +// 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); +// +// //when +// ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByDate(userId,food.getDate()); +// assertEquals(1400, responseNutritionSumByDateDto.getTotalKcal()); +// assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); +// assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); +// assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); +// +// assertEquals(Math.round((((double)1400 / (double)2000) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioKcal()); +// assertEquals(Math.round((((double)150 / (double)300) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); +// assertEquals(Math.round((((double)200 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioProtein()); +// assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); +// } +// +// @Test +// void testNutritionSumByWeekAndMonth(){ +// //given +// BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(100,150,200,250); +// 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", "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))); +// foodService.saveFood(CreateFoodDto.of(userId, "Food4", BaseNutrition.createNutrition(100, 100 ,4, 4))); +// foodService.saveFood(CreateFoodDto.of(userId, "Food5", BaseNutrition.createNutrition(100, 100 ,2, 5))); +// +// +// // when +// ResponseFoodRankDto response = foodService.getBestFoodByWeek(userId); +// List top3Foods = response.getRankFoodList(); +// +// // then +// assertEquals(3, top3Foods.size()); +// assertEquals("Food1", top3Foods.get(0).getName()); +// assertEquals("Food2", top3Foods.get(1).getName()); +// assertEquals("Food3", top3Foods.get(2).getName()); +// } +// +// @Test +// void getWorst3FoodsTest() { +// // given +// 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))); +// foodService.saveFood(CreateFoodDto.of(userId, "Food4", BaseNutrition.createNutrition(100, 100 ,4, 5))); +// foodService.saveFood(CreateFoodDto.of(userId, "Food5", BaseNutrition.createNutrition(100, 90 ,2, 6))); +// +// +// // when +// ResponseFoodRankDto response = foodService.getWorstFoodByWeek(userId); +// List top3Foods = response.getRankFoodList(); +// +// // then +// assertEquals(3, top3Foods.size()); +// assertEquals("Food2", top3Foods.get(0).getName()); +// assertEquals("Food4", top3Foods.get(1).getName()); +// assertEquals("Food5", top3Foods.get(2).getName()); +// } } From 30d7c11497113dfd764012190e7889d2b6fb81e0 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 22:21:44 +0900 Subject: [PATCH 02/15] :recycle: refactor: testUpdateFood() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 652e35f..ff62b90 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -52,8 +52,7 @@ class FoodServiceTest { void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리스트 불러오기 // given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); - CreateUserDto createUserDto = CreateUserDto.of("testUser", "testImage","testPassword", 1,180, 80,18); - User user = User.createUser(createUserDto.getName(), createUserDto.getImage(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), testBaseNutrition); + User user = User.createUser("testUser", "testImage","testPassword", 1,180, 80,18, testBaseNutrition); user.setId(1L); CreateFoodDto createFoodDto = CreateFoodDto.of(user.getId(), "testFood", testBaseNutrition); @@ -74,26 +73,31 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리 verify(foodRepository, times(1)).save(any(Food.class)); } -// @Test -// void testUpdateFood() { -// //given -// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); -// Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); -// Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); -// -// //when -// BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); -// foodService.updateFood(UpdateFoodDto.of(foodId, "testChangedFood", testChangedBaseNutrition)); -// -// Food changedFood = foodRepository.getReferenceById(foodId); -// -// assertNotNull(changedFood); -// assertEquals("testChangedFood", changedFood.getName()); -// assertEquals(2,changedFood.getBaseNutrition().getKcal()); -// assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); -// assertEquals(4,changedFood.getBaseNutrition().getProtein()); -// assertEquals(5,changedFood.getBaseNutrition().getFat()); -// } + @Test + void testUpdateFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition); + Food food = Food.createFood("testFood", user, testBaseNutrition); + food.setId(1L); + + given(foodRepository.findById(any(Long.class))).willReturn(Optional.of(food)); + given(foodRepository.getReferenceById(any(Long.class))).willReturn(food); + + + //when + BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); + foodService.updateFood(UpdateFoodDto.of(food.getId(), "testChangedFood", testChangedBaseNutrition)); + + Food changedFood = foodRepository.getReferenceById(food.getId()); + + assertNotNull(changedFood); + assertEquals("testChangedFood", changedFood.getName()); + assertEquals(2,changedFood.getBaseNutrition().getKcal()); + assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); + assertEquals(4,changedFood.getBaseNutrition().getProtein()); + assertEquals(5,changedFood.getBaseNutrition().getFat()); + } // // @Test // void testDeleteFood() { From 2ec2700dd81f9f1cc478e7e909a497f06481722f Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 22:41:32 +0900 Subject: [PATCH 03/15] :recycle: refactor: testDeleteFood() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index ff62b90..fc8ecaf 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -59,7 +59,7 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리 Food food = Food.createFood("testFood", user, testBaseNutrition); food.setId(2L); - given(userRepository.findById(any(Long.class))).willReturn(Optional.of(user)); + given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); given(foodRepository.save(any(Food.class))).willReturn(food); given(foodRepository.findAllByUserIdAndDate(any(Long.class), any(LocalDate.class))).willReturn(List.of(food)); @@ -81,8 +81,8 @@ void testUpdateFood() { Food food = Food.createFood("testFood", user, testBaseNutrition); food.setId(1L); - given(foodRepository.findById(any(Long.class))).willReturn(Optional.of(food)); - given(foodRepository.getReferenceById(any(Long.class))).willReturn(food); + given(foodRepository.findById(food.getId())).willReturn(Optional.of(food)); + given(foodRepository.getReferenceById(food.getId())).willReturn(food); //when @@ -99,19 +99,20 @@ void testUpdateFood() { assertEquals(5,changedFood.getBaseNutrition().getFat()); } // -// @Test -// void testDeleteFood() { -// //given -// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); -// Long userId = userService.saveUser(CreateUserDto.of("testUser","testImage", "tessPassword", 1, 180, 80, 18)); -// Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); -// -// //when -// foodService.deleteFood(foodId); -// -// assertNull(foodRepository.findById(foodId).orElse(null)); -// } -// + @Test + void testDeleteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition); + Food food = Food.createFood("testFood", user, testBaseNutrition); + food.setId(1L); + + //when + foodService.deleteFood(food.getId()); + + verify(foodRepository, times(1)).deleteById(food.getId()); + } + // @Test // void testSaveAndGetFavoriteFood() { // //given From 2e85c3a6145953d746789bb7326edb0478fe8210 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 22:54:03 +0900 Subject: [PATCH 04/15] =?UTF-8?q?:recycle:=20refactor:=20User=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=EC=9C=84=ED=95=9C=20validateUser()=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/food/service/FoodService.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index f339d56..ce7d9d8 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -15,7 +15,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.rmi.registry.LocateRegistry; import java.time.LocalDate; import java.util.Comparator; import java.util.List; @@ -41,6 +40,7 @@ public Long saveFood(CreateFoodDto createFoodDto) { // 회원이 특정 날짜에 먹은 음식 반환 @Transactional(readOnly = true) public List getFoodListByDate(Long userId, LocalDate date){ + validateUser(userId); List foodList = foodRepository.findAllByUserIdAndDate(userId, date); return foodList.stream() .map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getDate(), food.getTime(), food.getBaseNutrition(), food.isFavorite())).collect(Collectors.toList()); @@ -57,6 +57,7 @@ public void updateFood(UpdateFoodDto updateFoodDto) { // 음식 삭제 @Transactional public void deleteFood(Long foodId) { + validateFood(foodId); foodRepository.deleteById(foodId); } @@ -73,6 +74,7 @@ public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) { //즐겨찾기 음식 리스트 반환 @Transactional(readOnly = true) public List getFavoriteFoodList(Long userId){ + validateUser(userId); List foodList = favoriteFoodRepository.findAllByUserId(userId); return foodList.stream() .map(favoriteFood -> ResponseFavoriteFoodDto.of(favoriteFood.getId(), favoriteFood.getName(), @@ -95,6 +97,7 @@ public void deleteFavoriteFood(Long favoriteFoodId) { @Transactional(readOnly = true) // 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) { + validateUser(userId); List foodList = foodRepository.findAllByUserIdAndDate(userId, date); return calculateNutritionSumAndRatio(userId, foodList, date, 1); } @@ -102,6 +105,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDat @Transactional(readOnly = true) // 유저의 최근 7일간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) { + validateUser(userId); LocalDate endDate = LocalDate.now(); List foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate); @@ -111,6 +115,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) { @Transactional(readOnly = true) // 유저의 최근 1개월간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) { + validateUser(userId); LocalDate endDate = LocalDate.now(); List foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate); @@ -120,6 +125,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) { @Transactional(readOnly = true) // 유저의 최근 7일간의 Best 3 음식 조회 (dto 구체적 협의 필요) public ResponseFoodRankDto getBestFoodByWeek(Long userId) { + validateUser(userId); LocalDate endDate = LocalDate.now(); List foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate); @@ -140,6 +146,7 @@ public ResponseFoodRankDto getBestFoodByWeek(Long userId) { @Transactional(readOnly = true) // 유저의 최근 7일간의 Worst 3 음식 조회 (dto 구체적 협의 필요) public ResponseFoodRankDto getWorstFoodByWeek(Long userId) { + validateUser(userId); LocalDate endDate = LocalDate.now(); List foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate); @@ -198,6 +205,16 @@ private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId, return ResponseNutritionSumByDateDto.of(userId, checkDate, nutritionSumType, totalKcal,totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat); } + private void validateUser(Long userId) { + if (!userRepository.existsById(userId)) + throw new UserException(ResponseCode.USER_NOT_FOUND); + } + + private void validateFood(Long foodId) { + if (!foodRepository.existsById(foodId)) + throw new UserException(ResponseCode.FOOD_NOT_FOUND); + } + /** * 메서드 구현 유의사항 From 48664dbfc2333a88f3849c2e7eb428d1156793a1 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 22:57:36 +0900 Subject: [PATCH 05/15] =?UTF-8?q?:recycle:=20refactor:=20validateUser(),?= =?UTF-8?q?=20validateFood()=20=EC=B6=94=EA=B0=80=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20testCode=20=EC=88=98=EC=A0=95=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/diareat/diareat/service/FoodServiceTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index fc8ecaf..d916833 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -60,6 +60,7 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리 food.setId(2L); given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); + given(userRepository.existsById(user.getId())).willReturn(true); given(foodRepository.save(any(Food.class))).willReturn(food); given(foodRepository.findAllByUserIdAndDate(any(Long.class), any(LocalDate.class))).willReturn(List.of(food)); @@ -107,6 +108,8 @@ void testDeleteFood() { Food food = Food.createFood("testFood", user, testBaseNutrition); food.setId(1L); + given(foodRepository.existsById(food.getId())).willReturn(true); + //when foodService.deleteFood(food.getId()); From 34f7762eb32b7073080a9291739beb6c3b014426 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:10:52 +0900 Subject: [PATCH 06/15] :recycle: refactor: testSaveAndGetFavoriteFood() mocking (#29) --- .../diareat/food/domain/FavoriteFood.java | 4 ++ .../diareat/food/service/FoodService.java | 6 +++ .../diareat/service/FoodServiceTest.java | 40 ++++++++++++------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java b/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java index ca1f811..266256a 100644 --- a/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java +++ b/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java @@ -55,4 +55,8 @@ public static Food createFoodFromFavoriteFood(FavoriteFood favoriteFood) { favoriteFood.addCount(); return Food.createFood(favoriteFood.getName(), favoriteFood.getUser(), favoriteFood.getBaseNutrition()); } + + public void setId(Long id) { + this.id = id; + } } diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index ce7d9d8..f4792c4 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -91,6 +91,7 @@ public void updateFavoriteFood(UpdateFavoriteFoodDto updateFavoriteFoodDto) { // 즐겨찾기 해제 @Transactional public void deleteFavoriteFood(Long favoriteFoodId) { + validateFavoriteFood(favoriteFoodId); favoriteFoodRepository.deleteById(favoriteFoodId); } @@ -215,6 +216,11 @@ private void validateFood(Long foodId) { throw new UserException(ResponseCode.FOOD_NOT_FOUND); } + private void validateFavoriteFood(Long favoriteFoodId) { + if (!foodRepository.existsById(favoriteFoodId)) + throw new UserException(ResponseCode.FAVORITE_NOT_FOUND); + } + /** * 메서드 구현 유의사항 diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index d916833..84ec537 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -1,5 +1,6 @@ package com.diareat.diareat.service; +import com.diareat.diareat.food.domain.FavoriteFood; import com.diareat.diareat.food.domain.Food; import com.diareat.diareat.food.dto.*; import com.diareat.diareat.food.repository.FavoriteFoodRepository; @@ -116,21 +117,30 @@ void testDeleteFood() { verify(foodRepository, times(1)).deleteById(food.getId()); } -// @Test -// void testSaveAndGetFavoriteFood() { -// //given -// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); -// Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","tessPassword", 1, 180, 80, 18)); -// Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); -// -// //when -// Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); -// -// List responseFavoriteFoodDtoList = foodService.getFavoriteFoodList(userId); -// -// assertNotNull(responseFavoriteFoodDtoList); -// assertEquals("testFood",responseFavoriteFoodDtoList.get(0).getName()); -// } + @Test + void testSaveAndGetFavoriteFood() { + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + User user = User.createUser("testUser", "testImage","testPassword", 1,180, 80,18, testBaseNutrition); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", user, testBaseNutrition); + user.setId(1L); + favoriteFood.setId(3L); + + CreateFavoriteFoodDto createFavoriteFoodDto = CreateFavoriteFoodDto.of(favoriteFood.getId(), user.getId(),"testFood", testBaseNutrition); + + given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); + given(userRepository.existsById(user.getId())).willReturn(true); + given(favoriteFoodRepository.save(any(FavoriteFood.class))).willReturn(favoriteFood); + given(favoriteFoodRepository.findAllByUserId(any(Long.class))).willReturn(List.of(favoriteFood)); + + //when + Long foodId = foodService.saveFavoriteFood(createFavoriteFoodDto); + + List responseFavoriteFoodDtoList = foodService.getFavoriteFoodList(user.getId()); + + assertEquals(3L, foodId); + assertEquals("testFavoriteFood",responseFavoriteFoodDtoList.get(0).getName()); + verify(favoriteFoodRepository, times(1)).save(any(FavoriteFood.class)); + } // // @Test // void testUpdateFavoriteFood() { From 76f95db9da30df8cb77fc85b3a86603b11954660 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:17:52 +0900 Subject: [PATCH 07/15] :recycle: refactor: testUpdateFavoriteFood() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 84ec537..9441077 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -84,21 +84,18 @@ void testUpdateFood() { food.setId(1L); given(foodRepository.findById(food.getId())).willReturn(Optional.of(food)); - given(foodRepository.getReferenceById(food.getId())).willReturn(food); //when BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); foodService.updateFood(UpdateFoodDto.of(food.getId(), "testChangedFood", testChangedBaseNutrition)); - Food changedFood = foodRepository.getReferenceById(food.getId()); - assertNotNull(changedFood); - assertEquals("testChangedFood", changedFood.getName()); - assertEquals(2,changedFood.getBaseNutrition().getKcal()); - assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); - assertEquals(4,changedFood.getBaseNutrition().getProtein()); - assertEquals(5,changedFood.getBaseNutrition().getFat()); + assertEquals("testChangedFood", food.getName()); + assertEquals(2,food.getBaseNutrition().getKcal()); + assertEquals(3,food.getBaseNutrition().getCarbohydrate()); + assertEquals(4,food.getBaseNutrition().getProtein()); + assertEquals(5,food.getBaseNutrition().getFat()); } // @Test @@ -141,29 +138,27 @@ void testSaveAndGetFavoriteFood() { assertEquals("testFavoriteFood",responseFavoriteFoodDtoList.get(0).getName()); verify(favoriteFoodRepository, times(1)).save(any(FavoriteFood.class)); } -// -// @Test -// void testUpdateFavoriteFood() { -// //given -// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); -// 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)); -// -// -// //when -// BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); -// foodService.updateFavoriteFood(UpdateFavoriteFoodDto.of(favoriteFoodId, "testChangedFood", testChangedBaseNutrition)); -// -// FavoriteFood changedFood = favoriteFoodRepository.getReferenceById(favoriteFoodId); -// -// assertNotNull(changedFood); -// assertEquals("testChangedFood", changedFood.getName()); -// assertEquals(2,changedFood.getBaseNutrition().getKcal()); -// assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); -// assertEquals(4,changedFood.getBaseNutrition().getProtein()); -// assertEquals(5,changedFood.getBaseNutrition().getFat()); -// } + @Test + void testUpdateFavoriteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", user, testBaseNutrition); + favoriteFood.setId(1L); + + given(favoriteFoodRepository.findById(favoriteFood.getId())).willReturn(Optional.of(favoriteFood)); + + + //when + BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); + foodService.updateFavoriteFood(UpdateFavoriteFoodDto.of(favoriteFood.getId(), "testChangedFood", testChangedBaseNutrition)); + + assertEquals("testChangedFood", favoriteFood.getName()); + assertEquals(2,favoriteFood.getBaseNutrition().getKcal()); + assertEquals(3,favoriteFood.getBaseNutrition().getCarbohydrate()); + assertEquals(4,favoriteFood.getBaseNutrition().getProtein()); + assertEquals(5,favoriteFood.getBaseNutrition().getFat()); + } // // @Test // void testDeleteFavoriteFood() { From 7d3ee8bc7fbd463b47aa93c415aedb66ca1a0e1a Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:22:33 +0900 Subject: [PATCH 08/15] =?UTF-8?q?:pencil2:=20fix:=20=EC=9E=98=EB=AA=BB?= =?UTF-8?q?=EB=90=9C=20Exception=20Type=20=EC=88=98=EC=A0=95=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/diareat/diareat/food/service/FoodService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index f4792c4..d9b5a58 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -9,6 +9,7 @@ import com.diareat.diareat.user.domain.User; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.util.api.ResponseCode; +import com.diareat.diareat.util.exception.FavoriteException; import com.diareat.diareat.util.exception.FoodException; import com.diareat.diareat.util.exception.UserException; import lombok.RequiredArgsConstructor; @@ -213,12 +214,12 @@ private void validateUser(Long userId) { private void validateFood(Long foodId) { if (!foodRepository.existsById(foodId)) - throw new UserException(ResponseCode.FOOD_NOT_FOUND); + throw new FoodException(ResponseCode.FOOD_NOT_FOUND); } private void validateFavoriteFood(Long favoriteFoodId) { - if (!foodRepository.existsById(favoriteFoodId)) - throw new UserException(ResponseCode.FAVORITE_NOT_FOUND); + if (!favoriteFoodRepository.existsById(favoriteFoodId)) + throw new FavoriteException(ResponseCode.FAVORITE_NOT_FOUND); } From 200e5f3669cf5ae8e8944c024fc0a403d7a3e304 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:22:56 +0900 Subject: [PATCH 09/15] :recycle: refactor: testDeleteFavoriteFood() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 9441077..3bb9c9a 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -160,19 +160,21 @@ void testUpdateFavoriteFood() { assertEquals(5,favoriteFood.getBaseNutrition().getFat()); } // -// @Test -// void testDeleteFavoriteFood() { -// //given -// BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); -// 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)); -// -// //when -// foodService.deleteFavoriteFood(favoriteFoodId); -// -// assertNull(favoriteFoodRepository.findById(favoriteFoodId).orElse(null)); -// } + @Test + void testDeleteFavoriteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFood", user, testBaseNutrition); + favoriteFood.setId(1L); + + given(favoriteFoodRepository.existsById(favoriteFood.getId())).willReturn(true); + + //when + foodService.deleteFavoriteFood(favoriteFood.getId()); + + verify(favoriteFoodRepository, times(1)).deleteById(favoriteFood.getId()); + } // // @Test // void testNutritionSumByDate(){ From 37042ae958d941f0f922456b2a53810b0476afea Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:28:38 +0900 Subject: [PATCH 10/15] :recycle: refactor: testNutritionSumByDate() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 3bb9c9a..323b256 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -176,26 +176,32 @@ void testDeleteFavoriteFood() { verify(favoriteFoodRepository, times(1)).deleteById(favoriteFood.getId()); } // -// @Test -// void testNutritionSumByDate(){ -// //given -// BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); -// 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); -// -// //when -// ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByDate(userId,food.getDate()); -// assertEquals(1400, responseNutritionSumByDateDto.getTotalKcal()); -// assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); -// assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); -// assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); -// -// assertEquals(Math.round((((double)1400 / (double)2000) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioKcal()); -// assertEquals(Math.round((((double)150 / (double)300) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); -// assertEquals(Math.round((((double)200 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioProtein()); -// assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); -// } + @Test + void testNutritionSumByDate(){ + //given + BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); + User user = User.createUser("testUser", "testImage","testPassword",1, 180, 80, 18, BaseNutrition.createNutrition(2000,300,80,80)); + user.setId(1L); + Food food = Food.createFood("testFood", user, testFoodNutrition); + food.setId(2L); + + given(userRepository.existsById(user.getId())).willReturn(true); + given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); + given(foodRepository.findAllByUserIdAndDate(any(Long.class), any(LocalDate.class))).willReturn(List.of(food)); + + + //when + ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByDate(user.getId(),food.getDate()); + assertEquals(1400, responseNutritionSumByDateDto.getTotalKcal()); + assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); + assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); + assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); + + assertEquals(Math.round((((double)1400 / (double)2000) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioKcal()); + assertEquals(Math.round((((double)150 / (double)300) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); + assertEquals(Math.round((((double)200 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioProtein()); + assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); + } // // @Test // void testNutritionSumByWeekAndMonth(){ From bbbedd779e43545092586816435bc4c734130558 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:32:40 +0900 Subject: [PATCH 11/15] :recycle: refactor: testNutritionSumByWeek(), testNutritionSumByMonth() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 65 ++++++++++++++++--- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 323b256..dc64e1a 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -203,14 +203,63 @@ void testNutritionSumByDate(){ assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); } // -// @Test -// void testNutritionSumByWeekAndMonth(){ -// //given -// BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(100,150,200,250); -// Long userId = userService.saveUser(CreateUserDto.of("testUser", "testImage","testPassword",1, 180, 80, 18)); -// Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood", testFoodNutrition)); -// -// } + @Test + void testNutritionSumByWeek(){ + //given + BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); + User user = User.createUser("testUser", "testImage","testPassword",1, 180, 80, 18, BaseNutrition.createNutrition(2000,300,80,80)); + user.setId(1L); + Food food = Food.createFood("testFood", user, testFoodNutrition); + food.setId(2L); + + given(userRepository.existsById(user.getId())).willReturn(true); + given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); + given(foodRepository.findAllByUserIdAndDateBetween(any(Long.class), any(LocalDate.class), any(LocalDate.class))).willReturn(List.of(food)); + + + + //when + ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByWeek(user.getId()); + assertEquals(1400, responseNutritionSumByDateDto.getTotalKcal()); + assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); + assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); + assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); + + assertEquals(Math.round((((double)1400 / (double)2000) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioKcal()); + assertEquals(Math.round((((double)150 / (double)300) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); + assertEquals(Math.round((((double)200 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioProtein()); + assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); + + } + + @Test + void testNutritionSumByMonth(){ + //given + BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(1400,150,200,250); + User user = User.createUser("testUser", "testImage","testPassword",1, 180, 80, 18, BaseNutrition.createNutrition(2000,300,80,80)); + user.setId(1L); + Food food = Food.createFood("testFood", user, testFoodNutrition); + food.setId(2L); + + given(userRepository.existsById(user.getId())).willReturn(true); + given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); + given(foodRepository.findAllByUserIdAndDateBetween(any(Long.class), any(LocalDate.class), any(LocalDate.class))).willReturn(List.of(food)); + + + + //when + ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByMonth(user.getId()); + assertEquals(1400, responseNutritionSumByDateDto.getTotalKcal()); + assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); + assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); + assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); + + assertEquals(Math.round((((double)1400 / (double)2000) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioKcal()); + assertEquals(Math.round((((double)150 / (double)300) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); + assertEquals(Math.round((((double)200 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioProtein()); + assertEquals(Math.round((((double)250 / (double)80) * 100.0)*100.0)/100.0, responseNutritionSumByDateDto.getRatioFat()); + + } // // @Test // void getBest3FoodTest() { From 2db260a31e37568cdbe2bf081b6ec22af767022b Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:37:28 +0900 Subject: [PATCH 12/15] :recycle: refactor: getBest3FoodTest() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index dc64e1a..eff6895 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -261,27 +261,32 @@ void testNutritionSumByMonth(){ } // -// @Test -// void getBest3FoodTest() { -// // given -// 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))); -// foodService.saveFood(CreateFoodDto.of(userId, "Food4", BaseNutrition.createNutrition(100, 100 ,4, 4))); -// foodService.saveFood(CreateFoodDto.of(userId, "Food5", BaseNutrition.createNutrition(100, 100 ,2, 5))); -// -// -// // when -// ResponseFoodRankDto response = foodService.getBestFoodByWeek(userId); -// List top3Foods = response.getRankFoodList(); -// -// // then -// assertEquals(3, top3Foods.size()); -// assertEquals("Food1", top3Foods.get(0).getName()); -// assertEquals("Food2", top3Foods.get(1).getName()); -// assertEquals("Food3", top3Foods.get(2).getName()); -// } + @Test + void getBest3FoodTest() { + // given + User user = User.createUser("testUser", "testImage","testPassword", 1, 180, 80, 18, BaseNutrition.createNutrition(1,1,1,1)); + Food food1 = Food.createFood( "Food1", user, BaseNutrition.createNutrition(100, 100 ,10, 1)); + Food food2 = Food.createFood( "Food2", user, BaseNutrition.createNutrition(100, 100 ,8, 2)); + Food food3 = Food.createFood( "Food3", user, BaseNutrition.createNutrition(100, 100 ,6, 3)); + Food food4 = Food.createFood( "Food4", user, BaseNutrition.createNutrition(100, 100 ,4, 4)); + Food food5 = Food.createFood( "Food5", user, BaseNutrition.createNutrition(100, 100 ,2, 5)); + user.setId(1L); + + List foodList = List.of(food1, food2, food3, food4, food5); + + given(foodRepository.findAllByUserIdAndDateBetween(any(Long.class), any(LocalDate.class), any(LocalDate.class))).willReturn(foodList); + given(userRepository.existsById(user.getId())).willReturn(true); + + // when + ResponseFoodRankDto response = foodService.getBestFoodByWeek(user.getId()); + List top3Foods = response.getRankFoodList(); + + // then + assertEquals(3, top3Foods.size()); + assertEquals("Food1", top3Foods.get(0).getName()); + assertEquals("Food2", top3Foods.get(1).getName()); + assertEquals("Food3", top3Foods.get(2).getName()); + } // // @Test // void getWorst3FoodsTest() { From 448cbe3125685d63fe173e438e35dab403421b0b Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:38:53 +0900 Subject: [PATCH 13/15] :recycle: refactor: getWorst3FoodsTest() mocking (#29) --- .../diareat/service/FoodServiceTest.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index eff6895..36e30e0 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -288,25 +288,30 @@ void getBest3FoodTest() { assertEquals("Food3", top3Foods.get(2).getName()); } // -// @Test -// void getWorst3FoodsTest() { -// // given -// 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))); -// foodService.saveFood(CreateFoodDto.of(userId, "Food4", BaseNutrition.createNutrition(100, 100 ,4, 5))); -// foodService.saveFood(CreateFoodDto.of(userId, "Food5", BaseNutrition.createNutrition(100, 90 ,2, 6))); -// -// -// // when -// ResponseFoodRankDto response = foodService.getWorstFoodByWeek(userId); -// List top3Foods = response.getRankFoodList(); -// -// // then -// assertEquals(3, top3Foods.size()); -// assertEquals("Food2", top3Foods.get(0).getName()); -// assertEquals("Food4", top3Foods.get(1).getName()); -// assertEquals("Food5", top3Foods.get(2).getName()); -// } + @Test + void getWorst3FoodsTest() { + // given + User user = User.createUser("testUser", "testImage","testPassword", 1, 180, 80, 18, BaseNutrition.createNutrition(1,1,1,1)); + Food food1 = Food.createFood( "Food1", user, BaseNutrition.createNutrition(100, 50 ,10, 1)); + Food food2 = Food.createFood( "Food2", user, BaseNutrition.createNutrition(100, 100 ,8, 20)); + Food food3 = Food.createFood( "Food3", user, BaseNutrition.createNutrition(100, 80 ,6, 7)); + Food food4 = Food.createFood( "Food4", user, BaseNutrition.createNutrition(100, 100 ,4, 5)); + Food food5 = Food.createFood( "Food5", user, BaseNutrition.createNutrition(100, 90 ,2, 6)); + user.setId(1L); + + List foodList = List.of(food1, food2, food3, food4, food5); + + given(foodRepository.findAllByUserIdAndDateBetween(any(Long.class), any(LocalDate.class), any(LocalDate.class))).willReturn(foodList); + given(userRepository.existsById(user.getId())).willReturn(true); + + // when + ResponseFoodRankDto response = foodService.getWorstFoodByWeek(user.getId()); + List top3Foods = response.getRankFoodList(); + + // then + assertEquals(3, top3Foods.size()); + assertEquals("Food2", top3Foods.get(0).getName()); + assertEquals("Food4", top3Foods.get(1).getName()); + assertEquals("Food5", top3Foods.get(2).getName()); + } } From ffd4ecd0ac20db86a3a9dd8aab8a03a642c1368c Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:44:51 +0900 Subject: [PATCH 14/15] =?UTF-8?q?:recycle:=20refactor:=20=EB=B6=88?= =?UTF-8?q?=ED=95=84=EC=9A=94=ED=95=9C=20=EC=9D=98=EC=A1=B4=EC=84=B1=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/diareat/diareat/service/FoodServiceTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 36e30e0..a40a8d3 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -36,9 +36,6 @@ class FoodServiceTest { @InjectMocks FoodService foodService; - @InjectMocks - UserService userService; - @Mock FoodRepository foodRepository; From 111c0f40192663c6f0f47bdcc801232d4779d929 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Sun, 15 Oct 2023 23:45:42 +0900 Subject: [PATCH 15/15] =?UTF-8?q?:recycle:=20refactor:=20=EB=B6=88?= =?UTF-8?q?=ED=95=84=EC=9A=94=ED=95=9C=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=20=EC=A0=9C=EA=B1=B0=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/diareat/diareat/service/FoodServiceTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index a40a8d3..31b9e8e 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -8,9 +8,7 @@ import com.diareat.diareat.food.service.FoodService; import com.diareat.diareat.user.domain.BaseNutrition; import com.diareat.diareat.user.domain.User; -import com.diareat.diareat.user.dto.CreateUserDto; import com.diareat.diareat.user.repository.UserRepository; -import com.diareat.diareat.user.service.UserService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith;