@Test
  public void shouldUpdateBranchInSession() throws Exception {
    sessionService.executeInLocalView(
        new SessionExecutionBody() {
          @Override
          public void executeWithoutResult() {
            final JaloSession currentSession = JaloSession.getCurrentSession();
            final DefaultSession session =
                (DefaultSession) sessionService.getSession(currentSession.getSessionID());
            Assert.assertEquals(currentSession, session.getJaloSession());

            session.setAttribute("test", new Object());
            Assert.assertNotNull(currentSession.getAttribute("test"));
            final B2BCustomerModel user = (B2BCustomerModel) userService.getUserForUID("IC CEO");
            session.setAttribute(B2BConstants.CTX_ATTRIBUTE_BRANCH, null);
            session.setAttribute(B2BConstants.CTX_ATTRIBUTE_ROOTUNIT, null);
            Assert.assertNull(currentSession.getAttribute(B2BConstants.CTX_ATTRIBUTE_BRANCH));
            Assert.assertNull(currentSession.getAttribute(B2BConstants.CTX_ATTRIBUTE_ROOTUNIT));

            b2bUnitService.updateBranchInSession(session, user);
            Assert.assertNotNull(currentSession.getAttribute(B2BConstants.CTX_ATTRIBUTE_BRANCH));
            Assert.assertNotNull(currentSession.getAttribute(B2BConstants.CTX_ATTRIBUTE_ROOTUNIT));
          }
        },
        userService.getAdminUser());
  }
  @Test
  public void testGetBranch() throws Exception {
    sessionService.executeInLocalView(
        new SessionExecutionBody() {
          @Override
          public void executeWithoutResult() {
            final String userId = "IC CEO";
            login(userId);

            // test getBranchMethod
            B2BUnitModel unit = b2bUnitService.getUnitForUid("IC");
            Assert.assertNotNull(unit);
            final Set<B2BUnitModel> decendents = b2bUnitService.getBranch(unit);
            Assert.assertNotNull(decendents);
            Assert.assertEquals(11, decendents.size());

            // test getAllUnitsOfOrganization
            final Set<B2BUnitModel> allUnits = b2bUnitService.getBranch(unit);
            Assert.assertNotNull(allUnits);
            Assert.assertEquals(11, allUnits.size());

            unit = b2bUnitService.getUnitForUid("IC Sales");
            Assert.assertNotNull(unit);
            Assert.assertEquals(b2bUnitService.getParent(unit).getUid(), "IC");
            Assert.assertEquals(b2bUnitService.getRootUnit(unit).getUid(), "IC");
            Assert.assertEquals(9, b2bUnitService.getBranch(unit).size());
          }
        },
        userService.getAdminUser());
  }
 /**
  * This test is specially added for fetching the tree of child Unit in the organization hierarchy.
  * So used a user Id which is not the root unit for example GC.
  */
 @Test
 public void shouldGetAllBranchAndParentUnitOfChildUnit() {
   sessionService.executeInLocalView(
       new SessionExecutionBody() {
         @Override
         public void executeWithoutResult() {
           final String userId = "GC Sales UK Boss";
           login(userId);
           final Set<B2BUnitModel> allUnitsOfOrganization =
               b2bUnitService.getAllUnitsOfOrganization(
                   b2bCustomerService.getCurrentB2BCustomer());
           Assert.assertTrue(allUnitsOfOrganization.size() >= 3);
         }
       },
       userService.getAdminUser());
 }
  @Test
  public void shouldGetParentUnitForCustomerWithMultipleUnitsAssigned() {
    sessionService.executeInLocalView(
        new SessionExecutionBody() {
          @Override
          public void executeWithoutResult() {
            final B2BCustomerModel user =
                userService.getUserForUID("GC CEO", B2BCustomerModel.class);
            // assign user to session
            login(user);
            final B2BUnitModel parent = b2bUnitService.getParent(user);
            Assert.assertNotNull(parent);
            // try to add the member which the user is already a member of
            b2bUnitService.addMember(parent, user);
            modelService.save(user);
            Assert.assertEquals(
                1,
                CollectionUtils.select(
                        user.getGroups(), PredicateUtils.instanceofPredicate(B2BUnitModel.class))
                    .size());

            // assign a 2nd unit to GC CEO
            final B2BUnitModel unitUK = b2bUnitService.getUnitForUid("GC Sales UK");
            b2bUnitService.addMember(unitUK, user);
            modelService.save(user);
            Assert.assertEquals(
                2,
                CollectionUtils.select(
                        user.getGroups(), PredicateUtils.instanceofPredicate(B2BUnitModel.class))
                    .size());

            b2bUnitService.updateParentB2BUnit(unitUK, user);
            modelService.save(user);

            Assert.assertEquals(unitUK, b2bUnitService.getParent(user));
            // make sure user is still belongs to 2 units
            Assert.assertEquals(
                2,
                CollectionUtils.select(
                        user.getGroups(), PredicateUtils.instanceofPredicate(B2BUnitModel.class))
                    .size());
          }
        },
        userService.getAdminUser());
  }
  @Test
  public void shouldDisableBranchofParentUnit() {
    sessionService.executeInLocalView(
        new SessionExecutionBody() {
          @Override
          public void executeWithoutResult() {
            final String userId = "GC CEO";
            login(userId);

            final B2BUnitModel unitUK = b2bUnitService.getUnitForUid("GC Sales UK");

            b2bUnitService.disableBranch(unitUK);

            LOG.debug("Test all units are disabled for : " + unitUK.getUid());

            final Set<B2BUnitModel> allUnitsOfOrganization = b2bUnitService.getBranch(unitUK);

            for (final B2BUnitModel unit : allUnitsOfOrganization) {
              Assert.assertFalse(unit.getActive().booleanValue());
            }
          }
        },
        userService.getAdminUser());
  }
  @Test
  public void testGetApprovalProcessCode() throws Exception {
    sessionService.executeInLocalView(
        new SessionExecutionBody() {
          @Override
          public void executeWithoutResult() {
            final String userId = "IC CEO";
            String approvalCode = null;
            login(userId);

            // test findApprovalProcessCodeForUnit
            final B2BUnitModel unit = b2bUnitService.getUnitForUid("IC");
            Assert.assertNotNull(unit);
            // test
            approvalCode = b2bUnitService.getApprovalProcessCodeForUnit(unit);
            Assert.assertNull(approvalCode);

            unit.setApprovalProcessCode("simpleapproval");
            modelService.save(unit);

            approvalCode = b2bUnitService.getApprovalProcessCodeForUnit(unit);
            Assert.assertNotNull(approvalCode);
            // test findApprovalProcessCodeForUnit - get value from parent
            final B2BUnitModel unitSales = b2bUnitService.getUnitForUid("IC Sales");
            Assert.assertNotNull(unitSales);
            approvalCode = b2bUnitService.getApprovalProcessCodeForUnit(unitSales);
            Assert.assertNotNull(approvalCode);

            unit.setApprovalProcessCode(null);
            modelService.save(unit);
            approvalCode = b2bUnitService.getApprovalProcessCodeForUnit(unitSales);
            Assert.assertNull(approvalCode);
          }
        },
        userService.getAdminUser());
  }
  @Test
  public void testRestrictionsOfUsersAndUnits() throws Exception {
    sessionService.executeInLocalView(
        new SessionExecutionBody() {
          @Override
          public void executeWithoutResult() {
            final String userId = "IC CEO";
            login(userId);

            // test restrictions on units and employees
            Assert.assertNull(b2bUnitService.getUnitForUid("GC"));
            Assert.assertNull(
                baseDao.findFirstByAttribute(B2BCustomer.UID, "GC CEO", B2BCustomerModel.class));

            // test costcenter and budget restriction
            Assert.assertNotNull(
                baseDao.findFirstByAttribute(
                    B2BCostCenterModel.CODE, "IC 0", B2BCostCenterModel.class));
            Assert.assertNull(
                baseDao.findFirstByAttribute(
                    B2BCostCenterModel.CODE, "GC 0", B2BCostCenterModel.class));
            Assert.assertNotNull(
                baseDao.findFirstByAttribute(
                    B2BBudgetModel.CODE, "IC BUDGET EUR 1M", B2BBudgetModel.class));
            Assert.assertNull(
                baseDao.findFirstByAttribute(
                    B2BBudgetModel.CODE, "GC BUDGET EUR 1M", B2BBudgetModel.class));

            // change the session IC_USER to the customer who belongs to different organization
            final UserModel GC_USER =
                (UserModel)
                    sessionService.executeInLocalView(
                        new SessionExecutionBody() {
                          @Override
                          public Object execute() {
                            searchRestrictionService.disableSearchRestrictions();
                            final UserModel user =
                                baseDao.findFirstByAttribute(
                                    B2BCustomerModel.UID, "GC CEO", B2BCustomerModel.class);
                            Assert.assertNotNull(user);
                            return user;
                          }
                        });
            Assert.assertNotNull(GC_USER);
            login(GC_USER);
            // should now we able to see only items linked to C_2.. units
            Assert.assertNull(
                baseDao.findFirstByAttribute(
                    B2BCostCenterModel.CODE, "IC 0", B2BCostCenterModel.class));
            Assert.assertNotNull(
                baseDao.findFirstByAttribute(
                    B2BCostCenterModel.CODE, "GC 0", B2BCostCenterModel.class));
            Assert.assertNull(
                baseDao.findFirstByAttribute(
                    B2BBudgetModel.CODE, "IC BUDGET EUR 1M", B2BBudgetModel.class));
            Assert.assertNotNull(
                baseDao.findFirstByAttribute(
                    B2BBudgetModel.CODE, "GC BUDGET EUR 1M", B2BBudgetModel.class));
          }
        },
        userService.getAdminUser());
  }