public void add(final String description, final URI catalogURI, final Class<?> objectType, final String objectName) { add(new ValidationError(description, catalogURI, objectType, objectName)); }
public static <T extends ValidatingConfig<T>> void validate(final URI uri, final T c) throws ValidationException { c.initialize(c, uri); final ValidationErrors errs = c.validate(c, new ValidationErrors()); log.info("Errors: " + errs.size() + " for " + uri); if (errs.size() > 0) { throw new ValidationException(errs); } }
public static <T extends ValidatingConfig<T>> T getObjectFromStream(final URI uri, final InputStream stream, final Class<T> clazz) throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException, ValidationException { if (stream == null) { return null; } final Object o = unmarshaller(clazz).unmarshal(stream); if (clazz.isInstance(o)) { @SuppressWarnings("unchecked") final T castObject = (T) o; try { validate(uri, castObject); } catch (ValidationException e) { e.getErrors().log(log); System.err.println(e.getErrors().toString()); throw e; } return castObject; } else { return null; } }
@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.setEffectiveDateForExistingSubscriptons(new Date((new Date().getTime()) - (1000 * 60 * 60 * 24))); final ValidationErrors errors = p1.validate(c, new ValidationErrors()); Assert.assertEquals(errors.size(), 1); errors.log(log); }
@Override public void printStackTrace(final PrintStream arg0) { arg0.print(errors.toString()); super.printStackTrace(arg0); }
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { for (final StandaloneCatalog c : versions) { errors.addAll(c.validate(c, errors)); } //TODO MDW validation - ensure all catalog versions have a single name //TODO MDW validation - ensure effective dates are different (actually do we want this?) //TODO MDW validation - check that all products are there //TODO MDW validation - check that all plans are there //TODO MDW validation - check that all currencies are there //TODO MDW validation - check that all pricelists are there return errors; }
@Test(groups = "fast") public void testValidation() { DefaultPlanPhase pp = MockPlanPhase.createUSDMonthlyEvergreen(null, "1.00").setPlan(MockPlan.createBicycleNoTrialEvergreen1USD());//new MockPlanPhase().setBillCycleDuration(BillingPeriod.MONTHLY).setRecurringPrice(null).setFixedPrice(new DefaultInternationalPrice()); ValidationErrors errors = pp.validate(new MockCatalog(), new ValidationErrors()); errors.log(log); Assert.assertEquals(errors.size(), 1); pp = MockPlanPhase.createUSDMonthlyEvergreen("1.00", null).setBillCycleDuration(BillingPeriod.NO_BILLING_PERIOD).setPlan(MockPlan.createBicycleNoTrialEvergreen1USD());// new MockPlanPhase().setBillCycleDuration(BillingPeriod.NO_BILLING_PERIOD).setRecurringPrice(new MockInternationalPrice()); errors = pp.validate(new MockCatalog(), new ValidationErrors()); errors.log(log); Assert.assertEquals(errors.size(), 2); pp = MockPlanPhase.createUSDMonthlyEvergreen(null, null).setBillCycleDuration(BillingPeriod.NO_BILLING_PERIOD).setPlan(MockPlan.createBicycleNoTrialEvergreen1USD());//new MockPlanPhase().setRecurringPrice(null).setFixedPrice(null).setBillCycleDuration(BillingPeriod.NO_BILLING_PERIOD); errors = pp.validate(new MockCatalog(), new ValidationErrors()); errors.log(log); Assert.assertEquals(errors.size(), 2); }
@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); } }
@Override public ValidationErrors validate(final OverdueConfig root, final ValidationErrors errors) { if (name.length() > MAX_NAME_LENGTH) { errors.add(new ValidationError(String.format("Name of state '%s' exceeds the maximum length of %d", name, MAX_NAME_LENGTH), root.getURI(), DefaultOverdueState.class, name)); } return errors; }
@Override public ValidationErrors validate(StandaloneCatalog root, ValidationErrors errors) { if(max == null && min == null) { errors.add(new ValidationError("max and min cannot both be ommitted",root.getCatalogURI(), Limit.class, "")); } else if (max != null && min != null && max.doubleValue() < min.doubleValue()) { errors.add(new ValidationError("max must be greater than min",root.getCatalogURI(), Limit.class, "")); } return errors; }
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { //Validation: TimeUnit UNLIMITED iff number == -1 if ((unit == TimeUnit.UNLIMITED && number != -1)) { errors.add(new ValidationError("Duration can only have 'UNLIMITED' unit if the number is omitted.", catalog.getCatalogURI(), DefaultPlanPhase.class, "")); } //TODO MDW - Validation TimeUnit UNLIMITED iff number == -1 return errors; }
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { if (effectiveDateForExistingSubscriptons != null && catalog.getEffectiveDate().getTime() > effectiveDateForExistingSubscriptons.getTime()) { errors.add(new ValidationError(String.format("Price effective date %s is before catalog effective date '%s'", effectiveDateForExistingSubscriptons, catalog.getEffectiveDate().getTime()), catalog.getCatalogURI(), DefaultInternationalPrice.class, "")); } return errors; }
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { final Currency[] supportedCurrencies = catalog.getCurrentSupportedCurrencies(); for (final Price p : prices) { final Currency currency = p.getCurrency(); if (!currencyIsSupported(currency, supportedCurrencies)) { errors.add("Unsupported currency: " + currency, catalog.getCatalogURI(), this.getClass(), ""); } try { if (p.getValue().doubleValue() < 0.0) { errors.add("Negative value for price in currency: " + currency, catalog.getCatalogURI(), this.getClass(), ""); } } catch (CurrencyValueNull e) { // No currency => nothing to check, ignore exception } } return errors; }
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { super.validate(catalog, errors); if (!getName().equals(PriceListSet.DEFAULT_PRICELIST_NAME)) { errors.add(new ValidationError("The name of the default pricelist must be 'DEFAULT'", catalog.getCatalogURI(), DefaultPriceList.class, getName())); } return errors; }
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { defaultPricelist.validate(catalog, errors); //Check that the default pricelist name is not in use in the children for (final DefaultPriceList pl : childPriceLists) { if (pl.getName().equals(PriceListSet.DEFAULT_PRICELIST_NAME)) { errors.add(new ValidationError("Pricelists cannot use the reserved name '" + PriceListSet.DEFAULT_PRICELIST_NAME + "'", catalog.getCatalogURI(), DefaultPriceListSet.class, pl.getName())); } pl.validate(catalog, errors); // and validate the individual pricelists } return errors; }
@Override public ValidationErrors validate(final OverdueConfig root, final ValidationErrors errors) { for (final DefaultOverdueState state : getStates()) { state.validate(root, errors); } try { getClearState(); } catch (OverdueApiException e) { if (e.getCode() == ErrorCode.CAT_MISSING_CLEAR_STATE.getCode()) { errors.add("Overdue state set is missing a clear state.", root.getURI(), this.getClass(), ""); } } return errors; }
errors.add(new ValidationError(String.format("Phase %s of plan %s has a recurring price but no billing period", type.toString(), plan.getName()), catalog.getCatalogURI(), DefaultPlanPhase.class, type.toString())); errors.add(new ValidationError(String.format("Phase %s of plan %s has a recurring price but no billing period", type.toString(), plan.getName()), catalog.getCatalogURI(), DefaultPlanPhase.class, type.toString())); errors.add(new ValidationError(String.format("Phase %s of plan %s has no recurring price but does have a billing period. The billing period should be set to '%s'", type.toString(), plan.getName(), BillingPeriod.NO_BILLING_PERIOD), catalog.getCatalogURI(), DefaultPlanPhase.class, type.toString())); errors.add(new ValidationError(String.format("Phase %s of plan %s has no billing period. It must have a fixed price set.", type.toString(), plan.getName()), catalog.getCatalogURI(), DefaultPlanPhase.class, type.toString())); errors.add(new ValidationError(String.format("Phase %s of plan %s has neither a recurring price or a fixed price.", type.toString(), plan.getName()), catalog.getCatalogURI(), DefaultPlanPhase.class, type.toString()));
@Override public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) { for (final DefaultPlan cur : getPlans()) { final int numPlans = findNumberOfPlans(cur.getProduct(), cur.getBillingPeriod()); if (numPlans > 1) { errors.add(new ValidationError( String.format("There are %d plans in pricelist %s and have the same product/billingPeriod (%s, %s)", numPlans, getName(), cur.getProduct().getName(), cur.getBillingPeriod()), catalog.getCatalogURI(), DefaultPriceListSet.class, getName())); } } return errors; }