Search in sources :

Example 1 with DefaultVersionedCatalog

use of org.killbill.billing.catalog.DefaultVersionedCatalog in project killbill by killbill.

the class TestDefaultCatalogCache method testDefaultCatalog.

// 
// Verify CatalogCache returns default catalog when system property has been set (and CatalogCache has been initialized)
// 
@Test(groups = "fast")
public void testDefaultCatalog() throws CatalogApiException {
    catalogCache.loadDefaultCatalog(Resources.getResource("org/killbill/billing/catalog/SpyCarBasic.xml").toExternalForm());
    final VersionedCatalog result = catalogCache.getCatalog(true, true, false, internalCallContext);
    Assert.assertNotNull(result);
    final StaticCatalog catalogVersion = result.getVersions().get(result.getVersions().size() - 1);
    final Collection<Product> products = catalogVersion.getProducts();
    Assert.assertEquals(products.size(), 3);
    // Verify the lookup with other contexts
    final DefaultVersionedCatalog resultForMultiTenantContext = new DefaultVersionedCatalog();
    for (final StaticCatalog 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());
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) Product(org.killbill.billing.catalog.api.Product) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) Test(org.testng.annotations.Test)

Example 2 with DefaultVersionedCatalog

use of org.killbill.billing.catalog.DefaultVersionedCatalog in project killbill by killbill.

the class TestDefaultSubscriptionTransferApi method beforeMethod.

@Override
@BeforeMethod(groups = "fast")
public void beforeMethod() throws Exception {
    super.beforeMethod();
    final SubscriptionDao dao = Mockito.mock(SubscriptionDao.class);
    final DefaultVersionedCatalog versionedCatalog = new DefaultVersionedCatalog();
    final MockCatalog mockCatalog = new MockCatalog();
    versionedCatalog.add(mockCatalog);
    final CatalogService catalogService = new MockCatalogService(versionedCatalog);
    final CatalogInternalApi catalogInternalApiWithMockCatalogService = new DefaultCatalogInternalApi(catalogService);
    final SubscriptionCatalogApi subscriptionCatalogInternalApiWithMockCatalogService = new DefaultSubscriptionCatalogApi(catalogInternalApiWithMockCatalogService, clock);
    final SubscriptionBaseApiService apiService = Mockito.mock(SubscriptionBaseApiService.class);
    final SubscriptionBaseTimelineApi timelineApi = Mockito.mock(SubscriptionBaseTimelineApi.class);
    transferApi = new DefaultSubscriptionBaseTransferApi(clock, dao, timelineApi, subscriptionCatalogInternalApiWithMockCatalogService, subscriptionInternalApi, apiService, internalCallContextFactory);
    // Overrride catalog with our MockCatalog
    this.catalog = DefaultSubscriptionCatalogApi.wrapCatalog(versionedCatalog, clock);
}
Also used : DefaultCatalogInternalApi(org.killbill.billing.catalog.api.DefaultCatalogInternalApi) CatalogInternalApi(org.killbill.billing.catalog.api.CatalogInternalApi) DefaultSubscriptionCatalogApi(org.killbill.billing.subscription.catalog.DefaultSubscriptionCatalogApi) SubscriptionCatalogApi(org.killbill.billing.subscription.catalog.SubscriptionCatalogApi) MockCatalog(org.killbill.billing.catalog.MockCatalog) DefaultSubscriptionCatalogApi(org.killbill.billing.subscription.catalog.DefaultSubscriptionCatalogApi) SubscriptionBaseTimelineApi(org.killbill.billing.subscription.api.timeline.SubscriptionBaseTimelineApi) MockCatalogService(org.killbill.billing.catalog.MockCatalogService) CatalogService(org.killbill.billing.catalog.api.CatalogService) SubscriptionBaseApiService(org.killbill.billing.subscription.api.SubscriptionBaseApiService) SubscriptionDao(org.killbill.billing.subscription.engine.dao.SubscriptionDao) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) MockCatalogService(org.killbill.billing.catalog.MockCatalogService) DefaultCatalogInternalApi(org.killbill.billing.catalog.api.DefaultCatalogInternalApi) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with DefaultVersionedCatalog

use of org.killbill.billing.catalog.DefaultVersionedCatalog in project killbill by killbill.

the class VersionedCatalogLoader method loadDefaultCatalog.

