Skip to content

Commit

Permalink
Feature ETP-756: Fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanrobledofutit committed Dec 17, 2024
1 parent 529e841 commit b6042fe
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openbravo.test.stockReservation;

import static org.openbravo.base.secureApp.LoginUtils.log4j;
import static org.openbravo.materialmgmt.ReservationUtils.processReserve;
import static org.openbravo.test.stockReservation.StockReservationTestUtils.createInventoryCount;
import static org.openbravo.test.stockReservation.StockReservationTestUtils.stockReservationPreference;
Expand All @@ -10,6 +9,7 @@

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -24,8 +24,6 @@
import org.openbravo.dal.service.OBDal;
import org.openbravo.materialmgmt.InventoryCountProcess;
import org.openbravo.model.ad.domain.Preference;
import org.openbravo.model.common.enterprise.Locator;
import org.openbravo.model.common.enterprise.Warehouse;
import org.openbravo.model.common.order.Order;
import org.openbravo.model.materialmgmt.onhandquantity.Reservation;
import org.openbravo.model.materialmgmt.transaction.InventoryCount;
Expand Down Expand Up @@ -62,25 +60,6 @@ public void setUp() throws Exception {
stockReservationPreference();
}

/**
* Processes a sales order by invoking the order processing utility.
*
* This method executes a specific document action on the provided order
* using the {@link ProcessOrderUtil} class. It ensures that the action is
* processed within the context of the current session.
*
* @param order
* the sales order to be processed
* @param docAction
* the document action to be performed on the order
*/
private void processOrder(Order order, String docAction) {
var processor = weldUtils.getInstance(ProcessOrderUtil.class);

processor.process(order.getId(), docAction, RequestContext.get().getVariablesSecureApp(),
new DalConnectionProvider(false));
}

