@Test
  public void testTableIdentifiers() {
    Session session = getNewSession("jboss");
    session.beginTransaction();
    Invoice orderJboss = new Invoice();
    session.save(orderJboss);
    Assert.assertEquals(Long.valueOf(1), orderJboss.getId());
    session.getTransaction().commit();
    session.close();

    session = getNewSession("acme");
    session.beginTransaction();
    Invoice orderAcme = new Invoice();
    session.save(orderAcme);
    Assert.assertEquals(Long.valueOf(1), orderAcme.getId());
    session.getTransaction().commit();
    session.close();

    session = getNewSession("jboss");
    session.beginTransaction();
    session.delete(orderJboss);
    session.getTransaction().commit();
    session.close();

    session = getNewSession("acme");
    session.beginTransaction();
    session.delete(orderAcme);
    session.getTransaction().commit();
    session.close();

    sessionFactory.getStatisticsImplementor().clear();
  }
Esempio n. 2
0
 @Test
 public void constructor() {
   Long id = new Random().nextLong();
   testInvoice = new Invoice(id);
   Long returnedId = testInvoice.getId();
   Assert.assertEquals(id, returnedId);
 }
  /**
   * Create an adjustment for a given invoice item. This just creates the object in memory, it
   * doesn't write it to disk.
   *
   * @param invoiceToBeAdjusted the invoice
   * @param invoiceItemId the invoice item id to adjust
   * @param positiveAdjAmount the amount to adjust. Pass null to adjust the full amount of the
   *     original item
   * @param currency the currency of the amount. Pass null to default to the original currency used
   * @param effectiveDate adjustment effective date, in the account timezone
   * @return the adjustment item
   */
  public InvoiceItem createAdjustmentItem(
      final Invoice invoiceToBeAdjusted,
      final UUID invoiceItemId,
      @Nullable final BigDecimal positiveAdjAmount,
      @Nullable final Currency currency,
      final LocalDate effectiveDate,
      final InternalCallContext context)
      throws InvoiceApiException {
    final InvoiceItem invoiceItemToBeAdjusted =
        Iterables.<InvoiceItem>tryFind(
                invoiceToBeAdjusted.getInvoiceItems(),
                new Predicate<InvoiceItem>() {
                  @Override
                  public boolean apply(final InvoiceItem input) {
                    return input.getId().equals(invoiceItemId);
                  }
                })
            .orNull();
    if (invoiceItemToBeAdjusted == null) {
      throw new InvoiceApiException(ErrorCode.INVOICE_ITEM_NOT_FOUND, invoiceItemId);
    }

    // Check the specified currency matches the one of the existing invoice
    final Currency currencyForAdjustment =
        Objects.firstNonNull(currency, invoiceItemToBeAdjusted.getCurrency());
    if (invoiceItemToBeAdjusted.getCurrency() != currencyForAdjustment) {
      throw new InvoiceApiException(
          ErrorCode.CURRENCY_INVALID, currency, invoiceItemToBeAdjusted.getCurrency());
    }

    // Reuse the same logic we have for refund with item adjustment
    final Map<UUID, BigDecimal> input = new HashMap<UUID, BigDecimal>();
    input.put(invoiceItemId, positiveAdjAmount);

    final Map<UUID, BigDecimal> output =
        dao.computeItemAdjustments(invoiceToBeAdjusted.getId().toString(), input, context);

    // If we pass that stage, it means the validation succeeded so we just need to extract resulting
    // amount and negate the result.
    final BigDecimal amountToAdjust = output.get(invoiceItemId).negate();
    // Finally, create the adjustment
    return new ItemAdjInvoiceItem(
        UUIDs.randomUUID(),
        context.getCreatedDate(),
        invoiceItemToBeAdjusted.getInvoiceId(),
        invoiceItemToBeAdjusted.getAccountId(),
        effectiveDate,
        null,
        amountToAdjust,
        currencyForAdjustment,
        invoiceItemToBeAdjusted.getId());
  }