Search in sources :

Example 11 with SubscriptionBaseTransition

use of org.killbill.billing.subscription.api.user.SubscriptionBaseTransition in project killbill by killbill.

the class TestPlanAligner method changeSubscription.

private void changeSubscription(final DateTime effectiveChangeDate, final DefaultSubscriptionBase defaultSubscriptionBase, final String previousProductName, final String newProductName, final PhaseType commonPhaseType) throws CatalogApiException {
    final SubscriptionBaseEvent previousEvent = createSubscriptionEvent(defaultSubscriptionBase.getStartDate(), previousProductName, commonPhaseType, ApiEventType.CREATE);
    final SubscriptionBaseEvent event = createSubscriptionEvent(effectiveChangeDate, newProductName, commonPhaseType, ApiEventType.CHANGE);
    final List<SubscriptionBaseEvent> events = new ArrayList<SubscriptionBaseEvent>();
    events.add(previousEvent);
    events.add(event);
    defaultSubscriptionBase.rebuildTransitions(events, catalogService.getFullCatalog(true, true, internalCallContext));
    final List<SubscriptionBaseTransition> newTransitions = defaultSubscriptionBase.getAllTransitions();
    Assert.assertEquals(newTransitions.size(), 2);
    Assert.assertNull(newTransitions.get(0).getPreviousPhase());
    Assert.assertEquals(newTransitions.get(0).getNextPhase(), newTransitions.get(1).getPreviousPhase());
    Assert.assertNotNull(newTransitions.get(1).getNextPhase());
}
Also used : ArrayList(java.util.ArrayList) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 12 with SubscriptionBaseTransition

use of org.killbill.billing.subscription.api.user.SubscriptionBaseTransition in project killbill by killbill.

the class TestDefaultSubscriptionBundleTimeline method testOneEntitlementWithOverduePauseThenCancelImpl.

