-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from yevr19/feature/product-8/add-product-unit…
…-tests [product-8] Add Product Unit Tests
- Loading branch information
Showing
5 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/test/java/com/zufar/onlinestore/product/api/PageableProductsProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ProductInfo> products; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
products = Instancio.ofList(ProductInfo.class).create(); | ||
} | ||
|
||
@Test | ||
void shouldFetchProductsUsingPageAttributes() { | ||
Page<ProductInfo> 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.<Page<ProductInfoDto>>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.<Page<ProductInfoDto>>any()); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/test/java/com/zufar/onlinestore/product/api/ProductApiImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/test/java/com/zufar/onlinestore/product/api/SingleProductProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Empty file.