Added product retriever with tests
This commit is contained in:
@@ -0,0 +1,67 @@
|
|||||||
|
package de.rwu.easydrop.service.retriever;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.api.client.AmazonProductDataSource;
|
||||||
|
import de.rwu.easydrop.api.client.DataSourceFactory;
|
||||||
|
import de.rwu.easydrop.api.client.EbayItemDataSource;
|
||||||
|
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||||
|
import de.rwu.easydrop.model.Product;
|
||||||
|
import de.rwu.easydrop.service.mapping.ProductMapper;
|
||||||
|
import de.rwu.easydrop.service.validation.ProductValidator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves Product Objects from various data sources.
|
||||||
|
*
|
||||||
|
* @since 0.2.0
|
||||||
|
*/
|
||||||
|
public class ProductRetriever {
|
||||||
|
/**
|
||||||
|
* Data source factory.
|
||||||
|
*/
|
||||||
|
private DataSourceFactory dataSourceFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newDataSourceFactory the dataSourceFactory to set
|
||||||
|
*/
|
||||||
|
public void setDataSourceFactory(final DataSourceFactory newDataSourceFactory) {
|
||||||
|
this.dataSourceFactory = newDataSourceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newDataSourceFactory
|
||||||
|
*/
|
||||||
|
public ProductRetriever(final DataSourceFactory newDataSourceFactory) {
|
||||||
|
this.setDataSourceFactory(newDataSourceFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a product from Amazon.
|
||||||
|
*
|
||||||
|
* @param asin Product identifier
|
||||||
|
* @return Product from Amazon
|
||||||
|
*/
|
||||||
|
public Product getProductFromAmazon(final String asin) {
|
||||||
|
AmazonProductDataSource dataSource = dataSourceFactory.createAmazonProductDataSource();
|
||||||
|
ProductDTO dto = dataSource.getProductDTOById(asin);
|
||||||
|
|
||||||
|
Product product = ProductMapper.mapProductFromDTO(dto);
|
||||||
|
ProductValidator.validate(product);
|
||||||
|
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a product from eBay.
|
||||||
|
*
|
||||||
|
* @param query Product search query
|
||||||
|
* @return Product from eBay
|
||||||
|
*/
|
||||||
|
public Product getProductFromEbay(final String query) {
|
||||||
|
EbayItemDataSource dataSource = dataSourceFactory.createEbayItemDataSource();
|
||||||
|
ProductDTO dto = dataSource.getProductDTOById(query);
|
||||||
|
|
||||||
|
Product product = ProductMapper.mapProductFromDTO(dto);
|
||||||
|
ProductValidator.validate(product);
|
||||||
|
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
/**
|
||||||
|
* Retrieves Objects from a data source.
|
||||||
|
*
|
||||||
|
* @since 0.2.0
|
||||||
|
*/
|
||||||
|
package de.rwu.easydrop.service.retriever;
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package de.rwu.easydrop.service.retriever;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import javax.naming.ConfigurationException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.api.client.AmazonProductDataSource;
|
||||||
|
import de.rwu.easydrop.api.client.DataSourceFactory;
|
||||||
|
import de.rwu.easydrop.api.client.EbayItemDataSource;
|
||||||
|
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||||
|
import de.rwu.easydrop.model.Product;
|
||||||
|
import de.rwu.easydrop.util.Config;
|
||||||
|
|
||||||
|
class ProductRetrieverTest {
|
||||||
|
@Mock
|
||||||
|
private Config config;
|
||||||
|
@Mock
|
||||||
|
private DataSourceFactory dataSourceFactory;
|
||||||
|
@Mock
|
||||||
|
private AmazonProductDataSource amazonDataSource;
|
||||||
|
@Mock
|
||||||
|
private EbayItemDataSource ebayDataSource;
|
||||||
|
@Mock
|
||||||
|
private ProductDTO productDTO;
|
||||||
|
@Mock
|
||||||
|
private Product product;
|
||||||
|
|
||||||
|
private ProductRetriever productRetriever;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() throws ConfigurationException {
|
||||||
|
MockitoAnnotations.openMocks(this);
|
||||||
|
when(config.getProperty("AMAZON_API_URL")).thenReturn("https://api.amazon.com");
|
||||||
|
when(config.getProperty("AMAZON_API_KEY")).thenReturn("amazon-api-key");
|
||||||
|
dataSourceFactory.setConfig(config);
|
||||||
|
productRetriever = new ProductRetriever(dataSourceFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getProductFromAmazon_ReturnsProduct() {
|
||||||
|
// Arrange
|
||||||
|
String asin = "B01234ABC";
|
||||||
|
when(dataSourceFactory.createAmazonProductDataSource()).thenReturn(amazonDataSource);
|
||||||
|
when(amazonDataSource.getProductDTOById(asin)).thenReturn(productDTO);
|
||||||
|
when(productDTO.getProductId()).thenReturn(asin);
|
||||||
|
when(productDTO.getCurrentPrice()).thenReturn(9.99);
|
||||||
|
when(productDTO.getDataOrigin()).thenReturn("Amazon");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Product result = productRetriever.getProductFromAmazon(asin);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(asin, result.getProductId());
|
||||||
|
assertEquals(9.99, result.getCurrentPrice());
|
||||||
|
verify(amazonDataSource, times(1)).getProductDTOById(asin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getProductFromEbay_ReturnsProduct() {
|
||||||
|
// Arrange
|
||||||
|
String productQuery = "MySearchQuery";
|
||||||
|
when(dataSourceFactory.createEbayItemDataSource()).thenReturn(ebayDataSource);
|
||||||
|
when(ebayDataSource.getProductDTOById(productQuery)).thenReturn(productDTO);
|
||||||
|
when(productDTO.getProductId()).thenReturn(productQuery);
|
||||||
|
when(productDTO.getCurrentPrice()).thenReturn(9.99);
|
||||||
|
when(productDTO.getDataOrigin()).thenReturn("eBay");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
Product result = productRetriever.getProductFromEbay(productQuery);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(productQuery, result.getProductId());
|
||||||
|
assertEquals(9.99, result.getCurrentPrice());
|
||||||
|
verify(ebayDataSource, times(1)).getProductDTOById(productQuery);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user