@Override
public VersionedCatalog loadDefaultCatalog(final String uriString) throws CatalogApiException {
    try {
        final List<URI> xmlURIs;
        if (uriString.endsWith(XML_EXTENSION)) {
            // Assume its an xml file
            xmlURIs = new ArrayList<URI>();
            xmlURIs.add(new URI(uriString));
        } else {
            // Assume its a directory
            final URL url = getURLFromString(uriString);
            final String directoryContents = UriAccessor.accessUriAsString(uriString);
            xmlURIs = findXmlReferences(directoryContents, url);
        }
        final DefaultVersionedCatalog result = new DefaultVersionedCatalog();
        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));
        }
        // Perform initialization and validation for VersionedCatalog
        XMLLoader.initializeAndValidate(result);
        return result;
    } catch (final ValidationException e) {
        logger.warn("Failed to load default catalog", e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
    } catch (final JAXBException e) {
        logger.warn("Failed to load default catalog", e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
    } catch (IllegalArgumentException e) {
        logger.warn("Failed to load default catalog", e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
    } catch (Exception e) {
        logger.warn("Failed to load default catalog", e);
        throw new IllegalStateException(e);
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) ValidationException(org.killbill.xmlloader.ValidationException) JAXBException(javax.xml.bind.JAXBException) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) URI(java.net.URI) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) MalformedURLException(java.net.MalformedURLException) JAXBException(javax.xml.bind.JAXBException) ExecutionException(java.util.concurrent.ExecutionException) ValidationException(org.killbill.xmlloader.ValidationException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PriceOverride(org.killbill.billing.catalog.override.PriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)

Example 4 with DefaultVersionedCatalog

use of org.killbill.billing.catalog.DefaultVersionedCatalog in project killbill by killbill.

the class VersionedCatalogLoader method load.

public VersionedCatalog load(final Collection<String> catalogXMLs, final boolean filterTemplateCatalog, final Long tenantRecordId) throws CatalogApiException {
    try {
        final Collection<Future<StandaloneCatalog>> catalogs = new ArrayList<Future<StandaloneCatalog>>(catalogXMLs.size());
        for (final String cur : catalogXMLs) {
            catalogs.add(executorService.submit(new Callable<StandaloneCatalog>() {

                @Override
                public StandaloneCatalog call() throws Exception {
                    final InputStream curCatalogStream = new ByteArrayInputStream(cur.getBytes());
                    final StandaloneCatalog catalog = XMLLoader.getObjectFromStream(curCatalogStream, StandaloneCatalog.class);
                    if (!filterTemplateCatalog || !catalog.isTemplateCatalog()) {
                        return new StandaloneCatalogWithPriceOverride(catalog, priceOverride, tenantRecordId, internalCallContextFactory);
                    }
                    return null;
                }
            }));
        }
        final DefaultVersionedCatalog result = new DefaultVersionedCatalog();
        for (final Future<StandaloneCatalog> standaloneCatalogFuture : catalogs) {
            final StandaloneCatalog catalog = standaloneCatalogFuture.get();
            if (catalog != null) {
                result.add(catalog);
            }
        }
        XMLLoader.initializeAndValidate(result);
        return result;
    } catch (final ValidationException e) {
        logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
        for (final ValidationError ve : e.getErrors()) {
            logger.warn(ve.toString());
        }
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
    } catch (final InterruptedException e) {
        logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
    } catch (final ExecutionException e) {
        logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
        throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) ValidationException(org.killbill.xmlloader.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) Callable(java.util.concurrent.Callable) ByteArrayInputStream(java.io.ByteArrayInputStream) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Future(java.util.concurrent.Future) ValidationError(org.killbill.xmlloader.ValidationError) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with DefaultVersionedCatalog

use of org.killbill.billing.catalog.DefaultVersionedCatalog in project killbill by killbill.

the class TestXMLWriter method testVersionedCatalog.

// Verifies we can generate the XML associated with a VersionedCatalog
@Test(groups = "fast")
public void testVersionedCatalog() throws Exception {
    final StandaloneCatalog catalog = getCatalog("SpyCarAdvanced.xml");
    final DefaultVersionedCatalog versionedCatalog = new DefaultVersionedCatalog();
    versionedCatalog.add(catalog);
    final Class serializableClass = versionedCatalog.getClass();
    final String newCatalogStr = XMLWriter.writeXML(versionedCatalog, serializableClass);
// System.err.println(newCatalogStr);
}
Also used : StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) DefaultVersionedCatalog(org.killbill.billing.catalog.DefaultVersionedCatalog) Test(org.testng.annotations.Test)

Aggregations

DefaultVersionedCatalog (org.killbill.billing.catalog.DefaultVersionedCatalog)8 StandaloneCatalogWithPriceOverride (org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)4 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)3 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)3 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)3 ValidationException (org.killbill.xmlloader.ValidationException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 JAXBException (javax.xml.bind.JAXBException)2 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)2 PriceOverride (org.killbill.billing.catalog.override.PriceOverride)2 Test (org.testng.annotations.Test)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1