diff --git a/product/src/test/java/com/yas/product/service/ProductServiceTest.java b/product/src/it/java/com/yas/product/service/ProductServiceIT.java similarity index 68% rename from product/src/test/java/com/yas/product/service/ProductServiceTest.java rename to product/src/it/java/com/yas/product/service/ProductServiceIT.java index 71ace023a1..d2815c5c3e 100644 --- a/product/src/test/java/com/yas/product/service/ProductServiceTest.java +++ b/product/src/it/java/com/yas/product/service/ProductServiceIT.java @@ -1,14 +1,29 @@ package com.yas.product.service; +import static org.assertj.core.api.Assertions.assertThat; +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 static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + import com.yas.commonlibrary.exception.NotFoundException; import com.yas.product.ProductApplication; +import com.yas.product.config.IntegrationTestConfiguration; import com.yas.product.model.Brand; import com.yas.product.model.Category; import com.yas.product.model.Product; import com.yas.product.model.ProductCategory; +import com.yas.product.model.ProductOption; +import com.yas.product.model.ProductOptionCombination; +import com.yas.product.model.ProductOptionValue; import com.yas.product.repository.BrandRepository; import com.yas.product.repository.CategoryRepository; import com.yas.product.repository.ProductCategoryRepository; +import com.yas.product.repository.ProductOptionCombinationRepository; +import com.yas.product.repository.ProductOptionRepository; +import com.yas.product.repository.ProductOptionValueRepository; import com.yas.product.repository.ProductRepository; import com.yas.product.viewmodel.NoFileMediaVm; import com.yas.product.viewmodel.product.ProductFeatureGetVm; @@ -17,31 +32,26 @@ import com.yas.product.viewmodel.product.ProductThumbnailGetVm; import com.yas.product.viewmodel.product.ProductThumbnailVm; import com.yas.product.viewmodel.product.ProductsGetVm; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; -import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; -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 static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; - -@SpringBootTest(classes = ProductApplication.class) -class ProductServiceTest { +@SpringBootTest +@Import(IntegrationTestConfiguration.class) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +class ProductServiceIT { private final ZonedDateTime CREATED_ON = ZonedDateTime.now(); @Autowired private ProductRepository productRepository; @@ -51,6 +61,12 @@ class ProductServiceTest { private CategoryRepository categoryRepository; @Autowired private ProductCategoryRepository productCategoryRepository; + @Autowired + private ProductOptionCombinationRepository productOptionCombinationRepository; + @Autowired + private ProductOptionValueRepository productOptionValueRepository; + @Autowired + private ProductOptionRepository productOptionRepository; @MockBean private MediaService mediaService; @Autowired @@ -117,13 +133,13 @@ private void generateTestData() { product.setCreatedOn(CREATED_ON); products.add(product); } - List productsDB = productRepository.saveAll(products); + List productsDb = productRepository.saveAll(products); productCategoryList = new ArrayList<>(); - for (int i = 1; i <= productsDB.size(); i++) { - Product productDB = productsDB.get(i - 1); + for (int i = 1; i <= productsDb.size(); i++) { + Product productDb = productsDb.get(i - 1); ProductCategory productCategory = new ProductCategory(); - productCategory.setProduct(productDB); + productCategory.setProduct(productDb); if (i % 2 == 0) { productCategory.setCategory(category2); } else { @@ -134,8 +150,26 @@ private void generateTestData() { productCategoryRepository.saveAll(productCategoryList); } + private Product getVariant(Product parent) { + return Product.builder() + .name("product variant name") + .slug("product variant slug") + .isAllowedToOrder(true) + .isPublished(true) + .isFeatured(true) + .isVisibleIndividually(true) + .stockTrackingEnabled(true) + .thumbnailMediaId(1L) + .taxClassId(1L) + .parent(parent) + .build(); + } + @AfterEach void tearDown() { + productOptionCombinationRepository.deleteAll(); + productOptionValueRepository.deleteAll(); + productOptionRepository.deleteAll(); productCategoryRepository.deleteAll(); categoryRepository.deleteAll(); productRepository.deleteAll(); @@ -148,8 +182,9 @@ void getFeaturedProducts_WhenEverythingIsOkay_Success() { generateTestData(); ProductFeatureGetVm actualResponse = productService.getListFeaturedProducts(pageNo, pageSize); assertThat(actualResponse.totalPage()).isEqualTo(totalPage); - assertThat(actualResponse.productList().size()).isEqualTo(5); - Map productMap = products.stream().collect(Collectors.toMap(Product::getSlug, product -> product)); + assertThat(actualResponse.productList()).hasSize(5); + Map productMap + = products.stream().collect(Collectors.toMap(Product::getSlug, product -> product)); for (int i = 0; i < actualResponse.productList().size(); i++) { ProductThumbnailGetVm productThumbnailGetVm = actualResponse.productList().get(i); Product product = productMap.get(productThumbnailGetVm.slug()); @@ -169,7 +204,8 @@ void getProductsByBrand_BrandSlugIsValid_Success() { @DisplayName("Get products by brand when brand is non exist then throws exception") @Test void getProductsByBrand_BrandIsNonExist_ThrowsNotFoundException() { - NotFoundException exception = assertThrows(NotFoundException.class, () -> productService.getProductsByBrand("brandSlug1")); + NotFoundException exception = assertThrows(NotFoundException.class, + () -> productService.getProductsByBrand("brandSlug1")); assertEquals(String.format("Brand %s is not found", "brandSlug1"), exception.getMessage()); } @@ -198,7 +234,8 @@ void getListFeaturedProductsByListProductIds_whenAllProductIdsValid_shouldSucces @Test void getProductsWithFilter_WhenFilterByBrandNameAndProductName_ThenSuccess() { generateTestData(); - ProductListGetVm actualResponse = productService.getProductsWithFilter(pageNo, pageSize, "product1", brand1.getName()); + ProductListGetVm actualResponse + = productService.getProductsWithFilter(pageNo, pageSize, "product1", brand1.getName()); assertEquals(1, actualResponse.productContent().size()); } @@ -219,7 +256,8 @@ void getProductsWithFilter_WhenFilterByProductName_ThenSuccess() { @Test void getProductsWithFilter_whenFindAll_thenSuccess() { generateTestData(); - ProductListGetVm actualResponse = productService.getProductsWithFilter(pageNo, pageSize, "product", brand2.getName()); + ProductListGetVm actualResponse + = productService.getProductsWithFilter(pageNo, pageSize, "product", brand2.getName()); assertEquals(5, actualResponse.productContent().size()); } @@ -227,7 +265,8 @@ void getProductsWithFilter_whenFindAll_thenSuccess() { @Test void getProductsFromCategory_WhenFindAllByCategory_ThenSuccess() { generateTestData(); - ProductListGetFromCategoryVm actualResponse = productService.getProductsFromCategory(pageNo, pageSize, "categorySlug1"); + ProductListGetFromCategoryVm actualResponse + = productService.getProductsFromCategory(pageNo, pageSize, "categorySlug1"); assertEquals(5, actualResponse.productContent().size()); } @@ -235,12 +274,13 @@ void getProductsFromCategory_WhenFindAllByCategory_ThenSuccess() { void getProductsFromCategory_CategoryIsNonExist_ThrowsNotFoundException() { generateTestData(); String categorySlug = "laptop-macbook"; - NotFoundException exception = assertThrows(NotFoundException.class, () -> productService.getProductsFromCategory(pageNo, pageSize, categorySlug)); + NotFoundException exception = assertThrows(NotFoundException.class, + () -> productService.getProductsFromCategory(pageNo, pageSize, categorySlug)); assertThat(exception.getMessage()).isEqualTo(String.format("Category %s is not found", categorySlug)); } @Test - void deleteProduct_givenProductIdValid_thenSuccess() { + void deleteProduct_givenParentProduct_thenSuccess() { generateTestData(); Long id = productRepository.findAll().getFirst().getId(); productService.deleteProduct(id); @@ -251,6 +291,49 @@ void deleteProduct_givenProductIdValid_thenSuccess() { } + @Test + void deleteProduct_givenVariant_thenSuccess() { + + generateTestData(); + + String value = "red"; + ProductOption productOption = new ProductOption(); + productOption.setName(value); + productOption.setCreatedOn(CREATED_ON); + ProductOption afterProductOption = productOptionRepository.save(productOption); + + ProductOptionCombination productOptionCombination = new ProductOptionCombination(); + productOptionCombination.setValue(value); + productOptionCombination.setDisplayOrder(1); + + Product parent = productRepository.findAll().getFirst(); + Product product = getVariant(parent); + Product afterProduct = productRepository.save(product); + + productOptionCombination.setProduct(afterProduct); + productOptionCombination.setProductOption(afterProductOption); + ProductOptionCombination productOptionCombinationSaved + = productOptionCombinationRepository.save(productOptionCombination); + assertTrue(productOptionCombinationRepository.findById(productOptionCombinationSaved.getId()).isPresent()); + + ProductOptionValue productOptionValue = new ProductOptionValue(); + productOptionValue.setProduct(parent); + productOptionValue.setValue(value); + productOptionValue.setDisplayOrder(1); + productOptionValue.setProductOption(afterProductOption); + ProductOptionValue productOptionValueSaved = productOptionValueRepository.save(productOptionValue); + assertTrue(productOptionValueRepository.findById(productOptionValueSaved.getId()).isPresent()); + + productService.deleteProduct(afterProduct.getId()); + Optional result = productRepository.findById(afterProduct.getId()); + + assertTrue(result.isPresent()); + assertFalse(result.get().isPublished()); + assertFalse(productOptionCombinationRepository.findById(productOptionCombinationSaved.getId()).isPresent()); + assertFalse(productOptionValueRepository.findById(productOptionValueSaved.getId()).isPresent()); + + } + @Test void deleteProduct_givenProductIdInvalid_thenThrowNotFoundException() { Long productId = 99999L; @@ -263,7 +346,8 @@ void getProductsByMultiQuery_WhenFilterByBrandNameAndProductName_ThenSuccess() { Double startPrice = 1.0; Double endPrice = 10.0; String productName = "product2"; - ProductsGetVm result = productService.getProductsByMultiQuery(pageNo, pageSize, productName, category2.getSlug(), startPrice, endPrice); + ProductsGetVm result + = productService.getProductsByMultiQuery(pageNo, pageSize, productName, category2.getSlug(), startPrice, endPrice); // Assert result assertEquals(1, result.productContent().size()); diff --git a/product/src/main/java/com/yas/product/repository/ProductOptionCombinationRepository.java b/product/src/main/java/com/yas/product/repository/ProductOptionCombinationRepository.java index 630fc0ff3e..7b6e8922e8 100644 --- a/product/src/main/java/com/yas/product/repository/ProductOptionCombinationRepository.java +++ b/product/src/main/java/com/yas/product/repository/ProductOptionCombinationRepository.java @@ -3,10 +3,15 @@ import com.yas.product.model.Product; import com.yas.product.model.ProductOptionCombination; import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface ProductOptionCombinationRepository extends JpaRepository { List findAllByProduct(Product product); + + Optional findByProductId(Long productId); + + void deleteByProductId(Long productId); } diff --git a/product/src/main/java/com/yas/product/repository/ProductOptionValueRepository.java b/product/src/main/java/com/yas/product/repository/ProductOptionValueRepository.java index 3174c185ef..5e5e226322 100644 --- a/product/src/main/java/com/yas/product/repository/ProductOptionValueRepository.java +++ b/product/src/main/java/com/yas/product/repository/ProductOptionValueRepository.java @@ -10,4 +10,7 @@ @Repository public interface ProductOptionValueRepository extends JpaRepository { List findAllByProduct(Product product); + + void deleteByProductIdAndValue(Long productId, String value); + } diff --git a/product/src/main/java/com/yas/product/service/ProductService.java b/product/src/main/java/com/yas/product/service/ProductService.java index 3616b792c4..b951f5102e 100644 --- a/product/src/main/java/com/yas/product/service/ProductService.java +++ b/product/src/main/java/com/yas/product/service/ProductService.java @@ -820,6 +820,18 @@ public void deleteProduct(Long id) { .findById(id) .orElseThrow(() -> new NotFoundException(Constants.ErrorCode.PRODUCT_NOT_FOUND, id)); product.setPublished(false); + + if (!Objects.isNull(product.getParent())) { + + Optional productOptionCombination = productOptionCombinationRepository + .findByProductId(id); + + productOptionCombination.ifPresent(poc -> { + productOptionCombinationRepository.deleteByProductId(id); + productOptionValueRepository.deleteByProductIdAndValue(product.getParent().getId(), poc.getValue()); + }); + } + productRepository.save(product); } diff --git a/product/src/test/java/com/yas/product/service/ProductAttributeGroupServiceTest.java b/product/src/test/java/com/yas/product/service/ProductAttributeGroupServiceTest.java index 2004841aad..6af8063ad7 100644 --- a/product/src/test/java/com/yas/product/service/ProductAttributeGroupServiceTest.java +++ b/product/src/test/java/com/yas/product/service/ProductAttributeGroupServiceTest.java @@ -1,22 +1,5 @@ package com.yas.product.service; -import com.yas.commonlibrary.exception.DuplicatedException; -import com.yas.product.model.attribute.ProductAttributeGroup; -import com.yas.product.repository.ProductAttributeGroupRepository; -import com.yas.product.viewmodel.productattribute.ProductAttributeGroupListGetVm; -import com.yas.product.viewmodel.productattribute.ProductAttributeGroupVm; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.Pageable; - -import java.util.ArrayList; -import java.util.List; - import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -28,9 +11,24 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) -class ProductAttributeGroupServiceTest { +import com.yas.commonlibrary.exception.DuplicatedException; +import com.yas.product.model.attribute.ProductAttributeGroup; +import com.yas.product.repository.ProductAttributeGroupRepository; +import com.yas.product.viewmodel.productattribute.ProductAttributeGroupListGetVm; +import com.yas.product.viewmodel.productattribute.ProductAttributeGroupVm; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +@ExtendWith(MockitoExtension.class) +class ProductAttributeGroupServiceTest { @Mock private ProductAttributeGroupRepository repository; diff --git a/product/src/test/java/com/yas/product/service/ProductAttributeServiceTest.java b/product/src/test/java/com/yas/product/service/ProductAttributeServiceTest.java index a222df4679..57253cf4c9 100644 --- a/product/src/test/java/com/yas/product/service/ProductAttributeServiceTest.java +++ b/product/src/test/java/com/yas/product/service/ProductAttributeServiceTest.java @@ -1,5 +1,11 @@ package com.yas.product.service; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + import com.yas.commonlibrary.exception.DuplicatedException; import com.yas.product.model.attribute.ProductAttribute; import com.yas.product.model.attribute.ProductAttributeGroup; @@ -7,26 +13,19 @@ import com.yas.product.repository.ProductAttributeRepository; import com.yas.product.viewmodel.productattribute.ProductAttributeListGetVm; import com.yas.product.viewmodel.productattribute.ProductAttributePostVm; -import org.junit.Test; -import org.junit.runner.RunWith; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) class ProductAttributeServiceTest { @Mock diff --git a/product/src/test/java/com/yas/product/service/ProductOptionServiceTest.java b/product/src/test/java/com/yas/product/service/ProductOptionServiceTest.java index bb32c50e48..a72a8682bd 100644 --- a/product/src/test/java/com/yas/product/service/ProductOptionServiceTest.java +++ b/product/src/test/java/com/yas/product/service/ProductOptionServiceTest.java @@ -1,36 +1,35 @@ package com.yas.product.service; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import com.yas.commonlibrary.exception.DuplicatedException; import com.yas.commonlibrary.exception.NotFoundException; import com.yas.product.model.ProductOption; import com.yas.product.repository.ProductOptionRepository; import com.yas.product.viewmodel.productoption.ProductOptionListGetVm; import com.yas.product.viewmodel.productoption.ProductOptionPostVm; -import org.junit.Test; -import org.junit.runner.RunWith; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import java.util.List; -import java.util.Optional; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.isNull; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) class ProductOptionServiceTest { @Mock