Related to issue 33650:Added junit test cases for persist org info task
authorAtul Gaware <atul.gaware@openbravo.com>
Thu, 26 Apr 2018 22:49:57 +0530
changeset 33888 8c8a72032173
parent 33887 793f3d0ba3f5
child 33889 7c2d30c7653f
Related to issue 33650:Added junit test cases for persist org info task

** Added junit tests to verify persist info data in organization
for corner case orgs (*, null, org that does not exists)
** Added junit tests which creates a complex org tree, sets org as ready
with cascade Yes and No option and verifies persist org info.
** Added junit tests which create single org, two orgs one under another,
sets org as ready with cascade = Yes, Cascade = No option.
src-test/src/org/openbravo/test/AllAntTaskTests.java
src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoConstants.java
src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoCornerCaseOrg.java
src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoSetReadyTest.java
src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoTest.java
src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoUtility.java
src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistOrgInfoComplexOrgTreeTest.java
--- a/src-test/src/org/openbravo/test/AllAntTaskTests.java	Thu Apr 26 23:45:59 2018 +0530
+++ b/src-test/src/org/openbravo/test/AllAntTaskTests.java	Thu Apr 26 22:49:57 2018 +0530
@@ -69,6 +69,7 @@
 import org.openbravo.test.db.model.functions.SqlCallableStatement;
 import org.openbravo.test.expression.EvaluationTest;
 import org.openbravo.test.expression.OBBindingsTest;
+import org.openbravo.test.generalsetup.enterprise.organization.ADOrgPersistInfoTest;
 import org.openbravo.test.inventoryStatus.InventoryStatusTest;
 import org.openbravo.test.materialMgmt.iscompletelyinvoicedshipment.IsCompletelyInvoicedShipment;
 import org.openbravo.test.model.ClassLoaderTest;
@@ -135,7 +136,7 @@
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
 
-// dal
+    // dal
     DalComplexQueryRequisitionTest.class, //
     DalComplexQueryTestOrderLine.class, //
     DalPerformanceInventoryLineTest.class, //
@@ -294,7 +295,10 @@
     CopyLinesFromOrdersTest.class,
 
     // Referenced Inventory
