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;
 
 public class TestRepairWithError extends SubscriptionTestSuiteNoDB {
 
     private static final String baseProduct = "Shotgun";
     private TestWithException test;
 
     @Override
     @BeforeMethod(groups = "fast")
     public void beforeMethod() throws Exception {
         super.beforeMethod();
          = new TestWithException();
         final DateTime startDate = .getUTCNow();
     }
 
     @Test(groups = "fast")
         .withException(new TestWithExceptionCallback() {
             @Override
             public void doTest() throws SubscriptionBaseRepairException {
                 // MOVE AFTER TRIAL
                 .pushExpectedEvent(.);
 
                 final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(40));
                 .addDeltaFromReality(it.toDurationMillis());
 
                 assertListenerStatus();
 
                 final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                 .sortEventsOnBundle(bundleRepair);
                 final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                 final NewEvent ne = .createNewEvent(..getStartDate().plusDays(10), spec);
 
                 final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(.getId(), Collections.<DeletedEvent>emptyList(), Collections.singletonList(ne));
 
                 final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
 
                 .repairBundle(bRepairtrue);
             }
     }
 
     @Test(groups = "fast")
     public void testENT_REPAIR_INVALID_DELETE_SET() throws Exception {
         .withException(new TestWithExceptionCallback() {
             @Override
             public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
 
                 Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(3));
                 .addDeltaFromReality(it.toDurationMillis());
 
                 .pushExpectedEvent(.);
                final DateTime changeTime = .getUTCNow();
                .changePlanWithDate("Assault-Rifle"..changeTime);
                assertListenerStatus();
                // MOVE AFTER TRIAL
                .pushExpectedEvent(.);
                it = new Interval(.getUTCNow(), .getUTCNow().plusDays(40));
                .addDeltaFromReality(it.toDurationMillis());
                assertListenerStatus();
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                final NewEvent ne = .createNewEvent(..getStartDate().plusDays(10), spec);
                final DeletedEvent de = .createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId());
                final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(.getId(), Collections.singletonList(de), Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                .repairBundle(bRepairtrue);
            }
    }
    @Test(groups = "fast")
    public void testENT_REPAIR_NON_EXISTENT_DELETE_EVENT() throws Exception {
            @Override
            public void doTest() throws SubscriptionBaseRepairException {
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                final NewEvent ne = .createNewEvent(..getStartDate().plusDays(10), spec);
                final DeletedEvent de = .createDeletedEvent(UUID.randomUUID());
                final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(.getId(), Collections.singletonList(de), Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                .repairBundle(bRepairtrue);
            }
    }
    @Test(groups = "fast")
    public void testENT_REPAIR_SUB_RECREATE_NOT_EMPTY() throws Exception {
            @Override
            public void doTest() throws SubscriptionBaseRepairException {
                // MOVE AFTER TRIAL
                .pushExpectedEvent(.);
                final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(40));
                .addDeltaFromReality(it.toDurationMillis());
                assertListenerStatus();
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                final NewEvent ne = .createNewEvent(..getStartDate().plusDays(10), spec);
                final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
                des.add(.createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
                final SubscriptionBaseTimeline sRepair = .createSubscriptionRepair(.getId(), des, Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                .repairBundle(bRepairtrue);
            }
    }
    @Test(groups = "fast")
    public void testENT_REPAIR_SUB_EMPTY() throws Exception {
            @Override
            public void doTest() throws SubscriptionBaseRepairException {
                // MOVE AFTER TRIAL
                .pushExpectedEvent(.);
                final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(40));
                .addDeltaFromReality(it.toDurationMillis());
                assertListenerStatus();
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Assault-Rifle"....);
                final NewEvent ne = .createNewEvent(..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(.getId(), des, Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                .repairBundle(bRepairtrue);
            }
        }, .);
    }
    @Test(groups = "fast")
    public void testENT_REPAIR_AO_CREATE_BEFORE_BP_START() throws Exception {
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                // MOVE CLOCK A LITTLE BIT-- STILL IN TRIAL
                Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(4));
                .addDeltaFromReality(it.toDurationMillis());
                final DefaultSubscriptionBase aoSubscription = .createSubscription("Telescopic-Scope"..);
                // MOVE CLOCK A LITTLE BIT MORE -- STILL IN TRIAL
                it = new Interval(.getUTCNow(), .getUTCNow().plusDays(4));
                .addDeltaFromReality(it.toDurationMillis());
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                // Quick check
                final SubscriptionBaseTimeline bpRepair = .getSubscriptionRepair(.getId(), bundleRepair);
                assertEquals(bpRepair.getExistingEvents().size(), 2);
                final SubscriptionBaseTimeline aoRepair = .getSubscriptionRepair(aoSubscription.getId(), bundleRepair);
                assertEquals(aoRepair.getExistingEvents().size(), 2);
                final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
                des.add(.createDeletedEvent(aoRepair.getExistingEvents().get(0).getEventId()));
                des.add(.createDeletedEvent(aoRepair.getExistingEvents().get(1).getEventId()));
                final DateTime aoRecreateDate = aoSubscription.getStartDate().minusDays(5);
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Telescopic-Scope"....);
                final NewEvent ne = .createNewEvent(.aoRecreateDatespec);
                final SubscriptionBaseTimeline saoRepair = .createSubscriptionRepair(aoSubscription.getId(), des, Collections.singletonList(ne));
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(saoRepair));
                final boolean dryRun = true;
                .repairBundle(bRepairdryRun);
            }
    }
    @Test(groups = "fast")
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                // MOVE CLOCK A LITTLE BIT-- STILL IN TRIAL
                Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(4));
                .addDeltaFromReality(it.toDurationMillis());
                final DefaultSubscriptionBase aoSubscription = .createSubscription("Telescopic-Scope"..);
                // MOVE CLOCK A LITTLE BIT MORE -- STILL IN TRIAL
                it = new Interval(.getUTCNow(), .getUTCNow().plusDays(4));
                .addDeltaFromReality(it.toDurationMillis());
                BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                // Quick check
                final SubscriptionBaseTimeline bpRepair = .getSubscriptionRepair(.getId(), bundleRepair);
                assertEquals(bpRepair.getExistingEvents().size(), 2);
                final SubscriptionBaseTimeline aoRepair = .getSubscriptionRepair(aoSubscription.getId(), bundleRepair);
                assertEquals(aoRepair.getExistingEvents().size(), 2);
                final List<DeletedEventdes = new LinkedList<SubscriptionBaseTimeline.DeletedEvent>();
                //des.add(createDeletedEvent(aoRepair.getExistingEvents().get(1).getEventId()));        
                final DateTime aoCancelDate = aoSubscription.getStartDate().plusDays(10);
                final NewEvent ne = .createNewEvent(.aoCancelDatenull);
                final SubscriptionBaseTimeline saoRepair = .createSubscriptionRepair(aoSubscription.getId(), des, Collections.singletonList(ne));
                bundleRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(saoRepair));
                final boolean dryRun = true;
                .repairBundle(bundleRepairdryRun);
            }
    }
    @Test(groups = "fast", enabled = false// TODO - fails on jdk7 on Travis
    public void testENT_REPAIR_BP_RECREATE_MISSING_AO() throws Exception {
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                //testListener.pushExpectedEvent(NextEvent.PHASE);
                final Interval it = new Interval(.getUTCNow(), .getUTCNow().plusDays(4));
                .addDeltaFromReality(it.toDurationMillis());
                //assertListenerStatus();
                final DefaultSubscriptionBase aoSubscription = .createSubscription("Laser-Scope"..);
                final BundleBaseTimeline bundleRepair = .getBundleTimeline(.getId(), );
                .sortEventsOnBundle(bundleRepair);
                final DateTime newCreateTime = .getStartDate().plusDays(3);
                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(.getId(), des, Collections.singletonList(ne));
                // FIRST ISSUE DRY RUN
                final BundleBaseTimeline bRepair = .createBundleRepair(.getId(), bundleRepair.getViewId(), Collections.singletonList(sRepair));
                final boolean dryRun = true;
                .repairBundle(bRepairdryRun);
            }
    }
    //
    // CAN'T seem to trigger such case easily, other errors trigger before...
    //
    @Test(groups = "fast", enabled = false)
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                /*
              //testListener.pushExpectedEvent(NextEvent.PHASE);
                Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(4));
                clock.addDeltaFromReality(it.toDurationMillis());
                DefaultSubscriptionBase aoSubscription = createSubscription("Laser-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME);
                
                BundleRepair bundleRepair = repairApi.getBundleRepair(bundle.getId());
                sortEventsOnBundle(bundleRepair);
                
                DateTime newCreateTime = baseSubscription.getStartDate().plusDays(3);
                PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Pistol", ProductCategory.BASE, BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.TRIAL);
                NewEvent ne = createNewEvent(SubscriptionBaseTransitionType.CREATE, newCreateTime, spec);
                List<DeletedEvent> des = new LinkedList<SubscriptionRepair.DeletedEvent>();
                des.add(createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(0).getEventId()));
                des.add(createDeletedEvent(bundleRepair.getSubscriptions().get(0).getExistingEvents().get(1).getEventId()));
                SubscriptionRepair bpRepair = createSubscriptionReapir(baseSubscription.getId(), des, Collections.singletonList(ne));
                
                ne = createNewEvent(SubscriptionBaseTransitionType.CANCEL, clock.getUTCNow().minusDays(1),  null);
                SubscriptionRepair aoRepair = createSubscriptionReapir(aoSubscription.getId(), Collections.<SubscriptionRepair.DeletedEvent>emptyList(), Collections.singletonList(ne));
                
                
                List<SubscriptionRepair> allRepairs = new LinkedList<SubscriptionRepair>();
                allRepairs.add(bpRepair);
                allRepairs.add(aoRepair);
                bundleRepair =  createBundleRepair(bundle.getId(), bundleRepair.getViewId(), allRepairs);
                // FIRST ISSUE DRY RUN
                BundleRepair bRepair =  createBundleRepair(bundle.getId(), bundleRepair.getViewId(), allRepairs);
                
                boolean dryRun = true;
                repairApi.repairBundle(bRepair, dryRun, callcontext);
                */
            }
    }
    @Test(groups = "fast", enabled = false)
    public void testENT_REPAIR_MISSING_AO_DELETE_EVENT() throws Exception {
            @Override
            public void doTest() throws SubscriptionBaseRepairExceptionSubscriptionBaseApiException {
                /*
                // MOVE CLOCK -- JUST BEFORE END OF TRIAL
                 *                 
                Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(29));
                clock.addDeltaFromReality(it.toDurationMillis());
                clock.setDeltaFromReality(getDurationDay(29), 0);
                
                DefaultSubscriptionBase aoSubscription = createSubscription("Laser-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME);
                
                // MOVE CLOCK -- RIGHT OUT OF TRIAL
                testListener.pushExpectedEvent(NextEvent.PHASE);                
                clock.addDeltaFromReality(getDurationDay(5));
                assertListenerStatus();
                DateTime requestedChange = clock.getUTCNow();
                baseSubscription.changePlanWithRequestedDate("Assault-Rifle", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, requestedChange, callcontext);
                DateTime reapairTime = clock.getUTCNow().minusDays(1);
                BundleRepair bundleRepair = repairApi.getBundleRepair(bundle.getId());
                sortEventsOnBundle(bundleRepair);
                
                SubscriptionRepair bpRepair = getSubscriptionRepair(baseSubscription.getId(), bundleRepair);
                SubscriptionRepair aoRepair = getSubscriptionRepair(aoSubscription.getId(), bundleRepair);
                List<DeletedEvent> bpdes = new LinkedList<SubscriptionRepair.DeletedEvent>();
                bpdes.add(createDeletedEvent(bpRepair.getExistingEvents().get(2).getEventId()));    
                bpRepair = createSubscriptionReapir(baseSubscription.getId(), bpdes, Collections.<NewEvent>emptyList());
                
                NewEvent ne = createNewEvent(SubscriptionBaseTransitionType.CANCEL, reapairTime, null);
                aoRepair = createSubscriptionReapir(aoSubscription.getId(), Collections.<SubscriptionRepair.DeletedEvent>emptyList(), Collections.singletonList(ne));
                
                List<SubscriptionRepair> allRepairs = new LinkedList<SubscriptionRepair>();
                allRepairs.add(bpRepair);
                allRepairs.add(aoRepair);
                bundleRepair =  createBundleRepair(bundle.getId(), bundleRepair.getViewId(), allRepairs);
                
                boolean dryRun = false;
                repairApi.repairBundle(bundleRepair, dryRun, callcontext);
                */
            }
    }
New to GrepCode? Check out our FAQ X