Search in sources :

Example 1 with ValidationErrors

use of org.killbill.xmlloader.ValidationErrors in project killbill by killbill.

the class TestPlanPhase method testValidation.

@Test(groups = "fast")
public void testValidation() {
    final MockCatalog catalog = new MockCatalog();
    DefaultPlanPhase pp = MockPlanPhase.createUSDMonthlyEvergreen(null, "1.00").setPlan(MockPlan.createBicycleNoTrialEvergreen1USD());
    pp.initialize(catalog, null);
    ValidationErrors errors = pp.validate(catalog, new ValidationErrors());
    errors.log(log);
    Assert.assertEquals(errors.size(), 1);
    pp = MockPlanPhase.createUSDMonthlyEvergreen("1.00", null).setRecurring(new MockRecurring(BillingPeriod.NO_BILLING_PERIOD, MockInternationalPrice.createUSD("1.00")).setPhase(pp)).setPlan(MockPlan.createBicycleNoTrialEvergreen1USD());
    pp.initialize(catalog, null);
    errors = pp.validate(catalog, new ValidationErrors());
    errors.log(log);
    Assert.assertEquals(errors.size(), 1);
}
Also used : ValidationErrors(org.killbill.xmlloader.ValidationErrors) Test(org.testng.annotations.Test)

Example 2 with ValidationErrors

use of org.killbill.xmlloader.ValidationErrors 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 3 with ValidationErrors

use of org.killbill.xmlloader.ValidationErrors in project killbill by killbill.

the class TestInternationalPrice method testNegativeValuePrices.

@Test(groups = "fast")
public void testNegativeValuePrices() {
    final StandaloneCatalog c = new MockCatalog();
    c.setSupportedCurrencies(new Currency[] { Currency.GBP, Currency.EUR, Currency.USD, Currency.BRL, Currency.MXN });
    final DefaultInternationalPrice p1 = new MockInternationalPrice();
    p1.setPrices(new DefaultPrice[] { new DefaultPrice().setCurrency(Currency.GBP).setValue(new BigDecimal(-1)), new DefaultPrice().setCurrency(Currency.EUR).setValue(new BigDecimal(-1)), new DefaultPrice().setCurrency(Currency.USD).setValue(new BigDecimal(-1)), new DefaultPrice().setCurrency(Currency.BRL).setValue(new BigDecimal(1)), new DefaultPrice().setCurrency(Currency.MXN).setValue(new BigDecimal(1)) });
    final ValidationErrors errors = p1.validate(c, new ValidationErrors());
    errors.log(log);
    Assert.assertEquals(errors.size(), 3);
}
Also used : ValidationErrors(org.killbill.xmlloader.ValidationErrors) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with ValidationErrors

use of org.killbill.xmlloader.ValidationErrors in project killbill by killbill.

the class TestPlan method testDateValidation.

@Test(groups = "fast")
public void testDateValidation() {
    final StandaloneCatalog c = new MockCatalog();
    c.setSupportedCurrencies(new Currency[] { Currency.GBP, Currency.EUR, Currency.USD, Currency.BRL, Currency.MXN });
    final DefaultPlan p1 = MockPlan.createBicycleTrialEvergreen1USD();
    p1.setPlansAllowedInBundle(-1);
    p1.setEffectiveDateForExistingSubscriptions(new Date((new Date().getTime()) - (1000 * 60 * 60 * 24)));
    final ValidationErrors errors = p1.validate(c, new ValidationErrors());
    Assert.assertEquals(errors.size(), 3);
    errors.log(log);
}
Also used : ValidationErrors(org.killbill.xmlloader.ValidationErrors) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

ValidationErrors (org.killbill.xmlloader.ValidationErrors)4 Test (org.testng.annotations.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)1 VersionedCatalog (org.killbill.billing.catalog.VersionedCatalog)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 InvalidConfigException (org.killbill.billing.catalog.api.InvalidConfigException)1 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)1 TenantApiException (org.killbill.billing.tenant.api.TenantApiException)1 ValidationException (org.killbill.xmlloader.ValidationException)1 SAXException (org.xml.sax.SAXException)1