Search in sources :

Example 1 with ClockMock

use of org.killbill.clock.ClockMock in project killbill by killbill.

the class TestResource method updateTestClockTime.

@PUT
@Path("/clock")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Move the current time", response = ClockResource.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid timezone supplied") })
public Response updateTestClockTime(@QueryParam("days") final Integer addDays, @QueryParam("weeks") final Integer addWeeks, @QueryParam("months") final Integer addMonths, @QueryParam("years") final Integer addYears, @QueryParam("timeZone") final String timeZoneStr, @QueryParam("timeoutSec") @DefaultValue("5") final Long timeoutSec, @javax.ws.rs.core.Context final HttpServletRequest request) {
    final ClockMock testClock = getClockMock();
    if (addDays != null) {
        testClock.addDays(addDays);
    } else if (addWeeks != null) {
        testClock.addWeeks(addWeeks);
    } else if (addMonths != null) {
        testClock.addMonths(addMonths);
    } else if (addYears != null) {
        testClock.addYears(addYears);
    }
    waitForNotificationToComplete(request, timeoutSec);
    return getCurrentTime(timeZoneStr);
}
Also used : ClockMock(org.killbill.clock.ClockMock) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with ClockMock

use of org.killbill.clock.ClockMock in project killbill by killbill.

the class TestResource method setTestClockTime.

@POST
@Path("/clock")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Set the current time", response = ClockResource.class)
@ApiResponses(value = { /* @ApiResponse(code = 200, message = "Successful"), */
@ApiResponse(code = 400, message = "Invalid time or timezone supplied") })
public Response setTestClockTime(@QueryParam(QUERY_REQUESTED_DT) final String requestedClockDate, @QueryParam("timeZone") final String timeZoneStr, @QueryParam("timeoutSec") @DefaultValue("5") final Long timeoutSec, @javax.ws.rs.core.Context final HttpServletRequest request) {
    final ClockMock testClock = getClockMock();
    if (requestedClockDate == null) {
        testClock.resetDeltaFromReality();
    } else {
        final DateTime newTime = DATE_TIME_FORMATTER.parseDateTime(requestedClockDate).toDateTime(DateTimeZone.UTC);
        testClock.setTime(newTime);
    }
    waitForNotificationToComplete(request, timeoutSec);
    return getCurrentTime(timeZoneStr);
}
Also used : ClockMock(org.killbill.clock.ClockMock) DateTime(org.joda.time.DateTime) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ClockMock

use of org.killbill.clock.ClockMock in project killbill by killbill.

the class TestInvoiceDao method testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair.

