private StandaloneCatalog getCurrentStandaloneCatalogForTenant(final InternalTenantContext internalTenantContext) throws CatalogApiException { final DefaultVersionedCatalog versionedCatalog = catalogService.getFullCatalog(false, false, internalTenantContext); if (versionedCatalog != null && !versionedCatalog.getVersions().isEmpty()) { final StandaloneCatalog standaloneCatalogWithPriceOverride = versionedCatalog.getVersions().get(versionedCatalog.getVersions().size() - 1); return standaloneCatalogWithPriceOverride; } else { return null; } }
@Override protected void configure() { final CatalogService catalogService = Mockito.mock(CatalogService.class); final CatalogInternalApi catalogInternalApi = Mockito.mock(CatalogInternalApi.class); try { final DefaultVersionedCatalog mockVersionedCatalog = new DefaultVersionedCatalog(); final MockCatalog mockCatalog = new MockCatalog(); mockVersionedCatalog.add(mockCatalog); Mockito.when(catalogService.getFullCatalogForInternalUse(Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(InternalCallContext.class))).thenReturn(mockVersionedCatalog); Mockito.when(catalogService.getFullCatalog(Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(InternalCallContext.class))).thenReturn(mockVersionedCatalog); bind(CatalogService.class).toInstance(catalogService); bind(CatalogInternalApi.class).toInstance(catalogInternalApi); } catch (final CatalogApiException e) { throw new RuntimeException(e); } } }
@Override public Catalog getFullCatalog(final boolean useDefaultCatalog, final boolean filterTemplateCatalog, final InternalTenantContext context) throws CatalogApiException { return catalogService.getFullCatalogForInternalUse(useDefaultCatalog, filterTemplateCatalog, context); } }
@Override public Catalog getFullCatalog(final boolean useDefaultCatalog, final boolean filterTemplateCatalog, final InternalTenantContext context) throws CatalogApiException { return catalogService.getFullCatalogForInternalUse(useDefaultCatalog, filterTemplateCatalog, context); } }
public Catalog initCatalog(final CatalogService catalogService, final InternalTenantContext context) throws Exception { ((DefaultCatalogService) catalogService).loadCatalog(); final Catalog catalog = catalogService.getFullCatalog(true, true, context); assertNotNull(catalog); return catalog; }
@Override protected void configure() { final CatalogService catalogService = Mockito.mock(CatalogService.class); final CatalogInternalApi catalogInternalApi = Mockito.mock(CatalogInternalApi.class); try { final DefaultVersionedCatalog mockVersionedCatalog = new DefaultVersionedCatalog(); final MockCatalog mockCatalog = new MockCatalog(); mockVersionedCatalog.add(mockCatalog); Mockito.when(catalogService.getFullCatalogForInternalUse(Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(InternalCallContext.class))).thenReturn(mockVersionedCatalog); Mockito.when(catalogService.getFullCatalog(Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(InternalCallContext.class))).thenReturn(mockVersionedCatalog); bind(CatalogService.class).toInstance(catalogService); bind(CatalogInternalApi.class).toInstance(catalogInternalApi); } catch (final CatalogApiException e) { throw new RuntimeException(e); } } }
@Override public StaticCatalog getCurrentCatalog(final String catalogName, final TenantContext tenantContext) throws CatalogApiException { final InternalTenantContext internalTenantContext; if (tenantContext.getAccountId() != null) { internalTenantContext = internalCallContextFactory.createInternalTenantContext(tenantContext.getAccountId(), tenantContext); } else { internalTenantContext = createInternalTenantContext(tenantContext); } return catalogService.getFullCatalog(true, true, internalTenantContext); }
@Override public VersionedCatalog<? extends StaticCatalog> getCatalog(final String catalogName, @Nullable final DateTime catalogDateVersion, final TenantContext tenantContext) throws CatalogApiException { final InternalTenantContext internalTenantContext; if (tenantContext.getAccountId() != null) { internalTenantContext = internalCallContextFactory.createInternalTenantContext(tenantContext.getAccountId(), tenantContext); } else { internalTenantContext = createInternalTenantContext(tenantContext); } final DefaultVersionedCatalog fullCatalog = catalogService.getFullCatalog(true, true, internalTenantContext); if (catalogDateVersion == null) { return fullCatalog; } else { final DefaultVersionedCatalog filteredCatalog = new DefaultVersionedCatalog(clock); for (final StandaloneCatalog v : fullCatalog.getVersions()) { if (v.getEffectiveDate().compareTo(catalogDateVersion.toDate()) >= 0) { filteredCatalog.add(v); break; } } return filteredCatalog; } }
private TimedPhase getNextTimedPhaseOnChange(final DefaultSubscriptionBase defaultSubscriptionBase, final String newProductName, final DateTime effectiveChangeDate) throws CatalogApiException, SubscriptionBaseApiException { // The date is used for different catalog versions - we don't care here final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); return planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); }
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()); }
private DefaultSubscriptionBase buildSubscription(final DefaultSubscriptionBase in, final InternalTenantContext context) { final DefaultSubscriptionBase subscription = new DefaultSubscriptionBase(new SubscriptionBuilder(in), null, clock); if (!events.isEmpty()) { try { subscription.rebuildTransitions(getEventsForSubscription(in.getId(), context), catalogService.getFullCatalog(true, true, context)); } catch (final CatalogApiException e) { log.warn("Failed to rebuild subscription", e); } } return subscription; }
private TimedPhase[] getTimedPhasesOnCreate(final String productName, final PhaseType initialPhase, final DefaultSubscriptionBase defaultSubscriptionBase, final DateTime effectiveDate) throws CatalogApiException, SubscriptionBaseApiException { // The date is used for different catalog versions - we don't care here final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); // Same here for the requested date final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(defaultSubscriptionBase.getAlignStartDate(), defaultSubscriptionBase.getBundleStartDate(), plan, initialPhase, priceList, effectiveDate, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); return phases; } }
private DefaultSubscriptionBase createSubscription(final DateTime bundleStartDate, final DateTime alignStartDate, final String productName, final PhaseType phaseType) throws CatalogApiException { final SubscriptionBuilder builder = new SubscriptionBuilder(); builder.setBundleStartDate(bundleStartDate); // Make sure to set the dates apart builder.setAlignStartDate(new DateTime(alignStartDate)); // Create the transitions final DefaultSubscriptionBase defaultSubscriptionBase = new DefaultSubscriptionBase(builder, null, clock); final SubscriptionBaseEvent event = createSubscriptionEvent(builder.getAlignStartDate(), productName, phaseType, ApiEventType.CREATE ); final List<SubscriptionBaseEvent> events = new ArrayList<SubscriptionBaseEvent>(); events.add(event); defaultSubscriptionBase.rebuildTransitions(events, catalogService.getFullCatalog(true, true, internalCallContext)); Assert.assertEquals(defaultSubscriptionBase.getAllTransitions().size(), 1); Assert.assertNull(defaultSubscriptionBase.getAllTransitions().get(0).getPreviousPhase()); Assert.assertNotNull(defaultSubscriptionBase.getAllTransitions().get(0).getNextPhase()); return defaultSubscriptionBase; }
try { final DefaultVersionedCatalog versionedCatalog = catalogService.getFullCatalog(false, true, internalTenantContext);
@Test(groups = "fast") public void testChangeWithTargetPhaseType2() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, null, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.TRIAL); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertEquals(phases[1].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[1].getStartPhase(), now.plusDays(30)); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.TRIAL); final String newProductName = "assault-rifle-annual-rescue"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusDays(5); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.EVERGREEN, catalog, internalCallContext); // We end up straight on EVERGREEN Phase and because we are CHANGE_OF_PLAN aligned the start is at the effective date of the change Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate.plusDays(5)); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); final TimedPhase nextPhase = planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.EVERGREEN, catalog, internalCallContext); Assert.assertNull(nextPhase); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType1() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "pistol-monthly-notrial"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType3() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "assault-rifle-annual-gunclub-discount"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType2() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "shotgun-monthly"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType5() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "pistol-monthly-fixedterm"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusDays(5); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); // Initial phase EVERGREEN does not exist in the new Plan so we ignore the original skipped Phase and proceed with default alignment (we only move the clock 5 days so we are still in TRIAL) Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.TRIAL); final TimedPhase nextPhase = planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(nextPhase.getStartPhase(), alignStartDate.plusDays(30)); Assert.assertEquals(nextPhase.getPhase().getPhaseType(), PhaseType.FIXEDTERM); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType4() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "pistol-annual-gunclub-discount-notrial"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }