Search in sources :

Example 6 with StaticCatalog

use of org.killbill.billing.catalog.api.StaticCatalog in project killbill by killbill.

the class TestIntegrationWithCatalogUpdate method testError_CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST.

@Test(groups = "slow")
public void testError_CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST() throws Exception {
    // Create a per-tenant catalog with one plan
    final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("zoe-monthly", "Zoe", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
    catalogUserApi.addSimplePlan(desc1, init, testCallContext);
    StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
    assertEquals(catalog.getCurrentPlans().size(), 1);
    final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("zoe-14-monthly", "Zoe", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 14, TimeUnit.DAYS, ImmutableList.<String>of());
    catalogUserApi.addSimplePlan(desc2, init, testCallContext);
    catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
    assertEquals(catalog.getCurrentPlans().size(), 2);
    try {
        final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Zoe", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
        entitlementApi.createBaseEntitlement(account.getId(), spec, UUID.randomUUID().toString(), null, null, null, false, ImmutableList.<PluginProperty>of(), testCallContext);
        fail("Creating entitlement should fail");
    } catch (final EntitlementApiException e) {
        assertEquals(e.getCode(), ErrorCode.CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST.getCode());
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) SimplePlanDescriptor(org.killbill.billing.catalog.api.SimplePlanDescriptor) DefaultSimplePlanDescriptor(org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor) DefaultSimplePlanDescriptor(org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor) Test(org.testng.annotations.Test)

Example 7 with StaticCatalog

use of org.killbill.billing.catalog.api.StaticCatalog in project killbill by killbill.

the class TestIntegrationWithCatalogUpdate method testWithThirtyDaysPlan.

// Use custom plan definition to create a THIRTY_DAYS plan with no trial and test issue #598
@Test(groups = "slow")
public void testWithThirtyDaysPlan() throws Exception {
    // Create a per-tenant catalog with one plan
    final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("thirty-monthly", "Thirty", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.THIRTY_DAYS, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
    catalogUserApi.addSimplePlan(desc1, init, testCallContext);
    StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
    assertEquals(catalog.getCurrentPlans().size(), 1);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("thirty-monthly", null);
    createEntitlement(spec, null, true);
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, testCallContext);
    assertEquals(invoices.size(), 1);
    assertEquals(invoices.get(0).getChargedAmount().compareTo(BigDecimal.TEN), 0);
    assertEquals(invoices.get(0).getInvoiceItems().size(), 1);
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2016, 6, 1), new LocalDate(2016, 7, 1), InvoiceItemType.RECURRING, BigDecimal.TEN));
    invoiceChecker.checkInvoiceNoAudits(invoices.get(0), callContext, expectedInvoices);
    int invoiceSize = 2;
    LocalDate startDate = new LocalDate(2016, 7, 1);
    for (int i = 0; i < 14; i++) {
        expectedInvoices.clear();
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
        clock.addDays(30);
        assertListenerStatus();
        LocalDate endDate = startDate.plusDays(30);
        invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, testCallContext);
        assertEquals(invoices.size(), invoiceSize);
        expectedInvoices.add(new ExpectedInvoiceItemCheck(startDate, endDate, InvoiceItemType.RECURRING, BigDecimal.TEN));
        invoiceChecker.checkInvoiceNoAudits(invoices.get(invoices.size() - 1), callContext, expectedInvoices);
        startDate = endDate;
        invoiceSize++;
    }
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Invoice(org.killbill.billing.invoice.api.Invoice) ArrayList(java.util.ArrayList) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) SimplePlanDescriptor(org.killbill.billing.catalog.api.SimplePlanDescriptor) DefaultSimplePlanDescriptor(org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor) DefaultSimplePlanDescriptor(org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor) Test(org.testng.annotations.Test)

Example 8 with StaticCatalog

use of org.killbill.billing.catalog.api.StaticCatalog in project killbill by killbill.

the class DefaultCatalogUserApi method uploadCatalog.