/**
* Tests the automatic reservation functionality for a single warehouse.
*
Expand All @@ -91,19 +70,19 @@ private void processOrder(Order order, String docAction) {
*
* Cleanup operations reprocess inventory levels and remove test data to reset the system state.
*
* @throws Exception if any error occurs during the test execution.
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void AutomaticReservationOneWarehouse() {
Reservation reservation = null;
Order salesOrder = null;

Locator storageBinRn = OBDal.getInstance().get(Locator.class, StockReservationTestUtils.LOCATOR_RN_ID);
Warehouse warehouseRnId = OBDal.getInstance().get(Warehouse.class, StockReservationTestUtils.WAREHOUSE_RN_ID);
InventoryCount warehouseRn = null;

try {
InventoryCount warehouseRn = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils._1000, storageBinRn, warehouseRnId);
warehouseRn = createInventoryCount(StockReservationTestUtils.LOCATOR_RN, StockReservationTestUtils._1000,
StockReservationTestUtils.LOCATOR_RN_ID, StockReservationTestUtils.WAREHOUSE_RN_ID);
new InventoryCountProcess().processInventory(warehouseRn, false, true);

salesOrder = createOrder("OW - 001", StockReservationTestUtils.DRAFT, StockReservationTestUtils.COMPLETED,
Expand All @@ -113,25 +92,12 @@ public void AutomaticReservationOneWarehouse() {
processOrder(salesOrder, StockReservationTestUtils.COMPLETED);
reservation = StockReservationTestUtils.findReservationForOrder(salesOrder);

StockReservationTestUtils.verifyAutomaticReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RN, BigDecimal.valueOf(1000));
StockReservationTestUtils.verifyAutomaticReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RN,
BigDecimal.valueOf(1000));
} catch (Exception e) {
Assert.fail(StockReservationTestUtils.ERROR + e.getMessage());
} finally {
if (reservation != null) {
processReserve(reservation, StockReservationTestUtils.REACTIVATE);
OBDal.getInstance().remove(reservation);
}
if (salesOrder != null) {
processOrder(salesOrder, StockReservationTestUtils.REACTIVATE);
OBDal.getInstance().remove(salesOrder);
}

InventoryCount warehouseRnDefault = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils.STOCK_DEFAULT, storageBinRn, warehouseRnId);
new InventoryCountProcess().processInventory(warehouseRnDefault, false, true);

OBDal.getInstance().flush();
OBDal.getInstance().commitAndClose();
cleanUpData(reservation, salesOrder, null, null);
}
}

Expand All @@ -145,26 +111,24 @@ public void AutomaticReservationOneWarehouse() {
*
* Cleanup operations reprocess inventory levels and remove test data to reset the system state.
*
* @throws Exception if any error occurs during the test execution.
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void AutomaticReservationMoreThanOneWarehouse() {
Reservation reservation = null;
Order salesOrder = null;

Locator storageBinRn = OBDal.getInstance().get(Locator.class, StockReservationTestUtils.LOCATOR_RN_ID);
Warehouse warehouseRnId = OBDal.getInstance().get(Warehouse.class, StockReservationTestUtils.WAREHOUSE_RN_ID);

Locator storageBinRs = OBDal.getInstance().get(Locator.class, StockReservationTestUtils.LOCATOR_RS_ID);
Warehouse warehouseRsId = OBDal.getInstance().get(Warehouse.class, StockReservationTestUtils.WAREHOUSE_RS_ID);
InventoryCount warehouseRn = null;
InventoryCount warehouseRs = null;

try {
InventoryCount warehouseRn = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils._1000, storageBinRn, warehouseRnId);
warehouseRn = createInventoryCount(StockReservationTestUtils.LOCATOR_RN, StockReservationTestUtils._1000,
StockReservationTestUtils.LOCATOR_RN_ID, StockReservationTestUtils.WAREHOUSE_RN_ID);
new InventoryCountProcess().processInventory(warehouseRn, false, true);

InventoryCount warehouseRs = createInventoryCount(StockReservationTestUtils.LOCATOR_RS,
StockReservationTestUtils._1000, storageBinRs, warehouseRsId);
warehouseRs = createInventoryCount(StockReservationTestUtils.LOCATOR_RS, StockReservationTestUtils._1000,
StockReservationTestUtils.LOCATOR_RS_ID, StockReservationTestUtils.WAREHOUSE_RS_ID);
new InventoryCountProcess().processInventory(warehouseRs, false, true);

salesOrder = createOrder("MOW - 001", StockReservationTestUtils.DRAFT, StockReservationTestUtils.COMPLETED,
Expand All @@ -173,30 +137,14 @@ public void AutomaticReservationMoreThanOneWarehouse() {
processOrder(salesOrder, StockReservationTestUtils.COMPLETED);
reservation = StockReservationTestUtils.findReservationForOrder(salesOrder);

StockReservationTestUtils.verifyAutomaticReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RN, BigDecimal.valueOf(1000));
StockReservationTestUtils.verifyAutomaticReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RS, BigDecimal.valueOf(1000));
StockReservationTestUtils.verifyAutomaticReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RN,
BigDecimal.valueOf(1000));
StockReservationTestUtils.verifyAutomaticReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RS,
BigDecimal.valueOf(1000));
} catch (Exception e) {
Assert.fail(StockReservationTestUtils.ERROR + e.getMessage());
} finally {
if (reservation != null) {
processReserve(reservation, StockReservationTestUtils.REACTIVATE);
OBDal.getInstance().remove(reservation);
}
if (salesOrder != null) {
processOrder(salesOrder, StockReservationTestUtils.REACTIVATE);
OBDal.getInstance().remove(salesOrder);
}

InventoryCount warehouseRnDefault = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils.STOCK_DEFAULT, storageBinRn, warehouseRnId);
new InventoryCountProcess().processInventory(warehouseRnDefault, false, true);

InventoryCount warehouseRsDefault = createInventoryCount(StockReservationTestUtils.LOCATOR_RS,
StockReservationTestUtils.ZERO, storageBinRs, warehouseRsId);
new InventoryCountProcess().processInventory(warehouseRsDefault, false, true);

OBDal.getInstance().flush();
OBDal.getInstance().commitAndClose();
cleanUpData(reservation, salesOrder, warehouseRn, warehouseRs);
}
}

Expand All @@ -210,19 +158,19 @@ public void AutomaticReservationMoreThanOneWarehouse() {
*
* Cleanup operations remove the created reservation and sales order, and reset inventory levels.
*
* @throws Exception if any error occurs during the test execution.
* @throws Exception
* if any error occurs during the test execution.
*/
@Test
public void ManualReservationTest() {
Reservation reservation = null;
Order salesOrder = null;

Locator storageBinRn = OBDal.getInstance().get(Locator.class, StockReservationTestUtils.LOCATOR_RN_ID);
Warehouse warehouseRnId = OBDal.getInstance().get(Warehouse.class, StockReservationTestUtils.WAREHOUSE_RN_ID);
InventoryCount warehouseRn = null;

try{
InventoryCount warehouseRn = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils._1000, storageBinRn, warehouseRnId);
try {
warehouseRn = createInventoryCount(StockReservationTestUtils.LOCATOR_RN, StockReservationTestUtils._1000,
StockReservationTestUtils.LOCATOR_RN_ID, StockReservationTestUtils.WAREHOUSE_RN_ID);
new InventoryCountProcess().processInventory(warehouseRn, false, true);

salesOrder = createOrder("MR - 001", StockReservationTestUtils.DRAFT, StockReservationTestUtils.COMPLETED,
Expand All @@ -232,24 +180,12 @@ public void ManualReservationTest() {
processOrder(salesOrder, StockReservationTestUtils.COMPLETED);
reservation = StockReservationTestUtils.findReservationForOrder(salesOrder);

StockReservationTestUtils.verifyManualReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RN, BigDecimal.valueOf(1000));
} catch (Exception e){
StockReservationTestUtils.verifyManualReservationDetails(reservation, StockReservationTestUtils.LOCATOR_RN,
BigDecimal.valueOf(1000));
} catch (Exception e) {
Assert.fail(StockReservationTestUtils.ERROR + e.getMessage());
} finally {
if (reservation != null) {
OBDal.getInstance().remove(reservation);
}
if (salesOrder != null) {
processOrder(salesOrder, StockReservationTestUtils.REACTIVATE);
OBDal.getInstance().remove(salesOrder);
}

InventoryCount warehouseRnDefault = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils.STOCK_DEFAULT, storageBinRn, warehouseRnId);
new InventoryCountProcess().processInventory(warehouseRnDefault, false, true);

OBDal.getInstance().flush();
OBDal.getInstance().commitAndClose();
cleanUpData(reservation, salesOrder, null, null);
}
}

