Added and rewrote tests
This commit is contained in:
@@ -108,4 +108,15 @@ class AbstractDataWriterTest {
|
||||
mockWriter.sendPutRequest(dto, "Purchase");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendPutRequest_wrongDataTarget() {
|
||||
ProductDTO dto = new ProductDTO(demoProductId, Webshop.EBAY);
|
||||
|
||||
DataWriterException e = assertThrows(DataWriterException.class, () -> {
|
||||
writer.sendPutRequest(dto, demoProductId);
|
||||
});
|
||||
|
||||
assertEquals("Product data source and target whateverId API are incompatible.", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +168,33 @@ class AmazonProductDataSourceTest {
|
||||
assertEquals("Nothing found: AMAZON API responded with error code 404", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetProductDTOById_emptyResponse() throws IOException {
|
||||
// Set up the test environment
|
||||
String mockResponse = "";
|
||||
|
||||
AmazonProductDataSource dataSource = mock(AmazonProductDataSource.class);
|
||||
URL mockURL = mock(URL.class);
|
||||
when(dataSource.createApiUrl(demoProductId)).thenReturn(mockURL);
|
||||
when(dataSource.buildProductDTO(any(), anyString())).thenCallRealMethod();
|
||||
HttpURLConnection mockConnection = mock(HttpURLConnection.class);
|
||||
when(mockURL.openConnection()).thenReturn(mockConnection);
|
||||
when(mockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
|
||||
when(mockConnection.getInputStream()).thenReturn(new ByteArrayInputStream(mockResponse.getBytes()));
|
||||
when(dataSource.getProductDTOById(demoProductId)).thenCallRealMethod();
|
||||
|
||||
// Invoke the method
|
||||
ProductDTO result = dataSource.getProductDTOById(demoProductId);
|
||||
|
||||
// Verify the product DTO properties
|
||||
assertEquals(demoProductId, result.getProductId());
|
||||
assertEquals(Webshop.AMAZON, result.getDataOrigin());
|
||||
assertEquals(false, result.isAvailable());
|
||||
assertEquals(0.0, result.getCurrentPrice());
|
||||
assertEquals(0.0, result.getDeliveryPrice());
|
||||
assertEquals(null, result.getMerchant());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetProductDTOById_ioException() throws IOException {
|
||||
// Set up the test environment
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package de.rwu.easydrop.api.client;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
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.util.Config;
|
||||
|
||||
class PurchaserFactoryTest {
|
||||
|
||||
@Mock
|
||||
private Config mockConfig;
|
||||
|
||||
private PurchaserFactory purchaserFactory;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws ConfigurationException {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
purchaserFactory = new PurchaserFactory(mockConfig);
|
||||
purchaserFactory.setConfig(mockConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAmazonPurchaser_ValidConfig_ReturnsAmazonPurchaser() {
|
||||
// Arrange
|
||||
String amazonApiUrl = "amazon_api_url";
|
||||
String amazonApiKey = "amazon_api_key";
|
||||
|
||||
when(mockConfig.getProperty("AMAZON_API_URL")).thenReturn(amazonApiUrl);
|
||||
when(mockConfig.getProperty("AMAZON_API_KEY")).thenReturn(amazonApiKey);
|
||||
|
||||
// Act
|
||||
AmazonPurchaser amazonPurchaser = purchaserFactory.createAmazonPurchaser();
|
||||
|
||||
// Assert
|
||||
assertNotNull(amazonPurchaser);
|
||||
assertEquals(amazonApiKey, amazonPurchaser.getApiKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEbayPurchaser_ValidConfig_ReturnsEbayPurchaser() {
|
||||
// Arrange
|
||||
String ebayApiUrl = "ebay_api_url";
|
||||
String ebayApiKey = "ebay_api_key";
|
||||
|
||||
when(mockConfig.getProperty("EBAY_API_URL")).thenReturn(ebayApiUrl);
|
||||
when(mockConfig.getProperty("EBAY_API_KEY")).thenReturn(ebayApiKey);
|
||||
|
||||
// Act
|
||||
EbayPurchaser ebayPurchaser = purchaserFactory.createEbayPurchaser();
|
||||
|
||||
// Assert
|
||||
assertNotNull(ebayPurchaser);
|
||||
assertEquals(ebayApiKey, ebayPurchaser.getApiKey());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package de.rwu.easydrop.api.client;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
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.util.Config;
|
||||
|
||||
class SellerFactoryTest {
|
||||
|
||||
@Mock
|
||||
private Config mockConfig;
|
||||
|
||||
private SellerFactory sellerFactory;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws ConfigurationException {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
sellerFactory = new SellerFactory(mockConfig);
|
||||
sellerFactory.setConfig(mockConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAmazonSeller_ValidConfig_ReturnsAmazonSeller() {
|
||||
// Arrange
|
||||
String amazonApiUrl = "amazon_api_url";
|
||||
String amazonApiKey = "amazon_api_key";
|
||||
|
||||
when(mockConfig.getProperty("AMAZON_API_URL")).thenReturn(amazonApiUrl);
|
||||
when(mockConfig.getProperty("AMAZON_API_KEY")).thenReturn(amazonApiKey);
|
||||
|
||||
// Act
|
||||
AmazonSeller amazonSeller = sellerFactory.createAmazonSeller();
|
||||
|
||||
// Assert
|
||||
assertNotNull(amazonSeller);
|
||||
assertEquals(amazonApiKey, amazonSeller.getApiKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEbaySeller_ValidConfig_ReturnsEbaySeller() {
|
||||
// Arrange
|
||||
String ebayApiUrl = "ebay_api_url";
|
||||
String ebayApiKey = "ebay_api_key";
|
||||
|
||||
when(mockConfig.getProperty("EBAY_API_URL")).thenReturn(ebayApiUrl);
|
||||
when(mockConfig.getProperty("EBAY_API_KEY")).thenReturn(ebayApiKey);
|
||||
|
||||
// Act
|
||||
EbaySeller ebaySeller = sellerFactory.createEbaySeller();
|
||||
|
||||
// Assert
|
||||
assertNotNull(ebaySeller);
|
||||
assertEquals(ebayApiKey, ebaySeller.getApiKey());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package de.rwu.easydrop.core;
|
||||
|
||||
public class CoreTest {
|
||||
}
|
||||
@@ -8,7 +8,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -17,13 +21,23 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.sqlite.SQLiteDataSource;
|
||||
|
||||
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
import de.rwu.easydrop.api.dto.TransactionDTO;
|
||||
import de.rwu.easydrop.exception.PersistenceException;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
|
||||
class SQLiteConnectorTest {
|
||||
private static final String TEST_PRODUCT_ID = "12345";
|
||||
private SQLiteConnector sqliteConnector = new SQLiteConnector(new SQLiteDataSource());
|
||||
@Mock
|
||||
private Connection connection;
|
||||
|
||||
@Mock
|
||||
private PreparedStatement statement;
|
||||
|
||||
@Mock
|
||||
private ResultSet resultSet;
|
||||
|
||||
@Mock
|
||||
private SQLiteDataSource mockDataSource;
|
||||
@@ -135,11 +149,179 @@ class SQLiteConnectorTest {
|
||||
}
|
||||
|
||||
private void insertSampleProduct() {
|
||||
ProductDTO ProductDTO = new ProductDTO(TEST_PRODUCT_ID, Webshop.AMAZON);
|
||||
ProductDTO.setCurrentPrice(9.99);
|
||||
ProductDTO.setMerchant("Sample Merchant");
|
||||
ProductDTO.setDeliveryPrice(2.50);
|
||||
ProductDTO.setAvailable(true);
|
||||
sqliteConnector.writeProduct(ProductDTO);
|
||||
ProductDTO productDTO = new ProductDTO(TEST_PRODUCT_ID, Webshop.AMAZON);
|
||||
productDTO.setCurrentPrice(9.99);
|
||||
productDTO.setMerchant("Sample Merchant");
|
||||
productDTO.setDeliveryPrice(2.50);
|
||||
productDTO.setAvailable(true);
|
||||
sqliteConnector.writeProduct(productDTO);
|
||||
}
|
||||
|
||||
private void insertSampleOffer() {
|
||||
ProductDTO p1 = new ProductDTO("p1", Webshop.AMAZON);
|
||||
p1.setCurrentPrice(123f);
|
||||
|
||||
ProductDTO p2 = new ProductDTO("p2", Webshop.EBAY);
|
||||
p2.setCurrentPrice(234f);
|
||||
|
||||
OfferDTO dto = new OfferDTO();
|
||||
dto.setOfferId("123");
|
||||
dto.setLastUpdate("2020-01-01 00:00:00");
|
||||
dto.setSourceProduct(p1);
|
||||
dto.setTargetProduct(p2);
|
||||
sqliteConnector.writeOffer(dto);
|
||||
}
|
||||
|
||||
private void insertSampleTransaction() {
|
||||
TransactionDTO dto = new TransactionDTO();
|
||||
dto.setOfferId("123");
|
||||
dto.setEarnings(12f);
|
||||
dto.setVolume(123f);
|
||||
dto.setTransactionTime("2020-01-01 00:00:00");
|
||||
sqliteConnector.writeTX(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputTransactionsToLog_NothingThrown() {
|
||||
insertSampleTransaction();
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
sqliteConnector.outputTransactionsToLog();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputSummaryToLog_NothingThrown() {
|
||||
insertSampleTransaction();
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
sqliteConnector.outputSummaryToLog();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputTransactionsToLog_ThrowsPersistenceException_OnSQLException() throws SQLException {
|
||||
// Arrange
|
||||
sqliteConnector.setDb(mockDataSource);
|
||||
doThrow(SQLException.class).when(mockDataSource).getConnection();
|
||||
|
||||
// Act and Assert
|
||||
PersistenceException e = assertThrows(PersistenceException.class,
|
||||
() -> sqliteConnector.outputTransactionsToLog());
|
||||
assertEquals("Something went wrong while reading transaction from SQLite", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputSummaryToLog_ThrowsPersistenceException_OnSQLException() throws SQLException {
|
||||
// Arrange
|
||||
sqliteConnector.setDb(mockDataSource);
|
||||
doThrow(SQLException.class).when(mockDataSource).getConnection();
|
||||
|
||||
// Act and Assert
|
||||
PersistenceException e = assertThrows(PersistenceException.class,
|
||||
() -> sqliteConnector.outputSummaryToLog());
|
||||
assertEquals("Something went wrong while reading transaction summary from SQLite", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOfferById_SuccessfulDelete() {
|
||||
// Arrange
|
||||
sqliteConnector.clearData();
|
||||
insertSampleOffer();
|
||||
|
||||
// Make sure it's there
|
||||
assertEquals(1, sqliteConnector.getOffers().size());
|
||||
|
||||
// Act
|
||||
sqliteConnector.deleteOfferById("123");
|
||||
|
||||
// Assert
|
||||
assertEquals(0, sqliteConnector.getOffers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOfferById_ThrowsPersistenceException_OnSQLException() throws SQLException {
|
||||
sqliteConnector.setDb(mockDataSource);
|
||||
doThrow(SQLException.class).when(mockDataSource).getConnection();
|
||||
|
||||
// Act and Assert
|
||||
PersistenceException e = assertThrows(PersistenceException.class,
|
||||
() -> sqliteConnector.deleteOfferById("123"));
|
||||
assertEquals("Something went wrong while deleting offer from SQLite", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOffers_ReturnsCorrectValues() {
|
||||
sqliteConnector.clearData();
|
||||
assertEquals(0, sqliteConnector.getOffers().size());
|
||||
|
||||
insertSampleOffer();
|
||||
|
||||
assertEquals(1, sqliteConnector.getOffers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOffers_ThrowsPersistenceException_OnSQLException() throws SQLException {
|
||||
sqliteConnector.setDb(mockDataSource);
|
||||
doThrow(SQLException.class).when(mockDataSource).getConnection();
|
||||
|
||||
// Act and Assert
|
||||
PersistenceException e = assertThrows(PersistenceException.class,
|
||||
() -> sqliteConnector.getOffers());
|
||||
assertEquals("Something went wrong while reading offers from SQLite", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeOffer_ValidDTO_DataSavedToDatabase() throws SQLException {
|
||||
// Arrange
|
||||
OfferDTO offerDTO = new OfferDTO();
|
||||
offerDTO.setOfferId("123");
|
||||
offerDTO.setSourceProduct(new ProductDTO(TEST_PRODUCT_ID, Webshop.AMAZON));
|
||||
offerDTO.setTargetProduct(new ProductDTO(TEST_PRODUCT_ID, Webshop.AMAZON));
|
||||
|
||||
String expectedQuery = "INSERT INTO offers (offerId, sourceWebshop, sourceId, sourcePrice, " +
|
||||
"targetWebshop, targetId, targetPrice, lastupdate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
when(connection.prepareStatement(expectedQuery)).thenReturn(statement);
|
||||
|
||||
// Act
|
||||
assertDoesNotThrow(() -> sqliteConnector.writeOffer(offerDTO));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeTX_NothingThrown() {
|
||||
TransactionDTO dto = new TransactionDTO();
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
sqliteConnector.writeTX(dto);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeTX_ThrowsPersistenceException_OnSQLException() throws SQLException {
|
||||
// Arrange
|
||||
sqliteConnector.setDb(mockDataSource);
|
||||
doThrow(SQLException.class).when(mockDataSource).getConnection();
|
||||
TransactionDTO dto = new TransactionDTO();
|
||||
|
||||
// Act and Assert
|
||||
PersistenceException e = assertThrows(PersistenceException.class,
|
||||
() -> sqliteConnector.writeTX(dto));
|
||||
assertEquals("Something went wrong while saving transaction to SQLite", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOfferDTOById_OfferExists_ReturnsProductDTO() {
|
||||
// Arrange
|
||||
sqliteConnector.clearData();
|
||||
insertSampleOffer();
|
||||
|
||||
// Act
|
||||
OfferDTO offerDTO = sqliteConnector.getOfferDTOById("123");
|
||||
|
||||
// Assert
|
||||
assertNotNull(offerDTO);
|
||||
assertEquals("123", offerDTO.getOfferId());
|
||||
assertEquals("2020-01-01 00:00:00", offerDTO.getLastUpdate());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.rwu.easydrop.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class InvalidOfferExceptionTest {
|
||||
|
||||
@Test
|
||||
void constructor_WithMessage_SetsMessage() {
|
||||
// Arrange
|
||||
String message = "Invalid Offer data";
|
||||
|
||||
// Act
|
||||
InvalidOfferException exception = new InvalidOfferException(message);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithMessageAndCause_SetsMessageAndCause() {
|
||||
// Arrange
|
||||
String message = "Invalid Offer data";
|
||||
Throwable cause = new IllegalArgumentException("Invalid argument");
|
||||
|
||||
// Act
|
||||
InvalidOfferException exception = new InvalidOfferException(message, cause);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullMessage_SetsNullMessage() {
|
||||
// Act
|
||||
InvalidOfferException exception = new InvalidOfferException(null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullCause_SetsNullCause() {
|
||||
// Act
|
||||
InvalidOfferException exception = new InvalidOfferException("Invalid Offer data", null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void throw_InvalidOfferException() {
|
||||
// Act and Assert
|
||||
assertThrows(InvalidOfferException.class, () -> {
|
||||
throw new InvalidOfferException("Invalid Offer data");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.rwu.easydrop.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class InvalidTransactionExceptionTest {
|
||||
|
||||
@Test
|
||||
void constructor_WithMessage_SetsMessage() {
|
||||
// Arrange
|
||||
String message = "Invalid Transaction data";
|
||||
|
||||
// Act
|
||||
InvalidTransactionException exception = new InvalidTransactionException(message);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithMessageAndCause_SetsMessageAndCause() {
|
||||
// Arrange
|
||||
String message = "Invalid Transaction data";
|
||||
Throwable cause = new IllegalArgumentException("Invalid argument");
|
||||
|
||||
// Act
|
||||
InvalidTransactionException exception = new InvalidTransactionException(message, cause);
|
||||
|
||||
// Assert
|
||||
assertEquals(message, exception.getMessage());
|
||||
assertEquals(cause, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullMessage_SetsNullMessage() {
|
||||
// Act
|
||||
InvalidTransactionException exception = new InvalidTransactionException(null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_WithNullCause_SetsNullCause() {
|
||||
// Act
|
||||
InvalidTransactionException exception = new InvalidTransactionException("Invalid Transaction data", null);
|
||||
|
||||
// Assert
|
||||
assertEquals(null, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
void throw_InvalidTransactionException() {
|
||||
// Act and Assert
|
||||
assertThrows(InvalidTransactionException.class, () -> {
|
||||
throw new InvalidTransactionException("Invalid Transaction data");
|
||||
});
|
||||
}
|
||||
}
|
||||
25
src/test/java/de/rwu/easydrop/model/ProductPairTest.java
Normal file
25
src/test/java/de/rwu/easydrop/model/ProductPairTest.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package de.rwu.easydrop.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ProductPairTest {
|
||||
|
||||
@Test
|
||||
void constructor_TwoProducts_ProductsInitializedCorrectly() {
|
||||
// Arrange
|
||||
Product product1 = new Product();
|
||||
product1.setProductId("123");
|
||||
|
||||
Product product2 = new Product();
|
||||
product2.setProductId("234");
|
||||
|
||||
// Act
|
||||
ProductPair pair = new ProductPair(product1, product2);
|
||||
|
||||
// Assert
|
||||
assertSame(product1, pair.getProduct1());
|
||||
assertSame(product2, pair.getProduct2());
|
||||
}
|
||||
}
|
||||
23
src/test/java/de/rwu/easydrop/model/WebshopTest.java
Normal file
23
src/test/java/de/rwu/easydrop/model/WebshopTest.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package de.rwu.easydrop.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WebshopTest {
|
||||
|
||||
@Test
|
||||
void testFromString_returnsIntendedConstant() {
|
||||
Webshop testShop = Webshop.fromString("Amazon");
|
||||
|
||||
assertEquals(Webshop.AMAZON, testShop);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFromString_invalidShop() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
Webshop.fromString("thisdoesnotexist");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package de.rwu.easydrop.service.mapping;
|
||||
|
||||
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.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.rwu.easydrop.api.dto.TransactionDTO;
|
||||
import de.rwu.easydrop.model.Transaction;
|
||||
|
||||
class TransactionMapperTest {
|
||||
@Test
|
||||
void testConstructorIsPrivate()
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
// Check for private constructor
|
||||
Constructor<TransactionMapper> constructor = TransactionMapper.class.getDeclaredConstructor();
|
||||
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
|
||||
|
||||
// Make sure exception is thrown when instantiating
|
||||
constructor.setAccessible(true);
|
||||
assertThrows(InvocationTargetException.class, () -> {
|
||||
constructor.newInstance();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapTXFromDTO_WithValidTransactionDTO_ReturnsTransaction() {
|
||||
// Arrange
|
||||
TransactionDTO transactionDTO = new TransactionDTO();
|
||||
transactionDTO.setOfferId("123");
|
||||
transactionDTO.setVolume(10.0);
|
||||
transactionDTO.setEarnings(5.0);
|
||||
transactionDTO.setTransactionTime("2023-06-25 03:26:59");
|
||||
|
||||
// Act
|
||||
Transaction transaction = TransactionMapper.mapTXFromDTO(transactionDTO);
|
||||
|
||||
// Assert
|
||||
assertNotNull(transaction);
|
||||
assertEquals(transactionDTO.getOfferId(), transaction.getOfferId());
|
||||
assertEquals(transactionDTO.getVolume(), transaction.getVolume());
|
||||
assertEquals(transactionDTO.getEarnings(), transaction.getEarnings());
|
||||
assertEquals(transactionDTO.getTransactionTime(), transaction.getTransactionTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapTXToDTO_WithValidTransaction_ReturnsTransactionDTO() {
|
||||
// Arrange
|
||||
Transaction transaction = new Transaction();
|
||||
transaction.setOfferId("123");
|
||||
transaction.setVolume(10.0);
|
||||
transaction.setEarnings(5.0);
|
||||
transaction.setTransactionTime("2023-06-25 03:26:59");
|
||||
|
||||
// Act
|
||||
TransactionDTO transactionDTO = TransactionMapper.mapTXToDTO(transaction);
|
||||
|
||||
// Assert
|
||||
assertNotNull(transactionDTO);
|
||||
assertEquals(transaction.getOfferId(), transactionDTO.getOfferId());
|
||||
assertEquals(transaction.getVolume(), transactionDTO.getVolume());
|
||||
assertEquals(transaction.getEarnings(), transactionDTO.getEarnings());
|
||||
assertEquals(transaction.getTransactionTime(), transactionDTO.getTransactionTime());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,176 @@
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
public class OfferIdentifierTest {
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidCatalogueException;
|
||||
import de.rwu.easydrop.exception.InvalidOfferException;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.model.ProductPair;
|
||||
|
||||
class OfferIdentifierTest {
|
||||
|
||||
@Mock
|
||||
private ProductCatalogue mockProductCatalogue;
|
||||
|
||||
private OfferIdentifier offerIdentifier;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
|
||||
offerIdentifier = new OfferIdentifier();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runIdentifier_WithValidProductCatalogues_IdentifiesOffers() {
|
||||
// Arrange
|
||||
List<ProductCatalogue> productCatalogues = new ArrayList<>();
|
||||
ProductCatalogue productCatalogue1 = new ProductCatalogue("Catalogue1", "desc");
|
||||
ProductCatalogue productCatalogue2 = new ProductCatalogue("Catalogue2", "desc");
|
||||
|
||||
Product product1 = new Product();
|
||||
product1.setCurrentPrice(100.0);
|
||||
Product product2 = new Product();
|
||||
product2.setCurrentPrice(200.0);
|
||||
Product product3 = new Product();
|
||||
product3.setCurrentPrice(300.0);
|
||||
Product product4 = new Product();
|
||||
product4.setCurrentPrice(400.0);
|
||||
|
||||
productCatalogue1.addProduct(product1);
|
||||
productCatalogue1.addProduct(product2);
|
||||
productCatalogue2.addProduct(product3);
|
||||
productCatalogue2.addProduct(product4);
|
||||
|
||||
productCatalogues.add(productCatalogue1);
|
||||
productCatalogues.add(productCatalogue2);
|
||||
|
||||
// Act
|
||||
List<Offer> identifiedOffers = offerIdentifier.runIdentifier(productCatalogues);
|
||||
|
||||
// Assert
|
||||
assertEquals(2, identifiedOffers.size());
|
||||
assertNotNull(identifiedOffers.get(0).getSourceProduct());
|
||||
assertNotNull(identifiedOffers.get(0).getTargetProduct());
|
||||
assertNotNull(identifiedOffers.get(1).getSourceProduct());
|
||||
assertNotNull(identifiedOffers.get(1).getTargetProduct());
|
||||
}
|
||||
|
||||
@Test
|
||||
void runIdentifier_WithOneGoodOneBadCatalogue_BadOneIsntIncluded() {
|
||||
// Arrange
|
||||
List<ProductCatalogue> productCatalogues = new ArrayList<>();
|
||||
ProductCatalogue productCatalogue1 = new ProductCatalogue("Catalogue1", "desc");
|
||||
ProductCatalogue productCatalogue2 = new ProductCatalogue("Catalogue2", "desc");
|
||||
|
||||
Product product1 = new Product();
|
||||
product1.setCurrentPrice(100.0);
|
||||
Product product2 = new Product();
|
||||
product2.setCurrentPrice(200.0);
|
||||
Product product3 = new Product();
|
||||
product3.setCurrentPrice(300.0);
|
||||
Product product4 = new Product();
|
||||
product4.setCurrentPrice(300.0);
|
||||
|
||||
productCatalogue1.addProduct(product1);
|
||||
productCatalogue1.addProduct(product2);
|
||||
productCatalogue2.addProduct(product3);
|
||||
productCatalogue2.addProduct(product4);
|
||||
|
||||
productCatalogues.add(productCatalogue1);
|
||||
productCatalogues.add(productCatalogue2);
|
||||
|
||||
// Act
|
||||
List<Offer> identifiedOffers = offerIdentifier.runIdentifier(productCatalogues);
|
||||
|
||||
// Assert
|
||||
assertEquals(1, identifiedOffers.size()); // Only 1 passed
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHighestMarginProducts_WithValidProductCatalogue_ReturnsProductPair() {
|
||||
// Arrange
|
||||
ProductCatalogue productCatalogue = new ProductCatalogue("TestCatalogue", "desc");
|
||||
Product product1 = new Product();
|
||||
product1.setCurrentPrice(100.0);
|
||||
Product product2 = new Product();
|
||||
product2.setCurrentPrice(200.0);
|
||||
productCatalogue.addProduct(product1);
|
||||
productCatalogue.addProduct(product2);
|
||||
|
||||
// Act
|
||||
ProductPair productPair = OfferIdentifier.getHighestMarginProducts(productCatalogue);
|
||||
|
||||
// Assert
|
||||
assertNotNull(productPair);
|
||||
assertNotNull(productPair.getProduct1());
|
||||
assertNotNull(productPair.getProduct2());
|
||||
assertNotEquals(productPair.getProduct1(), productPair.getProduct2());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHighestMarginProducts_SecondProductCheaper() {
|
||||
// Arrange
|
||||
ProductCatalogue productCatalogue = new ProductCatalogue("TestCatalogue", "desc");
|
||||
Product product1 = new Product();
|
||||
product1.setCurrentPrice(200.0);
|
||||
Product product2 = new Product();
|
||||
product2.setCurrentPrice(100.0);
|
||||
productCatalogue.addProduct(product1);
|
||||
productCatalogue.addProduct(product2);
|
||||
|
||||
// Act
|
||||
ProductPair productPair = OfferIdentifier.getHighestMarginProducts(productCatalogue);
|
||||
|
||||
// Assert
|
||||
assertNotNull(productPair);
|
||||
assertNotNull(productPair.getProduct1());
|
||||
assertNotNull(productPair.getProduct2());
|
||||
assertNotEquals(productPair.getProduct1(), productPair.getProduct2());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHighestMarginProducts_WithCatalogueSizeLessThan2_ThrowsInvalidCatalogueException() {
|
||||
// Arrange
|
||||
ProductCatalogue productCatalogue = new ProductCatalogue("SmallCatalogue", "desc");
|
||||
|
||||
// Act and Assert
|
||||
assertThrows(InvalidCatalogueException.class, () -> {
|
||||
OfferIdentifier.getHighestMarginProducts(productCatalogue);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHighestMarginProducts_WithZeroPriceMargin_ThrowsInvalidOfferException() {
|
||||
// Arrange
|
||||
ProductCatalogue productCatalogue = new ProductCatalogue("name", "desc");
|
||||
Product product1 = new Product();
|
||||
product1.setCurrentPrice(200.0);
|
||||
|
||||
Product product2 = new Product();
|
||||
product2.setCurrentPrice(200.0);
|
||||
|
||||
productCatalogue.getProducts().add(product1);
|
||||
productCatalogue.getProducts().add(product2);
|
||||
|
||||
// Act and Assert
|
||||
InvalidOfferException e = assertThrows(InvalidOfferException.class, () -> {
|
||||
OfferIdentifier.getHighestMarginProducts(productCatalogue);
|
||||
});
|
||||
|
||||
assertEquals("Price margin is zero!", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,105 @@
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
public class OfferProvisionerTest {
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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.AmazonPurchaser;
|
||||
import de.rwu.easydrop.api.client.AmazonSeller;
|
||||
import de.rwu.easydrop.api.client.EbayPurchaser;
|
||||
import de.rwu.easydrop.api.client.EbaySeller;
|
||||
import de.rwu.easydrop.api.client.PurchaserFactory;
|
||||
import de.rwu.easydrop.api.client.SellerFactory;
|
||||
import de.rwu.easydrop.data.connector.OfferPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.Webshop;
|
||||
import de.rwu.easydrop.service.writer.OfferWriter;
|
||||
|
||||
class OfferProvisionerTest {
|
||||
|
||||
@Mock
|
||||
private OfferPersistenceInterface mockPersistence;
|
||||
|
||||
@Mock
|
||||
private SellerFactory mockSellerFactory;
|
||||
|
||||
@Mock
|
||||
private PurchaserFactory mockPurchaserFactory;
|
||||
|
||||
@Mock
|
||||
private OfferWriter mockOfferWriter;
|
||||
|
||||
@Mock
|
||||
private AmazonSeller mockAmazonSeller;
|
||||
|
||||
@Mock
|
||||
private EbaySeller mockEbaySeller;
|
||||
|
||||
@Mock
|
||||
private AmazonPurchaser mockAmazonPurchaser;
|
||||
|
||||
@Mock
|
||||
private EbayPurchaser mockEbayPurchaser;
|
||||
|
||||
private OfferProvisioner offerProvisioner;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
|
||||
when(mockSellerFactory.createAmazonSeller()).thenReturn(mockAmazonSeller);
|
||||
when(mockSellerFactory.createEbaySeller()).thenReturn(mockEbaySeller);
|
||||
when(mockPurchaserFactory.createAmazonPurchaser()).thenReturn(mockAmazonPurchaser);
|
||||
when(mockPurchaserFactory.createEbayPurchaser()).thenReturn(mockEbayPurchaser);
|
||||
|
||||
offerProvisioner = new OfferProvisioner(mockPersistence, mockSellerFactory, mockPurchaserFactory);
|
||||
offerProvisioner.setOfferWriter(mockOfferWriter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void runProvisioner_WithValidOffers_PlacesOrders() {
|
||||
// Arrange
|
||||
List<Offer> offers = new ArrayList<>();
|
||||
Offer offer1 = createOffer(Webshop.AMAZON, "ASIN123", Webshop.EBAY, "12345");
|
||||
Offer offer2 = createOffer(Webshop.EBAY, "54321", Webshop.AMAZON, "ASIN456");
|
||||
offers.add(offer1);
|
||||
offers.add(offer2);
|
||||
|
||||
// Act
|
||||
offerProvisioner.runProvisioner(offers);
|
||||
|
||||
// Assert
|
||||
verify(mockAmazonSeller, times(1)).sellProduct(any());
|
||||
verify(mockEbaySeller, times(1)).sellProduct(any());
|
||||
verify(mockAmazonPurchaser, times(1)).purchaseProduct(any());
|
||||
verify(mockEbayPurchaser, times(1)).purchaseProduct(any());
|
||||
verify(mockOfferWriter, times(2)).writeOfferToPersistence(any());
|
||||
}
|
||||
|
||||
private Offer createOffer(Webshop sourceWebshop, String sourceProductId, Webshop targetWebshop,
|
||||
String targetProductId) {
|
||||
Product sourceProduct = new Product();
|
||||
sourceProduct.setDataOrigin(sourceWebshop);
|
||||
sourceProduct.setProductId(sourceProductId);
|
||||
|
||||
Product targetProduct = new Product();
|
||||
targetProduct.setDataOrigin(targetWebshop);
|
||||
targetProduct.setProductId(targetProductId);
|
||||
|
||||
Offer offer = new Offer();
|
||||
offer.setSourceProduct(sourceProduct);
|
||||
offer.setTargetProduct(targetProduct);
|
||||
|
||||
return offer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import de.rwu.easydrop.api.dto.TransactionDTO;
|
||||
import de.rwu.easydrop.data.connector.TransactionPersistenceInterface;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.Transaction;
|
||||
import de.rwu.easydrop.service.mapping.TransactionMapper;
|
||||
|
||||
class TransactionHandlerTest {
|
||||
|
||||
@Mock
|
||||
private TransactionPersistenceInterface mockPersistence;
|
||||
|
||||
public TransactionHandlerTest() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void turnOfferToTransaction_ValidOffer_TransactionCreatedAndWrittenToPersistence() {
|
||||
// Arrange
|
||||
Offer offer = new Offer();
|
||||
Product product = new Product();
|
||||
offer.setSourceProduct(product);
|
||||
offer.setTargetProduct(product);
|
||||
|
||||
TransactionHandler handler = new TransactionHandler(mockPersistence);
|
||||
|
||||
// Act
|
||||
handler.turnOfferToTransaction(offer);
|
||||
|
||||
// Assert
|
||||
ArgumentCaptor<TransactionDTO> transactionDtoCaptor = ArgumentCaptor.forClass(TransactionDTO.class);
|
||||
verify(mockPersistence, times(1)).writeTX(transactionDtoCaptor.capture());
|
||||
|
||||
TransactionDTO capturedTransactionDto = transactionDtoCaptor.getValue();
|
||||
Transaction capturedTransaction = TransactionMapper.mapTXFromDTO(capturedTransactionDto);
|
||||
|
||||
assertEquals(offer.getOfferId(), capturedTransaction.getOfferId());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ 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.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -108,4 +110,49 @@ class ProductRetrieverTest {
|
||||
// Verify the interactions
|
||||
verify(persistence, times(1)).getProductDTOById(productId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getProductFromWebshop_ValidAmazonWebshop_ReturnsProduct() {
|
||||
// Arrange
|
||||
String productIdentifier = "ASIN1";
|
||||
ProductDTO dto = new ProductDTO(productIdentifier, Webshop.AMAZON);
|
||||
dto.setCurrentPrice(1.23);
|
||||
AmazonProductDataSource dataSource = mock(AmazonProductDataSource.class);
|
||||
Product expectedProduct = new Product();
|
||||
expectedProduct.setProductId(productIdentifier);
|
||||
expectedProduct.setDataOrigin(Webshop.AMAZON);
|
||||
expectedProduct.setCurrentPrice(1.23);
|
||||
when(dataSource.getProductDTOById(productIdentifier)).thenReturn(dto);
|
||||
when(dataSourceFactory.createAmazonProductDataSource()).thenReturn(dataSource);
|
||||
|
||||
// Act
|
||||
Product actualProduct = productRetriever.getProductFromWebshop(Webshop.AMAZON, productIdentifier);
|
||||
|
||||
// Assert
|
||||
assertEquals(expectedProduct, actualProduct);
|
||||
verify(dataSourceFactory).createAmazonProductDataSource();
|
||||
verify(dataSource).getProductDTOById(productIdentifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getProductFromWebshop_ValidEbayWebshop_ReturnsProduct() {
|
||||
// Arrange
|
||||
String productIdentifier = "ASIN1";
|
||||
ProductDTO dto = new ProductDTO(productIdentifier, Webshop.EBAY);
|
||||
dto.setCurrentPrice(1.23);
|
||||
Product expectedProduct = new Product();
|
||||
expectedProduct.setProductId(productIdentifier);
|
||||
expectedProduct.setDataOrigin(Webshop.EBAY);
|
||||
expectedProduct.setCurrentPrice(1.23);
|
||||
when(ebayDataSource.getProductDTOById(productIdentifier)).thenReturn(dto);
|
||||
when(dataSourceFactory.createEbayItemDataSource()).thenReturn(ebayDataSource);
|
||||
|
||||
// Act
|
||||
Product actualProduct = productRetriever.getProductFromWebshop(Webshop.EBAY, productIdentifier);
|
||||
|
||||
// Assert
|
||||
assertEquals(expectedProduct, actualProduct);
|
||||
verify(dataSourceFactory).createEbayItemDataSource();
|
||||
verify(ebayDataSource).getProductDTOById(productIdentifier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package de.rwu.easydrop.service.validation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -12,8 +17,20 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
import de.rwu.easydrop.exception.InvalidOfferException;
|
||||
import de.rwu.easydrop.model.Offer;
|
||||
|
||||
|
||||
class OfferValidatorTest {
|
||||
@Test
|
||||
void testConstructorIsPrivate()
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
// Check for private constructor
|
||||
Constructor<OfferValidator> constructor = OfferValidator.class.getDeclaredConstructor();
|
||||
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
|
||||
|
||||
// Make sure exception is thrown when instantiating
|
||||
constructor.setAccessible(true);
|
||||
assertThrows(InvocationTargetException.class, () -> {
|
||||
constructor.newInstance();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_Offer_ValidProduct_NoExceptionThrown() {
|
||||
@@ -26,6 +43,18 @@ class OfferValidatorTest {
|
||||
assertDoesNotThrow(() -> OfferValidator.validate(offer));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_Offer_NullPointer() {
|
||||
// Arrange
|
||||
Offer offer = new Offer();
|
||||
|
||||
InvalidOfferException e = assertThrows(InvalidOfferException.class, () -> {
|
||||
OfferValidator.validate(offer);
|
||||
});
|
||||
|
||||
assertEquals("Required information is missing in the offer", e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidOfferProvider")
|
||||
void validate_InvalidOffer_ThrowsInvalidOfferException(Offer offer) {
|
||||
@@ -36,8 +65,8 @@ class OfferValidatorTest {
|
||||
|
||||
static Stream<Offer> invalidOfferProvider() {
|
||||
return Stream.of(
|
||||
createOfferWithEmptylastUpdate(),
|
||||
createOfferWithEmptyId());
|
||||
createOfferWithEmptylastUpdate(),
|
||||
createOfferWithEmptyId());
|
||||
}
|
||||
|
||||
private static Offer createOfferWithEmptylastUpdate() {
|
||||
@@ -53,5 +82,5 @@ class OfferValidatorTest {
|
||||
offer.setLastUpdate("8798476");
|
||||
return offer;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package de.rwu.easydrop.service.validation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidTransactionException;
|
||||
import de.rwu.easydrop.model.Transaction;
|
||||
|
||||
class TransactionValidatorTest {
|
||||
@Test
|
||||
void testConstructorIsPrivate()
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
// Check for private constructor
|
||||
Constructor<TransactionValidator> constructor = TransactionValidator.class
|
||||
.getDeclaredConstructor();
|
||||
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
|
||||
|
||||
// Make sure exception is thrown when instantiating
|
||||
constructor.setAccessible(true);
|
||||
assertThrows(InvocationTargetException.class, () -> {
|
||||
constructor.newInstance();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_ValidTransaction_NoExceptionThrown() {
|
||||
// Arrange
|
||||
Transaction transaction = new Transaction();
|
||||
transaction.setOfferId("123");
|
||||
transaction.setVolume(123f);
|
||||
transaction.setEarnings(12.23);
|
||||
transaction.setTransactionTime("2020-01-01 00:00:00");
|
||||
|
||||
// Act & Assert
|
||||
assertDoesNotThrow(() -> TransactionValidator.validate(transaction));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_InvalidTransaction_InvalidTransactionExceptionThrown() {
|
||||
// Arrange
|
||||
Transaction tx1 = new Transaction();
|
||||
tx1.setOfferId(""); // Set an empty Offer ID to make it invalid
|
||||
Transaction tx2 = new Transaction();
|
||||
tx2.setOfferId("123");
|
||||
Transaction tx3 = new Transaction();
|
||||
tx3.setOfferId("123");
|
||||
tx3.setEarnings(123f);
|
||||
|
||||
// Act & Assert
|
||||
assertThrows(InvalidTransactionException.class, () -> TransactionValidator.validate(tx1));
|
||||
assertThrows(InvalidTransactionException.class, () -> TransactionValidator.validate(tx2));
|
||||
assertThrows(InvalidTransactionException.class, () -> TransactionValidator.validate(tx3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_NullTransaction_InvalidTransactionExceptionThrown() {
|
||||
// Arrange
|
||||
Transaction transaction = null;
|
||||
|
||||
// Act & Assert
|
||||
assertThrows(InvalidTransactionException.class, () -> TransactionValidator.validate(transaction));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package de.rwu.easydrop.service.writer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
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.dto.TransactionDTO;
|
||||
import de.rwu.easydrop.data.connector.TransactionPersistenceInterface;
|
||||
import de.rwu.easydrop.exception.InvalidTransactionException;
|
||||
import de.rwu.easydrop.model.Transaction;
|
||||
import de.rwu.easydrop.service.mapping.TransactionMapper;
|
||||
|
||||
class TransactionWriterTest {
|
||||
|
||||
@Mock
|
||||
private TransactionPersistenceInterface persistence;
|
||||
|
||||
private TransactionWriter writer;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
writer = new TransactionWriter(persistence);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeTXToPersistence_ValidTransaction_TransactionSaved() {
|
||||
// Arrange
|
||||
Transaction transaction = new Transaction();
|
||||
transaction.setOfferId("123");
|
||||
transaction.setVolume(123f);
|
||||
transaction.setEarnings(12.23);
|
||||
transaction.setTransactionTime("2020-01-01 00:00:00");
|
||||
|
||||
TransactionDTO expectedDTO = TransactionMapper.mapTXToDTO(transaction);
|
||||
|
||||
// Act
|
||||
writer.writeTXToPersistence(transaction);
|
||||
|
||||
// Assert
|
||||
verify(persistence).writeTX(expectedDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeTXToPersistence_InvalidTransaction_ValidationExceptionThrown() {
|
||||
// Arrange
|
||||
Transaction transaction = new Transaction();
|
||||
transaction.setVolume(123f);
|
||||
transaction.setEarnings(12.23);
|
||||
transaction.setTransactionTime("2020-01-01 00:00:00");
|
||||
|
||||
// Act & Assert
|
||||
assertThrows(InvalidTransactionException.class, () -> writer.writeTXToPersistence(transaction));
|
||||
|
||||
// Verify that persistence.writeTX is not called
|
||||
verifyNoInteractions(persistence);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.rwu.easydrop.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -55,4 +56,17 @@ class FormattingUtilTest {
|
||||
|
||||
Assertions.assertEquals(expectedFormattedAmount, formattedAmount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveSpaces_RemovesAllSpaces() {
|
||||
String test1 = FormattingUtil.removeSpaces("this is a test");
|
||||
String test2 = FormattingUtil.removeSpaces("another test");
|
||||
String test3 = FormattingUtil.removeSpaces(" daring today, aren't we? ");
|
||||
String test4 = FormattingUtil.removeSpaces("hehe");
|
||||
|
||||
assertEquals("thisisatest", test1);
|
||||
assertEquals("anothertest", test2);
|
||||
assertEquals("daringtoday,aren'twe?", test3);
|
||||
assertEquals("hehe", test4);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user