#11 Updated and moved tests
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package de.rwu.easydrop.api;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AmazonPriceFetcherTest {
|
||||
private static final String BASE_URL = "https://api.amazon.com";
|
||||
private static final String API_KEY = "your_api_key";
|
||||
private static final String PRODUCT_ID = "12345";
|
||||
|
||||
private AmazonPriceFetcher priceFetcher;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
priceFetcher = new AmazonPriceFetcher(BASE_URL, API_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProductPrice_ValidProductId_ReturnsPrice() throws MalformedURLException {
|
||||
// Arrange
|
||||
double expectedPrice = -1.0;
|
||||
|
||||
// Act
|
||||
double actualPrice = priceFetcher.getProductPrice(PRODUCT_ID);
|
||||
|
||||
// Assert
|
||||
Assertions.assertEquals(expectedPrice, actualPrice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProductPrice_InvalidProductId_ReturnsNegativePrice() throws MalformedURLException {
|
||||
// Arrange
|
||||
double expectedPrice = -1.0;
|
||||
|
||||
// Act
|
||||
double actualPrice = priceFetcher.getProductPrice("invalid_product_id");
|
||||
|
||||
// Assert
|
||||
Assertions.assertEquals(expectedPrice, actualPrice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProductPrice_ExceptionThrown_ReturnsNegativePrice() throws MalformedURLException {
|
||||
// Arrange
|
||||
double expectedPrice = -1.0;
|
||||
|
||||
// Act
|
||||
double actualPrice = priceFetcher.getProductPrice("exception_thrown");
|
||||
|
||||
// Assert
|
||||
Assertions.assertEquals(expectedPrice, actualPrice);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
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;
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.model.ProductPair;
|
||||
|
||||
class OrderManagerTest {
|
||||
|
||||
@Test
|
||||
void testCreateOrders_ValidCatalogues_OrdersCreated() {
|
||||
// Arrange
|
||||
List<ProductCatalogue> catalogues = createSampleCatalogues();
|
||||
|
||||
// Act
|
||||
OrderManager.createOrders(catalogues);
|
||||
|
||||
// No assertions, just checking if the orders are created successfully
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetHighestMarginProducts_ValidCatalogue_CheapestAndMostExpensiveProductsReturned() {
|
||||
// Arrange
|
||||
ProductCatalogue catalogue = createSampleCatalogue();
|
||||
|
||||
// Act
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetHighestMarginProducts_InvalidCatalogue_ThrowsInvalidCatalogueException() {
|
||||
// Arrange
|
||||
ProductCatalogue catalogue = new ProductCatalogue("Catalogue1", "Sample catalogue");
|
||||
catalogue.addProduct(new Product());
|
||||
// The catalogue has only one product, which is invalid
|
||||
|
||||
// Act & Assert
|
||||
Assertions.assertThrows(InvalidCatalogueException.class,
|
||||
() -> OrderManager.getHighestMarginProducts(catalogue));
|
||||
}
|
||||
|
||||
private List<ProductCatalogue> createSampleCatalogues() {
|
||||
List<ProductCatalogue> catalogues = new ArrayList<>();
|
||||
|
||||
ProductCatalogue catalogue1 = createSampleCatalogue();
|
||||
catalogues.add(catalogue1);
|
||||
|
||||
ProductCatalogue catalogue2 = new ProductCatalogue("Catalogue2", "Sample catalogue 2");
|
||||
Product product4 = new Product();
|
||||
product4.setProductId("Product4");
|
||||
product4.setCurrentPrice(6.78);
|
||||
catalogue2.addProduct(product4);
|
||||
Product product5 = new Product();
|
||||
product5.setProductId("Product5");
|
||||
product5.setCurrentPrice(7.89);
|
||||
catalogue2.addProduct(product5);
|
||||
catalogues.add(catalogue2);
|
||||
|
||||
return catalogues;
|
||||
}
|
||||
|
||||
private ProductCatalogue createSampleCatalogue() {
|
||||
ProductCatalogue catalogue = new ProductCatalogue("Catalogue1", "Sample catalogue");
|
||||
Product product1 = new Product();
|
||||
product1.setProductId("Product1");
|
||||
product1.setCurrentPrice(1.23);
|
||||
catalogue.addProduct(product1);
|
||||
Product product2 = new Product();
|
||||
product2.setProductId("Product2");
|
||||
product2.setCurrentPrice(2.34);
|
||||
catalogue.addProduct(product2);
|
||||
Product product3 = new Product();
|
||||
product3.setProductId("Product3");
|
||||
product3.setCurrentPrice(4.56);
|
||||
catalogue.addProduct(product3);
|
||||
return catalogue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user