@Test(groups = "slow")
public void testRefundedInvoiceWithInvoiceItemAdjustmentWithRepair() throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2010, 1, 1);
    ((ClockMock) clock).setDay(startDate);
    final LocalDate recuringStartDate = clock.getUTCNow().plusDays(30).toLocalDate();
    final LocalDate recuringEndDate = recuringStartDate.plusMonths(1);
    final LocalDate targetDate = recuringStartDate.plusDays(1);
    // FIRST CREATE INITIAL INVOICE WITH ONE RECURRING ITEM
    final Invoice invoice = new DefaultInvoice(accountId, targetDate, targetDate, Currency.USD);
    final UUID invoiceId = invoice.getId();
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "test product", "test-plan", "test-phase-rec", null, recuringStartDate, recuringEndDate, new BigDecimal("239.00"), new BigDecimal("239.00"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(invoice, context);
    ((ClockMock) clock).addDays(1);
    // SECOND CREATE THE PAYMENT
    final BigDecimal paymentAmount = new BigDecimal("239.00");
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoiceId, clock.getUTCNow(), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), UUID.randomUUID(), context);
    // AND THEN THIRD THE REFUND
    final Map<UUID, BigDecimal> invoiceItemMap = new HashMap<UUID, BigDecimal>();
    invoiceItemMap.put(invoiceItem.getId(), new BigDecimal("239.00"));
    invoiceDao.createRefund(paymentId, UUID.randomUUID(), paymentAmount, true, invoiceItemMap, UUID.randomUUID().toString(), true, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(savedInvoice);
    assertEquals(savedInvoice.getInvoiceItems().size(), 2);
    final List<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(new DefaultInvoice(savedInvoice));
    // NOW COMPUTE A DIFFERENT ITEM TO TRIGGER REPAIR
    final BillingEventSet events = new MockBillingEventSet();
    final SubscriptionBase subscription = getZombieSubscription(subscriptionId);
    final Product product = Mockito.mock(Product.class);
    Mockito.when(product.getName()).thenReturn("product");
    final Plan plan = Mockito.mock(Plan.class);
    Mockito.when(plan.getName()).thenReturn("plan");
    Mockito.when(plan.getProduct()).thenReturn(product);
    Mockito.when(plan.getRecurringBillingMode()).thenReturn(BillingMode.IN_ADVANCE);
    final PlanPhase phase1 = Mockito.mock(PlanPhase.class);
    Mockito.when(phase1.getName()).thenReturn("plan-phase1");
    final BillingEvent event1 = invoiceUtil.createMockBillingEvent(null, subscription, recuringStartDate.toDateTimeAtStartOfDay(), plan, phase1, null, TEN, Currency.USD, BillingPeriod.MONTHLY, 31, BillingMode.IN_ADVANCE, "new-event", 1L, SubscriptionBaseTransitionType.CREATE);
    events.add(event1);
    final InvoiceWithMetadata newInvoiceWithMetadata = generator.generateInvoice(account, events, new AccountInvoices(null, null, invoices), null, targetDate, Currency.USD, null, context);
    final Invoice newInvoice = newInvoiceWithMetadata.getInvoice();
    invoiceUtil.createInvoice(newInvoice, context);
    // VERIFY THAT WE STILL HAVE ONLY 2 ITEMS, MEANING THERE WERE NO REPAIR AND NO CBA GENERATED
    final Invoice firstInvoice = new DefaultInvoice(invoiceDao.getById(invoiceId, context));
    assertNotNull(firstInvoice);
    assertEquals(firstInvoice.getInvoiceItems().size(), 2);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) AccountInvoices(org.killbill.billing.invoice.optimizer.InvoiceOptimizerBase.AccountInvoices) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) ClockMock(org.killbill.clock.ClockMock) MockPlan(org.killbill.billing.catalog.MockPlan) Plan(org.killbill.billing.catalog.api.Plan) InvoiceWithMetadata(org.killbill.billing.invoice.generator.InvoiceWithMetadata) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) MockBillingEventSet(org.killbill.billing.invoice.MockBillingEventSet) BillingEventSet(org.killbill.billing.junction.BillingEventSet) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) MockPlanPhase(org.killbill.billing.catalog.MockPlanPhase) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) Test(org.testng.annotations.Test)

Example 4 with ClockMock

use of org.killbill.clock.ClockMock in project killbill by killbill.

the class TestDateConversion method testNullConversion.

// 
// BASIC Tests to understand how toLocalDate converts different inputs (null, LocalDate, DateTime)
// 
@Test(groups = "fast")
public void testNullConversion() throws AccountApiException {
    final String input = null;
    final LocalDate result = toLocalDate(input);
    Assert.assertNull(result);
    final UUID accountId = setupAccount(DateTimeZone.forOffsetHours(-8));
    ((ClockMock) clock).setTime(new DateTime("2013-08-26T06:50:20Z"));
    final LocalDate result2 = toLocalDateDefaultToday(accountId, input, null);
    Assert.assertTrue(result2.compareTo(new LocalDate(2013, 8, 25)) == 0);
    ((ClockMock) clock).resetDeltaFromReality();
}
Also used : ClockMock(org.killbill.clock.ClockMock) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

ClockMock (org.killbill.clock.ClockMock)4 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 UUID (java.util.UUID)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 DateTime (org.joda.time.DateTime)2 LocalDate (org.joda.time.LocalDate)2 Test (org.testng.annotations.Test)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 MockPlan (org.killbill.billing.catalog.MockPlan)1 MockPlanPhase (org.killbill.billing.catalog.MockPlanPhase)1 Plan (org.killbill.billing.catalog.api.Plan)1 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)1 Product (org.killbill.billing.catalog.api.Product)1 MockBillingEventSet (org.killbill.billing.invoice.MockBillingEventSet)1