@Test
  public void testSetPaymentIdentificationUpdate() throws Exception {
    Organization org =
        runTX(
            new Callable<Organization>() {
              @Override
              public Organization call() throws Exception {
                Organization org =
                    Organizations.createOrganization(mgr, OrganizationRoleType.CUSTOMER);
                return org;
              }
            });

    OrganizationRefToPaymentType orgPayType = addPaymentTypeToOrganization(org, DIRECT_DEBIT);
    PaymentInfo pi = getPaymentInfoForOrg(orgPayType);

    VOPaymentData pd =
        getData(
            pi.getKey(),
            "initialValueToSet",
            null,
            null,
            org.getKey(),
            pi.getPaymentType().getKey());
    paymentMgmt.savePaymentIdentificationForOrganization(pd);

    PaymentInfo savedPi = findPaymentInfo(pi.getKey());

    Assert.assertEquals(
        "Wrong external identifier for payment info stored",
        "initialValueToSet",
        savedPi.getExternalIdentifier());
    Assert.assertEquals(
        "Wrong payment info type stored",
        DIRECT_DEBIT,
        savedPi.getPaymentType().getPaymentTypeId());

    pd =
        getData(
            savedPi.getKey(),
            "initialValueToSetUpdated",
            null,
            null,
            org.getKey(),
            pi.getPaymentType().getKey());
    paymentMgmt.savePaymentIdentificationForOrganization(pd);

    PaymentInfo updatedPi = findPaymentInfo(pi.getKey());

    Assert.assertEquals(
        "Wrong payment info type updated",
        DIRECT_DEBIT,
        updatedPi.getPaymentType().getPaymentTypeId());

    Assert.assertEquals("Wrong object has been updated", updatedPi.getKey(), savedPi.getKey());
  }
  @Test
  public void testSetPaymentIdentificationForOrganizationValidateActivationOfSubs()
      throws Exception {
    // create org and sub with status suspended
    final Organization org =
        runTX(
            new Callable<Organization>() {
              @Override
              public Organization call() throws Exception {
                Organization org =
                    Organizations.createOrganization(mgr, OrganizationRoleType.CUSTOMER);

                return org;
              }
            });

    OrganizationRefToPaymentType orgPayType = addPaymentTypeToOrganization(org, CREDIT_CARD);
    final PaymentInfo pi = getPaymentInfoForOrg(orgPayType);

    container.login(String.valueOf(supplierUser.getKey()));
    enablePaymentTypes(org.getOrganizationId(), OrganizationRoleType.CUSTOMER);
    Subscription sub =
        runTX(
            new Callable<Subscription>() {
              @Override
              public Subscription call() throws Exception {
                prepareProducts(null);
                Organization supplier = new Organization();
                supplier.setOrganizationId(supplierId);
                supplier = (Organization) mgr.getReferenceByBusinessKey(supplier);
                Subscription sub =
                    Subscriptions.createSubscription(
                        mgr, org.getOrganizationId(), "testProd1", "subId", supplier);
                sub.setStatus(SubscriptionStatus.SUSPENDED);
                sub.setPaymentInfo(pi);
                return sub;
              }
            });
    final long subKey = sub.getKey();

    VOPaymentData pd =
        getData(
            pi.getKey(), "someIdFromPSP", null, null, org.getKey(), pi.getPaymentType().getKey());
    paymentMgmt.savePaymentIdentificationForOrganization(pd);

    // validate subscription status is active
    sub =
        runTX(
            new Callable<Subscription>() {
              @Override
              public Subscription call() throws Exception {
                return mgr.getReference(Subscription.class, subKey);
              }
            });
    Assert.assertEquals(
        "subscription was not activated", SubscriptionStatus.ACTIVE, sub.getStatus());
    Assert.assertTrue(instanceActivated);
  }
  @Test
  public void testSetPaymentIdentificationCreation() throws Exception {
    final String localOrgId = "orgForPSPIdTest2";
    Organization org = initPlainOrgWithId(localOrgId);
    OrganizationRefToPaymentType orgPayType = addPaymentTypeToOrganization(org, CREDIT_CARD);
    PaymentInfo pi = getPaymentInfoForOrg(orgPayType);

    String id = "initialValueToSet";
    String provider = "Platin Card";
    String account = "0123456";

    // Create a new PI (Registration)
    VOPaymentData pd =
        getData(0, id, provider, account, org.getKey(), pi.getPaymentType().getKey());
    paymentMgmt.savePaymentIdentificationForOrganization(pd);

    PaymentInfo savedPi = findPaymentInfo(0);
    Assert.assertEquals(
        "Wrong external identifier for payment info stored", id, savedPi.getExternalIdentifier());
    Assert.assertEquals(
        "Wrong payment info type stored", CREDIT_CARD, savedPi.getPaymentType().getPaymentTypeId());
    Assert.assertEquals(provider, savedPi.getProviderName());
    Assert.assertEquals(account, savedPi.getAccountNumber());

    // Now update an existing PI (Reregistration)
    pd = getData(pi.getKey(), id, provider, account, org.getKey(), pi.getPaymentType().getKey());
    pd.setPaymentInfoId(pd.getPaymentInfoId() + "2");
    paymentMgmt.savePaymentIdentificationForOrganization(pd);

    savedPi = findPaymentInfo(pi.getKey());
    Assert.assertEquals(
        "Wrong external identifier for payment info stored", id, savedPi.getExternalIdentifier());
    Assert.assertEquals(
        "Wrong payment info type stored", CREDIT_CARD, savedPi.getPaymentType().getPaymentTypeId());
    Assert.assertEquals(provider, savedPi.getProviderName());
    Assert.assertEquals(account, savedPi.getAccountNumber());
  }
  @Test(expected = PaymentDataException.class)
  public void testSetPaymentIdentificationUpdateForbiddenPaymentType() throws Exception {
    Organization org =
        runTX(
            new Callable<Organization>() {
              @Override
              public Organization call() throws Exception {
                Organization org =
                    Organizations.createOrganization(mgr, OrganizationRoleType.CUSTOMER);
                return org;
              }
            });

    OrganizationRefToPaymentType orgPayType = addPaymentTypeToOrganization(org, INVOICE);
    PaymentInfo pi = getPaymentInfoForOrg(orgPayType);

    VOPaymentData pd =
        getData(pi.getKey(), "externalId", null, null, org.getKey(), pi.getPaymentType().getKey());
    paymentMgmt.savePaymentIdentificationForOrganization(pd);
  }