From c172fd5a9a295922df722b21941fd42d91ca77e5 Mon Sep 17 00:00:00 2001 From: mirzaei Date: Sat, 3 Aug 2024 13:49:45 +0330 Subject: [PATCH] add test and change Product services --- .../com/dishDash/common/enums/ErrorCode.java | 2 +- .../service/DeliveryPersonOrderService.java | 2 +- .../application/service/FoodService.java | 87 ++++++----- .../application/service/MenuService.java | 67 +++++---- .../repository/MenuRepository.java | 2 + .../product/FoodServiceIntegrationTest.java | 135 ++++++++++-------- .../product/MenuServiceIntegrationTest.java | 117 +++++++++------ .../repository/LocationRepository.java | 11 +- 8 files changed, 247 insertions(+), 176 deletions(-) diff --git a/common/src/main/java/com/dishDash/common/enums/ErrorCode.java b/common/src/main/java/com/dishDash/common/enums/ErrorCode.java index 19abb11..e10fc42 100644 --- a/common/src/main/java/com/dishDash/common/enums/ErrorCode.java +++ b/common/src/main/java/com/dishDash/common/enums/ErrorCode.java @@ -17,7 +17,7 @@ public enum ErrorCode { NO_CONTENT(HttpStatus.NO_CONTENT, HttpStatus.NO_CONTENT, 204, "No_content"), INVALID_CREDENTIALS(HttpStatus.UNAUTHORIZED, HttpStatus.UNAUTHORIZED, 401, "Invalid_credentials"), NOT_FOUND(HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND, 404, "Resource_not_found"), - NOT_FOUND_No_CONTENT(HttpStatus.NOT_FOUND, HttpStatus.NO_CONTENT, 204, "Resource_not_found"), + NOT_FOUND_NO_CONTENT(HttpStatus.NOT_FOUND, HttpStatus.NO_CONTENT, 204, "Resource_not_found"), CONFLICT(HttpStatus.CONFLICT, HttpStatus.CONFLICT, 409, "Conflict"), UNPROCESSABLE_ENTITY(HttpStatus.UNPROCESSABLE_ENTITY, HttpStatus.UNPROCESSABLE_ENTITY, 422, "Unprocessable_entity"), PAYMENT_REQUIRED(HttpStatus.PAYMENT_REQUIRED, HttpStatus.PAYMENT_REQUIRED, 402, "Payment_required"), diff --git a/order/src/main/java/com/dish_dash/order/application/service/DeliveryPersonOrderService.java b/order/src/main/java/com/dish_dash/order/application/service/DeliveryPersonOrderService.java index d36265c..fa4221f 100644 --- a/order/src/main/java/com/dish_dash/order/application/service/DeliveryPersonOrderService.java +++ b/order/src/main/java/com/dish_dash/order/application/service/DeliveryPersonOrderService.java @@ -39,6 +39,6 @@ public OrderDto getDeliveryPersonCurrentOrder(long deliveryPersonID) { userApi.getRestaurantOwnerProfile(orderDto.getRestaurantOwnerId())); return orderDto; } - throw new CustomException(ErrorCode.NOT_FOUND_No_CONTENT, "order not found"); + throw new CustomException(ErrorCode.NOT_FOUND_NO_CONTENT, "order not found"); } } diff --git a/product/src/main/java/com/dish_dash/product/application/service/FoodService.java b/product/src/main/java/com/dish_dash/product/application/service/FoodService.java index c778da0..10a0080 100644 --- a/product/src/main/java/com/dish_dash/product/application/service/FoodService.java +++ b/product/src/main/java/com/dish_dash/product/application/service/FoodService.java @@ -2,18 +2,22 @@ import com.dishDash.common.dto.FoodDto; import com.dishDash.common.dto.FoodViewDto; +import com.dishDash.common.enums.ErrorCode; +import com.dishDash.common.exception.CustomException; import com.dish_dash.product.domain.mapper.ProductMapper; import com.dish_dash.product.domain.model.Food; +import com.dish_dash.product.domain.model.Menu; import com.dish_dash.product.infrastructure.repository.FoodRepository; import com.dish_dash.product.infrastructure.repository.MenuRepository; import java.util.List; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor +@Slf4j public class FoodService { private final FoodRepository foodRepository; @@ -21,13 +25,18 @@ public class FoodService { private final MenuRepository menuRepository; public List getAllFoods(long userId) { + log.info("Retrieving all foods for restaurant ID: {}", userId); return foodRepository.findByMenu_RestaurantId(userId).stream() - .map(ProductMapper.INSTANCE::foodToViewDto) - .collect(Collectors.toList()); + .map(ProductMapper.INSTANCE::foodToViewDto).toList(); } public FoodViewDto getFoodById(long id) { - return foodRepository.findById(id).map(ProductMapper.INSTANCE::foodToViewDto).orElse(null); + log.info("Retrieving food by ID: {}", id); + return foodRepository.findById(id).map(ProductMapper.INSTANCE::foodToViewDto) + .orElseThrow(() -> { + log.error("Food with ID: {} not found", id); + return new CustomException(ErrorCode.NOT_FOUND, "Food not found for ID: " + id); + }); } @Transactional @@ -35,44 +44,56 @@ public FoodDto saveFood(FoodDto foodDto, Long userId) { Food food = ProductMapper.INSTANCE.dtoToFood(foodDto); food.setCategory(categoryService.getReferenceCategory(foodDto.getCategoryId())); - var menu = menuRepository.findByRestaurantId(userId); - if (menu.isPresent()) { - food.setMenu(menu.get()); - } else { - throw new IllegalArgumentException("Menu not found for the specified restaurant ID."); - } + Menu menu = menuRepository.findByRestaurantId(userId).orElseThrow(() -> { + log.error("Menu not found for restaurant ID: {}", userId); + return new CustomException(ErrorCode.NOT_FOUND, + "Menu not found for restaurant ID: " + userId); + }); + food.setMenu(menu); - return ProductMapper.INSTANCE.foodToDto(foodRepository.save(food)); + Food savedFood = foodRepository.save(food); + log.info("Food saved with ID: {}", savedFood.getId()); + return ProductMapper.INSTANCE.foodToDto(savedFood); } public void deleteFood(long id) { - if (foodRepository.existsById(id)) { - foodRepository.deleteById(id); - } else { - throw new IllegalArgumentException("Food item not found with the specified ID."); + log.info("Deleting food with ID: {}", id); + + if (!foodRepository.existsById(id)) { + log.error("Attempt to delete non-existing food with ID: {}", id); + throw new CustomException(ErrorCode.NOT_FOUND, "Food item not found with the specified ID."); } + + foodRepository.deleteById(id); + log.info("Food with ID: {} deleted successfully", id); } @Transactional public FoodDto modifyFood(long id, FoodDto foodDto, Long userId) { - Food existingFood = - foodRepository - .findById(id) - .orElseThrow( - () -> new IllegalArgumentException("Food item not found with the specified ID.")); - - existingFood.setName(foodDto.getName()); - existingFood.setDescription(foodDto.getDescription()); - existingFood.setPrice(foodDto.getPrice()); - existingFood.setCategory(categoryService.getReferenceCategory(foodDto.getCategoryId())); - - var menu = menuRepository.findByRestaurantId(userId); - if (menu.isPresent()) { - existingFood.setMenu(menu.get()); - } else { - throw new IllegalArgumentException("Menu not found for the specified restaurant ID."); - } + log.info("Modifying food with ID: {} for restaurant ID: {}", id, userId); + Food existingFood = foodRepository.findById(id).orElseThrow(() -> { + log.error("Food with ID: {} not found", id); + return new CustomException(ErrorCode.NOT_FOUND, "Food item not found with the specified ID."); + }); + + updateFoodDetails(existingFood, foodDto, userId); + + Food updatedFood = foodRepository.save(existingFood); + log.info("Food modified with ID: {}", updatedFood.getId()); + return ProductMapper.INSTANCE.foodToDto(updatedFood); + } + + private void updateFoodDetails(Food food, FoodDto foodDto, Long userId) { + food.setName(foodDto.getName()); + food.setDescription(foodDto.getDescription()); + food.setPrice(foodDto.getPrice()); + food.setCategory(categoryService.getReferenceCategory(foodDto.getCategoryId())); - return ProductMapper.INSTANCE.foodToDto(foodRepository.save(existingFood)); + Menu menu = menuRepository.findByRestaurantId(userId).orElseThrow(() -> { + log.error("Menu not found for restaurant ID: {}", userId); + return new CustomException(ErrorCode.NOT_FOUND, + "Menu not found for restaurant ID: " + userId); + }); + food.setMenu(menu); } } diff --git a/product/src/main/java/com/dish_dash/product/application/service/MenuService.java b/product/src/main/java/com/dish_dash/product/application/service/MenuService.java index 9415246..e7c2a9f 100644 --- a/product/src/main/java/com/dish_dash/product/application/service/MenuService.java +++ b/product/src/main/java/com/dish_dash/product/application/service/MenuService.java @@ -10,46 +10,43 @@ import com.dish_dash.product.infrastructure.repository.FoodRepository; import com.dish_dash.product.infrastructure.repository.MenuRepository; import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor +@Slf4j public class MenuService { + private final MenuRepository menuRepository; private final FoodRepository foodRepository; @Transactional(readOnly = true) public List getAllMenus() { - return menuRepository.findAll().stream() - .map(ProductMapper.INSTANCE::menuToDto) - .collect(Collectors.toList()); + log.info("Retrieving all menus"); + return menuRepository.findAll().stream().map(ProductMapper.INSTANCE::menuToDto).toList(); } @Transactional(readOnly = true) public MenuDto getMenuById(long id) { - Optional menuOptional = menuRepository.findByRestaurantId(id); - if (menuOptional.isPresent()) { - MenuDto menuDto = ProductMapper.INSTANCE.menuToDto(menuOptional.get()); - menuDto.setFoods( - menuOptional.get().getFoodList().stream() - .map( - food -> { - var dto = ProductMapper.INSTANCE.foodToDto(food); - dto.setCategoryId(food.getCategory().getId()); - return dto; - }) - .toList()); - menuDto.setCategories( - menuOptional.get().getCategories().stream() - .map(ProductMapper.INSTANCE::categoryToViewDto) - .collect(Collectors.toList())); - return menuDto; - } - throw new CustomException(ErrorCode.NO_CONTENT, "Menu not found"); + log.info("Retrieving menu by restaurant ID: {}", id); + Menu menu = menuRepository.findByRestaurantId(id).orElseThrow(() -> { + log.error("Menu not found for restaurant ID: {}", id); + return new CustomException(ErrorCode.NOT_FOUND_NO_CONTENT, + "Menu not found for restaurant ID: " + id); + }); + + MenuDto menuDto = ProductMapper.INSTANCE.menuToDto(menu); + menuDto.setFoods(menu.getFoodList().stream().map(food -> { + FoodDto dto = ProductMapper.INSTANCE.foodToDto(food); + dto.setCategoryId(food.getCategory().getId()); + return dto; + }).toList()); + menuDto.setCategories( + menu.getCategories().stream().map(ProductMapper.INSTANCE::categoryToViewDto).toList()); + return menuDto; } public MenuDto saveMenu(MenuDto menu) { @@ -58,16 +55,28 @@ public MenuDto saveMenu(MenuDto menu) { } public void deleteMenu(long id) { + log.info("Deleting menu for restaurant owner ID: {}", id); + if (!menuRepository.existsByRestaurantId(id)) { + log.error("Attempt to delete non-existing menu for restaurant owner ID: {}", id); + throw new CustomException(ErrorCode.NOT_FOUND, + "Menu not found for restaurant owner ID: " + id); + } menuRepository.deleteByRestaurantOwnerId(id); + log.info("Menu deleted successfully for restaurant owner ID: {}", id); } public FoodDto addFoodToMenu(long menuId, FoodDto foodDto) { - Menu menu = - menuRepository - .findById(menuId) - .orElseThrow(() -> new IllegalArgumentException("Menu not found")); + log.info("Adding food to menu ID: {}", menuId); + Menu menu = menuRepository.findById(menuId).orElseThrow(() -> { + log.error("Menu with ID: {} not found", menuId); + return new CustomException(ErrorCode.NOT_FOUND_NO_CONTENT, + "Menu not found for ID: " + menuId); + }); + Food food = ProductMapper.INSTANCE.dtoToFood(foodDto); food.setMenu(menu); - return ProductMapper.INSTANCE.foodToDto(foodRepository.save(food)); + Food savedFood = foodRepository.save(food); + log.info("Food added to menu ID: {} with food ID: {}", menuId, savedFood.getId()); + return ProductMapper.INSTANCE.foodToDto(savedFood); } } diff --git a/product/src/main/java/com/dish_dash/product/infrastructure/repository/MenuRepository.java b/product/src/main/java/com/dish_dash/product/infrastructure/repository/MenuRepository.java index 104c7f6..9e35119 100644 --- a/product/src/main/java/com/dish_dash/product/infrastructure/repository/MenuRepository.java +++ b/product/src/main/java/com/dish_dash/product/infrastructure/repository/MenuRepository.java @@ -16,4 +16,6 @@ public interface MenuRepository extends JpaRepository { @Transactional @Query("DELETE FROM Menu m WHERE m.restaurantId = :ownerId") void deleteByRestaurantOwnerId(long ownerId); + + boolean existsByRestaurantId(long id); } diff --git a/product/src/test/java/com/dish_dash/product/FoodServiceIntegrationTest.java b/product/src/test/java/com/dish_dash/product/FoodServiceIntegrationTest.java index 42c7dc8..d8c4068 100644 --- a/product/src/test/java/com/dish_dash/product/FoodServiceIntegrationTest.java +++ b/product/src/test/java/com/dish_dash/product/FoodServiceIntegrationTest.java @@ -1,10 +1,15 @@ package com.dish_dash.product; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.dishDash.common.Price; import com.dishDash.common.dto.FoodDto; import com.dishDash.common.dto.FoodViewDto; +import com.dishDash.common.enums.ErrorCode; +import com.dishDash.common.exception.CustomException; import com.dish_dash.product.application.service.FoodService; import com.dish_dash.product.domain.model.Category; import com.dish_dash.product.domain.model.Food; @@ -14,25 +19,28 @@ import com.dish_dash.product.infrastructure.repository.MenuRepository; import java.util.List; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; @SpringBootTest -@Disabled +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) public class FoodServiceIntegrationTest { - @Autowired private FoodRepository foodRepository; + @Autowired + private FoodRepository foodRepository; - @Autowired private MenuRepository menuRepository; + @Autowired + private MenuRepository menuRepository; - @Autowired private CategoryRepository categoryRepository; + @Autowired + private CategoryRepository categoryRepository; - @Autowired private FoodService foodService; + @Autowired + private FoodService foodService; private FoodDto foodDto; - private Category category; private Menu menu; @@ -46,85 +54,67 @@ void setUp() { menuRepository.flush(); categoryRepository.flush(); - category = Category.builder().name("CATEGORY_NAME").build(); - category = categoryRepository.saveAndFlush(category); - menu = menuRepository.saveAndFlush(Menu.builder().restaurantId(1L).build()); + category = categoryRepository.saveAndFlush( + Category.builder().name("CATEGORY_NAME").menu(menu).build()); - foodDto = - FoodDto.builder() - .name("FOOD_NAME") - .description("FOOD_DESCRIPTION") - .price(Price.builder().amount(10).build()) - .categoryId(category.getId()) - .build(); + foodDto = FoodDto.builder().name("FOOD_NAME").description("FOOD_DESCRIPTION") + .price(Price.builder().amount(10).build()).categoryId(category.getId()).build(); } @Test void getAllFoods_ShouldReturnAllFoods() { - Food food = - Food.builder() - .name("FOOD_NAME") - .description("FOOD_DESCRIPTION") - .price(Price.builder().amount(10).build()) - .category(category) - .menu(menu) - .build(); - foodRepository.saveAndFlush(food); + Food food = foodRepository.saveAndFlush( + Food.builder().name("FOOD_NAME").description("FOOD_DESCRIPTION") + .price(Price.builder().amount(10).build()).category(category).menu(menu).build()); List foods = foodService.getAllFoods(menu.getRestaurantId()); assertNotNull(foods, "Foods should not be null"); assertEquals(1, foods.size(), "There should be exactly one food item"); - assertEquals("FOOD_NAME", foods.get(0).getName(), "Food name should be FOOD_NAME"); - assertEquals( - "FOOD_DESCRIPTION", - foods.get(0).getDescription(), - "Food description should be FOOD_DESCRIPTION"); + assertEquals(food.getName(), foods.get(0).getName(), "Food name should be FOOD_NAME"); + assertEquals(food.getDescription(), foods.get(0).getDescription(), + "Food description should be " + "FOOD_DESCRIPTION"); } @Test void getFoodById_ShouldReturnFoodViewDto_WhenFoodExists() { - Food food = - Food.builder() - .name("FOOD_NAME") - .description("FOOD_DESCRIPTION") - .price(Price.builder().amount(10).build()) - .category(category) - .menu(menu) - .build(); - food = foodRepository.saveAndFlush(food); + Food food = foodRepository.saveAndFlush( + Food.builder().name("FOOD_NAME").description("FOOD_DESCRIPTION") + .price(Price.builder().amount(10).build()).category(category).menu(menu).build()); FoodViewDto foodViewDto = foodService.getFoodById(food.getId()); assertNotNull(foodViewDto, "Food should not be null"); assertEquals(food.getId(), foodViewDto.getId(), "Food ID should match"); assertEquals("FOOD_NAME", foodViewDto.getName(), "Food name should be FOOD_NAME"); - assertEquals( - "FOOD_DESCRIPTION", - foodViewDto.getDescription(), + assertEquals("FOOD_DESCRIPTION", foodViewDto.getDescription(), "Food description should be FOOD_DESCRIPTION"); } @Test - void getFoodById_ShouldReturnNull_WhenFoodDoesNotExist() { - FoodViewDto foodViewDto = foodService.getFoodById(1L); + void getFoodById_ShouldThrowException_WhenFoodDoesNotExist() { + long nonExistingFoodId = 999L; + + CustomException exception = assertThrows(CustomException.class, () -> { + foodService.getFoodById(nonExistingFoodId); + }); - assertNull(foodViewDto, "Food should be null for non-existing food item"); + assertEquals(ErrorCode.NOT_FOUND, exception.getErrorCode(), "Error code should be NOT_FOUND"); + assertEquals("Food not found for ID: " + nonExistingFoodId, exception.getMessage(), + "Exception message should indicate the food was not found"); } @Test void saveFood_ShouldSaveAndReturnFoodDto() { - FoodDto savedFoodDto = foodService.saveFood(foodDto, 1L); + FoodDto savedFoodDto = foodService.saveFood(foodDto, menu.getRestaurantId()); assertNotNull(savedFoodDto, "Saved food should not be null"); assertEquals("FOOD_NAME", savedFoodDto.getName(), "Food name should be FOOD_NAME"); - assertEquals( - "FOOD_DESCRIPTION", - savedFoodDto.getDescription(), + assertEquals("FOOD_DESCRIPTION", savedFoodDto.getDescription(), "Food description should be FOOD_DESCRIPTION"); - Food savedFood = foodRepository.findById(1L).orElse(null); + Food savedFood = foodRepository.findById(savedFoodDto.getId()).orElse(null); assertNotNull(savedFood, "Food in repository should not be null"); assertEquals("FOOD_NAME", savedFood.getName(), "Food name should match"); assertEquals("FOOD_DESCRIPTION", savedFood.getDescription(), "Food description should match"); @@ -133,20 +123,41 @@ void saveFood_ShouldSaveAndReturnFoodDto() { @Test void deleteFood_ShouldRemoveFood() { - Food food = - Food.builder() - .name("FOOD_NAME") - .description("FOOD_DESCRIPTION") - .price(Price.builder().amount(10).build()) - .category(category) - .menu(menu) - .build(); - food = foodRepository.saveAndFlush(food); + Food food = foodRepository.saveAndFlush( + Food.builder().name("FOOD_NAME").description("FOOD_DESCRIPTION") + .price(Price.builder().amount(10).build()).category(category).menu(menu).build()); foodService.deleteFood(food.getId()); Food deletedFood = foodRepository.findById(food.getId()).orElse(null); - assertNull(deletedFood, "Food should be null after deletion"); } + + @Test + void modifyFood_ShouldUpdateAndReturnUpdatedFoodDto() { + Food food = foodRepository.saveAndFlush( + Food.builder().name("ORIGINAL_NAME").description("ORIGINAL_DESCRIPTION") + .price(Price.builder().amount(10).build()).category(category).menu(menu).build()); + + FoodDto updateFoodDto = FoodDto.builder().name("UPDATED_NAME") + .description("UPDATED_DESCRIPTION").price(Price.builder().amount(15).build()) + .categoryId(category.getId()).build(); + + FoodDto updatedFoodDto = foodService.modifyFood(food.getId(), updateFoodDto, + menu.getRestaurantId()); + + assertNotNull(updatedFoodDto, "Updated food should not be null"); + assertEquals("UPDATED_NAME", updatedFoodDto.getName(), "Food name should be UPDATED_NAME"); + assertEquals("UPDATED_DESCRIPTION", updatedFoodDto.getDescription(), + "Food description should be UPDATED_DESCRIPTION"); + assertEquals(15, updatedFoodDto.getPrice().getAmount(), "Food price should be updated to 15"); + + Food updatedFood = foodRepository.findById(updatedFoodDto.getId()).orElse(null); + assertNotNull(updatedFood, "Food in repository should not be null"); + assertEquals("UPDATED_NAME", updatedFood.getName(), "Food name should match"); + assertEquals("UPDATED_DESCRIPTION", updatedFood.getDescription(), + "Food description should match"); + assertEquals(15, updatedFood.getPrice().getAmount(), "Food price should match"); + assertEquals(category.getId(), updatedFood.getCategory().getId(), "Category ID should match"); + } } diff --git a/product/src/test/java/com/dish_dash/product/MenuServiceIntegrationTest.java b/product/src/test/java/com/dish_dash/product/MenuServiceIntegrationTest.java index 6dae945..eb46a96 100644 --- a/product/src/test/java/com/dish_dash/product/MenuServiceIntegrationTest.java +++ b/product/src/test/java/com/dish_dash/product/MenuServiceIntegrationTest.java @@ -1,18 +1,24 @@ package com.dish_dash.product; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.dishDash.common.Price; import com.dishDash.common.dto.FoodDto; import com.dishDash.common.dto.MenuDto; +import com.dishDash.common.enums.ErrorCode; +import com.dishDash.common.exception.CustomException; import com.dish_dash.product.application.service.MenuService; +import com.dish_dash.product.domain.model.Category; import com.dish_dash.product.domain.model.Food; import com.dish_dash.product.domain.model.Menu; +import com.dish_dash.product.infrastructure.repository.CategoryRepository; import com.dish_dash.product.infrastructure.repository.FoodRepository; import com.dish_dash.product.infrastructure.repository.MenuRepository; import java.util.List; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -20,96 +26,121 @@ @SpringBootTest @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) -@Disabled -public class MenuServiceIntegrationTest { +class MenuServiceIntegrationTest { - @Autowired private MenuRepository menuRepository; + @Autowired + private MenuRepository menuRepository; + @Autowired + private FoodRepository foodRepository; + @Autowired + private CategoryRepository categoryRepository; + @Autowired + private MenuService menuService; - @Autowired private FoodRepository foodRepository; - - @Autowired private MenuService menuService; - - private MenuDto menuDto; - private FoodDto foodDto; + private Menu menu; + private Category category; @BeforeEach void setUp() { - menuRepository.deleteAll(); foodRepository.deleteAll(); + menuRepository.deleteAll(); + categoryRepository.deleteAll(); - menuRepository.flush(); foodRepository.flush(); + menuRepository.flush(); + categoryRepository.flush(); - menuDto = MenuDto.builder().restaurantId(1L).build(); - - foodDto = - FoodDto.builder() - .name("FOOD_NAME") - .description("FOOD_DESCRIPTION") - .price(Price.builder().amount(10).build()) - .build(); + menu = menuRepository.saveAndFlush(Menu.builder().restaurantId(1L).build()); + category = categoryRepository.saveAndFlush( + Category.builder().name("CATEGORY_NAME").menu(menu).build()); } @Test void getAllMenus_ShouldReturnAllMenus() { - Menu menu = Menu.builder().restaurantId(1L).build(); - menuRepository.saveAndFlush(menu); - List menus = menuService.getAllMenus(); assertNotNull(menus, "Menus should not be null"); - assertEquals(1, menus.size(), "There should be exactly one menu"); + assertEquals(1, menus.size(), "There should be exactly one menu item"); + assertEquals(menu.getId(), menus.get(0).getId(), "Menu ID should match"); + assertEquals(1L, menus.get(0).getRestaurantId(), "Restaurant ID should match"); } @Test void getMenuById_ShouldReturnMenuDto_WhenMenuExists() { - Menu menu = menuRepository.saveAndFlush(Menu.builder().restaurantId(1L).build()); + MenuDto menuDto = menuService.getMenuById(menu.getRestaurantId()); - MenuDto retrievedMenu = menuService.getMenuById(menu.getId()); - - assertNotNull(retrievedMenu, "Menu should not be null"); - assertEquals(menu.getId(), retrievedMenu.getId(), "Menu ID should match"); + assertNotNull(menuDto, "Menu should not be null"); + assertEquals(menu.getId(), menuDto.getId(), "Menu ID should match"); + assertEquals(1L, menuDto.getRestaurantId(), "Restaurant ID should match"); } @Test - void getMenuById_ShouldReturnNull_WhenMenuDoesNotExist() { - MenuDto menuDto = menuService.getMenuById(1L); + void getMenuById_ShouldThrowException_WhenMenuDoesNotExist() { + long nonExistingMenuId = 999L; + + CustomException exception = assertThrows(CustomException.class, + () -> menuService.getMenuById(nonExistingMenuId)); - assertNull(menuDto, "Menu should be null for non-existing menu"); + assertEquals(ErrorCode.NOT_FOUND_NO_CONTENT, exception.getErrorCode(), + "Error code should be NOT_FOUND_NO_CONTENT"); + assertEquals("Menu not found for restaurant ID: " + nonExistingMenuId, exception.getMessage(), + "Exception message should indicate the menu was not found"); } @Test void saveMenu_ShouldSaveAndReturnMenuDto() { - MenuDto savedMenuDto = menuService.saveMenu(menuDto); + MenuDto newMenuDto = MenuDto.builder().restaurantId(2L).build(); + + MenuDto savedMenuDto = menuService.saveMenu(newMenuDto); assertNotNull(savedMenuDto, "Saved menu should not be null"); + assertEquals(2L, savedMenuDto.getRestaurantId(), "Restaurant ID should be 2L"); - Menu savedMenu = menuRepository.findById(savedMenuDto.getId()).orElse(null); + Menu savedMenu = menuRepository.findByRestaurantId(2L).orElse(null); assertNotNull(savedMenu, "Menu in repository should not be null"); + assertEquals(2L, savedMenu.getRestaurantId(), "Restaurant ID should match"); } @Test void deleteMenu_ShouldRemoveMenu() { - Menu menu = menuRepository.saveAndFlush(Menu.builder().restaurantId(1L).build()); - - menuService.deleteMenu(menu.getId()); - - Menu deletedMenu = menuRepository.findById(menu.getId()).orElse(null); + menuService.deleteMenu(menu.getRestaurantId()); - assertNull(deletedMenu, "Menu should be null after deletion"); + boolean exists = menuRepository.existsByRestaurantId(menu.getRestaurantId()); + assertFalse(exists, "Menu should be null after deletion"); } @Test void addFoodToMenu_ShouldAddFoodAndReturnFoodDto() { - Menu menu = menuRepository.saveAndFlush(Menu.builder().restaurantId(1L).build()); + FoodDto foodDto = FoodDto.builder().name("FOOD_NAME").description("FOOD_DESCRIPTION") + .price(Price.builder().amount(10).build()).categoryId(category.getId()).build(); FoodDto savedFoodDto = menuService.addFoodToMenu(menu.getId(), foodDto); assertNotNull(savedFoodDto, "Saved food should not be null"); assertEquals("FOOD_NAME", savedFoodDto.getName(), "Food name should be FOOD_NAME"); + assertEquals("FOOD_DESCRIPTION", savedFoodDto.getDescription(), + "Food description should be FOOD_DESCRIPTION"); - Food savedFood = foodRepository.findById(1L).orElse(null); + Food savedFood = foodRepository.findById(savedFoodDto.getId()).orElse(null); assertNotNull(savedFood, "Food in repository should not be null"); + assertEquals("FOOD_NAME", savedFood.getName(), "Food name should match"); + assertEquals("FOOD_DESCRIPTION", savedFood.getDescription(), "Food description should match"); assertEquals(menu.getId(), savedFood.getMenu().getId(), "Menu ID should match"); } + + @Test + void addFoodToMenu_ShouldThrowException_WhenMenuDoesNotExist() { + FoodDto foodDto = FoodDto.builder().name("FOOD_NAME").description("FOOD_DESCRIPTION") + .price(Price.builder().amount(10).build()).categoryId(category.getId()).build(); + + long nonExistingMenuId = 999L; + + CustomException exception = assertThrows(CustomException.class, + () -> menuService.addFoodToMenu(nonExistingMenuId, foodDto)); + + assertEquals(ErrorCode.NOT_FOUND_NO_CONTENT, exception.getErrorCode(), + "Error code should be NOT_FOUND_NO_CONTENT"); + assertEquals("Menu not found for ID: " + nonExistingMenuId, exception.getMessage(), + "Exception message should indicate the menu was not found"); + } } diff --git a/user/src/main/java/com/dish_dash/user/adapters/repository/LocationRepository.java b/user/src/main/java/com/dish_dash/user/adapters/repository/LocationRepository.java index 46324a4..a051118 100644 --- a/user/src/main/java/com/dish_dash/user/adapters/repository/LocationRepository.java +++ b/user/src/main/java/com/dish_dash/user/adapters/repository/LocationRepository.java @@ -11,15 +11,12 @@ @Repository public interface LocationRepository extends JpaRepository { + Optional findByDeliveryID(Long deliveryID); - @Query( - "UPDATE Location SET latitude =:latitude, longitude =:longitude, timestamp =:timestamp where id=:id") + @Query("UPDATE Location SET latitude =:latitude, longitude =:longitude, timestamp =:timestamp where id=:id") @Modifying @Transactional - void modify( - @Param("latitude") long latitude, - @Param("longitude") long longitude, - @Param("timestamp") long timestamp, - @Param("id") long id); + void modify(@Param("latitude") long latitude, @Param("longitude") long longitude, + @Param("timestamp") long timestamp, @Param("id") long id); }