Merge remote-tracking branch 'origin/#70-test-cases' into #75-Transactions
This commit is contained in:
@@ -28,4 +28,6 @@ public class OfferDTO {
|
|||||||
* Date of last update of the offer.
|
* Date of last update of the offer.
|
||||||
*/
|
*/
|
||||||
private String lastUpdate;
|
private String lastUpdate;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ public final class OfferValidator {
|
|||||||
if (offer.getOfferId().equals("")) {
|
if (offer.getOfferId().equals("")) {
|
||||||
throw new InvalidOfferException("Offer ID cannot be empty");
|
throw new InvalidOfferException("Offer ID cannot be empty");
|
||||||
}
|
}
|
||||||
|
if (offer.getLastUpdate().equals("")) {
|
||||||
|
throw new InvalidOfferException("LastUpdate cannot be empty");
|
||||||
|
}
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
throw new InvalidOfferException("Required information is missing in the offer", e);
|
throw new InvalidOfferException("Required information is missing in the offer", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package de.rwu.easydrop.model;
|
package de.rwu.easydrop.model;
|
||||||
|
|
||||||
import static org.mockito.Mockito.description;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package de.rwu.easydrop.service.mapping;
|
||||||
|
|
||||||
|
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 org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||||
|
import de.rwu.easydrop.model.Offer;
|
||||||
|
import de.rwu.easydrop.model.Product;
|
||||||
|
import de.rwu.easydrop.model.Webshop;
|
||||||
|
|
||||||
|
class OfferMapperTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructorIsPrivate()
|
||||||
|
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||||
|
// Check for private constructor
|
||||||
|
Constructor<OfferMapper> constructor = OfferMapper.class.getDeclaredConstructor();
|
||||||
|
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
|
||||||
|
|
||||||
|
// Make sure exception is thrown when instantiating
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
assertThrows(InvocationTargetException.class, () -> {
|
||||||
|
constructor.newInstance();
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mapOfferToDTO() {
|
||||||
|
// Arrange
|
||||||
|
Product product = new Product();
|
||||||
|
product.setProductId("12345");
|
||||||
|
product.setDataOrigin(Webshop.AMAZON);
|
||||||
|
product.setAvailable(true);
|
||||||
|
product.setCurrentPrice(9.99);
|
||||||
|
product.setDeliveryPrice(2.50);
|
||||||
|
product.setMerchant("Seller1");
|
||||||
|
|
||||||
|
Offer offer = new Offer();
|
||||||
|
offer.setOfferId("68735");
|
||||||
|
offer.setLastUpdate("2020-07-07");
|
||||||
|
offer.setSourceProduct(product);
|
||||||
|
offer.setTargetProduct(product);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
OfferDTO dto = OfferMapper.mapOfferToDTO(offer);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertEquals("68735", dto.getOfferId());
|
||||||
|
assertEquals("2020-07-07", dto.getLastUpdate());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package de.rwu.easydrop.service.processing;
|
||||||
|
|
||||||
|
public class OfferIdentifierTest {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package de.rwu.easydrop.service.processing;
|
||||||
|
|
||||||
|
public class OfferProvisionerTest {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package de.rwu.easydrop.service.retriever;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
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.OfferDTO;
|
||||||
|
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||||
|
import de.rwu.easydrop.data.connector.OfferPersistenceInterface;
|
||||||
|
import de.rwu.easydrop.model.Offer;
|
||||||
|
import de.rwu.easydrop.util.Config;
|
||||||
|
|
||||||
|
|
||||||
|
class OfferRetrieverTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Config config;
|
||||||
|
@Mock
|
||||||
|
private DataSourceFactory dataSourceFactory;
|
||||||
|
@Mock
|
||||||
|
private AmazonProductDataSource amazonDataSource;
|
||||||
|
@Mock
|
||||||
|
private EbayItemDataSource ebayDataSource;
|
||||||
|
@Mock
|
||||||
|
private OfferDTO offerDTO;
|
||||||
|
@Mock
|
||||||
|
private Offer offer;
|
||||||
|
@Mock
|
||||||
|
private OfferPersistenceInterface persistence;
|
||||||
|
|
||||||
|
private OfferRetriever offerRetriever;
|
||||||
|
|
||||||
|
@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);
|
||||||
|
offerRetriever = new OfferRetriever(persistence);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getOfferFromPersistence(){
|
||||||
|
|
||||||
|
//Mock ProductDTO object to return from the offerDTO mock
|
||||||
|
ProductDTO productDTO = mock(ProductDTO.class);
|
||||||
|
when(productDTO.isAvailable()).thenReturn(true);
|
||||||
|
|
||||||
|
//Arrange
|
||||||
|
String offerId = "187";
|
||||||
|
String lastUpdate = "2023-01-01";
|
||||||
|
when(persistence.getOfferDTOById(offerId)).thenReturn(offerDTO);
|
||||||
|
when(offerDTO.getOfferId()).thenReturn(offerId);
|
||||||
|
when(offerDTO.getLastUpdate()).thenReturn(lastUpdate);
|
||||||
|
when(offerDTO.getSourceProduct()).thenReturn(productDTO);
|
||||||
|
when(offerDTO.getTargetProduct()).thenReturn(productDTO);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
Offer result = offerRetriever.getOfferFromPersistence(offerId);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
assertEquals(offerId, result.getOfferId());
|
||||||
|
assertEquals(lastUpdate, result.getLastUpdate());
|
||||||
|
|
||||||
|
//Verify
|
||||||
|
verify(persistence, times(1)).getOfferDTOById(offerId);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package de.rwu.easydrop.service.validation;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.exception.InvalidOfferException;
|
||||||
|
import de.rwu.easydrop.model.Offer;
|
||||||
|
|
||||||
|
|
||||||
|
class OfferValidatorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validate_Offer_ValidProduct_NoExceptionThrown() {
|
||||||
|
// Arrange
|
||||||
|
Offer offer = new Offer();
|
||||||
|
offer.setOfferId("3672");
|
||||||
|
offer.setLastUpdate("2021-02-03");
|
||||||
|
|
||||||
|
// Act and Assert
|
||||||
|
assertDoesNotThrow(() -> OfferValidator.validate(offer));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("invalidOfferProvider")
|
||||||
|
void validate_InvalidOffer_ThrowsInvalidOfferException(Offer offer) {
|
||||||
|
// Act and Assert
|
||||||
|
assertThrows(InvalidOfferException.class, () -> OfferValidator.validate(offer));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static Stream<Offer> invalidOfferProvider() {
|
||||||
|
return Stream.of(
|
||||||
|
createOfferWithEmptylastUpdate(),
|
||||||
|
createOfferWithEmptyId());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Offer createOfferWithEmptylastUpdate() {
|
||||||
|
Offer offer = new Offer();
|
||||||
|
offer.setOfferId("3729798");
|
||||||
|
offer.setLastUpdate("");
|
||||||
|
return offer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Offer createOfferWithEmptyId() {
|
||||||
|
Offer offer = new Offer();
|
||||||
|
offer.setOfferId("");
|
||||||
|
offer.setLastUpdate("8798476");
|
||||||
|
return offer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package de.rwu.easydrop.service.writer;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.api.dto.OfferDTO;
|
||||||
|
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;
|
||||||
|
|
||||||
|
class OfferWriterTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private OfferDTO offerDTO;
|
||||||
|
@Mock
|
||||||
|
private OfferPersistenceInterface persistence;
|
||||||
|
|
||||||
|
private OfferWriter offerWriter;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
MockitoAnnotations.openMocks(this);
|
||||||
|
offerWriter = new OfferWriter(persistence);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void writeOfferToPersistence_InvalidProduct_ThrowsException() {
|
||||||
|
//Arrange
|
||||||
|
Offer offer = new Offer();
|
||||||
|
offer.setOfferId("");
|
||||||
|
offer.setLastUpdate("");
|
||||||
|
|
||||||
|
//Act and Assert
|
||||||
|
assertThrows(Exception.class, () -> offerWriter.writeOfferToPersistence(offer));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void writeOfferToPresistence_ValidProduct_CallsSaveProduct(){
|
||||||
|
|
||||||
|
// Arrange
|
||||||
|
Product product = new Product();
|
||||||
|
product.setProductId("12345");
|
||||||
|
product.setDataOrigin(Webshop.AMAZON);
|
||||||
|
product.setCurrentPrice(9.99);
|
||||||
|
|
||||||
|
// Arrange
|
||||||
|
Offer offer = new Offer();
|
||||||
|
offer.setOfferId("26876");
|
||||||
|
offer.setLastUpdate("2022-12-25");
|
||||||
|
offer.setSourceProduct(product);
|
||||||
|
offer.setTargetProduct(product);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
offerWriter.writeOfferToPersistence(offer);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Mockito.verify(persistence).writeOffer(any(OfferDTO.class));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user