private DefaultPlan maybeGetOverriddenPlan(final String planName) throws CatalogApiException { final InternalTenantContext internalTenantContext = createInternalTenantContext(); try { return priceOverride.getOverriddenPlan(planName, this, internalTenantContext); } catch (final RuntimeException e) { if (e.getCause() == null || e.getCause().getCause() == null || !(e.getCause().getCause() instanceof CatalogApiException) || ((CatalogApiException) e.getCause().getCause()).getCode() != ErrorCode.CAT_NO_SUCH_PLAN.getCode()) { throw e; } else { // Otherwise, ambiguous name? See https://github.com/killbill/killbill/issues/842. return null; } } }
private void tCreateSubscriptionInternal(@Nullable final SubscriptionBaseBundle bundle, @Nullable final String productName, @Nullable final BillingPeriod term, final String planSet, final ErrorCode expected) { try { testUtil.createSubscription(true, bundle, productName, term, planSet); Assert.fail("Exception expected, error code: " + expected); } catch (final SubscriptionBaseApiException e) { assertEquals(e.getCode(), expected.getCode()); } }
private void remove_AUTO_INVOICE_OFF_on_clear(final UUID accountId, final InternalCallContext context) throws OverdueApiException { try { tagApi.removeTag(accountId, ObjectType.ACCOUNT, ControlTagType.AUTO_INVOICING_OFF.getId(), context); } catch (final TagApiException e) { if (e.getCode() != ErrorCode.TAG_DOES_NOT_EXIST.getCode()) { throw new OverdueApiException(e); } } }
private void addBadSimplePlanDescriptor(final CatalogUpdater catalogUpdater, final SimplePlanDescriptor desc) { try { catalogUpdater.addSimplePlanDescriptor(desc); fail("Should have failed to add invalid desc " + desc); } catch (final CatalogApiException e) { assertEquals(e.getCode(), ErrorCode.CAT_FAILED_SIMPLE_PLAN_VALIDATION.getCode()); } } }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final InternalCallContext context) throws TagApiException { final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, context); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
private void testInvalidPermissionScenario(final List<String> permissions) { try { securityApi.addRoleDefinition("failed", permissions, callContext); Assert.fail("Should fail permissions " + permissions + " were invalid"); } catch (SecurityApiException expected) { Assert.assertEquals(expected.getCode(), ErrorCode.SECURITY_INVALID_PERMISSIONS.getCode()); } }
@Override public ValidationErrors validate(final DefaultOverdueConfig 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.", this.getClass(), ""); } } return errors; }
protected void assertionException(final DefaultCaseResult cr, final String productName, final ProductCategory productCategory, final BillingPeriod bp, final String priceListName, final PhaseType phaseType, final StandaloneCatalog cat) { try { Assert.assertNull(cr.getResult(new PlanPhaseSpecifier(productName, bp, priceListName, phaseType), cat)); Assert.fail("Exception expected"); } catch (CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_PRICE_LIST_NOT_FOUND.getCode()); } }
@Test(groups = "fast") public void testErrorOnDateTooEarly() throws CatalogApiException { // We find it although the date provided is too early because we default to first catalog version vc.findPlan("shotgun-monthly", dt0); try { // We **don't find it** because date is too early and not part of first catalog version vc.findPlan("shotgun-quarterly", dt0); Assert.fail("Date is too early an exception should have been thrown"); } catch (final CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PLAN.getCode()); } }
protected void assertionException(final DefaultCaseResult cr, final String productName, final ProductCategory productCategory, final BillingPeriod bp, final String priceListName, final StandaloneCatalog cat) { try { cr.getResult(new PlanSpecifier(productName, bp, priceListName), cat); Assert.fail("Expecting an exception"); } catch (CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_PRICE_LIST_NOT_FOUND.getCode()); } }
@Test(groups = "fast") public void testUncancelBadState() throws SubscriptionBaseApiException { final SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); try { subscription.uncancel(callContext); Assert.fail("Exception expected, error code: " + ErrorCode.SUB_UNCANCEL_BAD_STATE); } catch (final SubscriptionBaseApiException e) { assertEquals(e.getCode(), ErrorCode.SUB_UNCANCEL_BAD_STATE.getCode()); } assertListenerStatus(); } }
@Test(groups = "slow", description = "Test Account DAO: duplicate emails throws an exception") public void testHandleDuplicateEmails() throws AccountApiException { final UUID accountId = UUID.randomUUID(); final AccountEmail email = new DefaultAccountEmail(accountId, "test@gmail.com"); Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 0); final AccountEmailModelDao accountEmailModelDao = new AccountEmailModelDao(email); accountDao.addEmail(accountEmailModelDao, internalCallContext); Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 1); try { accountDao.addEmail(accountEmailModelDao, internalCallContext); Assert.fail(); } catch (AccountApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.ACCOUNT_EMAIL_ALREADY_EXISTS.getCode()); } }
protected void assertionException(final DefaultCaseChangeResult cr, final String fromProductName, final String toProductName, final BillingPeriod fromBp, final BillingPeriod toBp, final String fromPriceListName, final String toPriceListName, final PhaseType phaseType, final StandaloneCatalog cat) { try { cr.getResult(new PlanPhaseSpecifier(fromProductName, fromBp, fromPriceListName, phaseType), new PlanSpecifier(toProductName, toBp, toPriceListName), cat); Assert.fail("Expecting an exception"); } catch (CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_PRICE_LIST_NOT_FOUND.getCode()); } }
@Test(groups = "fast") public void testWithDeletedPlan() throws CatalogApiException { // We find it because this is version 2 whose effectiveDate is "2011-02-02T00:00:00+00:00" vc.findPlan("shotgun-quarterly", dt2); try { // We **don't find it** because date provided matches version 3 where plan was removed vc.findPlan("shotgun-quarterly", dt3); Assert.fail("Plan has been removed"); } catch (final CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PLAN.getCode()); } // Similar test but for existing subscription: we want to find the plan in the original catalog in this case. // This would be called for instance when computing billing events (dt3 could be a future PHASE event for instance) vc.findPlan("shotgun-quarterly", dt3, dt1); }
@Test(groups = "slow", description = "Test Tenant creation with External Key over limit") public void testCreateTenantWithExternalKeyOverLimit() throws Exception { final TenantData tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,.", "TTR445ee2", "dskjhfs^^54R"); try { tenantUserApi.createTenant(tenantdata, callContext); Assert.fail(); } catch (final TenantApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } } }
@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()); } }
@Test(groups = "slow", description = "Test Account creation with External Key over limit") public void testCreateAccountWithExternalKeyOverLimit() throws Exception { AccountModelDao accountModelDao = createTestAccount(); // Set an externalKey of 256 characters (over limit) accountModelDao.setExternalKey("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,."); final AccountData accountData = new DefaultAccount(accountModelDao); try { accountUserApi.createAccount(accountData, callContext); Assert.fail(); } catch (final AccountApiException e) { assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } }
private Period getReevaluationInterval(final OverdueStateSet overdueStateSet, final OverdueState nextOverdueState) throws OverdueException { try { if (nextOverdueState.isClearState()) { return overdueStateSet.getInitialReevaluationInterval(); } else { return nextOverdueState.getAutoReevaluationInterval().toJodaPeriod(); } } catch (final OverdueApiException e) { if (e.getCode() == ErrorCode.OVERDUE_NO_REEVALUATION_INTERVAL.getCode()) { return null; } else { throw new OverdueException(e); } } }
@Test(groups = "slow") public void testCreatePlanInvalidProduct() 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("INVALID", BillingPeriod.ANNUAL, "DEFAULT"); standaloneCatalogWithPriceOverride.createOrFindCurrentPlan(specWithNullProduct, null); Assert.fail(); } catch (final CatalogApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_SUCH_PRODUCT.getCode()); } } }
@Test(groups = "fast") public void testChangeSubscriptionNonActive() throws SubscriptionBaseApiException { final SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME); testListener.pushExpectedEvent(NextEvent.CANCEL); subscription.cancelWithDate(clock.getUTCNow(), callContext); assertListenerStatus(); try { final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); subscription.changePlanWithDate(new DefaultEntitlementSpecifier(planPhaseSpecifier), clock.getUTCNow(), callContext); Assert.fail("Exception expected, error code: " + ErrorCode.SUB_CHANGE_NON_ACTIVE); } catch (final SubscriptionBaseApiException e) { assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_NON_ACTIVE.getCode()); } }