use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.
the class CatalogTest method testCatalog.
@Test(groups = { "testCatalog" })
@Transactional
public void testCatalog() throws Exception {
Category category = new CategoryImpl();
category.setName("Soaps");
category = catalogService.saveCategory(category);
Category category2 = new CategoryImpl();
category2.setName("Towels");
category2 = catalogService.saveCategory(category2);
Category category3 = new CategoryImpl();
category3.setName("SuperCategory");
category3 = catalogService.saveCategory(category3);
CategoryXref temp = new CategoryXrefImpl();
temp.setCategory(category);
temp.setSubCategory(category3);
category3.getAllParentCategoryXrefs().add(temp);
category3 = catalogService.saveCategory(category3);
// Test category hierarchy
Long cat3Id = category3.getId();
category3 = null;
category3 = catalogService.findCategoryById(cat3Id);
category3.getAllParentCategoryXrefs().clear();
CategoryXref temp2 = new CategoryXrefImpl();
temp2.setCategory(category);
temp2.setSubCategory(category3);
category3.getAllParentCategoryXrefs().add(temp2);
CategoryXref temp3 = new CategoryXrefImpl();
temp3.setCategory(category2);
temp3.setSubCategory(category3);
category3.getAllParentCategoryXrefs().add(temp3);
category3 = catalogService.saveCategory(category3);
assert category3.getAllParentCategoryXrefs().size() == 2;
Product newProduct = new ProductImpl();
Sku newDefaultSku = new SkuImpl();
newDefaultSku = catalogService.saveSku(newDefaultSku);
newProduct.setDefaultSku(newDefaultSku);
newProduct.setName("Lavender Soap");
Calendar activeStartCal = Calendar.getInstance();
activeStartCal.add(Calendar.DAY_OF_YEAR, -2);
newProduct.setActiveStartDate(activeStartCal.getTime());
// newProduct.setAllParentCategories(allParentCategories);
newProduct.setDefaultCategory(category);
newProduct.getAllParentCategoryXrefs().clear();
newProduct = catalogService.saveProduct(newProduct);
CategoryProductXref categoryXref = new CategoryProductXrefImpl();
categoryXref.setProduct(newProduct);
categoryXref.setCategory(category);
newProduct.getAllParentCategoryXrefs().add(categoryXref);
CategoryProductXref categoryXref2 = new CategoryProductXrefImpl();
categoryXref2.setProduct(newProduct);
categoryXref2.setCategory(category2);
newProduct.getAllParentCategoryXrefs().add(categoryXref2);
newProduct = catalogService.saveProduct(newProduct);
Long newProductId = newProduct.getId();
Product testProduct = catalogService.findProductById(newProductId);
assert testProduct.getId().equals(testProduct.getId());
Category testCategory = catalogService.findCategoryByName("Soaps");
assert testCategory.getId().equals(category.getId());
testCategory = catalogService.findCategoryById(category.getId());
assert testCategory.getId().equals(category.getId());
Media media = new MediaImpl();
media.setAltText("test");
media.setTitle("large");
media.setUrl("http://myUrl");
category.getCategoryMediaXref().put("large", new CategoryMediaXrefImpl(category, media, "large"));
catalogService.saveCategory(testCategory);
testCategory = catalogService.findCategoryById(category.getId());
assert (testCategory.getCategoryMediaXref().get("large") != null);
List<Category> categories = catalogService.findAllCategories();
assert categories != null && categories.size() == 3;
List<Product> products = catalogService.findAllProducts();
boolean foundProduct = false;
for (Product product : products) {
if (product.getId().equals(newProductId)) {
foundProduct = true;
}
}
assert foundProduct == true;
products = catalogService.findProductsByName(newProduct.getName());
foundProduct = false;
for (Product product : products) {
if (product.getId().equals(newProductId)) {
foundProduct = true;
}
}
assert foundProduct == true;
Sku newSku = new SkuImpl();
newSku.setName("Under Armor T-Shirt -- Red");
newSku.setRetailPrice(new Money(14.99));
newSku.setActiveStartDate(activeStartCal.getTime());
newSku = catalogService.saveSku(newSku);
List<Sku> allSkus = new ArrayList<>();
allSkus.add(newSku);
newProduct.setAdditionalSkus(allSkus);
newProduct = catalogService.saveProduct(newProduct);
Long skuId = newProduct.getSkus().get(0).getId();
Sku testSku = catalogService.findSkuById(skuId);
assert testSku.getId().equals(skuId);
List<Sku> testSkus = catalogService.findAllSkus();
boolean foundSku = false;
for (Sku sku : testSkus) {
if (sku.getId().equals(skuId)) {
foundSku = true;
}
}
assert foundSku == true;
List<Long> skuIds = new ArrayList<>();
skuIds.add(skuId);
testSkus = catalogService.findSkusByIds(skuIds);
foundSku = false;
for (Sku sku : testSkus) {
if (sku.getId().equals(skuId)) {
foundSku = true;
}
}
assert foundSku == true;
}
use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.
the class OfferTest method createDiscreteOrderItem.
private DiscreteOrderItem createDiscreteOrderItem(Long skuId, Double retailPrice, Double salePrice, boolean isDiscountable, int quantity, Order order) {
DiscreteOrderItem item = new DiscreteOrderItemImpl();
Sku sku = catalogService.findSkuById(skuId);
sku.setRetailPrice(new Money(retailPrice));
if (salePrice != null) {
sku.setSalePrice(new Money(salePrice));
} else {
sku.setSalePrice(null);
}
sku.setDiscountable(isDiscountable);
sku.setName("test");
sku = catalogService.saveSku(sku);
item.setSku(sku);
item.setQuantity(quantity);
Product product = new ProductImpl();
product.setDefaultSku(sku);
product = catalogService.saveProduct(product);
item.setProduct(product);
item.setOrder(order);
item = (DiscreteOrderItem) orderItemService.saveOrderItem(item);
return item;
}
use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.
the class OrderBaseTest method setUpNamedOrder.
public Order setUpNamedOrder() throws AddToCartException {
Customer customer = customerService.saveCustomer(createNamedCustomer());
Order order = orderService.createNamedOrderForCustomer("Boxes Named Order", customer);
Product newProduct = addTestProduct("Cube Box", "Boxes");
Category newCategory = newProduct.getDefaultCategory();
order = orderService.addItem(order.getId(), new OrderItemRequestDTO(newProduct.getId(), newProduct.getDefaultSku().getId(), newCategory.getId(), 2), true);
return order;
}
use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.
the class CommonSetupBaseTest method addTestProduct.
public Product addTestProduct(String productName, String categoryName, boolean active) {
Calendar activeStartCal = Calendar.getInstance();
activeStartCal.add(Calendar.DAY_OF_YEAR, -2);
Calendar activeEndCal = Calendar.getInstance();
activeEndCal.add(Calendar.DAY_OF_YEAR, -1);
Category category = new CategoryImpl();
category.setName(categoryName);
category.setActiveStartDate(activeStartCal.getTime());
category = catalogService.saveCategory(category);
Sku newSku = new SkuImpl();
newSku.setName(productName);
newSku.setRetailPrice(new Money(44.99));
newSku.setActiveStartDate(activeStartCal.getTime());
if (!active) {
newSku.setActiveEndDate(activeEndCal.getTime());
}
newSku.setDiscountable(true);
newSku = catalogService.saveSku(newSku);
Product newProduct = new ProductImpl();
newProduct.setDefaultCategory(category);
newProduct.setDefaultSku(newSku);
newProduct = catalogService.saveProduct(newProduct);
return newProduct;
}
use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.
the class ValidateProductOptionsActivity method execute.
@Override
public ProcessContext<CheckoutSeed> execute(ProcessContext<CheckoutSeed> context) throws Exception {
if (!useSku) {
Order order = context.getSeedData().getOrder();
List<DiscreteOrderItem> orderItems = getOrderItems(order);
for (DiscreteOrderItem discreteOI : orderItems) {
Map<String, OrderItemAttribute> attributeValues = discreteOI.getOrderItemAttributes();
Product product = discreteOI.getProduct();
if (product != null) {
for (ProductOptionXref productOptionXref : ListUtils.emptyIfNull(product.getProductOptionXrefs())) {
ProductOption productOption = productOptionXref.getProductOption();
String attributeName = productOption.getAttributeName();
OrderItemAttribute attribute = attributeValues.get(attributeName);
String attributeValue = (attribute != null) ? attribute.getValue() : null;
boolean isRequired = productOption.getRequired();
boolean hasStrategy = productOptionValidationService.hasProductOptionValidationStrategy(productOption);
boolean isAddOrNoneType = productOptionValidationService.isAddOrNoneType(productOption);
boolean isSubmitType = productOptionValidationService.isSubmitType(productOption);
if (isMissingRequiredAttribute(isRequired, hasStrategy, isAddOrNoneType, isSubmitType, attributeValue)) {
String message = "Unable to validate cart, product (" + product.getId() + ") required" + " attribute was not provided: " + attributeName;
throw new RequiredAttributeNotProvidedException(message, attributeName, String.valueOf(product.getId()));
}
boolean hasValidationType = productOption.getProductOptionValidationType() != null;
if (shouldValidateWithException(hasValidationType, hasStrategy, isAddOrNoneType, isSubmitType)) {
productOptionValidationService.validate(productOption, attributeValue);
}
if (hasStrategy && !(isAddOrNoneType || isSubmitType)) {
// we need to validate however, we will not error out
ActivityMessages messages = (ActivityMessages) context;
productOptionValidationService.validateWithoutException(productOption, attributeValue, messages);
}
}
}
}
}
return context;
}
Aggregations