Added more tests

This commit is contained in:
Shan Ruhhammer
2023-06-25 03:28:52 +02:00
parent 5edc523c6e
commit 7bfee9d719
2 changed files with 83 additions and 11 deletions

View File

@@ -0,0 +1,33 @@
package de.rwu.easydrop.exception;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class InvalidCatalogueExceptionTest {
@Test
void testConstructorWithMessage_ExceptionWithMessageCreated() {
// Arrange
String message = "Invalid catalogue";
// Act
InvalidCatalogueException exception = new InvalidCatalogueException(message);
// Assert
Assertions.assertEquals(message, exception.getMessage());
}
@Test
void testConstructorWithMessageAndCause_ExceptionWithMessageAndCauseCreated() {
// Arrange
String message = "Invalid catalogue";
Throwable cause = new RuntimeException("Cause exception");
// Act
InvalidCatalogueException exception = new InvalidCatalogueException(message, cause);
// Assert
Assertions.assertEquals(message, exception.getMessage());
Assertions.assertEquals(cause, exception.getCause());
}
}

View File

@@ -1,9 +1,17 @@
package de.rwu.easydrop.service.processing;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
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 java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import de.rwu.easydrop.exception.InvalidCatalogueException;
@@ -13,15 +21,29 @@ import de.rwu.easydrop.model.ProductPair;
class OrderManagerTest {
@Test
void testConstructorIsPrivate()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// Check for private constructor
Constructor<OrderManager> constructor = OrderManager.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
// Make sure exception is thrown when instantiating
constructor.setAccessible(true);
assertThrows(InvocationTargetException.class, () -> {
constructor.newInstance();
});
}
@Test
void testCreateOrders_ValidCatalogues_OrdersCreated() {
// Arrange
List<ProductCatalogue> catalogues = createSampleCatalogues();
// Act
OrderManager.createOrders(catalogues);
// No assertions, just checking if the orders are created successfully
assertDoesNotThrow(() -> {
OrderManager.createOrders(catalogues);
});
}
@Test
@@ -33,11 +55,11 @@ class OrderManagerTest {
ProductPair pair = OrderManager.getHighestMarginProducts(catalogue);
// Assert
Assertions.assertNotNull(pair);
Assertions.assertNotNull(pair.getProduct1());
Assertions.assertNotNull(pair.getProduct2());
Assertions.assertEquals("Product1", pair.getProduct1().getProductId());
Assertions.assertEquals("Product3", pair.getProduct2().getProductId());
assertNotNull(pair);
assertNotNull(pair.getProduct1());
assertNotNull(pair.getProduct2());
assertEquals("Product2", pair.getProduct1().getProductId());
assertEquals("Product3", pair.getProduct2().getProductId());
}
@Test
@@ -48,8 +70,25 @@ class OrderManagerTest {
// The catalogue has only one product, which is invalid
// Act & Assert
Assertions.assertThrows(InvalidCatalogueException.class,
Exception e = assertThrows(InvalidCatalogueException.class,
() -> OrderManager.getHighestMarginProducts(catalogue));
assertEquals("Product Catalogue holds less than 2 products!", e.getMessage());
}
@Test
void testGetHighestMarginProducts_InvalidCatalogue_NoMargin() {
// Arrange
ProductCatalogue catalogue = new ProductCatalogue("Catalogue1", "Sample catalogue");
catalogue.addProduct(new Product());
catalogue.addProduct(new Product());
// The catalogue has only one product, which is invalid
// Act & Assert
Exception e = assertThrows(InvalidCatalogueException.class,
() -> OrderManager.getHighestMarginProducts(catalogue));
assertEquals("Price margin is zero!", e.getMessage());
}
private List<ProductCatalogue> createSampleCatalogues() {
@@ -80,7 +119,7 @@ class OrderManagerTest {
catalogue.addProduct(product1);
Product product2 = new Product();
product2.setProductId("Product2");
product2.setCurrentPrice(2.34);
product2.setCurrentPrice(0.89);
catalogue.addProduct(product2);
Product product3 = new Product();
product3.setProductId("Product3");