@Test
  public void shouldStartApprovalProcessAndIgnoreInactiveCreditLimit() throws Exception {
    final String userId = "GC S HH";
    login(userId);

    // Set up credit limit data for test
    final B2BUnitModel unitLoggedIn = b2bUnitService.getUnitForUid("GC Sales Hamburg");
    final B2BCreditLimitModel creditLimitToUse =
        b2bUnitService.getParent(unitLoggedIn).getCreditLimit();
    creditLimitToUse.setActive(Boolean.TRUE);
    creditLimitToUse.setDateRange(B2BPeriodRange.DAY);
    creditLimitToUse.setDatePeriod(null);
    modelService.save(creditLimitToUse);

    // Credit Limit is active, so unit is returned
    final B2BUnitModel unitWithCreditLimit = b2bUnitService.getUnitWithCreditLimit(unitLoggedIn);
    Assert.assertNotNull(unitWithCreditLimit);

    // Update credit limit with past start/end dates as date range
    final B2BCreditLimitModel creditLimit = unitWithCreditLimit.getCreditLimit();
    creditLimit.setActive(Boolean.FALSE);

    modelService.save(creditLimit);

    // Create order which crosses credit limit
    final OrderModel order =
        createOrder(
            this.login("GC S HH"),
            140,
            OrderStatus.CREATED,
            productService.getProductForCode("testProduct0"));
    Assert.assertNotNull(order);

    final B2BApprovalProcessModel b2bApprovalProcessModel =
        getB2BApprovalProcessModelForOrder(order);

    // Process does not stop at waitProcessCreditLimit, so it should continue after waitProcess
    if (this.waitForProcessAction(b2bApprovalProcessModel.getCode(), "waitProcess", 20000)) {
      modelService.refresh(order);
      final Collection<WorkflowActionModel> actions =
          b2bWorkflowIntegrationService.getStartWorkflowActions(order.getWorkflow());
      Assert.assertEquals(1, actions.size());
      b2bWorkflowIntegrationService.approveWorkflowAction(actions.iterator().next());
    }

    // Credit limit is inactive, so unit is null
    final B2BUnitModel unitWithCreditLimitNull =
        b2bUnitService.getUnitWithCreditLimit(order.getUnit());
    Assert.assertNull(unitWithCreditLimitNull);

    // Process finishes and order placed above is approved - Order total is more than credit limit
    // amount, but
    // credit limit is inactive, so its ignored
    this.waitForProcessToEnd(b2bApprovalProcessModel.getCode(), 20000);
    this.modelService.refresh(order);
    this.modelService.refresh(b2bApprovalProcessModel);
    Assert.assertEquals(OrderStatus.APPROVED, order.getStatus());
    Assert.assertEquals(ProcessState.SUCCEEDED, b2bApprovalProcessModel.getProcessState());
  }
  @Test
  public void shouldStartApprovalProcessAndAssertApprovalFromMerchant() throws Exception {
    login("GC S HH");

    // Set up credit limit data for test
    final B2BUnitModel unitLoggedIn = b2bUnitService.getUnitForUid("GC Sales Hamburg");
    final B2BCreditLimitModel creditLimitToUse =
        b2bUnitService.getParent(unitLoggedIn).getCreditLimit();
    creditLimitToUse.setActive(Boolean.TRUE);
    creditLimitToUse.setDateRange(B2BPeriodRange.DAY);
    creditLimitToUse.setDatePeriod(null);
    modelService.save(creditLimitToUse);

    final OrderModel order =
        createOrder(
            this.login("GC S HH"),
            140,
            OrderStatus.CREATED,
            productService.getProductForCode("testProduct0"));
    Assert.assertNotNull(order);

    final B2BApprovalProcessModel b2bApprovalProcessModel =
        getB2BApprovalProcessModelForOrder(order);

    final B2BUnitModel unitWithCreditLimit = b2bUnitService.getUnitWithCreditLimit(order.getUnit());

    Assert.assertEquals("GC Sales DE", unitWithCreditLimit.getUid());

    if (this.waitForProcessAction(b2bApprovalProcessModel.getCode(), "waitProcess", 20000)) {
      modelService.refresh(order);
      final Collection<WorkflowActionModel> actions =
          b2bWorkflowIntegrationService.getStartWorkflowActions(order.getWorkflow());
      Assert.assertEquals(1, actions.size());
      b2bWorkflowIntegrationService.decideAction(
          actions.iterator().next(), B2BWorkflowIntegrationService.DECISIONCODES.APPROVE.name());
    }

    if (this.waitForProcessAction(
        b2bApprovalProcessModel.getCode(), "waitProcessCreditLimit", 20000)) {
      modelService.refresh(order);
      final Collection<WorkflowActionModel> actions =
          b2bWorkflowIntegrationService.getStartWorkflowActions(order.getWorkflow());
      Assert.assertEquals(1, actions.size());
      this.approveWorkflowAction(actions.iterator().next());
    }

    this.waitForProcessToEnd(b2bApprovalProcessModel.getCode(), 20000);
    this.modelService.refresh(order);
    this.modelService.refresh(b2bApprovalProcessModel);
    Assert.assertEquals(OrderStatus.APPROVED, order.getStatus());
    Assert.assertEquals(ProcessState.SUCCEEDED, b2bApprovalProcessModel.getProcessState());
  }
  @Override
  public void executeAction(final B2BApprovalProcessModel process) throws RetryLaterException {
    OrderModel order = null;
    try {
      order = process.getOrder();
      Assert.notNull(
          order,
          String.format("Order of BusinessProcess %s should have be set for accelerator", process));
      final B2BCustomerModel user = (B2BCustomerModel) order.getUser();
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            String.format(
                "Process for accelerator: %s in step %s order: %s user: %s ",
                process.getCode(), getClass(), order.getUnit(), user.getUid()));
      }
    } catch (final Exception exception) {
      LOG.error(exception.getMessage(), exception);
      this.handleError(order, exception);

      throw new RuntimeException(exception.getMessage(), exception);
    }
  }