protected Collection<Payment> getPaymentsWithPluginInfoByAccountId(
      final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
    PaymentApiException error;

    final PaymentApi paymentApi = getPaymentUserApi();
    try {
      return paymentApi.getAccountPayments(accountId, true, PLUGIN_PROPERTIES, context);
    } catch (PaymentApiException e) {
      error = e;
      if (e.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT_PLUGIN.getCode()) {
        logService.log(
            LogService.LOG_WARNING,
            e.getMessage() + ". Analytics tables will be missing plugin specific information");

        try {
          return paymentApi.getAccountPayments(accountId, false, PLUGIN_PROPERTIES, context);
        } catch (PaymentApiException e1) {
          error = e1;
        }
      }
    }

    logService.log(
        LogService.LOG_WARNING, "Error retrieving payments for account id " + accountId, error);
    throw new AnalyticsRefreshException(error);
  }
  protected List<PaymentMethod> getPaymentMethodsForAccount(
      final UUID accountId, final TenantContext context) throws AnalyticsRefreshException {
    PaymentApiException error;

    final PaymentApi paymentApi = getPaymentUserApi();
    try {
      // Try to get all payment methods, with plugin information
      // TODO this will not return deleted payment methods
      return paymentApi.getAccountPaymentMethods(accountId, true, PLUGIN_PROPERTIES, context);
    } catch (PaymentApiException e) {
      error = e;
      if (e.getCode() == ErrorCode.PAYMENT_NO_SUCH_PAYMENT_PLUGIN.getCode()) {
        logService.log(
            LogService.LOG_WARNING,
            e.getMessage() + ". Analytics tables will be missing plugin specific information");

        try {
          return paymentApi.getAccountPaymentMethods(accountId, false, PLUGIN_PROPERTIES, context);
        } catch (PaymentApiException e1) {
          error = e1;
        }
      }
    }

    logService.log(
        LogService.LOG_WARNING,
        "Error retrieving payment methods for account id " + accountId,
        error);
    throw new AnalyticsRefreshException(error);
  }
  @Test(groups = "slow", description = "refresh payment methods")
  public void testRefreshPaymentMethods() throws Exception {
    Account account = createAccountWithDefaultPaymentMethod("someExternalKey");

    final PaymentMethods paymentMethodsBeforeRefreshing =
        killBillClient.getPaymentMethodsForAccount(account.getAccountId());
    assertEquals(paymentMethodsBeforeRefreshing.size(), 1);
    assertEquals(paymentMethodsBeforeRefreshing.get(0).getExternalKey(), "someExternalKey");

    // WITH NAME OF AN EXISTING PLUGIN
    killBillClient.refreshPaymentMethods(
        account.getAccountId(),
        PLUGIN_NAME,
        ImmutableMap.<String, String>of(),
        createdBy,
        reason,
        comment);

    final PaymentMethods paymentMethodsAfterExistingPluginCall =
        killBillClient.getPaymentMethodsForAccount(account.getAccountId());

    assertEquals(paymentMethodsAfterExistingPluginCall.size(), 1);
    assertEquals(paymentMethodsAfterExistingPluginCall.get(0).getExternalKey(), "someExternalKey");

    // WITHOUT PLUGIN NAME
    killBillClient.refreshPaymentMethods(
        account.getAccountId(), ImmutableMap.<String, String>of(), createdBy, reason, comment);

    final PaymentMethods paymentMethodsAfterNoPluginNameCall =
        killBillClient.getPaymentMethodsForAccount(account.getAccountId());
    assertEquals(paymentMethodsAfterNoPluginNameCall.size(), 1);
    assertEquals(paymentMethodsAfterNoPluginNameCall.get(0).getExternalKey(), "someExternalKey");

    // WITH WRONG PLUGIN NAME
    try {
      killBillClient.refreshPaymentMethods(
          account.getAccountId(),
          "GreatestPluginEver",
          ImmutableMap.<String, String>of(),
          createdBy,
          reason,
          comment);
      Assert.fail();
    } catch (KillBillClientException e) {
      Assert.assertEquals(
          e.getBillingException().getCode(),
          (Integer) ErrorCode.PAYMENT_NO_SUCH_PAYMENT_PLUGIN.getCode());
    }
  }