Skip to content

Commit

Permalink
Merge pull request #94 from yevr19/feature/product-8/add-product-unit…
Browse files Browse the repository at this point in the history
…-tests

[product-8] Add Product Unit Tests
  • Loading branch information
Sunagatov authored Sep 9, 2023
2 parents 97fa1a1 + af089f2 commit c8a0772
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
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());
}
}
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);
}
}
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.

0 comments on commit c8a0772

Please sign in to comment.