Added Product mapping and validation
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
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.ProductDTO;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
|
||||
class ProductMapperTest {
|
||||
@Test
|
||||
void testConstructorIsPrivate()
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
// Check for private constructor
|
||||
Constructor<ProductMapper> constructor = ProductMapper.class.getDeclaredConstructor();
|
||||
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
|
||||
|
||||
// Make sure exception is thrown when instantiating
|
||||
constructor.setAccessible(true);
|
||||
assertThrows(InvocationTargetException.class, () -> {
|
||||
constructor.newInstance();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapProductFromDTO_ReturnsProductWithMappedFields() {
|
||||
// Arrange
|
||||
ProductDTO dto = createProductDTO();
|
||||
|
||||
// Act
|
||||
Product product = ProductMapper.mapProductFromDTO(dto);
|
||||
|
||||
// Assert
|
||||
assertEquals(dto.isAvailable(), product.isAvailable());
|
||||
assertEquals(dto.getCurrentPrice(), product.getCurrentPrice());
|
||||
assertEquals(dto.getDataOrigin(), product.getDataOrigin());
|
||||
assertEquals(dto.getDeliveryPrice(), product.getDeliveryPrice());
|
||||
assertEquals(dto.getMerchant(), product.getMerchant());
|
||||
assertEquals(dto.getProductId(), product.getProductId());
|
||||
}
|
||||
|
||||
private ProductDTO createProductDTO() {
|
||||
ProductDTO dto = new ProductDTO("12345", "Amazon");
|
||||
dto.setAvailable(true);
|
||||
dto.setCurrentPrice(9.99);
|
||||
dto.setDeliveryPrice(2.50);
|
||||
dto.setMerchant("Example Merchant");
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package de.rwu.easydrop.service.validation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
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;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import de.rwu.easydrop.exception.InvalidProductException;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
|
||||
class ProductValidatorTest {
|
||||
|
||||
@Test
|
||||
void testConstructorIsPrivate()
|
||||
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
// Check for private constructor
|
||||
Constructor<ProductValidator> constructor = ProductValidator.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_ValidProduct_NoExceptionThrown() {
|
||||
// Arrange
|
||||
Product product = new Product();
|
||||
product.setCurrentPrice(9.99);
|
||||
product.setDataOrigin("Amazon");
|
||||
product.setProductId("12345");
|
||||
|
||||
// Act and Assert
|
||||
assertDoesNotThrow(() -> ProductValidator.validate(product));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isInValidDataOrigins_ValidDataOrigin_ReturnsTrue() {
|
||||
// Arrange
|
||||
String dataOrigin = "Amazon";
|
||||
|
||||
// Act
|
||||
boolean result = ProductValidator.isInValidDataOrigins(dataOrigin);
|
||||
|
||||
// Assert
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void isInValidDataOrigins_InvalidDataOrigin_ReturnsFalse() {
|
||||
// Arrange
|
||||
String dataOrigin = "UnknownOrigin";
|
||||
|
||||
// Act
|
||||
boolean result = ProductValidator.isInValidDataOrigins(dataOrigin);
|
||||
|
||||
// Assert
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidProductProvider")
|
||||
void validate_InvalidProduct_ThrowsInvalidProductException(Product product) {
|
||||
// Act and Assert
|
||||
assertThrows(InvalidProductException.class, () -> ProductValidator.validate(product));
|
||||
}
|
||||
|
||||
static Stream<Product> invalidProductProvider() {
|
||||
return Stream.of(
|
||||
createProductWithZeroPrice(),
|
||||
createProductWithUnknownDataOrigin(),
|
||||
createProductWithEmptyProductId());
|
||||
}
|
||||
|
||||
private static Product createProductWithZeroPrice() {
|
||||
Product product = new Product();
|
||||
product.setCurrentPrice(0.00);
|
||||
product.setDataOrigin("Amazon");
|
||||
product.setProductId("12345");
|
||||
return product;
|
||||
}
|
||||
|
||||
private static Product createProductWithUnknownDataOrigin() {
|
||||
Product product = new Product();
|
||||
product.setCurrentPrice(9.99);
|
||||
product.setDataOrigin("UnknownOrigin");
|
||||
product.setProductId("12345");
|
||||
return product;
|
||||
}
|
||||
|
||||
private static Product createProductWithEmptyProductId() {
|
||||
Product product = new Product();
|
||||
product.setCurrentPrice(9.99);
|
||||
product.setDataOrigin("Amazon");
|
||||
product.setProductId("");
|
||||
return product;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user