final StandaloneCatalog catalog = XMLLoader.getObjectFromStream(curCatalogStream, StandaloneCatalog.class); if (!filterTemplateCatalog || !catalog.isTemplateCatalog()) { result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, tenantRecordId, internalCallContextFactory));
private DefaultPlan maybeGetOverriddenPlan(final String planName) throws CatalogApiException { final InternalTenantContext internalTenantContext = createInternalTenantContext(); try { return priceOverride.getOverriddenPlan(planName, this, internalTenantContext); } catch (final RuntimeException e) { if (e.getCause() == null || e.getCause().getCause() == null || !(e.getCause().getCause() instanceof CatalogApiException) || ((CatalogApiException) e.getCause().getCause()).getCode() != ErrorCode.CAT_NO_SUCH_PLAN.getCode()) { throw e; } else { // Otherwise, ambiguous name? See https://github.com/killbill/killbill/issues/842. return null; } } }
@Override public Plan createOrFindCurrentPlan(final PlanSpecifier spec, final PlanPhasePriceOverridesWithCallContext overrides) throws CatalogApiException { final Plan defaultPlan = super.createOrFindCurrentPlan(spec, null); if (overrides == null || overrides.getOverrides() == null || overrides.getOverrides().isEmpty()) { return defaultPlan; } final InternalCallContext internalCallContext = overrides.getCallContext() != null ? internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(overrides.getCallContext()) : null; return priceOverride.getOrCreateOverriddenPlan(this, defaultPlan, CatalogDateHelper.toUTCDateTime(getEffectiveDate()), overrides.getOverrides(), internalCallContext); }
public StandaloneCatalogWithPriceOverride(final StandaloneCatalog catalog, final PriceOverride priceOverride, final Long tenantRecordId, final InternalCallContextFactory internalCallContextFactory) { // Initialize from input catalog setCatalogName(catalog.getCatalogName()); setEffectiveDate(catalog.getEffectiveDate()); setProducts(catalog.getCurrentProducts()); setPlans(catalog.getCurrentPlans()); setPriceLists(catalog.getPriceLists()); setPlanRules(catalog.getPlanRules()); setSupportedCurrencies(catalog.getCurrentSupportedCurrencies()); setUnits(catalog.getCurrentUnits()); this.tenantRecordId = tenantRecordId; this.priceOverride = priceOverride; this.internalCallContextFactory = internalCallContextFactory; }
@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 DefaultPlan findCurrentPlan(final String planName) throws CatalogApiException { final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName); if (m.matches()) { final DefaultPlan plan = maybeGetOverriddenPlan(planName); if (plan != null) { return plan; } } return super.findCurrentPlan(planName); }
private void initializeCatalog(final DefaultVersionedCatalog tenantCatalog) { tenantCatalog.initialize(defaultCatalog.getClock(), tenantCatalog); for (final StandaloneCatalog cur : tenantCatalog.getVersions()) { if (cur instanceof StandaloneCatalogWithPriceOverride) { ((StandaloneCatalogWithPriceOverride) cur).initialize(cur, priceOverride, internalCallContextFactory); } else { cur.initialize(cur); } } }
public StandaloneCatalogWithPriceOverride(final StandaloneCatalog catalog, final PriceOverride priceOverride, final Long tenantRecordId, final InternalCallContextFactory internalCallContextFactory) { // Initialize from input catalog setCatalogName(catalog.getCatalogName()); setEffectiveDate(catalog.getEffectiveDate()); setProducts(catalog.getCurrentProducts()); setPlans(catalog.getCurrentPlans()); setPriceLists(catalog.getPriceLists()); setPlanRules(catalog.getPlanRules()); setSupportedCurrencies(catalog.getCurrentSupportedCurrencies()); setUnits(catalog.getCurrentUnits()); this.tenantRecordId = tenantRecordId; this.priceOverride = priceOverride; this.internalCallContextFactory = internalCallContextFactory; }
@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 PlanPhase findCurrentPhase(final String phaseName) throws CatalogApiException { final String planName = DefaultPlanPhase.planName(phaseName); final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName); if (m.matches()) { final DefaultPlan plan = maybeGetOverriddenPlan(planName); if (plan != null) { return plan.findPhase(phaseName); } } return super.findCurrentPhase(phaseName); }
private void initializeCatalog(final DefaultVersionedCatalog tenantCatalog) { tenantCatalog.initialize(defaultCatalog.getClock(), tenantCatalog); for (final StandaloneCatalog cur : tenantCatalog.getVersions()) { if (cur instanceof StandaloneCatalogWithPriceOverride) { ((StandaloneCatalogWithPriceOverride) cur).initialize(cur, priceOverride, internalCallContextFactory); } else { cur.initialize(cur); } } }
private StandaloneCatalogWithPriceOverride toStandaloneCatalogWithPriceOverride(final VersionedPluginCatalog pluginCatalog, final StandalonePluginCatalog input, final InternalTenantContext internalTenantContext) { final StandaloneCatalogMapper mapper = new StandaloneCatalogMapper(pluginCatalog.getCatalogName()); final StandaloneCatalog catalog = mapper.toStandaloneCatalog(input); final StandaloneCatalogWithPriceOverride result = new StandaloneCatalogWithPriceOverride(catalog, priceOverride, internalTenantContext.getTenantRecordId(), internalCallContextFactory); return result; } }
@Override public DefaultPlan findCurrentPlan(final String planName) throws CatalogApiException { final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName); if (m.matches()) { final DefaultPlan plan = maybeGetOverriddenPlan(planName); if (plan != null) { return plan; } } return super.findCurrentPlan(planName); }
@Override public Plan createOrFindCurrentPlan(final PlanSpecifier spec, final PlanPhasePriceOverridesWithCallContext overrides) throws CatalogApiException { final Plan defaultPlan = super.createOrFindCurrentPlan(spec, null); if (overrides == null || overrides.getOverrides() == null || overrides.getOverrides().isEmpty()) { return defaultPlan; } final InternalCallContext internalCallContext = overrides.getCallContext() != null ? internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(overrides.getCallContext()) : null; return priceOverride.getOrCreateOverriddenPlan(this, defaultPlan, CatalogDateHelper.toUTCDateTime(getEffectiveDate()), overrides.getOverrides(), internalCallContext); }
private DefaultPlan maybeGetOverriddenPlan(final String planName) throws CatalogApiException { final InternalTenantContext internalTenantContext = createInternalTenantContext(); try { return priceOverride.getOverriddenPlan(planName, this, internalTenantContext); } catch (final RuntimeException e) { if (e.getCause() == null || e.getCause().getCause() == null || !(e.getCause().getCause() instanceof CatalogApiException) || ((CatalogApiException) e.getCause().getCause()).getCode() != ErrorCode.CAT_NO_SUCH_PLAN.getCode()) { throw e; } else { // Otherwise, ambiguous name? See https://github.com/killbill/killbill/issues/842. return null; } } }
for (final URI u : xmlURIs) { final StandaloneCatalog catalog = XMLLoader.getObjectFromUri(u, StandaloneCatalog.class); result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID, internalCallContextFactory));
@Override public PlanPhase findCurrentPhase(final String phaseName) throws CatalogApiException { final String planName = DefaultPlanPhase.planName(phaseName); final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName); if (m.matches()) { final DefaultPlan plan = maybeGetOverriddenPlan(planName); if (plan != null) { return plan.findPhase(phaseName); } } return super.findCurrentPhase(phaseName); }
@Test(groups = "slow") public void testCreatePlanInvalidProduct() throws Exception { final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class); final StaticCatalog standaloneCatalogWithPriceOverride = new StandaloneCatalogWithPriceOverride(catalog, priceOverride, internalCallContext.getTenantRecordId(), internalCallContextFactory); try { final PlanSpecifier specWithNullProduct = new PlanSpecifier("INVALID", BillingPeriod.ANNUAL, "DEFAULT"); standaloneCatalogWithPriceOverride.createOrFindCurrentPlan(specWithNullProduct, null); Assert.fail(); } catch (final CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PRODUCT.getCode()); } } }
@Test(groups = "slow") public void testCreatePlanNoProduct() throws Exception { final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class); final StaticCatalog standaloneCatalogWithPriceOverride = new StandaloneCatalogWithPriceOverride(catalog, priceOverride, internalCallContext.getTenantRecordId(), internalCallContextFactory); try { final PlanSpecifier specWithNullProduct = new PlanSpecifier(null, BillingPeriod.ANNUAL, "DEFAULT"); standaloneCatalogWithPriceOverride.createOrFindCurrentPlan(specWithNullProduct, null); Assert.fail(); } catch (final CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_NULL_PRODUCT_NAME.getCode()); } }
@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/842") public void testCreateAmbiguousPlan() throws Exception { final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarAdvanced.xml").toExternalForm(), StandaloneCatalog.class); final StaticCatalog standaloneCatalogWithPriceOverride = new StandaloneCatalogWithPriceOverride(catalog, priceOverride, internalCallContext.getTenantRecordId(),