Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2013 Ning, Inc.
   *
   * Ning licenses this file to you under the Apache License, version 2.0
   * (the "License"); you may not use this file except in compliance with the
   * License.  You may obtain a copy of the License at:
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
 
 package com.ning.billing.subscription.api.timeline;
 
 import java.util.List;
 import java.util.UUID;
 
 
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 
     @Test(groups = "slow")
     public void testFetchBundleRepair() throws Exception {
         final String baseProduct = "Shotgun";
         final BillingPeriod baseTerm = .;
         final String basePriceList = .;
 
         // CREATE BP
         final SubscriptionBase baseSubscription = .createSubscription(baseProductbaseTermbasePriceList);
 
         final String aoProduct = "Telescopic-Scope";
         final BillingPeriod aoTerm = .;
         final String aoPriceList = .;
 
         final DefaultSubscriptionBase aoSubscription = .createSubscription(aoProductaoTermaoPriceList);
 
         final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
         final List<SubscriptionBaseTimelinesubscriptionRepair = bundleRepair.getSubscriptions();
         assertEquals(subscriptionRepair.size(), 2);
 
         for (final SubscriptionBaseTimeline cur : subscriptionRepair) {
             assertNull(cur.getDeletedEvents());
             assertNull(cur.getNewEvents());
 
             final List<ExistingEventevents = cur.getExistingEvents();
             assertEquals(events.size(), 2);
             .sortExistingEvent(events);
 
             final boolean isBP = cur.getId().equals(baseSubscription.getId());
             if (isBP) {
                 assertEquals(cur.getId(), baseSubscription.getId());
 
                 assertEquals(events.get(0).getPlanPhaseSpecifier().getProductName(), baseProduct);
                 assertEquals(events.get(0).getPlanPhaseSpecifier().getPhaseType(), .);
                 assertEquals(events.get(0).getPlanPhaseSpecifier().getProductCategory(), .);
                 assertEquals(events.get(0).getPlanPhaseSpecifier().getPriceListName(), basePriceList);
                 assertEquals(events.get(0).getPlanPhaseSpecifier().getBillingPeriod(), .);
 
                 assertEquals(events.get(1).getPlanPhaseSpecifier().getProductName(), baseProduct);
                 assertEquals(events.get(1).getPlanPhaseSpecifier().getPhaseType(), .);
                 assertEquals(events.get(1).getPlanPhaseSpecifier().getProductCategory(), .);
                 assertEquals(events.get(1).getPlanPhaseSpecifier().getPriceListName(), basePriceList);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getBillingPeriod(), baseTerm);
            } else {
                assertEquals(cur.getId(), aoSubscription.getId());
                assertEquals(events.get(0).getPlanPhaseSpecifier().getProductName(), aoProduct);
                assertEquals(events.get(0).getPlanPhaseSpecifier().getPhaseType(), .);
                assertEquals(events.get(0).getPlanPhaseSpecifier().getProductCategory(), .);
                assertEquals(events.get(0).getPlanPhaseSpecifier().getPriceListName(), aoPriceList);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getBillingPeriod(), aoTerm);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getProductName(), aoProduct);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getPhaseType(), .);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getProductCategory(), .);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getPriceListName(), aoPriceList);
                assertEquals(events.get(1).getPlanPhaseSpecifier().getBillingPeriod(), aoTerm);
            }
        }
        assertListenerStatus();
    }
    @Test(groups = "slow")
    public void testBPRepairWithCancellationOnstart() throws Exception {
        final String baseProduct = "Shotgun";
        final DateTime startDate = .getUTCNow();
        // CREATE BP
        final SubscriptionBase baseSubscription = .createSubscription(baseProduct..startDate);
        // Stays in trial-- for instance
        final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(10));
        final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
        .sortEventsOnBundle(bundleRepair);
        final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
        des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
        final NewEvent ne = .createNewEvent(.baseSubscription.getStartDate(), null);
        final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(baseSubscription.getId(), des, Collections.singletonList(ne));
        // FIRST ISSUE DRY RUN
        final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
        boolean dryRun = true;
        final BundleBaseTimeline dryRunBundleRepair = .repairBundle(bRepairdryRun);
        .sortEventsOnBundle(dryRunBundleRepair);
        List<SubscriptionBaseTimelinesubscriptionRepair = dryRunBundleRepair.getSubscriptions();
        assertEquals(subscriptionRepair.size(), 1);
        SubscriptionBaseTimeline cur = subscriptionRepair.get(0);
        int index = 0;
        final List<ExistingEventevents = subscriptionRepair.get(0).getExistingEvents();
        assertEquals(events.size(), 2);
        final List<ExistingEventexpected = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
                                                              ...baseSubscription.getStartDate()));
                                                              ...baseSubscription.getStartDate()));
        for (final ExistingEvent e : expected) {
            .validateExistingEventForAssertion(eevents.get(index++));
        }
        final DefaultSubscriptionBase dryRunBaseSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(dryRunBaseSubscription.getActiveVersion(), .);
        assertEquals(dryRunBaseSubscription.getBundleId(), .getId());
        assertEquals(dryRunBaseSubscription.getStartDate(), baseSubscription.getStartDate());
        final Plan currentPlan = dryRunBaseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), baseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        final PlanPhase currentPhase = dryRunBaseSubscription.getCurrentPhase();
        assertNotNull(currentPhase);
        assertEquals(currentPhase.getPhaseType(), .);
        // SECOND RE-ISSUE CALL-- NON DRY RUN
        dryRun = false;
        final BundleBaseTimeline realRunBundleRepair = .repairBundle(bRepairdryRun);
        assertListenerStatus();
        subscriptionRepair = realRunBundleRepair.getSubscriptions();
        assertEquals(subscriptionRepair.size(), 1);
        cur = subscriptionRepair.get(0);
        assertEquals(cur.getId(), baseSubscription.getId());
        index = 0;
        for (final ExistingEvent e : expected) {
            .validateExistingEventForAssertion(eevents.get(index++));
        }
        final DefaultSubscriptionBase realRunBaseSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(realRunBaseSubscription.getAllTransitions().size(), 2);
        assertEquals(realRunBaseSubscription.getActiveVersion(), . + 1);
        assertEquals(realRunBaseSubscription.getBundleId(), .getId());
        assertEquals(realRunBaseSubscription.getStartDate(), startDate);
        assertEquals(realRunBaseSubscription.getState(), .);
        assertListenerStatus();
    }
    @Test(groups = "slow")
    public void testBPRepairReplaceCreateBeforeTrial() throws Exception {
        final String baseProduct = "Shotgun";
        final String newBaseProduct = "Assault-Rifle";
        final DateTime startDate = .getUTCNow();
        final int clockShift = -1;
        final DateTime restartDate = startDate.plusDays(clockShift).minusDays(1);
        final LinkedList<ExistingEventexpected = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
                                                              ...restartDate));
                                                              ...restartDate.plusDays(30)));
        testBPRepairCreate(truestartDateclockShiftbaseProductnewBaseProductexpected);
        assertListenerStatus();
    }
    @Test(groups = "slow")
    public void testBPRepairReplaceCreateInTrial() throws Exception {
        final String baseProduct = "Shotgun";
        final String newBaseProduct = "Assault-Rifle";
        final DateTime startDate = .getUTCNow();
        final int clockShift = 10;
        final DateTime restartDate = startDate.plusDays(clockShift).minusDays(1);
        final LinkedList<ExistingEventexpected = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
                                                              ...restartDate));
                                                              ...restartDate.plusDays(30)));
        final UUID baseSubscriptionId = testBPRepairCreate(truestartDateclockShiftbaseProductnewBaseProductexpected);
        final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(32));
        assertListenerStatus();
        // CHECK WHAT"S GOING ON AFTER WE MOVE CLOCK-- FUTURE MOTIFICATION SHOULD KICK IN
        assertEquals(subscription.getActiveVersion(), . + 1);
        assertEquals(subscription.getBundleId(), .getId());
        assertEquals(subscription.getStartDate(), restartDate);
        assertEquals(subscription.getBundleStartDate(), restartDate);
        final Plan currentPlan = subscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), newBaseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        final PlanPhase currentPhase = subscription.getCurrentPhase();
        assertNotNull(currentPhase);
        assertEquals(currentPhase.getPhaseType(), .);
        assertListenerStatus();
    }
    @Test(groups = "slow")
    public void testBPRepairReplaceCreateAfterTrial() throws Exception {
        final String baseProduct = "Shotgun";
        final String newBaseProduct = "Assault-Rifle";
        final DateTime startDate = .getUTCNow();
        final int clockShift = 40;
        final DateTime restartDate = startDate.plusDays(clockShift).minusDays(1);
        final LinkedList<ExistingEventexpected = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
                                                              ...restartDate));
                                                              ...restartDate.plusDays(30)));
        testBPRepairCreate(falsestartDateclockShiftbaseProductnewBaseProductexpected);
        assertListenerStatus();
    }
    private UUID testBPRepairCreate(final boolean inTrialfinal DateTime startDatefinal int clockShift,
                                    final String baseProductfinal String newBaseProductfinal List<ExistingEventexpectedEventsthrows Exception {
        // CREATE BP
        final SubscriptionBase baseSubscription = .createSubscription(baseProduct..startDate);
        // MOVE CLOCK
        if (clockShift > 0) {
            if (!inTrial) {
                .pushExpectedEvent(.);
            }
            final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(clockShift));
            .addDeltaFromReality(it.toDurationMillis());
            if (!inTrial) {
                assertListenerStatus();
            }
        }
        final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
        .sortEventsOnBundle(bundleRepair);
        final DateTime newCreateTime = baseSubscription.getStartDate().plusDays(clockShift - 1);
        final NewEvent ne = .createNewEvent(.newCreateTimespec);
        final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
        des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(0).getEventId()));
        des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
        final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(baseSubscription.getId(), des, Collections.singletonList(ne));
        // FIRST ISSUE DRY RUN
        final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
        boolean dryRun = true;
        final BundleBaseTimeline dryRunBundleRepair = .repairBundle(bRepairdryRun);
        List<SubscriptionBaseTimelinesubscriptionRepair = dryRunBundleRepair.getSubscriptions();
        assertEquals(subscriptionRepair.size(), 1);
        SubscriptionBaseTimeline cur = subscriptionRepair.get(0);
        assertEquals(cur.getId(), baseSubscription.getId());
        List<ExistingEventevents = cur.getExistingEvents();
        assertEquals(expectedEvents.size(), events.size());
        int index = 0;
        for (final ExistingEvent e : expectedEvents) {
            .validateExistingEventForAssertion(eevents.get(index++));
        }
        final DefaultSubscriptionBase dryRunBaseSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(dryRunBaseSubscription.getActiveVersion(), .);
        assertEquals(dryRunBaseSubscription.getBundleId(), .getId());
        assertTrue(dryRunBaseSubscription.getStartDate().compareTo(baseSubscription.getStartDate()) == 0);
        Plan currentPlan = dryRunBaseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), baseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        PlanPhase currentPhase = dryRunBaseSubscription.getCurrentPhase();
        assertNotNull(currentPhase);
        if (inTrial) {
            assertEquals(currentPhase.getPhaseType(), .);
        } else {
            assertEquals(currentPhase.getPhaseType(), .);
        }
        // SECOND RE-ISSUE CALL-- NON DRY RUN
        dryRun = false;
        final BundleBaseTimeline realRunBundleRepair = .repairBundle(bRepairdryRun);
        assertListenerStatus();
        subscriptionRepair = realRunBundleRepair.getSubscriptions();
        assertEquals(subscriptionRepair.size(), 1);
        cur = subscriptionRepair.get(0);
        assertEquals(cur.getId(), baseSubscription.getId());
        events = cur.getExistingEvents();
        for (final ExistingEvent e : events) {
            .info(String.format("%s, %s, %s, %s"e.getSubscriptionTransitionType(), e.getEffectiveDate(), e.getPlanPhaseSpecifier().getProductName(), e.getPlanPhaseSpecifier().getPhaseType()));
        }
        assertEquals(events.size(), expectedEvents.size());
        index = 0;
        for (final ExistingEvent e : expectedEvents) {
            .validateExistingEventForAssertion(eevents.get(index++));
        }
        final DefaultSubscriptionBase realRunBaseSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(realRunBaseSubscription.getAllTransitions().size(), 2);
        assertEquals(realRunBaseSubscription.getActiveVersion(), . + 1);
        assertEquals(realRunBaseSubscription.getBundleId(), .getId());
        assertEquals(realRunBaseSubscription.getStartDate(), newCreateTime);
        currentPlan = realRunBaseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), newBaseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        currentPhase = realRunBaseSubscription.getCurrentPhase();
        assertNotNull(currentPhase);
        assertEquals(currentPhase.getPhaseType(), .);
        return baseSubscription.getId();
    }
    @Test(groups = "slow")
    public void testBPRepairAddChangeInTrial() throws Exception {
        final String baseProduct = "Shotgun";
        final String newBaseProduct = "Assault-Rifle";
        final DateTime startDate = .getUTCNow();
        final int clockShift = 10;
        final DateTime changeDate = startDate.plusDays(clockShift).minusDays(1);
        final LinkedList<ExistingEventexpected = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
                                                              ...startDate));
                                                              ...changeDate));
                                                              ...startDate.plusDays(30)));
        final UUID baseSubscriptionId = testBPRepairAddChange(truestartDateclockShiftbaseProductnewBaseProductexpected, 3);
        // CHECK WHAT"S GOING ON AFTER WE MOVE CLOCK-- FUTURE MOTIFICATION SHOULD KICK IN
        final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(32));
        assertListenerStatus();
        assertEquals(subscription.getActiveVersion(), . + 1);
        assertEquals(subscription.getBundleId(), .getId());
        assertEquals(subscription.getStartDate(), startDate);
        assertEquals(subscription.getBundleStartDate(), startDate);
        final Plan currentPlan = subscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), newBaseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        final PlanPhase currentPhase = subscription.getCurrentPhase();
        assertNotNull(currentPhase);
        assertEquals(currentPhase.getPhaseType(), .);
        assertListenerStatus();
    }
    @Test(groups = "slow")
    public void testBPRepairAddChangeAfterTrial() throws Exception {
        final String baseProduct = "Shotgun";
        final String newBaseProduct = "Assault-Rifle";
        final DateTime startDate = .getUTCNow();
        final int clockShift = 40;
        final DateTime changeDate = startDate.plusDays(clockShift).minusDays(1);
        final LinkedList<ExistingEventexpected = new LinkedList<SubscriptionBaseTimeline.ExistingEvent>();
                                                              ...startDate));
                                                              ...startDate.plusDays(30)));
                                                              ...changeDate));
        testBPRepairAddChange(falsestartDateclockShiftbaseProductnewBaseProductexpected, 3);
        assertListenerStatus();
    }
    private UUID testBPRepairAddChange(final boolean inTrialfinal DateTime startDatefinal int clockShift,
                                       final String baseProductfinal String newBaseProductfinal List<ExistingEventexpectedEventsfinal int expectedTransitionsthrows Exception {
        // CREATE BP
        final SubscriptionBase baseSubscription = .createSubscription(baseProduct..startDate);
        // MOVE CLOCK
        if (!inTrial) {
        }
        final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(clockShift));
        if (!inTrial) {
            assertListenerStatus();
        }
        final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
        .sortEventsOnBundle(bundleRepair);
        final DateTime changeTime = baseSubscription.getStartDate().plusDays(clockShift - 1);
        final NewEvent ne = .createNewEvent(.changeTimespec);
        final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
        if (inTrial) {
            des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
        }
        final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(baseSubscription.getId(), des, Collections.singletonList(ne));
        // FIRST ISSUE DRY RUN
        final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
        boolean dryRun = true;
        final BundleBaseTimeline dryRunBundleRepair = .repairBundle(bRepairdryRun);
        List<SubscriptionBaseTimelinesubscriptionRepair = dryRunBundleRepair.getSubscriptions();
        assertEquals(subscriptionRepair.size(), 1);
        SubscriptionBaseTimeline cur = subscriptionRepair.get(0);
        assertEquals(cur.getId(), baseSubscription.getId());
        List<ExistingEventevents = cur.getExistingEvents();
        assertEquals(expectedEvents.size(), events.size());
        int index = 0;
        for (final ExistingEvent e : expectedEvents) {
            .validateExistingEventForAssertion(eevents.get(index++));
        }
        final DefaultSubscriptionBase dryRunBaseSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(dryRunBaseSubscription.getActiveVersion(), .);
        assertEquals(dryRunBaseSubscription.getBundleId(), .getId());
        assertEquals(dryRunBaseSubscription.getStartDate(), baseSubscription.getStartDate());
        Plan currentPlan = dryRunBaseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), baseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        PlanPhase currentPhase = dryRunBaseSubscription.getCurrentPhase();
        assertNotNull(currentPhase);
        if (inTrial) {
            assertEquals(currentPhase.getPhaseType(), .);
        } else {
            assertEquals(currentPhase.getPhaseType(), .);
        }
        // SECOND RE-ISSUE CALL-- NON DRY RUN
        dryRun = false;
        final BundleBaseTimeline realRunBundleRepair = .repairBundle(bRepairdryRun);
        assertListenerStatus();
        subscriptionRepair = realRunBundleRepair.getSubscriptions();
        assertEquals(subscriptionRepair.size(), 1);
        cur = subscriptionRepair.get(0);
        assertEquals(cur.getId(), baseSubscription.getId());
        events = cur.getExistingEvents();
        assertEquals(expectedEvents.size(), events.size());
        index = 0;
        for (final ExistingEvent e : expectedEvents) {
            .validateExistingEventForAssertion(eevents.get(index++));
        }
        final DefaultSubscriptionBase realRunBaseSubscription = (DefaultSubscriptionBase.getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(realRunBaseSubscription.getAllTransitions().size(), expectedTransitions);
        assertEquals(realRunBaseSubscription.getActiveVersion(), . + 1);
        assertEquals(realRunBaseSubscription.getBundleId(), .getId());
        assertEquals(realRunBaseSubscription.getStartDate(), baseSubscription.getStartDate());
        currentPlan = realRunBaseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), newBaseProduct);
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        currentPhase = realRunBaseSubscription.getCurrentPhase();
        assertNotNull(currentPhase);
        if (inTrial) {
            assertEquals(currentPhase.getPhaseType(), .);
        } else {
            assertEquals(currentPhase.getPhaseType(), .);
        }
        return baseSubscription.getId();
    }
    @Test(groups = "slow")
    public void testRepairWithFutureCancelEvent() throws Exception {
        final DateTime startDate = .getUTCNow();
        // CREATE BP
        SubscriptionBase baseSubscription = .createSubscription("Shotgun"..startDate);
        // MOVE CLOCK -- OUT OF TRIAL
        final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(35));
        assertListenerStatus();
        // SET CTD to BASE SUBSCRIPTION SP CANCEL OCCURS EOT
        final DateTime newChargedThroughDate = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
        .setChargedThroughDate(baseSubscription.getId(), newChargedThroughDate);
        baseSubscription = .getSubscriptionFromId(baseSubscription.getId(), );
        baseSubscription.changePlan("Pistol"..);
        // CHECK CHANGE DID NOT OCCUR YET
        Plan currentPlan = baseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), "Shotgun");
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        final DateTime repairTime = .getUTCNow().minusDays(1);
        final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
        .sortEventsOnBundle(bundleRepair);
        final NewEvent ne = .createNewEvent(.repairTimespec);
        final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
        des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(2).getEventId()));
        final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(baseSubscription.getId(), des, Collections.singletonList(ne));
        // SKIP DRY RUN AND DO REPAIR...
        final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
        final boolean dryRun = false;
        .repairBundle(bRepairdryRun);
        assertListenerStatus();
        baseSubscription = .getSubscriptionFromId(baseSubscription.getId(), );
        assertEquals(baseSubscription.getBundleId(), .getId());
        assertEquals(baseSubscription.getStartDate(), baseSubscription.getStartDate());
        currentPlan = baseSubscription.getCurrentPlan();
        assertNotNull(currentPlan);
        assertEquals(currentPlan.getProduct().getName(), "Assault-Rifle");
        assertEquals(currentPlan.getProduct().getCategory(), .);
        assertEquals(currentPlan.getBillingPeriod(), .);
        final PlanPhase currentPhase = baseSubscription.getCurrentPhase();
        assertNotNull(currentPhase);
        assertEquals(currentPhase.getPhaseType(), .);
        assertListenerStatus();
    }
    // Needs real SQL backend to be tested properly
    @Test(groups = "slow")
    public void testENT_REPAIR_VIEW_CHANGED_newEvent() throws Exception {
        final TestWithException test = new TestWithException();
        final DateTime startDate = .getUTCNow();
        final SubscriptionBase baseSubscription = .createSubscription("Shotgun"..startDate);
        test.withException(new TestWithExceptionCallback() {
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                final NewEvent ne = .createNewEvent(.baseSubscription.getStartDate().plusDays(10), spec);
                final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
                des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(0).getEventId()));
                des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
                final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(baseSubscription.getId(), des, Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                .pushExpectedEvent(.);
                final DateTime changeTime = .getUTCNow();
                baseSubscription.changePlanWithDate("Assault-Rifle"..changeTime);
                assertListenerStatus();
                .repairBundle(bRepairtrue);
                assertListenerStatus();
            }
    }
    @Test(groups = "slow")
    public void testENT_REPAIR_VIEW_CHANGED_ctd() throws Exception {
        final TestWithException test = new TestWithException();
        final DateTime startDate = .getUTCNow();
        final SubscriptionBase baseSubscription = .createSubscription("Shotgun"..startDate);
        test.withException(new TestWithExceptionCallback() {
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                final NewEvent ne = .createNewEvent(.baseSubscription.getStartDate().plusDays(10), spec);
                final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
                des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(0).getEventId()));
                des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
                final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(baseSubscription.getId(), des, Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                final DateTime newChargedThroughDate = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
                // Move clock at least a sec to make sure the last_sys_update from bundle is different-- and therefore generates a different viewId
                .setDeltaFromReality(1000);
                .setChargedThroughDate(baseSubscription.getId(), newChargedThroughDate);
                .getSubscriptionFromId(baseSubscription.getId(), );
                .repairBundle(bRepairtrue);
                assertListenerStatus();
            }
    }