From 4143a8681c43b0f3d247f57d6140fde08ea4b1a7 Mon Sep 17 00:00:00 2001 From: yevr19 Date: Wed, 6 Sep 2023 00:37:08 +0200 Subject: [PATCH 1/4] test(product): add unit tests for product package --- .../product/api/SingleProductProvider.java | 2 +- .../api/PageableProductsProviderTest.java | 69 +++++++++++++++++ .../product/api/ProductApiImplTest.java | 74 +++++++++++++++++++ .../api/SingleProductProviderTest.java | 69 +++++++++++++++++ .../util/ProductPaginationDefaults.java | 26 +++++++ src/main/test/resources/application-test.yaml | 0 6 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java create mode 100644 src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java create mode 100644 src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java create mode 100644 src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java create mode 100644 src/main/test/resources/application-test.yaml 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..8e3a219f --- /dev/null +++ b/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.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.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.Pageable; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.List; + +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.PAGE; +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SIZE; +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_ATTRIBUTE; +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_DIRECTION; +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); + + when(productRepository.findAll(any(Pageable.class))).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(PAGE.getIntValue(), SIZE.getIntValue(), + SORT_ATTRIBUTE.getStringValue(), SORT_DIRECTION.getStringValue() + ); + + assertNotNull(productList); + + verify(productRepository, times(1)).findAll(any(Pageable.class)); + 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..3305d3a8 --- /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.test.context.junit.jupiter.SpringExtension; +import java.util.UUID; + +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.PAGE; +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SIZE; +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_ATTRIBUTE; +import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_DIRECTION; +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() { + when(pageableProductsProvider.getProducts( + PAGE.getIntValue(), + SIZE.getIntValue(), + SORT_ATTRIBUTE.getStringValue(), + SORT_DIRECTION.getStringValue() + ) + ).thenReturn(mock(ProductListWithPaginationInfoDto.class)); + + ProductListWithPaginationInfoDto result = productApi.getProducts( + PAGE.getIntValue(), + SIZE.getIntValue(), + SORT_ATTRIBUTE.getStringValue(), + SORT_DIRECTION.getStringValue() + ); + + assertNotNull(result); + + verify(pageableProductsProvider, times(1)).getProducts( + PAGE.getIntValue(), + SIZE.getIntValue(), + SORT_ATTRIBUTE.getStringValue(), + SORT_DIRECTION.getStringValue() + ); + } + + @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..ee3efa91 --- /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.fail; +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()); + + ProductInfoDto retrievedProduct; + + try { + retrievedProduct = productProvider.getProductById(productId); + if (retrievedProduct == null) { + fail("Expected ProductNotFoundException to be thrown"); + } + } catch (ProductNotFoundException e) { + assertEquals(String.format("The product with productId = %s is not found.", productId), e.getMessage()); + } + verify(productRepository, times(1)).findById(productId); + } +} diff --git a/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java b/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java new file mode 100644 index 00000000..6ba1ebee --- /dev/null +++ b/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java @@ -0,0 +1,26 @@ +package com.zufar.onlinestore.product.util; + +import lombok.Getter; + +@Getter +public enum ProductPaginationDefaults { + + PAGE(1), + SIZE(10), + SORT_ATTRIBUTE("name"), + SORT_DIRECTION("ASC"); + + private final int intValue; + + private final String stringValue; + + ProductPaginationDefaults(int intValue) { + this.intValue = intValue; + this.stringValue = null; + } + + ProductPaginationDefaults(String stringValue) { + this.stringValue = stringValue; + this.intValue = -1; + } +} 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 From 352b878fa47fd8cfa2eb93a9346e465880cf428f Mon Sep 17 00:00:00 2001 From: yevr19 Date: Wed, 6 Sep 2023 01:07:52 +0200 Subject: [PATCH 2/4] test(product): add unit tests for product package --- .../onlinestore/product/api/PageableProductsProviderTest.java | 1 - .../onlinestore/product/util/ProductPaginationDefaults.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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 index 8e3a219f..edb94e61 100644 --- a/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java +++ b/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java @@ -16,7 +16,6 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.test.context.junit.jupiter.SpringExtension; - import java.util.List; import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.PAGE; diff --git a/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java b/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java index 6ba1ebee..55e7fb97 100644 --- a/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java +++ b/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java @@ -8,7 +8,7 @@ public enum ProductPaginationDefaults { PAGE(1), SIZE(10), SORT_ATTRIBUTE("name"), - SORT_DIRECTION("ASC"); + SORT_DIRECTION("asc"); private final int intValue; From c99124874ea51538b09b8990ee888ab11e5c7ed4 Mon Sep 17 00:00:00 2001 From: yevr19 Date: Fri, 8 Sep 2023 23:51:19 +0200 Subject: [PATCH 3/4] test(product): add unit tests for product package --- .../api/PageableProductsProviderTest.java | 17 +++++---- .../product/api/ProductApiImplTest.java | 36 +++++++++---------- .../api/SingleProductProviderTest.java | 20 +++++------ .../util/ProductPaginationDefaults.java | 26 -------------- 4 files changed, 38 insertions(+), 61 deletions(-) delete mode 100644 src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java 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 index edb94e61..946c40fc 100644 --- a/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java +++ b/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java @@ -14,14 +14,12 @@ 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 com.zufar.onlinestore.product.util.ProductPaginationDefaults.PAGE; -import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SIZE; -import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_ATTRIBUTE; -import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_DIRECTION; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -51,13 +49,18 @@ void setUp() { @Test void shouldFetchProductsUsingPageAttributes() { Page page = new PageImpl<>(products); + final int pageNumber = 1; + final int size = 10; + final String sortAttribute = "name"; - when(productRepository.findAll(any(Pageable.class))).thenReturn(page); + 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(PAGE.getIntValue(), SIZE.getIntValue(), - SORT_ATTRIBUTE.getStringValue(), SORT_DIRECTION.getStringValue() + ProductListWithPaginationInfoDto productList = productsProvider.getProducts(pageNumber, size, + sortAttribute, Sort.Direction.ASC.name() ); assertNotNull(productList); 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 index 3305d3a8..1946a589 100644 --- a/src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java +++ b/src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java @@ -6,13 +6,10 @@ 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 com.zufar.onlinestore.product.util.ProductPaginationDefaults.PAGE; -import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SIZE; -import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_ATTRIBUTE; -import static com.zufar.onlinestore.product.util.ProductPaginationDefaults.SORT_DIRECTION; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -33,28 +30,32 @@ class ProductApiImplTest { @Test void shouldReturnProductsForValidPaginationParameters() { + final int pageNumber = 1; + final int size = 10; + final String sortAttribute = "name"; + when(pageableProductsProvider.getProducts( - PAGE.getIntValue(), - SIZE.getIntValue(), - SORT_ATTRIBUTE.getStringValue(), - SORT_DIRECTION.getStringValue() + pageNumber, + size, + sortAttribute, + Sort.Direction.ASC.name() ) ).thenReturn(mock(ProductListWithPaginationInfoDto.class)); ProductListWithPaginationInfoDto result = productApi.getProducts( - PAGE.getIntValue(), - SIZE.getIntValue(), - SORT_ATTRIBUTE.getStringValue(), - SORT_DIRECTION.getStringValue() + pageNumber, + size, + sortAttribute, + Sort.Direction.ASC.name() ); assertNotNull(result); verify(pageableProductsProvider, times(1)).getProducts( - PAGE.getIntValue(), - SIZE.getIntValue(), - SORT_ATTRIBUTE.getStringValue(), - SORT_DIRECTION.getStringValue() + pageNumber, + size, + sortAttribute, + Sort.Direction.ASC.name() ); } @@ -62,8 +63,7 @@ void shouldReturnProductsForValidPaginationParameters() { void shouldReturnProductForValidProductId() { UUID productId = UUID.randomUUID(); - when(singleProductProvider.getProductById(productId)) - .thenReturn(mock(ProductInfoDto.class)); + when(singleProductProvider.getProductById(productId)).thenReturn(mock(ProductInfoDto.class)); ProductInfoDto result = productApi.getProduct(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 index ee3efa91..66ecb091 100644 --- a/src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java +++ b/src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java @@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +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; @@ -54,16 +54,16 @@ void shouldThrowExceptionWhenProductIdNotExists() { when(productRepository.findById(productId)).thenReturn(Optional.empty()); - ProductInfoDto retrievedProduct; + ProductNotFoundException thrownException = assertThrows( + ProductNotFoundException.class, + () -> productProvider.getProductById(productId) + ); + + assertEquals( + String.format("The product with productId = %s is not found.", productId), + thrownException.getMessage() + ); - try { - retrievedProduct = productProvider.getProductById(productId); - if (retrievedProduct == null) { - fail("Expected ProductNotFoundException to be thrown"); - } - } catch (ProductNotFoundException e) { - assertEquals(String.format("The product with productId = %s is not found.", productId), e.getMessage()); - } verify(productRepository, times(1)).findById(productId); } } diff --git a/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java b/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java deleted file mode 100644 index 55e7fb97..00000000 --- a/src/main/test/java/com/zufar/onlinestore/product/util/ProductPaginationDefaults.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.zufar.onlinestore.product.util; - -import lombok.Getter; - -@Getter -public enum ProductPaginationDefaults { - - PAGE(1), - SIZE(10), - SORT_ATTRIBUTE("name"), - SORT_DIRECTION("asc"); - - private final int intValue; - - private final String stringValue; - - ProductPaginationDefaults(int intValue) { - this.intValue = intValue; - this.stringValue = null; - } - - ProductPaginationDefaults(String stringValue) { - this.stringValue = stringValue; - this.intValue = -1; - } -} From af089f2236b95d4edb1aa1e0e673fb43aa0c4b60 Mon Sep 17 00:00:00 2001 From: yevr19 Date: Fri, 8 Sep 2023 23:57:48 +0200 Subject: [PATCH 4/4] test(product): add unit tests for product package --- .../onlinestore/product/api/PageableProductsProviderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 946c40fc..11db21e3 100644 --- a/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java +++ b/src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java @@ -65,7 +65,7 @@ void shouldFetchProductsUsingPageAttributes() { assertNotNull(productList); - verify(productRepository, times(1)).findAll(any(Pageable.class)); + verify(productRepository, times(1)).findAll(pageRequest); verify(productInfoConverter, times(1)).toProductPaginationDto(ArgumentMatchers.>any()); } }