private void testOneEntitlementWithOverduePauseThenCancelImpl(final boolean regressionFlagForOlderVersionThan_0_17_X) throws CatalogApiException {
    clock.setDay(new LocalDate(2013, 1, 1));
    final DateTimeZone accountTimeZone = DateTimeZone.UTC;
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final String externalKey = "foo";
    final UUID entitlementId = UUID.randomUUID();
    final List<SubscriptionBaseTransition> allTransitions = new ArrayList<SubscriptionBaseTransition>();
    final List<BlockingState> blockingStates = new ArrayList<BlockingState>();
    DateTime effectiveDate = new DateTime(2013, 1, 1, 15, 43, 25, 0, DateTimeZone.UTC);
    final SubscriptionBaseTransition tr1 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CREATE, effectiveDate, clock.getUTCNow(), null, "trial");
    allTransitions.add(tr1);
    if (!regressionFlagForOlderVersionThan_0_17_X) {
        final BlockingState bsCreate = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, false, false, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
        blockingStates.add(bsCreate);
    }
    effectiveDate = effectiveDate.plusDays(30);
    clock.addDays(30);
    final SubscriptionBaseTransition tr2 = createTransition(entitlementId, EventType.PHASE, null, effectiveDate, clock.getUTCNow(), "trial", "phase");
    allTransitions.add(tr2);
    final String overdueService = "overdue-service";
    effectiveDate = effectiveDate.plusDays(12);
    clock.addDays(12);
    final BlockingState bs1 = new DefaultBlockingState(UUID.randomUUID(), accountId, BlockingStateType.ACCOUNT, "ODE1", overdueService, true, false, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
    blockingStates.add(bs1);
    effectiveDate = effectiveDate.plusDays(42);
    clock.addDays(42);
    final BlockingState bs2 = new DefaultBlockingState(UUID.randomUUID(), accountId, BlockingStateType.ACCOUNT, "ODE2", overdueService, true, false, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 1L);
    blockingStates.add(bs2);
    effectiveDate = effectiveDate.plusDays(15);
    clock.addDays(15);
    final BlockingState bs3 = new DefaultBlockingState(UUID.randomUUID(), accountId, BlockingStateType.ACCOUNT, "ODE3", overdueService, true, true, true, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 2L);
    blockingStates.add(bs3);
    effectiveDate = effectiveDate.plusDays(15);
    clock.addDays(15);
    final BlockingState bs4 = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_CANCELLED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 3L);
    blockingStates.add(bs4);
    effectiveDate = effectiveDate.plusDays(1);
    clock.addDays(1);
    final BlockingState bs5 = new DefaultBlockingState(UUID.randomUUID(), accountId, BlockingStateType.ACCOUNT, "ODE4", overdueService, true, true, true, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 4L);
    blockingStates.add(bs5);
    // Note: cancellation event and ODE4 at the same effective date (see https://github.com/killbill/killbill/issues/148)
    final SubscriptionBaseTransition tr3 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CANCEL, effectiveDate, clock.getUTCNow(), "phase", null);
    allTransitions.add(tr3);
    final List<Entitlement> entitlements = new ArrayList<Entitlement>();
    final Entitlement entitlement = createEntitlement(entitlementId, allTransitions, blockingStates);
    entitlements.add(entitlement);
    final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, entitlements, internalCallContext);
    assertEquals(timeline.getAccountId(), accountId);
    assertEquals(timeline.getBundleId(), bundleId);
    assertEquals(timeline.getExternalKey(), externalKey);
    final List<SubscriptionEvent> events = timeline.getSubscriptionEvents();
    assertEquals(events.size(), 10);
    assertEquals(events.get(0).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(1).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(2).getEffectiveDate().compareTo(new LocalDate(tr2.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(3).getEffectiveDate().compareTo(new LocalDate(bs1.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(4).getEffectiveDate().compareTo(new LocalDate(bs2.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(5).getEffectiveDate().compareTo(new LocalDate(bs3.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(6).getEffectiveDate().compareTo(new LocalDate(bs3.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(7).getEffectiveDate().compareTo(new LocalDate(bs4.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(8).getEffectiveDate().compareTo(new LocalDate(tr3.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(9).getEffectiveDate().compareTo(new LocalDate(tr3.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertEquals(events.get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING);
    assertEquals(events.get(2).getSubscriptionEventType(), SubscriptionEventType.PHASE);
    assertEquals(events.get(3).getSubscriptionEventType(), SubscriptionEventType.SERVICE_STATE_CHANGE);
    assertEquals(events.get(4).getSubscriptionEventType(), SubscriptionEventType.SERVICE_STATE_CHANGE);
    assertEquals(events.get(5).getSubscriptionEventType(), SubscriptionEventType.PAUSE_ENTITLEMENT);
    assertEquals(events.get(6).getSubscriptionEventType(), SubscriptionEventType.PAUSE_BILLING);
    assertEquals(events.get(7).getSubscriptionEventType(), SubscriptionEventType.STOP_ENTITLEMENT);
    assertEquals(events.get(8).getSubscriptionEventType(), SubscriptionEventType.SERVICE_STATE_CHANGE);
    assertEquals(events.get(9).getSubscriptionEventType(), SubscriptionEventType.STOP_BILLING);
    assertEquals(events.get(0).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(1).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertEquals(events.get(2).getServiceName(), EntitlementOrderingBase.ENT_BILLING_SERVICE_NAME);
    assertEquals(events.get(3).getServiceName(), overdueService);
    assertEquals(events.get(4).getServiceName(), overdueService);
    assertEquals(events.get(5).getServiceName(), overdueService);
    assertEquals(events.get(6).getServiceName(), overdueService);
    assertEquals(events.get(7).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(8).getServiceName(), overdueService);
    assertEquals(events.get(9).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertNull(events.get(0).getPrevPhase());
    assertNull(events.get(1).getPrevPhase());
    assertEquals(events.get(1).getNextPhase().getName(), "trial");
    assertEquals(events.get(2).getPrevPhase().getName(), "trial");
    assertEquals(events.get(2).getNextPhase().getName(), "phase");
    assertEquals(events.get(3).getPrevPhase().getName(), "phase");
    assertEquals(events.get(3).getNextPhase().getName(), "phase");
    assertEquals(events.get(4).getPrevPhase().getName(), "phase");
    assertEquals(events.get(4).getNextPhase().getName(), "phase");
    assertEquals(events.get(5).getPrevPhase().getName(), "phase");
    assertEquals(events.get(5).getNextPhase().getName(), "phase");
    assertEquals(events.get(6).getPrevPhase().getName(), "phase");
    assertEquals(events.get(6).getNextPhase().getName(), "phase");
    assertEquals(events.get(7).getPrevPhase().getName(), "phase");
    assertNull(events.get(7).getNextPhase());
    assertEquals(events.get(8).getPrevPhase().getName(), "phase");
    assertNull(events.get(8).getNextPhase());
    assertEquals(events.get(9).getPrevPhase().getName(), "phase");
    assertNull(events.get(9).getNextPhase());
}
Also used : ArrayList(java.util.ArrayList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) UUID(java.util.UUID) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Example 13 with SubscriptionBaseTransition

use of org.killbill.billing.subscription.api.user.SubscriptionBaseTransition in project killbill by killbill.

the class TestDefaultSubscriptionBundleTimeline method testOneEntitlementWithBlockingStatesSubscriptionImpl.

private void testOneEntitlementWithBlockingStatesSubscriptionImpl(final boolean regressionFlagForOlderVersionThan_0_17_X) throws CatalogApiException {
    clock.setDay(new LocalDate(2013, 1, 1));
    final DateTimeZone accountTimeZone = DateTimeZone.UTC;
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final String externalKey = "foo";
    final UUID entitlementId = UUID.randomUUID();
    final List<SubscriptionBaseTransition> allTransitions = new ArrayList<SubscriptionBaseTransition>();
    final List<BlockingState> blockingStates = new ArrayList<BlockingState>();
    DateTime effectiveDate = new DateTime(2013, 1, 1, 15, 43, 25, 0, DateTimeZone.UTC);
    final SubscriptionBaseTransition tr1 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CREATE, effectiveDate, clock.getUTCNow(), null, "trial");
    allTransitions.add(tr1);
    if (!regressionFlagForOlderVersionThan_0_17_X) {
        final BlockingState bsCreate = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, false, false, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
        blockingStates.add(bsCreate);
    }
    effectiveDate = effectiveDate.plusDays(30);
    clock.addDays(30);
    final SubscriptionBaseTransition tr2 = createTransition(entitlementId, EventType.PHASE, null, effectiveDate, clock.getUTCNow(), "trial", "phase");
    allTransitions.add(tr2);
    effectiveDate = effectiveDate.plusDays(5);
    clock.addDays(5);
    final BlockingState bs1 = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_BLOCKED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
    blockingStates.add(bs1);
    effectiveDate = effectiveDate.plusDays(15);
    clock.addDays(15);
    final SubscriptionBaseTransition tr3 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CANCEL, effectiveDate, clock.getUTCNow(), "phase", null);
    allTransitions.add(tr3);
    final BlockingState bs2 = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_CANCELLED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 1L);
    blockingStates.add(bs2);
    final List<Entitlement> entitlements = new ArrayList<Entitlement>();
    final Entitlement entitlement = createEntitlement(entitlementId, allTransitions, blockingStates);
    entitlements.add(entitlement);
    final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, entitlements, internalCallContext);
    final List<SubscriptionEvent> events = timeline.getSubscriptionEvents();
    assertEquals(events.size(), 6);
    assertEquals(events.get(0).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(1).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(2).getEffectiveDate().compareTo(new LocalDate(tr2.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(3).getEffectiveDate().compareTo(new LocalDate(bs1.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(4).getEffectiveDate().compareTo(new LocalDate(tr3.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(5).getEffectiveDate().compareTo(new LocalDate(bs2.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertEquals(events.get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING);
    assertEquals(events.get(2).getSubscriptionEventType(), SubscriptionEventType.PHASE);
    assertEquals(events.get(3).getSubscriptionEventType(), SubscriptionEventType.PAUSE_ENTITLEMENT);
    assertEquals(events.get(4).getSubscriptionEventType(), SubscriptionEventType.STOP_ENTITLEMENT);
    assertEquals(events.get(5).getSubscriptionEventType(), SubscriptionEventType.STOP_BILLING);
    assertEquals(events.get(0).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(1).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertEquals(events.get(2).getServiceName(), EntitlementOrderingBase.ENT_BILLING_SERVICE_NAME);
    assertEquals(events.get(3).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(4).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(5).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertNull(events.get(0).getPrevPhase());
    assertNull(events.get(1).getPrevPhase());
    assertEquals(events.get(1).getNextPhase().getName(), "trial");
    assertEquals(events.get(2).getPrevPhase().getName(), "trial");
    assertEquals(events.get(2).getNextPhase().getName(), "phase");
    assertEquals(events.get(3).getPrevPhase().getName(), "phase");
    assertEquals(events.get(3).getNextPhase().getName(), "phase");
    assertEquals(events.get(4).getPrevPhase().getName(), "phase");
    assertNull(events.get(4).getNextPhase());
    assertEquals(events.get(5).getPrevPhase().getName(), "phase");
    assertNull(events.get(5).getNextPhase());
}
Also used : ArrayList(java.util.ArrayList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) UUID(java.util.UUID) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Example 14 with SubscriptionBaseTransition

use of org.killbill.billing.subscription.api.user.SubscriptionBaseTransition in project killbill by killbill.

the class TestDefaultSubscriptionBundleTimeline method testRemoveOverlappingBlockingStatesImpl.

public void testRemoveOverlappingBlockingStatesImpl(final boolean regressionFlagForOlderVersionThan_0_17_X) throws CatalogApiException {
    clock.setDay(new LocalDate(2013, 1, 1));
    final DateTimeZone accountTimeZone = DateTimeZone.UTC;
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final String externalKey = "foo";
    final UUID entitlementId = UUID.randomUUID();
    final List<SubscriptionBaseTransition> allTransitions = new ArrayList<SubscriptionBaseTransition>();
    final List<BlockingState> blockingStates = new ArrayList<BlockingState>();
    DateTime effectiveDate = new DateTime(2013, 1, 1, 15, 43, 25, 0, DateTimeZone.UTC);
    final SubscriptionBaseTransition tr1 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CREATE, effectiveDate, clock.getUTCNow(), null, "trial");
    allTransitions.add(tr1);
    if (!regressionFlagForOlderVersionThan_0_17_X) {
        final BlockingState bsCreate = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, false, false, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
        blockingStates.add(bsCreate);
    }
    // Overlapping ENT_STATE_BLOCKED - should merge
    effectiveDate = effectiveDate.plusDays(5);
    clock.addDays(5);
    final BlockingState bs1 = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_BLOCKED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
    blockingStates.add(bs1);
    effectiveDate = effectiveDate.plusDays(1);
    clock.addDays(1);
    final BlockingState bs2 = new DefaultBlockingState(UUID.randomUUID(), bundleId, BlockingStateType.SUBSCRIPTION_BUNDLE, DefaultEntitlementApi.ENT_STATE_BLOCKED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 1L);
    blockingStates.add(bs2);
    // Overlapping ENT_STATE_CANCELLED - should merge
    effectiveDate = effectiveDate.plusDays(1);
    clock.addDays(1);
    final BlockingState bs3 = new DefaultBlockingState(UUID.randomUUID(), accountId, BlockingStateType.ACCOUNT, DefaultEntitlementApi.ENT_STATE_CANCELLED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 2L);
    blockingStates.add(bs3);
    final BlockingState bs4 = new DefaultBlockingState(UUID.randomUUID(), bundleId, BlockingStateType.SUBSCRIPTION_BUNDLE, DefaultEntitlementApi.ENT_STATE_CANCELLED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 3L);
    blockingStates.add(bs4);
    final List<Entitlement> entitlements = new ArrayList<Entitlement>();
    final Entitlement entitlement = createEntitlement(entitlementId, allTransitions, blockingStates);
    entitlements.add(entitlement);
    final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, entitlements, internalCallContext);
    final List<SubscriptionEvent> events = timeline.getSubscriptionEvents();
    assertEquals(events.size(), 4);
    assertEquals(events.get(0).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(1).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(2).getEffectiveDate().compareTo(new LocalDate(bs1.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(3).getEffectiveDate().compareTo(new LocalDate(bs3.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertEquals(events.get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING);
    assertEquals(events.get(2).getSubscriptionEventType(), SubscriptionEventType.PAUSE_ENTITLEMENT);
    assertEquals(events.get(3).getSubscriptionEventType(), SubscriptionEventType.STOP_ENTITLEMENT);
    assertEquals(events.get(0).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(1).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertEquals(events.get(2).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(3).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertNull(events.get(0).getPrevPhase());
    assertNull(events.get(1).getPrevPhase());
    assertEquals(events.get(1).getNextPhase().getName(), "trial");
    assertEquals(events.get(2).getPrevPhase().getName(), "trial");
    assertEquals(events.get(2).getNextPhase().getName(), "trial");
    assertEquals(events.get(3).getPrevPhase().getName(), "trial");
    assertNull(events.get(3).getNextPhase());
}
Also used : ArrayList(java.util.ArrayList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) UUID(java.util.UUID) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Example 15 with SubscriptionBaseTransition

use of org.killbill.billing.subscription.api.user.SubscriptionBaseTransition in project killbill by killbill.

the class TestDefaultSubscriptionBundleTimeline method testWithOverdueOfflineImpl.

private void testWithOverdueOfflineImpl(final boolean regressionFlagForOlderVersionThan_0_17_X) throws CatalogApiException {
    clock.setDay(new LocalDate(2013, 1, 1));
    final DateTimeZone accountTimeZone = DateTimeZone.UTC;
    final UUID accountId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final String externalKey = "foo";
    final UUID entitlementId = UUID.randomUUID();
    final List<SubscriptionBaseTransition> allTransitions = new ArrayList<SubscriptionBaseTransition>();
    final List<BlockingState> blockingStates = new ArrayList<BlockingState>();
    DateTime effectiveDate = new DateTime(2013, 1, 1, 23, 11, 8, 0, DateTimeZone.UTC);
    final SubscriptionBaseTransition tr1 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CREATE, effectiveDate, clock.getUTCNow(), null, "trial");
    allTransitions.add(tr1);
    if (!regressionFlagForOlderVersionThan_0_17_X) {
        final BlockingState bsCreate = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, false, false, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
        blockingStates.add(bsCreate);
    }
    effectiveDate = effectiveDate.plusDays(30);
    clock.addDays(30);
    final SubscriptionBaseTransition tr2 = createTransition(entitlementId, EventType.PHASE, null, effectiveDate, clock.getUTCNow(), "trial", "phase");
    allTransitions.add(tr2);
    // 2013-03-12
    effectiveDate = effectiveDate.plusDays(40);
    clock.addDays(40);
    final SubscriptionBaseTransition tr3 = createTransition(entitlementId, EventType.API_USER, ApiEventType.CANCEL, effectiveDate, clock.getUTCNow(), "phase", null);
    allTransitions.add(tr3);
    // Verify the timeline without the blocking state events
    final ImmutableList<Entitlement> entitlementsWithoutBlockingStates = ImmutableList.<Entitlement>of(createEntitlement(entitlementId, allTransitions, blockingStates));
    final List<SubscriptionEvent> eventsWithoutBlockingStates = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, entitlementsWithoutBlockingStates, internalCallContext).getSubscriptionEvents();
    assertEquals(eventsWithoutBlockingStates.size(), 4);
    assertEquals(eventsWithoutBlockingStates.get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertEquals(eventsWithoutBlockingStates.get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING);
    assertEquals(eventsWithoutBlockingStates.get(2).getSubscriptionEventType(), SubscriptionEventType.PHASE);
    assertEquals(eventsWithoutBlockingStates.get(3).getSubscriptionEventType(), SubscriptionEventType.STOP_BILLING);
    final String service = "overdue-service";
    final BlockingState bs1 = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.ACCOUNT, "OFFLINE", service, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 0L);
    blockingStates.add(bs1);
    // Verify the timeline with the overdue event blocking the entitlement
    final ImmutableList<Entitlement> entitlementsWithOverdueEvent = ImmutableList.<Entitlement>of(createEntitlement(entitlementId, allTransitions, blockingStates));
    final List<SubscriptionEvent> eventsWithOverdueEvent = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, entitlementsWithOverdueEvent, internalCallContext).getSubscriptionEvents();
    assertEquals(eventsWithOverdueEvent.size(), 5);
    assertEquals(eventsWithOverdueEvent.get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertEquals(eventsWithOverdueEvent.get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING);
    assertEquals(eventsWithOverdueEvent.get(2).getSubscriptionEventType(), SubscriptionEventType.PHASE);
    assertEquals(eventsWithOverdueEvent.get(3).getSubscriptionEventType(), SubscriptionEventType.PAUSE_ENTITLEMENT);
    assertEquals(eventsWithOverdueEvent.get(4).getSubscriptionEventType(), SubscriptionEventType.STOP_BILLING);
    final BlockingState bs2 = new DefaultBlockingState(UUID.randomUUID(), entitlementId, BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_CANCELLED, DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate, clock.getUTCNow(), clock.getUTCNow(), 1L);
    blockingStates.add(bs2);
    final List<Entitlement> entitlements = new ArrayList<Entitlement>();
    final Entitlement entitlement = createEntitlement(entitlementId, allTransitions, blockingStates);
    entitlements.add(entitlement);
    // Verify the timeline with both the overdue event and the entitlement cancel event
    final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, externalKey, entitlements, internalCallContext);
    assertEquals(timeline.getAccountId(), accountId);
    assertEquals(timeline.getBundleId(), bundleId);
    assertEquals(timeline.getExternalKey(), externalKey);
    final List<SubscriptionEvent> events = timeline.getSubscriptionEvents();
    assertEquals(events.size(), 6);
    assertEquals(events.get(0).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(1).getEffectiveDate().compareTo(new LocalDate(tr1.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(2).getEffectiveDate().compareTo(new LocalDate(tr2.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(3).getEffectiveDate().compareTo(new LocalDate(bs1.getEffectiveDate(), accountTimeZone)), 0);
    assertEquals(events.get(4).getEffectiveDate().compareTo(new LocalDate(tr3.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(5).getEffectiveDate().compareTo(new LocalDate(tr3.getEffectiveTransitionTime(), accountTimeZone)), 0);
    assertEquals(events.get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT);
    assertEquals(events.get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING);
    assertEquals(events.get(2).getSubscriptionEventType(), SubscriptionEventType.PHASE);
    assertEquals(events.get(3).getSubscriptionEventType(), SubscriptionEventType.PAUSE_ENTITLEMENT);
    assertEquals(events.get(4).getSubscriptionEventType(), SubscriptionEventType.STOP_ENTITLEMENT);
    assertEquals(events.get(5).getSubscriptionEventType(), SubscriptionEventType.STOP_BILLING);
    assertEquals(events.get(0).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(1).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertEquals(events.get(2).getServiceName(), EntitlementOrderingBase.ENT_BILLING_SERVICE_NAME);
    assertEquals(events.get(3).getServiceName(), service);
    assertEquals(events.get(4).getServiceName(), DefaultEntitlementService.ENTITLEMENT_SERVICE_NAME);
    assertEquals(events.get(5).getServiceName(), EntitlementOrderingBase.BILLING_SERVICE_NAME);
    assertNull(events.get(0).getPrevPhase());
    assertNull(events.get(1).getPrevPhase());
    assertEquals(events.get(1).getNextPhase().getName(), "trial");
    assertEquals(events.get(2).getPrevPhase().getName(), "trial");
    assertEquals(events.get(2).getNextPhase().getName(), "phase");
    assertEquals(events.get(3).getPrevPhase().getName(), "phase");
    assertEquals(events.get(3).getNextPhase().getName(), "phase");
    assertEquals(events.get(4).getPrevPhase().getName(), "phase");
    assertNull(events.get(4).getNextPhase());
    assertEquals(events.get(5).getPrevPhase().getName(), "phase");
    assertNull(events.get(5).getNextPhase());
}
Also used : ArrayList(java.util.ArrayList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) LocalDate(org.joda.time.LocalDate) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) UUID(java.util.UUID) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Aggregations

SubscriptionBaseTransition (org.killbill.billing.subscription.api.user.SubscriptionBaseTransition)17 ArrayList (java.util.ArrayList)12 UUID (java.util.UUID)12 DateTime (org.joda.time.DateTime)12 LocalDate (org.joda.time.LocalDate)12 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)12 DateTimeZone (org.joda.time.DateTimeZone)10 Product (org.killbill.billing.catalog.api.Product)2 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)2 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Account (org.killbill.billing.account.api.Account)1 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)1 PhaseType (org.killbill.billing.catalog.api.PhaseType)1 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)1 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)1 ProductCategory (org.killbill.billing.catalog.api.ProductCategory)1 BlockingState (org.killbill.billing.entitlement.api.BlockingState)1