diff --git a/src/main/java/de/rwu/easydrop/Main.java b/src/main/java/de/rwu/easydrop/Main.java index 5441d96..efc9a60 100644 --- a/src/main/java/de/rwu/easydrop/Main.java +++ b/src/main/java/de/rwu/easydrop/Main.java @@ -1,5 +1,6 @@ package de.rwu.easydrop; +import java.net.MalformedURLException; import java.util.List; import javax.naming.ConfigurationException; @@ -11,6 +12,7 @@ import org.sqlite.SQLiteDataSource; import de.rwu.easydrop.api.client.DataSourceFactory; import de.rwu.easydrop.data.connector.AbstractProductPersistence; import de.rwu.easydrop.data.connector.SQLiteConnector; +import de.rwu.easydrop.model.Product; import de.rwu.easydrop.model.ProductCatalogue; import de.rwu.easydrop.service.retriever.CatalogueRetriever; import de.rwu.easydrop.service.retriever.ProductRetriever; @@ -58,5 +60,15 @@ public final class Main { String pCatStr = pCat.toString(); LOGGER.info(pCatStr); } + + for (ProductCatalogue pCat : pCats) { + List products = pCat.getProducts(); + + for (Product product : products) { + String productId = product.getProductId(); + double price = product.getCurrentPrice(); + LOGGER.info("Product ID: " + productId + ", Price: " + price); + } + } } } diff --git a/src/main/java/de/rwu/easydrop/PriceFetcher.java b/src/main/java/de/rwu/easydrop/PriceFetcher.java new file mode 100644 index 0000000..f25a371 --- /dev/null +++ b/src/main/java/de/rwu/easydrop/PriceFetcher.java @@ -0,0 +1,61 @@ +package de.rwu.easydrop; + +import java.net.MalformedURLException; +import java.util.List; + +import javax.naming.ConfigurationException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sqlite.SQLiteDataSource; + +import de.rwu.easydrop.api.client.DataSourceFactory; +import de.rwu.easydrop.data.connector.AbstractProductPersistence; +import de.rwu.easydrop.data.connector.SQLiteConnector; +import de.rwu.easydrop.model.Product; +import de.rwu.easydrop.model.ProductCatalogue; +import de.rwu.easydrop.service.retriever.CatalogueRetriever; +import de.rwu.easydrop.service.retriever.ProductRetriever; +import de.rwu.easydrop.service.writer.CatalogueWriter; +import de.rwu.easydrop.util.Config; +import de.rwu.easydrop.util.ProductsConfig; + + +/** + * Diese Klasse PriceFetcher enthält die Methode getProductPrice, + * die den Preis eines Produkts von den jeweiligen API's abruft. + */ +public final class PriceFetcher { + + private static final Logger LOGGER = LoggerFactory.getLogger(PriceFetcher.class); + + public static void getProductPrice(final String[] args) throws ConfigurationException{ + + Config config = Config.getInstance(); + ProductsConfig pConfig = ProductsConfig.getInstance(); + DataSourceFactory dataSourceFactory = new DataSourceFactory(config); + ProductRetriever retriever = new ProductRetriever(dataSourceFactory); + CatalogueRetriever catRetriever = new CatalogueRetriever(pConfig, retriever); + AbstractProductPersistence db = new SQLiteConnector(new SQLiteDataSource()); + CatalogueWriter catWriter = new CatalogueWriter(db); + + catRetriever.loadCatalogues(); + List pCats = catRetriever.getProductCatalogues(); + catWriter.writeCatalogues(pCats); + + for (ProductCatalogue pCat : pCats) { + String pCatStr = pCat.toString(); + LOGGER.info(pCatStr); + } + + for (ProductCatalogue pCat : pCats) { + List products = pCat.getProducts(); + + for (Product product : products) { + String productId = product.getProductId(); + double price = product.getCurrentPrice(); + LOGGER.info("Product ID: " + productId + ", Price: " + price); + } + } + } +} diff --git a/src/main/java/de/rwu/easydrop/api/BeforeEach.java b/src/main/java/de/rwu/easydrop/api/BeforeEach.java new file mode 100644 index 0000000..b316851 --- /dev/null +++ b/src/main/java/de/rwu/easydrop/api/BeforeEach.java @@ -0,0 +1,5 @@ +package de.rwu.easydrop.api; + +public @interface BeforeEach { + +} diff --git a/src/main/java/de/rwu/easydrop/api/Preisentnahme.java b/src/main/java/de/rwu/easydrop/api/Preisentnahme.java deleted file mode 100644 index 8041bb5..0000000 --- a/src/main/java/de/rwu/easydrop/api/Preisentnahme.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.rwu.easydrop.api.client; - -import java.net.MalformedURLException; -import java.net.URL; - -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.PathNotFoundException; -import com.jayway.jsonpath.ReadContext; - -import de.rwu.easydrop.api.dto.ProductDTO; - -/** - * Diese Klasse AmazonPriceFetcher enthält die Methode getProductPrice, - * die den Preis eines Produkts von der Amazon API abruft. - * Die Methode verwendet die Base URL, den API-Schlüssel und die Produkt-ID, - * um die API-URL zu erstellen und die Daten von der API abzurufen. - * Anschließend wird der Preis aus der JSON-Antwort extrahiert und zurückgegeben. - */ -public final class AmazonPriceFetcher { - /** - * URL zur Amazon API. - */ - private String baseUrl; - /** - * API-Schlüssel zur Autorisierung. - */ - private String apiKey; - /** - * Produktregion für den Datenzugriff. - */ - private static final String PRODUCT_REGION = "DE"; - /** - * Sprachparameter für den Datenzugriff. - */ - private static final String LOCALE = "de_DE"; - - /** - * Initialisiert die Klasse mit der Base URL und dem API-Schlüssel. - * - * @param newBaseUrl Die Base URL zur Amazon API. - * @param newApiKey Der API-Schlüssel zur Autorisierung. - */ - - public AmazonPriceFetcher(final String newBaseUrl, final String newApiKey) { - this.baseUrl = newBaseUrl; - this.apiKey = newApiKey; - } - - - -} \ No newline at end of file diff --git a/src/main/java/de/rwu/easydrop/api/Test.java b/src/main/java/de/rwu/easydrop/api/Test.java new file mode 100644 index 0000000..ca98b8c --- /dev/null +++ b/src/main/java/de/rwu/easydrop/api/Test.java @@ -0,0 +1,5 @@ +package de.rwu.easydrop.api; + +public @interface Test { + +} diff --git a/src/main/java/de/rwu/easydrop/model/ProductCatalogue.java b/src/main/java/de/rwu/easydrop/model/ProductCatalogue.java index 97f31dc..7f0ad97 100644 --- a/src/main/java/de/rwu/easydrop/model/ProductCatalogue.java +++ b/src/main/java/de/rwu/easydrop/model/ProductCatalogue.java @@ -24,6 +24,7 @@ public class ProductCatalogue { * Product collection. */ private List products; + public float getProducts; /** * Creates new Product Catalogue. diff --git a/src/test/java/de/rwu/easydrop/api/AmazonPriceFetcherTest.java b/src/test/java/de/rwu/easydrop/api/AmazonPriceFetcherTest.java new file mode 100644 index 0000000..4be1200 --- /dev/null +++ b/src/test/java/de/rwu/easydrop/api/AmazonPriceFetcherTest.java @@ -0,0 +1,56 @@ +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); + } +} \ No newline at end of file