private void checkPagination(final long nbRecords) { final Pagination<CustomField> foundCustomFields = customFieldUserApi.searchCustomFields("ACCOUNT", 0L, nbRecords + 1L, callContext); Assert.assertEquals(foundCustomFields.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(foundCustomFields.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(foundCustomFields.getTotalNbRecords(), (Long) nbRecords); final Pagination<CustomField> gotCustomFields = customFieldUserApi.getCustomFields(0L, nbRecords + 1L, callContext); Assert.assertEquals(gotCustomFields.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(gotCustomFields.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(gotCustomFields.getTotalNbRecords(), (Long) nbRecords); } }
Assert.assertEquals(search1.getCurrentOffset(), (Long) 0L); Assert.assertNull(search1.getNextOffset()); Assert.assertEquals(search1.getMaxNbRecords(), (Long) 2L); Assert.assertEquals(search1.getTotalNbRecords(), (Long) 2L); Assert.assertEquals(ImmutableList.<Account>copyOf(search1.iterator()).size(), 2); Assert.assertEquals(search2.getCurrentOffset(), (Long) 0L); Assert.assertEquals(search2.getNextOffset(), (Long) 1L); Assert.assertEquals(search2.getMaxNbRecords(), (Long) 2L); Assert.assertEquals(search2.getTotalNbRecords(), (Long) 2L); Assert.assertEquals(ImmutableList.<Account>copyOf(search2.iterator()).size(), 1); Assert.assertEquals(search3.getCurrentOffset(), (Long) 0L); Assert.assertNull(search3.getNextOffset()); Assert.assertEquals(search3.getMaxNbRecords(), (Long) 2L); Assert.assertEquals(search3.getTotalNbRecords(), (Long) 1L); Assert.assertEquals(ImmutableList.<Account>copyOf(search3.iterator()).size(), 1); Assert.assertEquals(search4.getCurrentOffset(), (Long) 0L); Assert.assertNull(search4.getNextOffset()); Assert.assertNull(search4.getMaxNbRecords()); Assert.assertEquals(search4.getTotalNbRecords(), (Long) 0L); Assert.assertEquals(ImmutableList.<Account>copyOf(search4.iterator()).size(), 0); Assert.assertEquals(search5.getCurrentOffset(), (Long) 0L); Assert.assertNull(search5.getNextOffset()); Assert.assertNull(search5.getMaxNbRecords());
public DefaultPagination(final Pagination original, final Long limit, final Iterator<T> delegate) { this(original.getCurrentOffset(), limit, original.getTotalNbRecords(), original.getMaxNbRecords(), delegate); }
totalNbRecords += pages.getTotalNbRecords(); if (!maxStatsCrossPlugins) { maxNbRecords += pages.getMaxNbRecords(); } else { maxNbRecords = Math.max(maxNbRecords, pages.getMaxNbRecords());
@Test(groups = "slow") public void testGetPaymentMethodsWithAndWithoutPluginInfo() throws PaymentApiException, PaymentPluginApiException { final BigDecimal requestedAmount = BigDecimal.TEN; final String paymentExternalKey = "externalKey"; final String transactionExternalKey = "transactionKey"; final Payment payment = paymentApi.createPurchase(account, account.getPaymentMethodId(), null, requestedAmount, Currency.AED, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), callContext); final Pagination<PaymentMethod> paymentMethods = paymentApi.getPaymentMethods(0L, 10L, false, null, callContext); final Pagination<PaymentMethod> paymentMethodsPlugin = paymentApi.getPaymentMethods(0L, 10L, true, null, callContext); Assert.assertTrue(paymentMethods.getTotalNbRecords() == 1); Assert.assertTrue(paymentMethodsPlugin.getTotalNbRecords() == 1); PaymentMethod paymentMethod = paymentMethods.iterator().next(); PaymentMethod paymentMethodPlugin = paymentMethodsPlugin.iterator().next(); Assert.assertEquals(paymentMethod.getAccountId(), paymentMethodPlugin.getAccountId()); Assert.assertEquals(paymentMethod.getId(), paymentMethodPlugin.getId()); Assert.assertEquals(paymentMethod.getExternalKey(), paymentMethodPlugin.getExternalKey()); Assert.assertEquals(paymentMethod.getPluginName(), paymentMethodPlugin.getPluginName()); Assert.assertNull(paymentMethod.getPluginDetail()); Assert.assertNotNull(paymentMethodPlugin.getPluginDetail()); Assert.assertTrue(paymentMethodPlugin.getPluginDetail().getProperties().isEmpty()); }
public static <E extends Entity, O, T extends BillingExceptionBase> Pagination<E> getEntityPagination(final Long limit, final SourcePaginationBuilder<O, T> sourcePaginationBuilder, final Function<O, E> function) throws T { final Pagination<O> modelsDao = sourcePaginationBuilder.build(); return new DefaultPagination<E>(modelsDao, limit, Iterators.<E>filter(Iterators.<O, E>transform(modelsDao.iterator(), function), Predicates.<E>notNull())); }
@Test(groups = "slow") public void testAddRemovePaymentMethodWithForcedDeletion() throws Exception { final Long baseNbRecords = paymentApi.getPaymentMethods(0L, 1000L, false, ImmutableList.<PluginProperty>of(), callContext).getMaxNbRecords(); Assert.assertEquals(baseNbRecords, (Long) 1L); final Account account = testHelper.createTestAccount(UUID.randomUUID().toString(), true); final UUID paymentMethodId = account.getPaymentMethodId(); checkPaymentMethodPagination(paymentMethodId, baseNbRecords + 1, false); paymentApi.deletePaymentMethod(account, paymentMethodId, false, true, ImmutableList.<PluginProperty>of(), callContext); checkPaymentMethodPagination(paymentMethodId, baseNbRecords, true); }
@Override public void write(final OutputStream output) throws IOException, WebApplicationException { final Iterator<E> iterator = entities.iterator(); try { final JsonGenerator generator = mapper.getFactory().createGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.writeStartArray(); while (iterator.hasNext()) { final E entity = iterator.next(); final J asJson = toJson.apply(entity); if (asJson != null) { generator.writeObject(asJson); } } generator.writeEndArray(); generator.close(); } finally { // In case the client goes away (IOException), make sure to close the underlying DB connection entities.close(); } } };
@TimedResource @GET @Path("/" + SEARCH + "/{searchKey:" + ANYTHING_PATTERN + "}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Search accounts", response = AccountJson.class, responseContainer = "List") @ApiResponses(value = {}) public Response searchAccounts(@PathParam("searchKey") final String searchKey, @QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_ACCOUNT_WITH_BALANCE) @DefaultValue("false") final Boolean accountWithBalance, @QueryParam(QUERY_ACCOUNT_WITH_BALANCE_AND_CBA) @DefaultValue("false") final Boolean accountWithBalanceAndCBA, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException { final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Pagination<Account> accounts = accountUserApi.searchAccounts(searchKey, offset, limit, tenantContext); final URI nextPageUri = uriBuilder.nextPage(AccountResource.class, "searchAccounts", accounts.getNextOffset(), limit, ImmutableMap.<String, String>of("searchKey", searchKey, QUERY_ACCOUNT_WITH_BALANCE, accountWithBalance.toString(), QUERY_ACCOUNT_WITH_BALANCE_AND_CBA, accountWithBalanceAndCBA.toString(), QUERY_AUDIT, auditMode.getLevel().toString())); return buildStreamingPaginationResponse(accounts, new Function<Account, AccountJson>() { @Override public AccountJson apply(final Account account) { final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(account.getId(), auditMode.getLevel(), tenantContext); return getAccount(account, accountWithBalance, accountWithBalanceAndCBA, accountAuditLogs, tenantContext); } }, nextPageUri ); }
@Test(groups = "slow") public void testPaymentAttemptsByStateAcrossTenants() throws Exception { final String externalKey1 = "gfhfg"; final String transactionExternalKey1 = "sadas"; final String externalKey2 = "asdwqeqw"; final String transactionExternalKey2 = "fghfg"; final DateTime createdAfterDate = clock.getUTCNow().minusDays(10); final DateTime createdBeforeDate = clock.getUTCNow().minusDays(1); final String stateName = "FOO"; final String pluginName = "miraculous"; clock.setTime(createdAfterDate); Account account = testHelper.createTestAccount(UUID.randomUUID().toString(), true); final PaymentAttemptModelDao attempt1 = new PaymentAttemptModelDao(account.getId(), account.getPaymentMethodId(), createdAfterDate, createdAfterDate, externalKey1, UUID.randomUUID(), transactionExternalKey1, TransactionType.AUTHORIZE, stateName, BigDecimal.ONE, Currency.USD, ImmutableList.<String>of(pluginName), null); paymentDao.insertPaymentAttemptWithProperties(attempt1, internalCallContext); account = testHelper.createTestAccount(UUID.randomUUID().toString(), true); final PaymentAttemptModelDao attempt2 = new PaymentAttemptModelDao(account.getId(), account.getPaymentMethodId(), createdAfterDate, createdAfterDate, externalKey2, UUID.randomUUID(), transactionExternalKey2, TransactionType.AUTHORIZE, stateName, BigDecimal.ONE, Currency.USD, ImmutableList.<String>of(pluginName), null); paymentDao.insertPaymentAttemptWithProperties(attempt2, internalCallContext); final Pagination<PaymentAttemptModelDao> result = paymentDao.getPaymentAttemptsByStateAcrossTenants(stateName, createdBeforeDate, 0L, 2L); Assert.assertEquals(result.getTotalNbRecords().longValue(), 2L); }
@Override public void write(final OutputStream output) throws IOException, WebApplicationException { try { final JsonGenerator generator = mapper.getFactory().createGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.writeStartObject(); while (iterator.hasNext()) { final Tag tag = iterator.next(); final UUID accountId = tag.getObjectId(); try { invoiceUserApi.triggerInvoiceGeneration(accountId, clock.getUTCToday(), callContext); generator.writeStringField(accountId.toString(), OK); } catch (final InvoiceApiException e) { if (e.getCode() != ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) { log.warn("Unable to trigger invoice generation for accountId='{}'", accountId); } generator.writeStringField(accountId.toString(), ErrorCode.fromCode(e.getCode()).toString()); } } generator.writeEndObject(); generator.close(); } finally { // In case the client goes away (IOException), make sure to close the underlying DB connection tags.close(); } } };
public DefaultPagination(final Pagination original, final Long limit, final Iterator<T> delegate) { this(original.getCurrentOffset(), limit, original.getTotalNbRecords(), original.getMaxNbRecords(), delegate); }
totalNbRecords += pages.getTotalNbRecords(); if (!maxStatsCrossPlugins) { maxNbRecords += pages.getMaxNbRecords(); } else { maxNbRecords = Math.max(maxNbRecords, pages.getMaxNbRecords());
@Test(groups = "slow") public void testPaginationForPaymentByStatesAcrossTenants() throws Exception { final DateTime createdDate1 = clock.getUTCNow().minusHours(1); clock.setTime(createdDate1); final Account account = testHelper.createTestAccount(UUID.randomUUID().toString(), true); final int NB_ENTRIES = 30; for (int i = 0; i < NB_ENTRIES; i++) { final PaymentModelDao paymentModelDao1 = new PaymentModelDao(createdDate1, createdDate1, account.getId(), account.getPaymentMethodId(), UUID.randomUUID().toString()); final PaymentTransactionModelDao transaction1 = new PaymentTransactionModelDao(createdDate1, createdDate1, null, UUID.randomUUID().toString(), paymentModelDao1.getId(), TransactionType.AUTHORIZE, createdDate1, TransactionStatus.UNKNOWN, BigDecimal.TEN, Currency.AED, "unknown", ""); paymentDao.insertPaymentWithFirstTransaction(paymentModelDao1, transaction1, internalCallContext); } clock.setTime(createdDate1.plusHours(1)); final Pagination<PaymentTransactionModelDao> result = paymentDao.getByTransactionStatusAcrossTenants(ImmutableList.of(TransactionStatus.UNKNOWN), clock.getUTCNow(), createdDate1, 0L, (long) NB_ENTRIES); Assert.assertEquals(result.getTotalNbRecords(), new Long(NB_ENTRIES)); final Iterator<PaymentTransactionModelDao> iterator = result.iterator(); for (int i = 0; i < NB_ENTRIES; i++) { Assert.assertTrue(iterator.hasNext()); final PaymentTransactionModelDao nextEntry = iterator.next(); Assert.assertEquals(nextEntry.getTransactionStatus(), TransactionStatus.UNKNOWN); } }
public static <E extends Entity, O, T extends BillingExceptionBase> Pagination<E> getEntityPagination(final Long limit, final SourcePaginationBuilder<O, T> sourcePaginationBuilder, final Function<O, E> function) throws T { final Pagination<O> modelsDao = sourcePaginationBuilder.build(); return new DefaultPagination<E>(modelsDao, limit, Iterators.<E>filter(Iterators.<O, E>transform(modelsDao.iterator(), function), Predicates.<E>notNull())); }
@Test(groups = "slow") public void testAddRemovePaymentMethodWithoutForcedDeletion() throws Exception { final Long baseNbRecords = paymentApi.getPaymentMethods(0L, 1000L, false, ImmutableList.<PluginProperty>of(), callContext).getMaxNbRecords(); Assert.assertEquals(baseNbRecords, (Long) 1L); final Account account = testHelper.createTestAccount(UUID.randomUUID().toString(), true); final UUID paymentMethodId = account.getPaymentMethodId(); try { paymentApi.deletePaymentMethod(account, paymentMethodId, false, false, ImmutableList.<PluginProperty>of(), callContext); } catch (final PaymentApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.PAYMENT_INTERNAL_ERROR.getCode()); } checkPaymentMethodPagination(paymentMethodId, baseNbRecords + 1, false); }
@TimedResource @GET @Path("/" + PAGINATION) @Produces(APPLICATION_JSON) @ApiOperation(value = "List accounts", response = AccountJson.class, responseContainer = "List") @ApiResponses(value = {}) public Response getAccounts(@QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_ACCOUNT_WITH_BALANCE) @DefaultValue("false") final Boolean accountWithBalance, @QueryParam(QUERY_ACCOUNT_WITH_BALANCE_AND_CBA) @DefaultValue("false") final Boolean accountWithBalanceAndCBA, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException { final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Pagination<Account> accounts = accountUserApi.getAccounts(offset, limit, tenantContext); final URI nextPageUri = uriBuilder.nextPage(AccountResource.class, "getAccounts", accounts.getNextOffset(), limit, ImmutableMap.<String, String>of(QUERY_ACCOUNT_WITH_BALANCE, accountWithBalance.toString(), QUERY_ACCOUNT_WITH_BALANCE_AND_CBA, accountWithBalanceAndCBA.toString(), QUERY_AUDIT, auditMode.getLevel().toString())); return buildStreamingPaginationResponse(accounts, new Function<Account, AccountJson>() { @Override public AccountJson apply(final Account account) { final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(account.getId(), auditMode.getLevel(), tenantContext); return getAccount(account, accountWithBalance, accountWithBalanceAndCBA, accountAuditLogs, tenantContext); } }, nextPageUri ); }
private void checkPagination(final long nbRecords) { final Pagination<Tag> foundTags = tagUserApi.searchTags("ACCOUNT", 0L, nbRecords + 1L, callContext); Assert.assertEquals(foundTags.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(foundTags.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(foundTags.getTotalNbRecords(), (Long) nbRecords); final Pagination<Tag> gotTags = tagUserApi.getTags(0L, nbRecords + 1L, callContext); Assert.assertEquals(gotTags.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(gotTags.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(gotTags.getTotalNbRecords(), (Long) nbRecords); } }
.header(HDR_PAGINATION_CURRENT_OFFSET, entities.getCurrentOffset()) .header(HDR_PAGINATION_NEXT_OFFSET, entities.getNextOffset()) .header(HDR_PAGINATION_TOTAL_NB_RECORDS, entities.getTotalNbRecords()) .header(HDR_PAGINATION_MAX_NB_RECORDS, entities.getMaxNbRecords()) .header(HDR_PAGINATION_NEXT_PAGE_URI, nextPageUri) .build();
private void verifyPayment(final PaymentModelDao loadedPaymentModelDao, final PaymentModelDao specifiedPaymentModelDao) { Assert.assertEquals(loadedPaymentModelDao.getAccountId(), specifiedPaymentModelDao.getAccountId()); Assert.assertTrue(loadedPaymentModelDao.getPaymentNumber() > 0); Assert.assertEquals(loadedPaymentModelDao.getPaymentMethodId(), specifiedPaymentModelDao.getPaymentMethodId()); Assert.assertEquals(loadedPaymentModelDao.getExternalKey(), specifiedPaymentModelDao.getExternalKey()); // Verify search APIs Assert.assertEquals(ImmutableList.<PaymentModelDao>copyOf(paymentDao.searchPayments(specifiedPaymentModelDao.getPaymentMethodId().toString(), 0L, 100L, internalCallContext).iterator()).size(), 1); Assert.assertEquals(ImmutableList.<PaymentModelDao>copyOf(paymentDao.searchPayments(specifiedPaymentModelDao.getExternalKey(), 0L, 100L, internalCallContext).iterator()).size(), 1); }