@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());
  }
  /**
   * Tries to save the passed list of {@link VOUdaDefinition}s. Checks if the passed values are
   * valid and permitted to be accessed.
   *
   * @param defs the {@link VOUdaDefinition}s to save
   * @param caller the calling (owning) {@link Organization}
   * @throws ValidationException in case of an invalid {@link VOUdaDefinition}
   * @throws OrganizationAuthoritiesException in case the calling {@link Organization} has
   *     insufficient roles to create {@link UdaDefinition}s of the set {@link UdaTargetType}.
   * @throws NonUniqueBusinessKeyException in case a {@link UdaDefinition} with the passed id and
   *     target type already exists for the owning {@link Organization}
   * @throws OperationNotPermittedException in case it was tries to update a {@link UdaDefinition}
   *     owned by another {@link Organization}.
   * @throws ConcurrentModificationException in case the {@link UdaDefinition} to update was
   *     concurrently changed
   * @throws ObjectNotFoundException in case on of the {@link UdaDefinition}s to update was not
   *     found
   */
  public void saveUdaDefinitions(List<VOUdaDefinition> defs, Organization caller)
      throws ValidationException, OrganizationAuthoritiesException, NonUniqueBusinessKeyException,
          OperationNotPermittedException, ConcurrentModificationException, ObjectNotFoundException {

    for (VOUdaDefinition voDef : defs) {
      // convert and validate
      UdaDefinition def;
      try {
        def = UdaAssembler.toUdaDefinition(voDef);
        def.setOrganization(caller);
      } catch (ValidationException e) {
        logger.logWarn(
            Log4jLogger.SYSTEM_LOG,
            e,
            LogMessageIdentifier.WARN_INVALID_UDA_DEFINITION,
            voDef.getUdaId());
        ctx.setRollbackOnly();
        throw e;
      }
      // check if target type is allowed for organization
      UdaTargetType type = def.getTargetType();
      if (!type.canSaveDefinition(caller.getGrantedRoleTypes())) {
        String roles = rolesToString(type.getRoles());
        OrganizationAuthoritiesException e =
            new OrganizationAuthoritiesException(
                "Insufficient authorization. Required role(s) '" + roles + "'.",
                new Object[] {roles});
        logger.logWarn(
            Log4jLogger.SYSTEM_LOG | Log4jLogger.AUDIT_LOG,
            e,
            LogMessageIdentifier.WARN_ORGANIZATION_ROLE_REQUIRED,
            Long.toString(caller.getKey()),
            roles);
        ctx.setRollbackOnly();
        throw e;
      }
      if (voDef.getKey() > 0) {
        updateDefinition(voDef, caller);
      } else {
        createDefinition(def);
      }
      UdaDefinition storedUda = (UdaDefinition) ds.find(def);
      if (storedUda == null) {
        return;
      }
      storeLocalizedAttributeName(storedUda.getKey(), voDef.getName(), voDef.getLanguage());
    }
  }
  @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);
  }