Added utility structures + tests
This commit is contained in:
@@ -6,8 +6,11 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.rwu.easydrop.api.client.DataSourceFactory;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.service.retriever.CatalogueRetriever;
|
||||
import de.rwu.easydrop.service.retriever.ProductRetriever;
|
||||
import de.rwu.easydrop.util.Config;
|
||||
import de.rwu.easydrop.util.ProductsConfig;
|
||||
|
||||
/**
|
||||
* Kickoff point for the service.
|
||||
@@ -34,12 +37,15 @@ public final class Main {
|
||||
*/
|
||||
public static void main(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);
|
||||
|
||||
String amznProduct = retriever.getProductFromAmazon("B096Y2TYKV").toString();
|
||||
LOGGER.info(amznProduct);
|
||||
String ebayProduct = retriever.getProductFromEbay("Gigabyte GeForce RTX 3060").toString();
|
||||
LOGGER.info(ebayProduct);
|
||||
catRetriever.loadCatalogues();
|
||||
for (ProductCatalogue pCat : catRetriever.getProductCatalogues()) {
|
||||
String pCatStr = pCat.toString();
|
||||
LOGGER.info(pCatStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
79
src/main/java/de/rwu/easydrop/model/ProductCatalogue.java
Normal file
79
src/main/java/de/rwu/easydrop/model/ProductCatalogue.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package de.rwu.easydrop.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Holds Product instances for one product from different data sources.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
@Data
|
||||
public class ProductCatalogue {
|
||||
/**
|
||||
* Product name.
|
||||
*/
|
||||
private String productName;
|
||||
/**
|
||||
* Product description.
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* Product collection.
|
||||
*/
|
||||
private List<Product> products;
|
||||
|
||||
/**
|
||||
* Creates new Product Catalogue.
|
||||
*
|
||||
* @param newProductName
|
||||
* @param newDescription
|
||||
*/
|
||||
public ProductCatalogue(final String newProductName, final String newDescription) {
|
||||
this.productName = newProductName;
|
||||
this.description = newDescription;
|
||||
this.products = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a product to the catalogue.
|
||||
*
|
||||
* @param product
|
||||
*/
|
||||
public void addProduct(final Product product) {
|
||||
products.add(product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a product from the catalogue.
|
||||
*
|
||||
* @param product
|
||||
*/
|
||||
public void removeProduct(final Product product) {
|
||||
products.remove(product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all products from the catalogue.
|
||||
*/
|
||||
public void clearProducts() {
|
||||
products = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the catalogue as a string.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Product Catalogue: ").append(productName).append("\n");
|
||||
sb.append("Description: ").append(description).append("\n");
|
||||
sb.append("Products:\n");
|
||||
for (Product product : products) {
|
||||
sb.append(product.toString()).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Packages for supporting business logic.
|
||||
*
|
||||
* TODO implement
|
||||
* @since 0.2.0
|
||||
*/
|
||||
package de.rwu.easydrop.service;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Supports diverse business processes and enforces business rules.
|
||||
*
|
||||
* TODO implement
|
||||
* @since 0.2.0
|
||||
*/
|
||||
package de.rwu.easydrop.service.processing;
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package de.rwu.easydrop.service.retriever;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
import de.rwu.easydrop.util.ProductsConfig;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Retrieves data for all products of multiple catalogues.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
@Data
|
||||
public class CatalogueRetriever {
|
||||
/**
|
||||
* User-configured products.
|
||||
*/
|
||||
private ProductsConfig productsConfig;
|
||||
/**
|
||||
* Product Retriever.
|
||||
*/
|
||||
private ProductRetriever productRetriever;
|
||||
/**
|
||||
* Product catalogue.
|
||||
*/
|
||||
private List<ProductCatalogue> productCatalogues;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param newProductsConfig
|
||||
* @param newProductRetriever
|
||||
*/
|
||||
public CatalogueRetriever(
|
||||
final ProductsConfig newProductsConfig, final ProductRetriever newProductRetriever) {
|
||||
productRetriever = newProductRetriever;
|
||||
productsConfig = newProductsConfig;
|
||||
productCatalogues = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads catalogues as configured by the user.
|
||||
*
|
||||
* @throws ConfigurationException
|
||||
*/
|
||||
public void loadCatalogues() throws ConfigurationException {
|
||||
productsConfig.loadConfig();
|
||||
|
||||
for (ProductCatalogue pCat : productsConfig.getProductCatalogues()) {
|
||||
ProductCatalogue newProductCatalogue = new ProductCatalogue(
|
||||
pCat.getProductName(), pCat.getDescription());
|
||||
|
||||
for (Product product : pCat.getProducts()) {
|
||||
Product newProduct = new Product();
|
||||
newProduct.setDataOrigin(product.getDataOrigin());
|
||||
newProduct.setProductId(product.getProductId());
|
||||
|
||||
if (product.getDataOrigin().equals("Amazon")) {
|
||||
newProduct = productRetriever.getProductFromAmazon(product.getProductId());
|
||||
} else if (product.getDataOrigin().equals("eBay")) {
|
||||
newProduct = productRetriever.getProductFromEbay(product.getProductId());
|
||||
}
|
||||
|
||||
newProductCatalogue.addProduct(newProduct);
|
||||
}
|
||||
|
||||
productCatalogues.add(newProductCatalogue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,6 @@ public final class Config {
|
||||
* Returns current config instance.
|
||||
*
|
||||
* @return Config instance
|
||||
* @throws ConfigurationException
|
||||
*/
|
||||
public static Config getInstance() {
|
||||
if (instance == null) {
|
||||
|
||||
142
src/main/java/de/rwu/easydrop/util/ProductsConfig.java
Normal file
142
src/main/java/de/rwu/easydrop/util/ProductsConfig.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package de.rwu.easydrop.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.JsonPathException;
|
||||
|
||||
import de.rwu.easydrop.model.Product;
|
||||
import de.rwu.easydrop.model.ProductCatalogue;
|
||||
|
||||
/**
|
||||
* Reads the user-specified catalogue of products.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public final class ProductsConfig {
|
||||
/**
|
||||
* Products config location.
|
||||
*/
|
||||
private String configLocation = "config/products-config.json";
|
||||
|
||||
/**
|
||||
* @return the configLocation
|
||||
*/
|
||||
public String getConfigLocation() {
|
||||
return configLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param newConfigLocation the configLocation to set
|
||||
*/
|
||||
public void setConfigLocation(final String newConfigLocation) {
|
||||
this.configLocation = newConfigLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds the configured products.
|
||||
*/
|
||||
private List<ProductCatalogue> productCatalogues;
|
||||
|
||||
/**
|
||||
* @return the product catalogues
|
||||
*/
|
||||
public List<ProductCatalogue> getProductCatalogues() {
|
||||
return productCatalogues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton instance.
|
||||
*/
|
||||
private static ProductsConfig instance = null;
|
||||
|
||||
/**
|
||||
* Private constructor to prevent external instantiation.
|
||||
*/
|
||||
private ProductsConfig() {
|
||||
productCatalogues = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current products config instance.
|
||||
*
|
||||
* @return Products Config instance
|
||||
*/
|
||||
public static ProductsConfig getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ProductsConfig();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads user-specified configuration into productCatalogues attribute.
|
||||
*
|
||||
* @throws ConfigurationException
|
||||
*/
|
||||
public void loadConfig() throws ConfigurationException {
|
||||
try {
|
||||
File jsonFile = new File(configLocation).getAbsoluteFile();
|
||||
ArrayList<HashMap<String, Object>> jsonProducts = JsonPath.read(jsonFile, "$.products");
|
||||
|
||||
setProductCatalogues(jsonProducts);
|
||||
} catch (IOException e) {
|
||||
throw new ConfigurationException("Couldn't load required products config file");
|
||||
} catch (JsonPathException e) {
|
||||
throw new ConfigurationException("Products config is empty or malformed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads catalogues from JSON.
|
||||
*
|
||||
* @param jsonCatalogues
|
||||
*/
|
||||
private void setProductCatalogues(final ArrayList<HashMap<String, Object>> jsonCatalogues) {
|
||||
for (HashMap<String, Object> productCatalogue : jsonCatalogues) {
|
||||
String name = productCatalogue.get("name").toString();
|
||||
String desc = productCatalogue.get("description").toString();
|
||||
String identifiers = productCatalogue.get("identifiers").toString();
|
||||
|
||||
ProductCatalogue pCat = new ProductCatalogue(name, desc);
|
||||
|
||||
addProductsToCatalogue(identifiers, pCat);
|
||||
productCatalogues.add(pCat);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads products from JSON.
|
||||
*
|
||||
* @param jsonIdentifiers
|
||||
* @param pCat
|
||||
*/
|
||||
private void addProductsToCatalogue(final String jsonIdentifiers, final ProductCatalogue pCat) {
|
||||
ArrayList<HashMap<String, Object>> identifiers = JsonPath.read(jsonIdentifiers, "$");
|
||||
|
||||
for (HashMap<String, Object> product : identifiers) {
|
||||
String dataOrigin = product.keySet().iterator().next();
|
||||
String identifier = product.get(dataOrigin).toString();
|
||||
|
||||
Product newProduct = new Product();
|
||||
newProduct.setDataOrigin(dataOrigin);
|
||||
newProduct.setProductId(identifier);
|
||||
|
||||
pCat.addProduct(newProduct);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the contained product catalogues.
|
||||
*/
|
||||
public void reset() {
|
||||
productCatalogues = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* General utility such as formatting helpers.
|
||||
*
|
||||
* TODO implement
|
||||
* @since 0.1.0
|
||||
*/
|
||||
package de.rwu.easydrop.util;
|
||||
|
||||
Reference in New Issue
Block a user