@Override public BigDecimal getPrice(final Currency currency) throws CatalogApiException { if (prices.length == 0) { return BigDecimal.ZERO; } for (final Price p : prices) { if (p.getCurrency() == currency) { return p.getValue(); } } throw new CatalogApiException(ErrorCode.CAT_NO_PRICE_FOR_CURRENCY, currency); }
found = ((Enum) input).name().equals(((Enum) newEntry).name()); } else if (input instanceof Price) { found = ((Price) input).getCurrency().equals(((Price) newEntry).getCurrency()); } else { throw new IllegalStateException("Unexpected type " + newEntry.getClass());
private DefaultPrice toDefaultPrice(final Price input) { try { final DefaultPrice result = new DefaultPrice(); result.setCurrency(input.getCurrency()); result.setValue(input.getValue()); return result; } catch (CurrencyValueNull currencyValueNull) { throw new IllegalStateException(currencyValueNull); } }
@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, this.getClass(), ""); } try { if (p.getValue().doubleValue() < 0.0) { errors.add("Negative value for price in currency: " + currency, this.getClass(), ""); } } catch (CurrencyValueNull e) { // No currency => nothing to check, ignore exception } } return errors; }
@Test(groups = "fast") public void testUsageCapacityInAdvance() { try { final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("UsageExperimental.xml").toExternalForm(), StandaloneCatalog.class); final Usage[] usages = getUsages(catalog, "capacity-in-advance-monthly"); assertEquals(usages.length, 1); final Usage usage = usages[0]; assertEquals(usage.getName(), "capacity-in-advance-monthly-usage1"); assertEquals(usage.getBillingPeriod(), BillingPeriod.MONTHLY); assertEquals(usage.getUsageType(), UsageType.CAPACITY); assertEquals(usage.getBillingMode(), BillingMode.IN_ADVANCE); assertEquals(usage.getTierBlockPolicy(), TierBlockPolicy.ALL_TIERS); assertEquals(usage.getBlocks().length, 0); assertEquals(usage.getTiers().length, 0); assertEquals(usage.getLimits().length, 1); assertEquals(usage.getLimits()[0].getUnit().getName(), "members"); assertEquals(usage.getLimits()[0].getMax(), new Double("100")); assertEquals(usage.getRecurringPrice().getPrices().length, 1); assertEquals(usage.getRecurringPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getRecurringPrice().getPrices()[0].getValue(), new BigDecimal("100.00")); } catch (Exception e) { Assert.fail(e.toString()); } }
@Test(groups = "fast") public void testUsageConsumableInAdvanceTopUp() { try { final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("UsageExperimental.xml").toExternalForm(), StandaloneCatalog.class); final Usage[] usages = getUsages(catalog, "consumable-in-advance-topup"); assertEquals(usages.length, 1); final Usage usage = usages[0]; assertEquals(usage.getName(), "consumable-in-advance-topup-usage1"); assertEquals(usage.getBillingPeriod(), BillingPeriod.NO_BILLING_PERIOD); assertEquals(usage.getUsageType(), UsageType.CONSUMABLE); assertEquals(usage.getBillingMode(), BillingMode.IN_ADVANCE); assertEquals(usage.getLimits().length, 0); assertEquals(usage.getTiers().length, 0); assertEquals(usage.getBlocks().length, 1); assertEquals(usage.getBlocks()[0].getUnit().getName(), "fastrack-tokens"); assertEquals(usage.getBlocks()[0].getSize(), new Double("10")); assertEquals(usage.getBlocks()[0].getPrice().getPrices().length, 1); assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getValue(), new BigDecimal("0.10")); assertEquals(usage.getBlocks()[0].getMinTopUpCredit(), new Double("5")); } catch (Exception e) { Assert.fail(e.toString()); } }
@Test(groups = "fast") public void testUsageConsumableInAdvancePrepayCredit() { try { final StandaloneCatalog catalog = XMLLoader.getObjectFromString(Resources.getResource("UsageExperimental.xml").toExternalForm(), StandaloneCatalog.class); final Usage[] usages = getUsages(catalog, "consumable-in-advance-prepay-credit-monthly"); assertEquals(usages.length, 1); final Usage usage = usages[0]; assertEquals(usage.getName(), "consumable-in-advance-prepay-credit-monthly-usage1"); assertEquals(usage.getBillingPeriod(), BillingPeriod.MONTHLY); assertEquals(usage.getUsageType(), UsageType.CONSUMABLE); assertEquals(usage.getBillingMode(), BillingMode.IN_ADVANCE); assertEquals(usage.getLimits().length, 0); assertEquals(usage.getTiers().length, 0); assertEquals(usage.getBlocks().length, 1); assertEquals(usage.getBlocks()[0].getUnit().getName(), "cell-phone-minutes"); assertEquals(usage.getBlocks()[0].getSize(), new Double("1000")); assertEquals(usage.getBlocks()[0].getPrice().getPrices().length, 1); assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getBlocks()[0].getPrice().getPrices()[0].getValue(), new BigDecimal("0.10")); } catch (Exception e) { Assert.fail(e.toString()); } }
private void assertTieredBlockInternationalPrice(final InternationalPrice newInternationalPrice, final InternationalPrice initInternationalPrice, final TieredBlockPriceOverride override) throws CurrencyValueNull { assertEquals(newInternationalPrice.getPrices().length, initInternationalPrice.getPrices().length); for (int i = 0; i < newInternationalPrice.getPrices().length; i++) { final Price initPrice = initInternationalPrice.getPrices()[i]; final Price newPrice = newInternationalPrice.getPrices()[i]; if (override != null && override.getCurrency() == initPrice.getCurrency() && override.getPrice() != null) { assertEquals(newPrice.getValue().compareTo(override.getPrice()), 0); } else { if (initPrice != null && initPrice.getValue() != null) { assertEquals(newPrice.getValue().compareTo(initPrice.getValue()), 0); } } } } }
@Test(groups = "fast") public void testAddPlanOnExistingCatalog() throws Exception { final StandaloneCatalog originalCatalog = XMLLoader.getObjectFromString(Resources.getResource("SpyCarBasic.xml").toExternalForm(), StandaloneCatalog.class); assertEquals(originalCatalog.getPriceLists().getAllPriceLists().size(), 1); assertEquals(originalCatalog.getPriceLists().getAllPriceLists().get(0).getName(), new PriceListDefault().getName()); assertEquals(originalCatalog.getPriceLists().getAllPriceLists().get(0).getPlans().size(), 3); final CatalogUpdater catalogUpdater = new CatalogUpdater(originalCatalog); final SimplePlanDescriptor desc = new DefaultSimplePlanDescriptor("standard-annual", "Standard", ProductCategory.BASE, Currency.USD, BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); catalogUpdater.addSimplePlanDescriptor(desc); final StandaloneCatalog catalog = catalogUpdater.getCatalog(); final Plan plan = catalog.findCurrentPlan("standard-annual"); assertEquals(plan.getName(), "standard-annual"); assertEquals(plan.getInitialPhases().length, 0); assertEquals(plan.getFinalPhase().getPhaseType(), PhaseType.EVERGREEN); assertNull(plan.getFinalPhase().getFixed()); assertEquals(plan.getFinalPhase().getName(), "standard-annual-evergreen"); assertEquals(plan.getFinalPhase().getRecurring().getBillingPeriod(), BillingPeriod.MONTHLY); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices().length, 1); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getValue(), BigDecimal.TEN); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getCurrency(), Currency.USD); assertEquals(catalog.getPriceLists().getAllPriceLists().size(), 1); final PriceList priceList = catalog.getPriceLists().getAllPriceLists().get(0); assertEquals(priceList.getName(), new PriceListDefault().getName()); assertEquals(priceList.getPlans().size(), 4); }
@Test(groups = "fast") public void testAddNoTrialPlanOnFirstCatalog() throws CatalogApiException { final DateTime now = clock.getUTCNow(); final SimplePlanDescriptor desc = new DefaultSimplePlanDescriptor("foo-monthly", "Foo", ProductCategory.BASE, Currency.EUR, BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final CatalogUpdater catalogUpdater = new CatalogUpdater(now, desc.getCurrency()); catalogUpdater.addSimplePlanDescriptor(desc); final StandaloneCatalog catalog = catalogUpdater.getCatalog(); assertEquals(catalog.getCurrentProducts().size(), 1); final Product product = catalog.getCurrentProducts().iterator().next(); assertEquals(product.getName(), "Foo"); assertEquals(product.getCategory(), ProductCategory.BASE); assertEquals(catalog.getCurrentPlans().size(), 1); final Plan plan = catalog.findCurrentPlan("foo-monthly"); assertEquals(plan.getName(), "foo-monthly"); assertEquals(plan.getInitialPhases().length, 0); assertEquals(plan.getFinalPhase().getPhaseType(), PhaseType.EVERGREEN); assertNull(plan.getFinalPhase().getFixed()); assertEquals(plan.getFinalPhase().getName(), "foo-monthly-evergreen"); assertEquals(plan.getFinalPhase().getRecurring().getBillingPeriod(), BillingPeriod.MONTHLY); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices().length, 1); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getValue(), BigDecimal.TEN); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getCurrency(), Currency.EUR); assertEquals(catalog.getPriceLists().getAllPriceLists().size(), 1); final PriceList priceList = catalog.getPriceLists().getAllPriceLists().get(0); assertEquals(priceList.getName(), new PriceListDefault().getName()); assertEquals(priceList.getPlans().size(), 1); assertEquals(priceList.getPlans().iterator().next().getName(), "foo-monthly"); }
assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getMax(), new Double("10000")); assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getPrice().getPrices().length, 1); assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getTiers()[0].getTieredBlocks()[0].getPrice().getPrices()[0].getValue(), new BigDecimal("0.5")); assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getMax(), new Double("512000")); assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getPrice().getPrices().length, 1); assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getTiers()[0].getTieredBlocks()[1].getPrice().getPrices()[0].getValue(), new BigDecimal("0.3")); } catch (Exception e) {
private void assertInternationalPrice(final InternationalPrice newInternationalPrice, final InternationalPrice initInternationalPrice, final PlanPhasePriceOverride override, final boolean isFixed) throws CatalogApiException { if (initInternationalPrice.getPrices().length == 0) { if (override != null) { assertEquals(newInternationalPrice.getPrices().length, 1); assertEquals(newInternationalPrice.getPrice(override.getCurrency()).compareTo(isFixed ? override.getFixedPrice() : override.getRecurringPrice()), 0); } } else { assertEquals(newInternationalPrice.getPrices().length, initInternationalPrice.getPrices().length); for (int i = 0; i < newInternationalPrice.getPrices().length; i++) { final Price initPrice = initInternationalPrice.getPrices()[i]; final Price newPrice = newInternationalPrice.getPrices()[i]; if (override != null && override.getCurrency() == initPrice.getCurrency() && ((isFixed && override.getFixedPrice() != null) || (!isFixed && override.getRecurringPrice() != null))) { assertEquals(newPrice.getValue().compareTo(isFixed ? override.getFixedPrice() : override.getRecurringPrice()), 0); } else { if (initPrice != null && initPrice.getValue() != null) { assertEquals(newPrice.getValue().compareTo(initPrice.getValue()), 0); } } } } }
assertEquals(usage.getTiers()[0].getLimits()[1].getMax(), new Double("500")); assertEquals(usage.getTiers()[0].getFixedPrice().getPrices().length, 1); assertEquals(usage.getTiers()[0].getFixedPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getTiers()[0].getFixedPrice().getPrices()[0].getValue(), new BigDecimal("0.007")); assertEquals(usage.getTiers()[0].getRecurringPrice().getPrices().length, 1); assertEquals(usage.getTiers()[0].getRecurringPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getTiers()[0].getRecurringPrice().getPrices()[0].getValue(), new BigDecimal("0.8")); assertEquals(usage.getTiers()[1].getLimits()[1].getMax(), new Double("1000")); assertEquals(usage.getTiers()[1].getFixedPrice().getPrices().length, 1); assertEquals(usage.getTiers()[1].getFixedPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getTiers()[1].getFixedPrice().getPrices()[0].getValue(), new BigDecimal("0.4")); assertEquals(usage.getTiers()[1].getRecurringPrice().getPrices().length, 1); assertEquals(usage.getTiers()[1].getRecurringPrice().getPrices()[0].getCurrency(), Currency.BTC); assertEquals(usage.getTiers()[1].getRecurringPrice().getPrices()[0].getValue(), new BigDecimal("1.2"));
assertEquals(plan.getInitialPhases()[0].getPhaseType(), PhaseType.TRIAL); assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices().length, 1); assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getCurrency(), Currency.EUR); assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getValue(), BigDecimal.ZERO); assertEquals(plan.getInitialPhases()[0].getName(), "foo-monthly-trial"); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices().length, 1); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getValue(), BigDecimal.TEN); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getCurrency(), Currency.EUR);
assertEquals(plan.getInitialPhases()[0].getPhaseType(), PhaseType.TRIAL); assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices().length, 1); assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getCurrency(), Currency.USD); assertEquals(plan.getInitialPhases()[0].getFixed().getPrice().getPrices()[0].getValue(), BigDecimal.ZERO); assertEquals(plan.getFinalPhase().getRecurring().getBillingPeriod(), BillingPeriod.MONTHLY); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices().length, 1); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getCurrency(), Currency.USD); assertEquals(plan.getFinalPhase().getRecurring().getRecurringPrice().getPrices()[0].getValue(), BigDecimal.TEN);
@Override public BigDecimal getPrice(final Currency currency) throws CatalogApiException { if (prices.length == 0) { return BigDecimal.ZERO; } for (final Price p : prices) { if (p.getCurrency() == currency) { return p.getValue(); } } throw new CatalogApiException(ErrorCode.CAT_NO_PRICE_FOR_CURRENCY, currency); }
private static List<PriceJson> buildPrices(final InternationalPrice internationalPrice) throws CurrencyValueNull { List<PriceJson> pricesJson = new ArrayList<PriceJson>(); Price[] prices = (internationalPrice != null) ? internationalPrice.getPrices() : null; if (prices != null && prices.length > 0) { for (int i=0; i < prices.length; i++) { pricesJson.add(new PriceJson(prices[i].getCurrency(), prices[i].getValue())); } } return pricesJson; }
@Override public PriceJson apply(final Price price) { try { return new PriceJson(price); } catch (final CurrencyValueNull e) { return new PriceJson(price.getCurrency(), BigDecimal.ZERO); } } });
private DefaultPrice toDefaultPrice(final Price input) { try { final DefaultPrice result = new DefaultPrice(); result.setCurrency(input.getCurrency()); result.setValue(input.getValue()); return result; } catch (CurrencyValueNull currencyValueNull) { throw new IllegalStateException(currencyValueNull); } }
private void assertTieredBlockInternationalPrice(final InternationalPrice newInternationalPrice, final InternationalPrice initInternationalPrice, final TieredBlockPriceOverride override) throws CurrencyValueNull { assertEquals(newInternationalPrice.getPrices().length, initInternationalPrice.getPrices().length); for (int i = 0; i < newInternationalPrice.getPrices().length; i++) { final Price initPrice = initInternationalPrice.getPrices()[i]; final Price newPrice = newInternationalPrice.getPrices()[i]; if (override != null && override.getCurrency() == initPrice.getCurrency() && override.getPrice() != null) { assertEquals(newPrice.getValue().compareTo(override.getPrice()), 0); } else { if (initPrice != null && initPrice.getValue() != null) { assertEquals(newPrice.getValue().compareTo(initPrice.getValue()), 0); } } } } }