@Override
public void uploadCatalog(final String catalogXML, final CallContext callContext) throws CatalogApiException {
    final InternalTenantContext internalTenantContext = createInternalTenantContext(callContext);
    try {
        final VersionedCatalog versionedCatalog = (VersionedCatalog) catalogService.getFullCatalog(false, true, internalTenantContext);
        // Validation purpose:  Will throw if bad XML or catalog validation fails
        final InputStream stream = new ByteArrayInputStream(catalogXML.getBytes());
        final StaticCatalog newCatalogVersion = XMLLoader.getObjectFromStream(new URI("dummy"), stream, StandaloneCatalog.class);
        if (versionedCatalog != null) {
            // currentCatalog.getCatalogName() could be null if tenant was created with a default catalog
            if (versionedCatalog.getCatalogName() != null && !newCatalogVersion.getCatalogName().equals(versionedCatalog.getCatalogName())) {
                final ValidationErrors errors = new ValidationErrors();
                errors.add(String.format("Catalog name '%s' should match previous catalog name '%s'", newCatalogVersion.getCatalogName(), versionedCatalog.getCatalogName()), new URI("dummy"), StandaloneCatalog.class, "");
                // Bummer ValidationException CTOR is private to package...
                //final ValidationException validationException = new ValidationException(errors);
                //throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
                logger.info("Failed to load new catalog version: " + errors.toString());
                throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
            }
            for (StandaloneCatalog c : versionedCatalog.getVersions()) {
                if (c.getEffectiveDate().compareTo(newCatalogVersion.getEffectiveDate()) == 0) {
                    final ValidationErrors errors = new ValidationErrors();
                    errors.add(String.format("Catalog version for effectiveDate '%s' already exists", newCatalogVersion.getEffectiveDate()), new URI("dummy"), StandaloneCatalog.class, "");
                    // Bummer ValidationException CTOR is private to package...
                    //final ValidationException validationException = new ValidationException(errors);
                    //throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
                    logger.info("Failed to load new catalog version: " + errors.toString());
                    throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
                }
            }
        }
        catalogCache.clearCatalog(internalTenantContext);
        tenantApi.addTenantKeyValue(TenantKey.CATALOG.toString(), catalogXML, callContext);
    } catch (final TenantApiException e) {
        throw new CatalogApiException(e);
    } catch (final ValidationException e) {
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
    } catch (final JAXBException e) {
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    } catch (final TransformerException e) {
        throw new IllegalStateException(e);
    } catch (final URISyntaxException e) {
        throw new IllegalStateException(e);
    } catch (final SAXException e) {
        throw new IllegalStateException(e);
    } catch (final InvalidConfigException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ValidationException(org.killbill.xmlloader.ValidationException) ValidationErrors(org.killbill.xmlloader.ValidationErrors) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvalidConfigException(org.killbill.billing.catalog.api.InvalidConfigException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) URI(java.net.URI) SAXException(org.xml.sax.SAXException) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) ByteArrayInputStream(java.io.ByteArrayInputStream) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) TransformerException(javax.xml.transform.TransformerException)

Example 9 with StaticCatalog

use of org.killbill.billing.catalog.api.StaticCatalog in project killbill by killbill.

the class TestStandaloneCatalogWithPriceOverride method testCreatePlanNoProduct.

@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());
    }
}
Also used : CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) PlanSpecifier(org.killbill.billing.catalog.api.PlanSpecifier) Test(org.testng.annotations.Test)

Example 10 with StaticCatalog

use of org.killbill.billing.catalog.api.StaticCatalog in project killbill by killbill.

the class OverriddenPlanCacheLoader method load.

@Override
public Object load(final Object key, final Object argument) {
    checkCacheLoaderStatus();
    if (!(key instanceof String)) {
        throw new IllegalArgumentException("Unexpected key type of " + key.getClass().getName());
    }
    if (!(argument instanceof CacheLoaderArgument)) {
        throw new IllegalArgumentException("Unexpected argument type of " + argument.getClass().getName());
    }
    final CacheLoaderArgument cacheLoaderArgument = (CacheLoaderArgument) argument;
    if (cacheLoaderArgument.getArgs() == null || cacheLoaderArgument.getArgs().length != 2) {
        throw new IllegalArgumentException("Invalid arguments for overridden plans");
    }
    if (!(cacheLoaderArgument.getArgs()[0] instanceof LoaderCallback)) {
        throw new IllegalArgumentException("Invalid arguments for overridden plans: missing loaderCallback from argument");
    }
    if (!(cacheLoaderArgument.getArgs()[1] instanceof StaticCatalog)) {
        throw new IllegalArgumentException("Invalid arguments for overridden plans: missing catalog from argument");
    }
    final String planName = (String) key;
    final LoaderCallback callback = (LoaderCallback) cacheLoaderArgument.getArgs()[0];
    final StaticCatalog catalog = (StaticCatalog) cacheLoaderArgument.getArgs()[1];
    final InternalTenantContext internalTenantContext = ((CacheLoaderArgument) argument).getInternalTenantContext();
    try {
        log.info("Loading overridden plan {} for tenant {}", planName, internalTenantContext.getTenantRecordId());
        return callback.loadPlan(planName, catalog, internalTenantContext);
    } catch (final CatalogApiException e) {
        throw new IllegalStateException(String.format("Failed to load overridden plan for tenant %s : %s", planName, internalTenantContext.getTenantRecordId()), e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog)

Aggregations

StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)14 Test (org.testng.annotations.Test)9 SimplePlanDescriptor (org.killbill.billing.catalog.api.SimplePlanDescriptor)7 DefaultSimplePlanDescriptor (org.killbill.billing.catalog.api.user.DefaultSimplePlanDescriptor)7 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)5 Entitlement (org.killbill.billing.entitlement.api.Entitlement)5 ArrayList (java.util.ArrayList)4 LocalDate (org.joda.time.LocalDate)4 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 BigDecimal (java.math.BigDecimal)3 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)2 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)2 Listing (org.killbill.billing.catalog.api.Listing)2 Subscription (org.killbill.billing.entitlement.api.Subscription)2