/*
************************************************************************************
* Copyright (C) 2017-2018 Openbravo S.L.U.
* Licensed under the Openbravo Commercial License version 1.0
* You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
* or in the legal folder of this module distribution.
************************************************************************************
*/
package org.openbravo.warehouse.advancedwarehouseoperations.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.provider.OBProvider;
import org.openbravo.base.secureApp.VariablesSecureApp;
import org.openbravo.base.weld.test.WeldBaseTest;
import org.openbravo.client.kernel.RequestContext;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.model.ad.access.User;
import org.openbravo.model.common.order.Order;
import org.openbravo.model.common.order.OrderLine;
import org.openbravo.model.common.plm.AttributeSet;
import org.openbravo.model.common.plm.AttributeSetInstance;
import org.openbravo.model.common.plm.Product;
import org.openbravo.warehouse.advancedwarehouseoperations.OBAWO_User_Verbosity_Log;
import org.openbravo.warehouse.advancedwarehouseoperations.centralbroker.CentralBroker;
import org.openbravo.warehouse.advancedwarehouseoperations.utils.AWOTestUtils;
import org.openbravo.warehouse.advancedwarehouseoperations.utils.OBAWO_Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Test performance of generation of Tasks with large volumes of data
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class AWOzz_HighVolume extends WeldBaseTest {
private static final Logger log = LoggerFactory.getLogger(AWOzz_HighVolume.class);
// AWO Client
private static final String CLIENT_ID = "A64C68776B544B0DB59C16456C43E608";
// AWO US Organization
private static final String ORG_ID = "F477B20FCDEB46D897DC5B361B92D95F";
// Openbravo User
private static final String USER_ID = "100";
// AWO Admin Role
private static final String ROLE_ID = "4501192AEBEA43FB9782062D043BF4AC";
private static final String LANGUAGE_CODE = "en_US";
private static final String PRODUCT_WITH_AUM = "888251332E9F42D78956E0A60A02D01A";
private static final String ORDER_WITH_LINE_WITH_AUM = "9A5484FA22E449F9A6CBB9A9EAF3F96B";
private static final String DUMMY_DESCRIPTION = "1234";
private static final String LEVEL_DEBUG = "DEBUG";
private static final int NUMBER_OF_PRODUCTS = 500;
private static Order purchaseOrderNoLog;
private static Order purchaseOrderWithLog;
@Before
public void initialize() {
OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORG_ID, LANGUAGE_CODE);
final OBContext obContext = OBContext.getOBContext();
final VariablesSecureApp vars = new VariablesSecureApp(obContext.getUser().getId(),
obContext.getCurrentClient().getId(), obContext.getCurrentOrganization().getId(),
obContext.getRole().getId(), obContext.getLanguage().getLanguage());
RequestContext.get().setVariableSecureApp(vars);
}
/**
* This test creates a Purchase Order with 1000 lines and then tests the creation of Tasks for it
* with and without the Verbosity Log enabled
*
*
* - 1. Create the data
* - 1.1. Create 500 Products with attribute and 500 Products without attribute
* - 1.2. Create two Purchase Orders over the same Products to be used later and process
* them
* - 2. Test generation of Tasks without Verbosity Log in first Order. It is done as a separate
* test so the times are not mixed with the creation of the data
* - 3. Test generation of Tasks with Verbosity Log in second Order. It is done as a separate
* test so the times are not mixed with the creation of the data
*
*/
@Test
public void aWOzz001A_GenerateAndProcessSalesOrders() {
AttributeSet attributeSet = AWOTestUtils.getNewAttributeSet();
List productsWhithoutAttribute = getProductsList(null);
List productsWhithAttribute = getProductsList(attributeSet);
createSalesOrders(productsWhithoutAttribute, productsWhithAttribute, attributeSet);
}
@Test
public void aWOzz001B_GenerateReceptionTasksWithoutLog() {
generateReceiptTasks(purchaseOrderNoLog.getId());
}
@Test
public void aWOzz001C_GenerateReceptionTasksWithLog() {
OBAWO_User_Verbosity_Log verbosityLog = activateVerbosity();
generateReceiptTasks(purchaseOrderWithLog.getId());
deActivateVerbosity(verbosityLog);
}
private List getProductsList(AttributeSet attributeSet) {
OBContext.setAdminMode();
long startTime = System.currentTimeMillis();
List productsList = new ArrayList();
try {
for (int i = 0; i < NUMBER_OF_PRODUCTS; i++) {
Product product = AWOTestUtils.cloneProduct(PRODUCT_WITH_AUM, "aWOzz", null);
if (attributeSet != null) {
product.setAttributeSet(attributeSet);
}
productsList.add(product);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new OBException(e);
} finally {
OBContext.restorePreviousMode();
}
log.info(String.format("Finished creation of %d products. Elapsed time: %d ms",
NUMBER_OF_PRODUCTS, System.currentTimeMillis() - startTime));
return productsList;
}
private void createSalesOrders(List productsWhithoutAttribute,
List productsWhithAttribute, AttributeSet attributeSet) {
OBContext.setAdminMode();
try {
int i = 0;
for (Product product : productsWhithoutAttribute) {
String searchKey = "aWOzz_SO" + String.valueOf(i);
Order purchaseOrder = AWOTestUtils.cloneOrder(ORDER_WITH_LINE_WITH_AUM, searchKey);
OrderLine referenceLine = purchaseOrder.getOrderLineList().get(0);
OrderLine newOrderLine = AWOTestUtils.cloneOrderLine(referenceLine, purchaseOrder);
newOrderLine.setProduct(product);
purchaseOrder.getOrderLineList().remove(referenceLine);
OBDal.getInstance().save(newOrderLine);
OBDal.getInstance().save(purchaseOrder);
processPurchaseOrder(purchaseOrder);
log.info(String.format("Finished creation of %s Sales Order.", searchKey));
i++;
}
AttributeSetInstance attributeSetInstance = getAttributeSetInstance(attributeSet);
for (Product product : productsWhithAttribute) {
String searchKey = "aWOzz_SO" + String.valueOf(i);
Order purchaseOrder = AWOTestUtils.cloneOrder(ORDER_WITH_LINE_WITH_AUM, searchKey);
OrderLine referenceLine = purchaseOrder.getOrderLineList().get(0);
OrderLine newOrderLine = AWOTestUtils.cloneOrderLine(referenceLine, purchaseOrder);
newOrderLine.setProduct(product);
newOrderLine.setAttributeSetValue(attributeSetInstance);
purchaseOrder.getOrderLineList().remove(referenceLine);
OBDal.getInstance().save(newOrderLine);
OBDal.getInstance().save(purchaseOrder);
processPurchaseOrder(purchaseOrder);
log.info(String.format("Finished creation of %s Sales Order.", searchKey));
i++;
}
OBDal.getInstance().flush();
log.info(String.format("--------- Finished creation of %d Sales Order ---------", i));
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new OBException(e);
} finally {
OBContext.restorePreviousMode();
}
}
private AttributeSetInstance getAttributeSetInstance(AttributeSet attributeSet) {
AttributeSetInstance attributeSetInstance = OBProvider.getInstance()
.get(AttributeSetInstance.class);
attributeSetInstance.setDescription(DUMMY_DESCRIPTION);
attributeSetInstance.setAttributeSet(attributeSet);
OBDal.getInstance().save(attributeSetInstance);
return attributeSetInstance;
}
private void processPurchaseOrder(Order purchaseOrder) {
OBContext.setAdminMode();
long startTime = System.currentTimeMillis();
try {
AWOTestUtils.callCOrderPost(purchaseOrder);
OBDal.getInstance().flush();
OBDal.getInstance().refresh(purchaseOrder);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new OBException(e);
} finally {
OBContext.restorePreviousMode();
}
log.info(String.format("Finished processing of Sales Order. Elapsed time: %d ms",
System.currentTimeMillis() - startTime));
}
private OBAWO_User_Verbosity_Log activateVerbosity() {
OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, "0", LANGUAGE_CODE);
OBContext.setAdminMode();
try {
OBAWO_User_Verbosity_Log userVerbosity = OBProvider.getInstance()
.get(OBAWO_User_Verbosity_Log.class);
userVerbosity.setUserContact(OBDal.getInstance().get(User.class, USER_ID));
userVerbosity.setLogLevel(LEVEL_DEBUG);
userVerbosity.setActive(true);
userVerbosity.setLog4java(true);
OBDal.getInstance().save(userVerbosity);
OBDal.getInstance().flush();
return userVerbosity;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new OBException(e);
} finally {
OBContext.restorePreviousMode();
OBContext.setOBContext(USER_ID, ROLE_ID, CLIENT_ID, ORG_ID, LANGUAGE_CODE);
}
}
private void deActivateVerbosity(OBAWO_User_Verbosity_Log userVerbosity) {
if (userVerbosity != null) {
OBDal.getInstance().remove(userVerbosity);
OBDal.getInstance().flush();
}
}
private void generateReceiptTasks(String orderId) {
OBContext.setAdminMode();
try {
long startTime = System.currentTimeMillis();
// Needs to reload the order object
Order order = OBDal.getInstance().get(Order.class, orderId);
CentralBroker.getNewInstance()
.doTheStuff(order, OBAWO_Constants.ACTION_RECEIPT, null, null, null);
log.info(String.format("Finished generating Receipt Tasks. Elapsed time: %d ms",
System.currentTimeMillis() - startTime));
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new OBException(e);
} finally {
OBContext.restorePreviousMode();
}
}
}