diff --git a/src/main/java/com/zufar/onlinestore/product/api/SingleProductProvider.java b/src/main/java/com/zufar/onlinestore/product/api/SingleProductProvider.java index 8df9c259..abacf1ee 100644 --- a/src/main/java/com/zufar/onlinestore/product/api/SingleProductProvider.java +++ b/src/main/java/com/zufar/onlinestore/product/api/SingleProductProvider.java @@ -27,7 +27,7 @@ public ProductInfoDto getProductById(final UUID productId) { .map(productInfoDtoConverter::toDto) .orElseThrow(() -> { log.error("The product with id = {} is not found.", productId); - throw new ProductNotFoundException(productId); + return new ProductNotFoundException(productId); }); } } \ No newline at end of file diff --git a/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java b/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java new file mode 100644 index 00000000..11db21e3 --- /dev/null +++ b/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java @@ -0,0 +1,71 @@ +package com.zufar.onlinestore.product.api; + +import com.zufar.onlinestore.product.converter.ProductInfoDtoConverter; +import com.zufar.onlinestore.product.dto.ProductInfoDto; +import com.zufar.onlinestore.product.dto.ProductListWithPaginationInfoDto; +import com.zufar.onlinestore.product.entity.ProductInfo; +import com.zufar.onlinestore.product.repository.ProductInfoRepository; +import org.instancio.Instancio; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +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 org.springframework.data.domain.Sort; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(SpringExtension.class) +class PageableProductsProviderTest { + + @Mock + private ProductInfoRepository productRepository; + + @Mock + private ProductInfoDtoConverter productInfoConverter; + + @InjectMocks + private PageableProductsProvider productsProvider; + + private List products; + + @BeforeEach + void setUp() { + products = Instancio.ofList(ProductInfo.class).create(); + } + + @Test + void shouldFetchProductsUsingPageAttributes() { + Page page = new PageImpl<>(products); + final int pageNumber = 1; + final int size = 10; + final String sortAttribute = "name"; + + Pageable pageRequest = PageRequest.of(pageNumber, size, Sort.Direction.ASC, sortAttribute); + + when(productRepository.findAll(pageRequest)).thenReturn(page); + when(productInfoConverter.toDto(any(ProductInfo.class))).thenReturn(mock(ProductInfoDto.class)); + when(productInfoConverter.toProductPaginationDto(ArgumentMatchers.>any())).thenReturn(mock(ProductListWithPaginationInfoDto.class)); + + ProductListWithPaginationInfoDto productList = productsProvider.getProducts(pageNumber, size, + sortAttribute, Sort.Direction.ASC.name() + ); + + assertNotNull(productList); + + verify(productRepository, times(1)).findAll(pageRequest); + verify(productInfoConverter, times(1)).toProductPaginationDto(ArgumentMatchers.>any()); + } +} diff --git a/src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java b/src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java new file mode 100644 index 00000000..1946a589 --- /dev/null +++ b/src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java @@ -0,0 +1,74 @@ +package com.zufar.onlinestore.product.api; + +import com.zufar.onlinestore.product.dto.ProductInfoDto; +import com.zufar.onlinestore.product.dto.ProductListWithPaginationInfoDto; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.data.domain.Sort; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(SpringExtension.class) +class ProductApiImplTest { + + @Mock + private PageableProductsProvider pageableProductsProvider; + + @Mock + private SingleProductProvider singleProductProvider; + + @InjectMocks + private ProductApiImpl productApi; + + @Test + void shouldReturnProductsForValidPaginationParameters() { + final int pageNumber = 1; + final int size = 10; + final String sortAttribute = "name"; + + when(pageableProductsProvider.getProducts( + pageNumber, + size, + sortAttribute, + Sort.Direction.ASC.name() + ) + ).thenReturn(mock(ProductListWithPaginationInfoDto.class)); + + ProductListWithPaginationInfoDto result = productApi.getProducts( + pageNumber, + size, + sortAttribute, + Sort.Direction.ASC.name() + ); + + assertNotNull(result); + + verify(pageableProductsProvider, times(1)).getProducts( + pageNumber, + size, + sortAttribute, + Sort.Direction.ASC.name() + ); + } + + @Test + void shouldReturnProductForValidProductId() { + UUID productId = UUID.randomUUID(); + + when(singleProductProvider.getProductById(productId)).thenReturn(mock(ProductInfoDto.class)); + + ProductInfoDto result = productApi.getProduct(productId); + + assertNotNull(result); + + verify(singleProductProvider, times(1)).getProductById(productId); + } +} diff --git a/src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java b/src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java new file mode 100644 index 00000000..66ecb091 --- /dev/null +++ b/src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java @@ -0,0 +1,69 @@ +package com.zufar.onlinestore.product.api; + +import com.zufar.onlinestore.product.converter.ProductInfoDtoConverter; +import com.zufar.onlinestore.product.dto.ProductInfoDto; +import com.zufar.onlinestore.product.entity.ProductInfo; +import com.zufar.onlinestore.product.exception.ProductNotFoundException; +import com.zufar.onlinestore.product.repository.ProductInfoRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(SpringExtension.class) +class SingleProductProviderTest { + + @Mock + private ProductInfoRepository productRepository; + + @Mock + private ProductInfoDtoConverter productInfoConverter; + + @InjectMocks + private SingleProductProvider productProvider; + + @Test + void shouldReturnProductWhenProductIdExists() { + UUID productId = UUID.randomUUID(); + + when(productRepository.findById(productId)).thenReturn(Optional.of(mock(ProductInfo.class))); + when(productInfoConverter.toDto(any(ProductInfo.class))).thenReturn(mock(ProductInfoDto.class)); + + ProductInfoDto result = productProvider.getProductById(productId); + + assertNotNull(result); + + verify(productInfoConverter, times(1)).toDto(any(ProductInfo.class)); + } + + @Test + void shouldThrowExceptionWhenProductIdNotExists() { + UUID productId = UUID.randomUUID(); + + when(productRepository.findById(productId)).thenReturn(Optional.empty()); + + ProductNotFoundException thrownException = assertThrows( + ProductNotFoundException.class, + () -> productProvider.getProductById(productId) + ); + + assertEquals( + String.format("The product with productId = %s is not found.", productId), + thrownException.getMessage() + ); + + verify(productRepository, times(1)).findById(productId); + } +} diff --git a/src/main/test/resources/application-test.yaml b/src/main/test/resources/application-test.yaml new file mode 100644 index 00000000..e69de29b