Expand All @@ -266,4 +202,68 @@ public void cleanUp() {
OBDal.getInstance().commitAndClose();
OBDal.getInstance().rollbackAndClose();
}

/**
* Processes a sales order by invoking the order processing utility.
* This method executes a specific document action on the provided order
* using the {@link ProcessOrderUtil} class. It ensures that the action is
* processed within the context of the current session.
*
* @param order
* the sales order to be processed
* @param docAction
* the document action to be performed on the order
*/
private void processOrder(Order order, String docAction) {
var processor = weldUtils.getInstance(ProcessOrderUtil.class);

processor.process(order.getId(), docAction, RequestContext.get().getVariablesSecureApp(),
new DalConnectionProvider(false));
}

/**
* Cleans up data related to reservations, sales orders, and inventory counts by deactivating or removing
* them from the system.
*
* @param reservation
* the Reservation object to clean up; if its status is "COMPLETED", it will be reactivated before removal
* @param salesOrder
* the Order object to clean up; it will be reactivated before removal
* @param inventoryCountRn
* the InventoryCount object for warehouse "RN" to clean up; it will be reset to default stock levels if not null
* @param inventoryCountRs
* the InventoryCount object for warehouse "RS" to clean up; it will be reset to default stock levels if not null
*/
private void cleanUpData(Reservation reservation, Order salesOrder, InventoryCount inventoryCountRn,
InventoryCount inventoryCountRs) {

if (reservation != null) {
if (StringUtils.equals(reservation.getRESStatus(), StockReservationTestUtils.COMPLETED)) {
processReserve(reservation, StockReservationTestUtils.REACTIVATE);
}
OBDal.getInstance().remove(reservation);
}

if (salesOrder != null) {
processOrder(salesOrder, StockReservationTestUtils.REACTIVATE);
OBDal.getInstance().remove(salesOrder);
}

if (inventoryCountRn != null) {
InventoryCount warehouseRnDefault = createInventoryCount(StockReservationTestUtils.LOCATOR_RN,
StockReservationTestUtils.STOCK_DEFAULT, StockReservationTestUtils.LOCATOR_RN_ID,
StockReservationTestUtils.WAREHOUSE_RN_ID);
new InventoryCountProcess().processInventory(warehouseRnDefault, false, true);
}

if (inventoryCountRs != null) {
InventoryCount warehouseRsDefault = createInventoryCount(StockReservationTestUtils.LOCATOR_RS,
StockReservationTestUtils.ZERO, StockReservationTestUtils.LOCATOR_RS_ID,
StockReservationTestUtils.WAREHOUSE_RS_ID);
new InventoryCountProcess().processInventory(warehouseRsDefault, false, true);
}

OBDal.getInstance().flush();
OBDal.getInstance().commitAndClose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public class StockReservationTestUtils {
public static final BigDecimal STOCK_DEFAULT = new BigDecimal(141640);
public static final BigDecimal ZERO = new BigDecimal(0);


/**
* Private constructor to prevent instantiation of the utility class.
*/
Expand All @@ -83,17 +82,19 @@ private StockReservationTestUtils() {
* the name of the inventory count
* @param bigDecimal
* the quantity to be counted for the inventory line
* @param storageBin
* @param storageBinId
* the storage bin where the inventory count takes place
* @param warehouse
* @param warehouseId
* the warehouse associated with the inventory count
* @return the created {@link InventoryCount} object with header and lines set
*/
public static InventoryCount createInventoryCount(String name, BigDecimal bigDecimal, Locator storageBin,
Warehouse warehouse) {
public static InventoryCount createInventoryCount(String name, BigDecimal bigDecimal, String storageBinId,
String warehouseId) {
InventoryCount inventoryCount = OBProvider.getInstance().get(InventoryCount.class);
Client client = OBContext.getOBContext().getCurrentClient();
Organization org = OBContext.getOBContext().getCurrentOrganization();
Locator storageBin = OBDal.getInstance().get(Locator.class, storageBinId);
Warehouse warehouse = OBDal.getInstance().get(Warehouse.class, warehouseId);
ProductPrice productPrice = OBDal.getInstance().get(ProductPrice.class, StockReservationTestUtils.PRODUCT_PRICE);

// Set header attributes
Expand Down

0 comments on commit b6042fe

Please sign in to comment.