-    ReferencedInventoryTestSuite.class
+    ReferencedInventoryTestSuite.class,
+
+    // AD_Org Persist Information
+    ADOrgPersistInfoTest.class
 
 })
 public class AllAntTaskTests {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoConstants.java	Thu Apr 26 22:49:57 2018 +0530
@@ -0,0 +1,120 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.test.generalsetup.enterprise.organization;
+
+public class ADOrgPersistInfoConstants {
+
+  /**
+   * Openbravo user
+   */
+  final static String OPENBRAVO_USER_ID = "100";
+
+  /**
+   * F&B International Group Admin Role
+   */
+  final static String FB_GROUP_ADMIN_ROLE_ID = "42D0EEB1C66F497A90DD526DC597E6F0";
+
+  /**
+   * QA Admin Role
+   */
+  final static String QA_TESTING_ADMIN_ROLE_ID = "4028E6C72959682B01295A071429011E";
+
+  /**
+   * Organization Type: 0 - Organization 2- Generic
+   */
+  final static String ORGTYPE_ORGANIZATION = "0";
+  final static String ORGTYPE_GENERIC = "2";
+  final static String ORGTYPE_LEGALWITHACCOUNTING = "1";
+  final static String ORGTYPE_LEGALWITHOUTACCOUNTING = "3";
+
+  /**
+   * System Client *
+   */
+
+  final static String CLIENT_0 = "0";
+  /**
+   * All Organization *
+   */
+  final static String ORG_0 = "0";
+  /**
+   * QA Testing Client
+   */
+  final static String CLIENT_QA = "4028E6C72959682B01295A070852010D";
+
+  /**
+   * QA Main Organization
+   */
+  final static String ORG_QA_MAIN = "43D590B4814049C6B85C6545E8264E37";
+
+  /**
+   * QA Spain Organization
+   */
+  final static String ORG_QA_SPAIN = "357947E87C284935AD1D783CF6F099A1";
+
+  /**
+   * QA USA Organization
+   */
+  final static String ORG_QA_USA = "5EFF95EB540740A3B10510D9814EFAD5";
+
+  /**
+   * FnB International Group Client
+   */
+  final static String CLIENT_FB = "23C59575B9CF467C9620760EB255B389";
+
+  /**
+   * FnB International Group Organization
+   */
+  final static String ORG_FB_FBGROUP = "19404EAD144C49A0AF37D54377CF452D";
+
+  /**
+   * FnB US, Inc.
+   */
+  protected static final String ORG_FB_US = "2E60544D37534C0B89E765FE29BC0B43";
+
+  /**
+   * FnB US East Coast
+   */
+  final static String ORG_FB_EAST = "7BABA5FF80494CAFA54DEBD22EC46F01";
+
+  /**
+   * FnB US West Coast
+   */
+  final static String ORG_FB_WEST = "BAE22373FEBE4CCCA24517E23F0C8A48";
+
+  /**
+   * FnB España, S.A.
+   */
+  final static String ORG_FB_SPAIN = "B843C30461EA4501935CB1D125C9C25A";
+
+  /**
+   * FnB España - Región Norte
+   */
+  final static String ORG_FB_NORTE = "E443A31992CB4635AFCAEABE7183CE85";
+
+  /**
+   * FnB España - Región Sur
+   */
+  final static String ORG_FB_SUR = "DC206C91AA6A4897B44DA897936E0EC3";
+
+  /**
+   * Currency ids for EUR, USD
+   */
+  final static String CUR_EURO = "102";
+  final static String CUR_USD = "100";
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoCornerCaseOrg.java	Thu Apr 26 22:49:57 2018 +0530
@@ -0,0 +1,74 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.test.generalsetup.enterprise.organization;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.openbravo.test.base.OBBaseTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ADOrgPersistInfoCornerCaseOrg extends OBBaseTest {
+
+  private static final Logger log = LoggerFactory.getLogger(ADOrgPersistInfoCornerCaseOrg.class);
+
+  /**
+   * Test corner case organization persist information
+   */
+  @Test
+  public void testCornerCaseOrganization() {
+
+    try {
+
+      // * Organization
+
+      assertEquals("Failed AD_Org_GetCalendarOwner for * Organization",
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getcalendarownertn",
+              ADOrgPersistInfoConstants.ORG_0),
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getcalendarowner",
+              ADOrgPersistInfoConstants.ORG_0));
+      assertEquals("Failed AD_Org_GetPeriodControlAllow for * Organization",
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getperiodcontrolallowtn",
+              ADOrgPersistInfoConstants.ORG_0),
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getperiodcontrolallow",
+              ADOrgPersistInfoConstants.ORG_0));
+
+      // Null Organization
+
+      assertEquals("Failed AD_Org_GetCalendarOwner for null Organization",
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getcalendarownertn", null),
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getcalendarowner", null));
+      assertEquals("Failed AD_Org_GetPeriodControlAllow for null Organization",
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getperiodcontrolallowtn", null),
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getperiodcontrolallow", null));
+
+      // Organizations that does not exists in database
+
+      assertEquals("Failed AD_Org_GetCalendarOwner for organization not in database",
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getcalendarownertn", "XX"),
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getcalendarowner", "XX"));
+      assertEquals("Failed AD_Org_GetPeriodControlAllow for organization not in database",
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getperiodcontrolallowtn", "XX"),
+          ADOrgPersistInfoUtility.getPersistOrgInfo("ad_org_getperiodcontrolallow", "XX"));
+    } catch (Exception e) {
+      log.error("Error in testCornerCaseOrganization", e);
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoSetReadyTest.java	Thu Apr 26 22:49:57 2018 +0530
@@ -0,0 +1,150 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.test.generalsetup.enterprise.organization;
+
+import org.junit.Test;
+import org.openbravo.test.base.OBBaseTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ADOrgPersistInfoSetReadyTest extends OBBaseTest {
+
+  private static final Logger log = LoggerFactory.getLogger(ADOrgPersistInfoSetReadyTest.class);
+
+  /**
+   * Create a new Organization of type Generic under F&B España, S.A Organization and test persist
+   * organization info after the organization is set as ready with Cascade No.
+   */
+  @Test
+  public void testSetReadyOneGenericOrganization() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      String orgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, ADOrgPersistInfoConstants.ORG_FB_SPAIN, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(orgId, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(orgId);
+    } catch (Exception e) {
+      log.error("Error in testSetReadyOrganization", e);
+    }
+  }
+
+  /**
+   * Create a new Organization of type Generic under F&B España, S.A Organization and test persist
+   * organization info after the organization is set as ready with Cascade Yes.
+   */
+  @Test
+  public void testSetReadyOneGenericOrganizationCascade() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      String orgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, ADOrgPersistInfoConstants.ORG_FB_SPAIN, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(orgId, "Y");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(orgId);
+    } catch (Exception e) {
+      log.error("Error in testSetReadyOrganization", e);
+    }
+  }
+
+  /**
+   * Create a new Organization of type Generic under F&B España, S.A Organization and a child under
+   * it and test persist organization info after the organization is set as ready with cascade as
+   * No.
+   */
+  @Test
+  public void testSetReadyTwoGenericOrganization() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      String firstOrgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, ADOrgPersistInfoConstants.ORG_FB_SPAIN, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      String secondOrgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, firstOrgId, false,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(firstOrgId, "N");
+      ADOrgPersistInfoUtility.setAsReady(secondOrgId, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(firstOrgId);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(secondOrgId);
+
+    } catch (Exception e) {
+      log.error("Error in testSetReadyTwoGenericOrganization", e);
+    }
+  }
+
+  /**
+   * Create a new Organization of type Generic under F&B España, S.A Organization and a child under
+   * it and test persist organization info after the organization is set as ready with cascade as
+   * Yes.
+   */
+  @Test
+  public void testSetReadyTwoGenericOrganizationCascade() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      String firstOrgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, ADOrgPersistInfoConstants.ORG_FB_SPAIN, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      String secondOrgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, firstOrgId, false,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(firstOrgId, "Y");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(firstOrgId);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(secondOrgId);
+
+    } catch (Exception e) {
+      log.error("Error in testSetReadyTwoGenericOrganization", e);
+    }
+  }
+
+  /**
+   * Create a new Organization of type Organization under * Organization and test persist
+   * organization information after the organization is set as ready cascade as No.
+   */
+  @Test
+  public void testSetReadyOrganization() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      String orgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_ORGANIZATION, ADOrgPersistInfoConstants.ORG_FB_SPAIN,
+          true, ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(orgId, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(orgId);
+    } catch (Exception e) {
+      log.error("Error in testSetReadyOrganization", e);
+    }
+  }
+
+  /**
+   * Create a new Organization of type Organization under * Organization and test persist
+   * organization information after the organization is set as ready with cascade as Yes.
+   */
+  @Test
+  public void testSetReadyOrganizationCascade() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      String orgId = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_ORGANIZATION, ADOrgPersistInfoConstants.ORG_FB_SPAIN,
+          true, ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(orgId, "Y");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(orgId);
+    } catch (Exception e) {
+      log.error("Error in testSetReadyOrganization", e);
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoTest.java	Thu Apr 26 22:49:57 2018 +0530
@@ -0,0 +1,29 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.test.generalsetup.enterprise.organization;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+@RunWith(Suite.class)
+@Suite.SuiteClasses({ ADOrgPersistInfoSetReadyTest.class,
+    ADOrgPersistOrgInfoComplexOrgTreeTest.class, ADOrgPersistInfoCornerCaseOrg.class })
+
+public class ADOrgPersistInfoTest {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistInfoUtility.java	Thu Apr 26 22:49:57 2018 +0530
@@ -0,0 +1,284 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2018 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.test.generalsetup.enterprise.organization;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.criterion.Restrictions;
+import org.openbravo.base.provider.OBProvider;
+import org.openbravo.base.session.OBPropertiesProvider;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.security.OrganizationStructureProvider;
+import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.businessUtility.InitialOrgSetup;
+import org.openbravo.model.ad.process.ProcessInstance;
+import org.openbravo.model.ad.system.Client;
+import org.openbravo.model.common.currency.Currency;
+import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.model.common.enterprise.OrganizationAcctSchema;
+import org.openbravo.model.common.enterprise.OrganizationType;
+import org.openbravo.model.financialmgmt.accounting.coa.AcctSchema;
+import org.openbravo.model.financialmgmt.calendar.Calendar;
+import org.openbravo.service.db.CallProcess;
+import org.openbravo.service.db.CallStoredProcedure;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ADOrgPersistInfoUtility {
+
+  private static final Logger log = LoggerFactory.getLogger(ADOrgPersistInfoUtility.class);
+
+  /**
+   * Set Context for FnB International Group Client Admin
+   */
+
+  public static void setTestContextFB() {
+    OBContext.setOBContext(ADOrgPersistInfoConstants.OPENBRAVO_USER_ID,
+        ADOrgPersistInfoConstants.FB_GROUP_ADMIN_ROLE_ID, ADOrgPersistInfoConstants.CLIENT_FB,
+        ADOrgPersistInfoConstants.ORG_FB_FBGROUP);
+  }
+
+  /**
+   * Set Context for QA Testing Client Admin
+   */
+
+  public static void setTestContextQA() {
+    OBContext.setOBContext(ADOrgPersistInfoConstants.OPENBRAVO_USER_ID,
+        ADOrgPersistInfoConstants.QA_TESTING_ADMIN_ROLE_ID, ADOrgPersistInfoConstants.CLIENT_QA,
+        ADOrgPersistInfoConstants.ORG_QA_SPAIN);
+  }
+
+  /**
+   * Create organization with type orgType under strParentOrg
+   * 
+   * @param newOrgType
+   * @param strParentOrg
+   * @param summary
+   * @param currencyId
+   * @return
+   */
+  public static String createOrganization(String newOrgType, String strParentOrg, boolean summary,
+      String currencyId) {
+    long number = System.currentTimeMillis();
+    Properties properties = OBPropertiesProvider.getInstance().getOpenbravoProperties();
+    String strSourcePath = properties.getProperty("source.path");
+    InitialOrgSetup initialOrg = new InitialOrgSetup(OBContext.getOBContext().getCurrentClient());
+    initialOrg.createOrganization("Test_" + number, "", newOrgType, strParentOrg, "", "", "", false,
+        null, "", false, false, false, false, false, strSourcePath);
+    Organization org = OBDal.getInstance().get(Organization.class, initialOrg.getOrgId());
+    org.setSummaryLevel(summary);
+    if (StringUtils.equals(newOrgType, ADOrgPersistInfoConstants.ORGTYPE_LEGALWITHACCOUNTING)) {
+      org.setCurrency(OBDal.getInstance().get(Currency.class, currencyId));
+      org.setAllowPeriodControl(true);
+      org.setCalendar(ADOrgPersistInfoUtility.createCalendar(org));
+      org.setGeneralLedger(ADOrgPersistInfoUtility.createAcctSchema(org, currencyId));
+      OBDal.getInstance().commitAndClose();
+      ADOrgPersistInfoUtility.setAcctSchema(org);
+    }
+    return org.getId();
+  }
+
+  /**
+   * Sets organization as ready
+   * 
+   * @param orgId
+   * @param isCascade
+   */
+  public static void setAsReady(final String orgId, final String isCascade) {
+    final Map<String, String> parameters = new HashMap<String, String>(1);
+    parameters.put("Cascade", isCascade);
+    final ProcessInstance pinstance = CallProcess.getInstance().call("AD_Org_Ready", orgId,
+        parameters);
+    if (pinstance.getResult() == 0L) {
+      throw new RuntimeException(pinstance.getErrorMsg());
+    }
+    OBDal.getInstance().commitAndClose();
+  }
+
+  public static Organization getCalendarOrganization(String orgId) {
+    Organization calOrg = null;
+    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider();
+    for (String org : osp.getParentList(orgId, true)) {
+      calOrg = OBDal.getInstance().get(Organization.class, org);
+      if (calOrg.getCalendar() != null) {
+        break;
+      } else {
+        calOrg = null;
+      }
+    }
+    return calOrg;
+  }
+
+  public static Organization getBusinessUnitOrganization(String orgId) {
+    Organization businessUnitOrg = null;
+    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider();
+    for (String org : osp.getParentList(orgId, true)) {
+      businessUnitOrg = OBDal.getInstance().get(Organization.class, org);
+      if (businessUnitOrg.getOrganizationType().isBusinessUnit()) {
+        break;
+      } else {
+        businessUnitOrg = null;
+      }
+    }
+    return businessUnitOrg;
+  }
+
+  public static AcctSchema createAcctSchema(final Organization org, final String currencyId) {
+    AcctSchema acctSchema = null;
+    try {
+      acctSchema = OBProvider.getInstance().get(AcctSchema.class);
+      acctSchema.setOrganization(org);
+      acctSchema.setName(org.getName() + " GL");
+      acctSchema.setClient(org.getClient());
+      acctSchema.setCurrency(OBDal.getInstance().get(Currency.class, currencyId));
+      OBDal.getInstance().save(acctSchema);
+      OBDal.getInstance().flush();
+    } catch (Exception e) {
+      log.error("Error in createAcctSchema", e);
+    }
+    return acctSchema;
+  }
+
+  public static void setAcctSchema(final Organization org) {
+    OrganizationAcctSchema orgAcctSchema = null;
+    try {
+      orgAcctSchema = OBProvider.getInstance().get(OrganizationAcctSchema.class);
+      orgAcctSchema.setOrganization(org);
+      orgAcctSchema.setClient(org.getClient());
+      orgAcctSchema.setAccountingSchema(org.getGeneralLedger());
+      OBDal.getInstance().save(orgAcctSchema);
+      OBDal.getInstance().flush();
+    } catch (Exception e) {
+      log.error("Error in setAcctSchema", e);
+    }
+  }
+
+  public static Calendar createCalendar(final Organization org) {
+    Calendar calendar = null;
+    try {
+      calendar = OBProvider.getInstance().get(Calendar.class);
+      calendar.setName(org.getName() + " Calendar");
+      calendar.setOrganization(org);
+      calendar.setClient(org.getClient());
+      OBDal.getInstance().save(calendar);
+      OBDal.getInstance().flush();
+    } catch (Exception e) {
+      log.error("Error in createCalendar", e);
+    }
+    return calendar;
+  }
+
+  public static String getBusinessUnitOrgType() {
+    String businessUnitOrgType = null;
+    try {
+      OBContext.setAdminMode(false);
+      Client client = OBDal.getInstance().get(Client.class, ADOrgPersistInfoConstants.CLIENT_0);
+      Organization org0 = OBDal.getInstance().get(Organization.class,
+          ADOrgPersistInfoConstants.ORG_0);
+      final OBCriteria<OrganizationType> criteria = OBDal.getInstance()
+          .createCriteria(OrganizationType.class);
+      criteria.add(Restrictions.eq(OrganizationType.PROPERTY_BUSINESSUNIT, true));
+      criteria.add(Restrictions.eq(OrganizationType.PROPERTY_ACTIVE, true));
+      criteria.add(Restrictions.eq(OrganizationType.PROPERTY_CLIENT, client));
+      criteria.add(Restrictions.eq(OrganizationType.PROPERTY_ORGANIZATION, org0));
+      criteria.setMaxResults(1);
+      if (criteria.uniqueResult() != null) {
+        businessUnitOrgType = ((OrganizationType) criteria.uniqueResult()).getId();
+      } else {
+        OrganizationType orgType = OBProvider.getInstance().get(OrganizationType.class);
+        orgType.setName("Business Unit");
+        orgType.setClient(client);
+        orgType.setOrganization(org0);
+        orgType.setActive(true);
+        orgType.setBusinessUnit(true);
+        OBDal.getInstance().save(orgType);
+        OBDal.getInstance().flush();
+        OBDal.getInstance().commitAndClose();
+        businessUnitOrgType = orgType.getId();
+      }
+    } catch (Exception e) {
+      log.error("Error in getBusinessUnitOrgType", e);
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    return businessUnitOrgType;
+  }
+
+  public static String getPersistOrgInfo(final String functionName, final String orgId) {
+    String returnValue = "";
+    try {
+      final ArrayList<Object> parameters = new ArrayList<Object>();
+      parameters.add(orgId);
+      returnValue = (String) CallStoredProcedure.getInstance().call(functionName, parameters, null);
+    } catch (Exception e) {
+      throw new IllegalStateException(e);
+    }
+    return returnValue;
+  }
+
+  public static String getLegalEntityOrBusinessUnitOrg(final String functionName,
+      final String orgId, final String ptype) {
+    String returnValue = "";
+    try {
+      final ArrayList<Object> parameters = new ArrayList<Object>();
+      parameters.add(orgId);
+      parameters.add(ptype);
+      returnValue = (String) CallStoredProcedure.getInstance().call(functionName, parameters, null);
+    } catch (Exception e) {
+      throw new IllegalStateException(e);
+    }
+    return returnValue;
+  }
+
+  /**
+   * Validates persist organization information set by AD_Org_Ready DB procedure against the
+   * information provided by OrganizationStructureProvider
+   * 
+   * @param orgId
+   */
+  public static void assertPersistOrgInfo(String orgId) {
+    if (StringUtils.isNotEmpty(orgId)) {
+      Organization org = OBDal.getInstance().get(Organization.class, orgId);
+      OrganizationStructureProvider osp = OBContext.getOBContext()
+          .getOrganizationStructureProvider();
+      osp.reInitialize();
+      assertEquals("Failed to match Legal Entity of Organization", osp.getLegalEntity(org),
+          org.getLegalEntityOrganization());
+      assertEquals("Failed to match Period Control Allowed Organization of Organization",
+          osp.getPeriodControlAllowedOrganization(org), org.getPeriodControlAllowedOrganization());
+      Organization calOrg = ADOrgPersistInfoUtility.getCalendarOrganization(orgId);
+      assertEquals("Failed to match Calendar Owner Organization of organization", calOrg,
+          org.getCalendarOwnerOrganization());
+      if (calOrg != null) {
+        assertEquals("Failed to match Calendar of Organization", calOrg.getCalendar(),
+            org.getInheritedCalendar());
+      }
+      assertEquals("Failed to match Business Unit Organization of Organization",
+          ADOrgPersistInfoUtility.getBusinessUnitOrganization(orgId),
+          org.getBusinessUnitOrganization());
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/generalsetup/enterprise/organization/ADOrgPersistOrgInfoComplexOrgTreeTest.java	Thu Apr 26 22:49:57 2018 +0530
@@ -0,0 +1,158 @@
+package org.openbravo.test.generalsetup.enterprise.organization;
+
+import org.junit.Test;
+import org.openbravo.test.base.OBBaseTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ADOrgPersistOrgInfoComplexOrgTreeTest extends OBBaseTest {
+
+  private static final Logger log = LoggerFactory
+      .getLogger(ADOrgPersistOrgInfoComplexOrgTreeTest.class);
+
+  /**
+   * Create a organization tree with different organization types set them ready with cascade No
+   * option and validate persist org info for each of them
+   */
+
+  @Test
+  public void testSetReadyOrganizationWithOutCascade() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      // Create a parent organization of type Organization under *
+      String org_0 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_ORGANIZATION, ADOrgPersistInfoConstants.ORG_0, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_0, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_0);
+
+      // Create a parent organization of type Organization under org_0
+      String org_01 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_ORGANIZATION, org_0, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01);
+
+      // Create a LE with Acct parent organization of type Organization under org_01
+      String org_01_LE1 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_LEGALWITHACCOUNTING, org_01, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE1, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1);
+
+      // Create a business unit parent organization of type Organization under org_01_LE1
+      String org_01_LE1_BU = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoUtility.getBusinessUnitOrgType(), org_01_LE1, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE1_BU, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_BU);
+
+      // Create a Generic organization of type Organization under org_01_LE1_BU
+      String org_01_LE1_BU_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE1_BU, false,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE1_BU_GE, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_BU_GE);
+
+      // Create a Generic parent organization of type Organization under org_01_LE1
+      String org_01_LE1_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE1, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE1_GE, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_GE);
+
+      // Create a Generic organization of type Organization under org_01_LE1_GE
+      String org_01_LE1_GE_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE1_GE, false,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE1_GE_GE, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_GE_GE);
+
+      // Create a LE without Acct parent organization of type Organization under org_01
+      String org_01_LE2 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_LEGALWITHOUTACCOUNTING, org_01, true,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE2, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE2);
+
+      // Create a Generic organization of type Organization under org_01_LE2
+      String org_01_LE2_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE2, false,
+          ADOrgPersistInfoConstants.CUR_EURO);
+      ADOrgPersistInfoUtility.setAsReady(org_01_LE2_GE, "N");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE2_GE);
+
+    } catch (Exception e) {
+      log.error("Error in testSetReadyOrganizationWithOutCascade", e);
+    }
+  }
+
+  /**
+   * Create a organization tree with different organization types set top node organization in the
+   * created tree ready with cascade Yes option and validate persist org info for each of them
+   */
+  @Test
+  public void testSetReadyOrganizationWithCascase() {
+    try {
+      ADOrgPersistInfoUtility.setTestContextFB();
+      // Create a parent organization of type Organization under *
+      String org_0 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_ORGANIZATION, ADOrgPersistInfoConstants.ORG_0, true,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a parent organization of type Organization under org_0
+      String org_01 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_ORGANIZATION, org_0, true,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a LE with Acct parent organization of type Organization under org_01
+      String org_01_LE1 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_LEGALWITHACCOUNTING, org_01, true,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a business unit parent organization of type Organization under org_01_LE1
+      String org_01_LE1_BU = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoUtility.getBusinessUnitOrgType(), org_01_LE1, true,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a Generic organization of type Organization under org_01_LE1_BU
+      String org_01_LE1_BU_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE1_BU, false,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a Generic parent organization of type Organization under org_01_LE1
+      String org_01_LE1_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE1, true,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a Generic organization of type Organization under org_01_LE1_GE
+      String org_01_LE1_GE_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE1_GE, false,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a LE without Acct parent organization of type Organization under org_01
+      String org_01_LE2 = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_LEGALWITHOUTACCOUNTING, org_01, true,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      // Create a Generic organization of type Organization under org_01_LE2
+      String org_01_LE2_GE = ADOrgPersistInfoUtility.createOrganization(
+          ADOrgPersistInfoConstants.ORGTYPE_GENERIC, org_01_LE2, false,
+          ADOrgPersistInfoConstants.CUR_USD);
+
+      ADOrgPersistInfoUtility.setAsReady(org_0, "Y");
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_0);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_BU);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_BU_GE);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_GE);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE1_GE_GE);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE2);
+      ADOrgPersistInfoUtility.assertPersistOrgInfo(org_01_LE2_GE);
+
+    } catch (Exception e) {
+      log.error("Error in testSetReadyOrganizationWithCascade", e);
+    }
+  }
+}