@Override public DefaultPlan getOverriddenPlan(final String planName, final StandaloneCatalog catalog, final InternalTenantContext context) throws CatalogApiException { return overriddenPlanCache.getOverriddenPlan(planName, catalog, context); } }
private DefaultVersionedCatalog getCatalog(final boolean useDefaultCatalog, final boolean filterTemplateCatalog, final boolean internalUse, final InternalTenantContext context) throws CatalogApiException { return catalogCache.getCatalog(useDefaultCatalog, filterTemplateCatalog, internalUse, context); } }
@Inject public DefaultCatalogCache(final OSGIServiceRegistration<CatalogPluginApi> pluginRegistry, final VersionedCatalogMapper versionedCatalogMapper, final CacheControllerDispatcher cacheControllerDispatcher, final VersionedCatalogLoader loader, final PriceOverride priceOverride, final InternalCallContextFactory internalCallContextFactory) { this.pluginRegistry = pluginRegistry; this.versionedCatalogMapper = versionedCatalogMapper; this.cacheController = cacheControllerDispatcher.getCacheController(CacheType.TENANT_CATALOG); this.loader = loader; this.priceOverride = priceOverride; this.internalCallContextFactory = internalCallContextFactory; this.cacheLoaderArgumentWithTemplateFiltering = initializeCacheLoaderArgument(true); this.cacheLoaderArgument = initializeCacheLoaderArgument(false); setDefaultCatalog(); }
@Test(groups = "fast") public void testMissingDefaultCatalog() throws CatalogApiException { catalogCache.loadDefaultCatalog(null); Assert.assertEquals(catalogCache.getCatalog(true, true, false, internalCallContext).getCatalogName(), "EmptyCatalog"); }
@BeforeMethod(groups = "fast") protected void beforeMethod() throws Exception { if (hasFailed()) { return; } cacheControllerDispatcher.clearAll(); multiTenantContext = Mockito.mock(InternalTenantContext.class); Mockito.when(multiTenantContext.getAccountRecordId()).thenReturn(456L); Mockito.when(multiTenantContext.getTenantRecordId()).thenReturn(99L); otherMultiTenantContext = Mockito.mock(InternalCallContext.class); Mockito.when(otherMultiTenantContext.getAccountRecordId()).thenReturn(123L); Mockito.when(otherMultiTenantContext.getTenantRecordId()).thenReturn(112233L); ((DefaultCatalogCache) catalogCache).setDefaultCatalog(); }
@Override public void invalidateCache(TenantKey key, final Object cookie, final InternalTenantContext tenantContext) { log.info("Invalidate catalog cache for tenantRecordId='{}'", tenantContext.getTenantRecordId()); catalogCache.clearCatalog(tenantContext); } }
@LifecycleHandlerType(LifecycleLevel.LOAD_CATALOG) public synchronized void loadCatalog() throws ServiceException { if (!isInitialized) { try { // In multi-tenant mode, the property is not required if (config.getCatalogURI() != null && !config.getCatalogURI().isEmpty()) { catalogCache.loadDefaultCatalog(config.getCatalogURI()); log.info("Successfully loaded the default catalog {}", config.getCatalogURI()); } isInitialized = true; } catch (final Exception e) { throw new ServiceException(e); } } }
final DefaultVersionedCatalog pluginVersionedCatalog = getCatalogFromPlugins(tenantContext); if (pluginVersionedCatalog != null) { return pluginVersionedCatalog; initializeCatalog(tenantCatalog);
private DefaultPlan loadOverriddenPlan(final String planName, final StandaloneCatalog catalog, final InternalTenantContext context) throws CatalogApiException { final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName); if (!m.matches()) { throw new CatalogApiException(ErrorCode.CAT_NO_SUCH_PLAN, planName); } final String parentPlanName = m.group(1); final Long planDefRecordId = Long.parseLong(m.group(2)); final List<CatalogOverridePhaseDefinitionModelDao> phaseDefs = overrideDao.getOverriddenPlanPhases(planDefRecordId, context); final DefaultPlan defaultPlan = catalog.findCurrentPlan(parentPlanName); final PlanPhasePriceOverride[] overrides = createOverrides(defaultPlan, phaseDefs, context); final DefaultPlan result = new DefaultPlan(catalog, planName, defaultPlan, overrides); result.initialize(catalog); return result; }
List<UsagePriceOverride> getUsagePriceOverrides(PlanPhase curPhase, CatalogOverridePhaseDefinitionModelDao overriddenPhase, final InternalTenantContext context) { final List<UsagePriceOverride> usagePriceOverrides = new ArrayList<UsagePriceOverride>(); final List<CatalogOverrideUsageDefinitionModelDao> usageDefs = overrideDao.getOverriddenPhaseUsages(overriddenPhase.getRecordId(), context); for (int i = 0; i < curPhase.getUsages().length; i++) { final Usage curUsage = curPhase.getUsages()[i]; final CatalogOverrideUsageDefinitionModelDao overriddenUsage = Iterables.tryFind(usageDefs, new Predicate<CatalogOverrideUsageDefinitionModelDao>() { @Override public boolean apply(final CatalogOverrideUsageDefinitionModelDao input) { return input.getParentUsageName().equals(curUsage.getName()); } }).orNull(); if (overriddenUsage != null) { List<TierPriceOverride> tierPriceOverrides = getTierPriceOverrides(curUsage, overriddenUsage, context); usagePriceOverrides.add(new DefaultUsagePriceOverride(overriddenUsage.getParentUsageName(), curUsage.getUsageType(), tierPriceOverrides)); } } return usagePriceOverrides; }
List<TieredBlockPriceOverride> tieredBlockPriceOverrides = getTieredBlockPriceOverrides(curTier, overriddenTier, context); tierPriceOverrides.add(new DefaultTierPriceOverride(tieredBlockPriceOverrides));
private PlanPhasePriceOverride[] createOverrides(final Plan defaultPlan, final List<CatalogOverridePhaseDefinitionModelDao> phaseDefs, final InternalTenantContext context) { final PlanPhasePriceOverride[] result = new PlanPhasePriceOverride[defaultPlan.getAllPhases().length]; for (int i = 0; i < defaultPlan.getAllPhases().length; i++) { final PlanPhase curPhase = defaultPlan.getAllPhases()[i]; final CatalogOverridePhaseDefinitionModelDao overriddenPhase = Iterables.tryFind(phaseDefs, new Predicate<CatalogOverridePhaseDefinitionModelDao>() { @Override public boolean apply(final CatalogOverridePhaseDefinitionModelDao input) { return input.getParentPhaseName().equals(curPhase.getName()); } }).orNull(); if (overriddenPhase != null) { List<UsagePriceOverride> usagePriceOverrides = getUsagePriceOverrides(curPhase, overriddenPhase, context); result[i] = new DefaultPlanPhasePriceOverride(curPhase.getName(), Currency.valueOf(overriddenPhase.getCurrency()), overriddenPhase.getFixedPrice(), overriddenPhase.getRecurringPrice(), usagePriceOverrides); } else { result[i] = null; } } return result; }
final DefaultVersionedCatalog tenantCatalog = cacheController.get(internalTenantContext.getTenantRecordId(), cacheLoaderArgument); if (tenantCatalog != null) { initializeCatalog(tenantCatalog); if (tenantCatalog.getEffectiveDate().compareTo(latestCatalogUpdatedDate.toDate()) == 0) {
result.initialize(standaloneCatalog); if (context == null) { overriddenPlanCache.addDryRunPlan(planName, result);
@Test(groups = "fast") public void testDefaultCatalog() throws CatalogApiException { catalogCache.loadDefaultCatalog(Resources.getResource("SpyCarBasic.xml").toExternalForm()); final DefaultVersionedCatalog result = catalogCache.getCatalog(true, true, false, internalCallContext); Assert.assertNotNull(result); final Collection<Product> products = result.getProducts(clock.getUTCNow()); Assert.assertEquals(products.size(), 3); // Verify the lookup with other contexts final DefaultVersionedCatalog resultForMultiTenantContext = new DefaultVersionedCatalog(result.getClock()); for (final StandaloneCatalog cur : result.getVersions()) { resultForMultiTenantContext.add(new StandaloneCatalogWithPriceOverride(cur, priceOverride, multiTenantContext.getTenantRecordId(), internalCallContextFactory)); } Assert.assertEquals(catalogCache.getCatalog(true, true, false, multiTenantContext).getCatalogName(), resultForMultiTenantContext.getCatalogName()); Assert.assertEquals(catalogCache.getCatalog(true, true, false, multiTenantContext).getVersions().size(), resultForMultiTenantContext.getVersions().size()); for (int i = 0; i < catalogCache.getCatalog(true, true, false, multiTenantContext).getVersions().size(); i++) { Assert.assertEquals(((StandaloneCatalogWithPriceOverride) catalogCache.getCatalog(true, true, false, multiTenantContext).getVersions().get(i)).getTenantRecordId(), ((StandaloneCatalogWithPriceOverride) resultForMultiTenantContext.getVersions().get(i)).getTenantRecordId()); } }
@Override public void deleteCatalog(final CallContext callContext) throws CatalogApiException { final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext); try { tenantApi.deleteTenantKey(TenantKey.CATALOG.toString(), callContext); catalogCache.clearCatalog(internalTenantContext); createDefaultEmptyCatalog(callContext.getCreatedDate(), callContext); } catch (final TenantApiException e) { throw new CatalogApiException(e); } }
VersionedCatalog differentResult = catalogCache.getCatalog(true, true, false, differentMultiTenantContext); Assert.assertNotNull(differentResult); Assert.assertEquals(differentResult.getCatalogName(), "EmptyCatalog"); differentResult = catalogCache.getCatalog(true, true, false, differentMultiTenantContext); Assert.assertNotNull(differentResult); Assert.assertEquals(differentResult.getCatalogName(), "EmptyCatalog"); catalogCache.loadDefaultCatalog(Resources.getResource("SpyCarBasic.xml").toExternalForm()); final DefaultVersionedCatalog result = catalogCache.getCatalog(true, true, false, multiTenantContext); Assert.assertNotNull(result); final Collection<Product> products = result.getProducts(clock.getUTCNow()); final DefaultVersionedCatalog otherResult = catalogCache.getCatalog(true, true, false, otherMultiTenantContext); Assert.assertNotNull(otherResult); final Collection<Product> otherProducts = otherResult.getProducts(clock.getUTCNow()); final VersionedCatalog result2 = catalogCache.getCatalog(true, true, false, multiTenantContext); Assert.assertEquals(result2, result); Mockito.when(sameMultiTenantContext.getAccountRecordId()).thenReturn(9102L); Mockito.when(sameMultiTenantContext.getTenantRecordId()).thenReturn(multiTenantRecordId); Assert.assertEquals(catalogCache.getCatalog(true, true, false, sameMultiTenantContext), result); Assert.assertEquals(catalogCache.getCatalog(true, true, false, otherMultiTenantContext), otherResult);
@Override public void createDefaultEmptyCatalog(@Nullable final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException { try { final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext); final StandaloneCatalog currentCatalog = getCurrentStandaloneCatalogForTenant(internalTenantContext); final CatalogUpdater catalogUpdater = (currentCatalog != null) ? new CatalogUpdater(currentCatalog) : new CatalogUpdater(getSafeFirstCatalogEffectiveDate(effectiveDate, callContext), null); tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(), callContext); catalogCache.clearCatalog(internalTenantContext); } catch (TenantApiException e) { throw new CatalogApiException(e); } }
@Override public void addSimplePlan(final SimplePlanDescriptor descriptor, @Nullable final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException { try { final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext); final StandaloneCatalog currentCatalog = getCurrentStandaloneCatalogForTenant(internalTenantContext); final CatalogUpdater catalogUpdater = (currentCatalog != null) ? new CatalogUpdater(currentCatalog) : new CatalogUpdater(getSafeFirstCatalogEffectiveDate(effectiveDate, callContext), descriptor.getCurrency()); catalogUpdater.addSimplePlanDescriptor(descriptor); tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(), callContext); catalogCache.clearCatalog(internalTenantContext); } catch (TenantApiException e) { throw new CatalogApiException(e); } }
catalogCache.clearCatalog(internalTenantContext); } catch (final TenantApiException e) { throw new CatalogApiException(e);