@Test(groups = "fast") public void testEquals() throws Exception { final UUID accountId = UUID.randomUUID(); final UUID tenantId = UUID.randomUUID(); final String userName = UUID.randomUUID().toString(); final DateTime createdDate = clock.getUTCNow(); final String reasonCode = UUID.randomUUID().toString(); final String comment = UUID.randomUUID().toString(); final UUID userToken = UUID.randomUUID(); final DefaultCallContext callContext = new DefaultCallContext(accountId, tenantId, userName, createdDate, reasonCode, comment, userToken); Assert.assertEquals(callContext, callContext); final DefaultCallContext sameCallContext = new DefaultCallContext(accountId, tenantId, userName, createdDate, reasonCode, comment, userToken); Assert.assertEquals(sameCallContext, callContext); final DefaultCallContext otherCallContext = new DefaultCallContext(accountId, tenantId, UUID.randomUUID().toString(), createdDate, reasonCode, comment, userToken); Assert.assertNotEquals(otherCallContext, callContext); } }
@Test(groups = "slow") public void testCancelPlanOnPendingSubscription3() throws SubscriptionBaseApiException { final String baseProduct = "Shotgun"; final BillingPeriod baseTerm = BillingPeriod.MONTHLY; final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME; final LocalDate startDate = clock.getUTCToday().plusDays(5); final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList, startDate); assertEquals(subscription.getState(), Entitlement.EntitlementState.PENDING); assertEquals(subscription.getStartDate().compareTo(startDate.toDateTime(accountData.getReferenceTime())), 0); subscription.cancelWithPolicy(BillingActionPolicy.IMMEDIATE, callContext); testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.CANCEL); clock.addDays(5); assertListenerStatus(); final DefaultSubscriptionBase subscription2 = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); assertEquals(subscription2.getStartDate().compareTo(subscription.getStartDate()), 0); assertEquals(subscription2.getState(), Entitlement.EntitlementState.CANCELLED); assertNull(subscription2.getCurrentPlan()); }
private void resetClockToStartOfTest(final ClockMock clock) { clock.resetDeltaFromReality(); // Date at which all tests start-- we create the date object here after the system properties which set the JVM in UTC have been set. final DateTime testStartDate = new DateTime(2012, 5, 7, 0, 3, 42, 0); clock.setDeltaFromReality(testStartDate.getMillis() - clock.getUTCNow().getMillis()); }
@Test(groups = "fast", description = "Verify Mockito setup in GuicyKillbillTestSuite") public void testSpy() { clock.setDay(new LocalDate("2012-08-01")); Assert.assertEquals(clock.getUTCToday().toString(), "2012-08-01"); Assert.assertEquals(theRealClock.getUTCToday().toString(), "2012-08-01"); clock.addMonths(1); Assert.assertEquals(clock.getUTCToday().toString(), "2012-09-01"); Assert.assertEquals(theRealClock.getUTCToday().toString(), "2012-09-01"); clock.addDays(30); Assert.assertEquals(clock.getUTCToday().toString(), "2012-10-01"); Assert.assertEquals(theRealClock.getUTCToday().toString(), "2012-10-01"); } }
@Test(groups = "slow", retryAnalyzer = FlakyRetryAnalyzer.class) public void testFixUp() throws Exception { testListener.throwRetryableException = true; final DateTime startTime = clock.getUTCNow(); testListener.handleEvent(event); assertListenerStatus(); Assert.assertEquals(testListener.eventsSeen.size(), 0); List<NotificationEventWithMetadata> futureRetryableEvents = getFutureRetryableEvents(); Assert.assertEquals(futureRetryableEvents.size(), 1); Assert.assertEquals(futureRetryableEvents.get(0).getEffectiveDate().compareTo(startTime.plus(RETRY_SCHEDULE.get(0))), 0); clock.setTime(futureRetryableEvents.get(0).getEffectiveDate()); assertListenerStatus(); Assert.assertEquals(testListener.eventsSeen.size(), 0); futureRetryableEvents = getFutureRetryableEvents(); Assert.assertEquals(futureRetryableEvents.size(), 1); Assert.assertEquals(futureRetryableEvents.get(0).getEffectiveDate().compareTo(startTime.plus(RETRY_SCHEDULE.get(1))), 0); testListener.throwRetryableException = false; clock.setTime(futureRetryableEvents.get(0).getEffectiveDate()); assertListenerStatus(); Assert.assertEquals(testListener.eventsSeen.size(), 1); Assert.assertEquals(getFutureRetryableEvents().size(), 0); }
@Test(groups = "fast") public void testEquals() throws Exception { final TableName tableName = TableName.ACCOUNT_EMAIL_HISTORY; final long recordId = Long.MAX_VALUE; final ChangeType changeType = ChangeType.DELETE; final EntityAudit entityAudit = new EntityAudit(tableName, recordId, changeType, null); final UUID accountId = UUID.randomUUID(); final UUID tenantId = UUID.randomUUID(); final String userName = UUID.randomUUID().toString(); final CallOrigin callOrigin = CallOrigin.EXTERNAL; final UserType userType = UserType.CUSTOMER; final UUID userToken = UUID.randomUUID(); final DefaultCallContext callContext = new DefaultCallContext(accountId, tenantId, userName, callOrigin, userType, userToken, clock); final AuditLogModelDao auditLog = new AuditLogModelDao(entityAudit, callContext); Assert.assertEquals(auditLog, auditLog); final AuditLogModelDao sameAuditLog = new AuditLogModelDao(entityAudit, callContext); Assert.assertEquals(sameAuditLog, auditLog); clock.addMonths(1); final DefaultCallContext otherCallContext = new DefaultCallContext(accountId, tenantId, userName, callOrigin, userType, userToken, clock); final AuditLogModelDao otherAuditLog = new AuditLogModelDao(entityAudit, otherCallContext); Assert.assertNotEquals(otherAuditLog, auditLog); } }
@Test(groups = "slow") public void testAddTaxItems() throws Exception { final BigDecimal taxItemAmount = BigDecimal.TEN; final UUID bundleId = UUID.randomUUID(); final InvoiceItem taxItem = new TaxInvoiceItem(null, accountId, bundleId, UUID.randomUUID().toString(), clock.getUTCToday(), taxItemAmount, accountCurrency); final List<InvoiceItem> resultTaxInvoiceItems = invoiceUserApi.insertTaxItems(accountId, clock.getUTCToday(), ImmutableList.<InvoiceItem>of(taxItem), true, null, callContext); Assert.assertEquals(resultTaxInvoiceItems.size(), 1); Assert.assertEquals(resultTaxInvoiceItems.get(0).getAmount().compareTo(taxItemAmount), 0); Assert.assertEquals(resultTaxInvoiceItems.get(0).getBundleId(), bundleId); }
protected AuditLog createAuditLog() { final AuditLog auditLog = Mockito.mock(AuditLog.class); final DateTime utcNow = clock.getUTCNow(); Mockito.when(auditLog.getCreatedDate()).thenReturn(utcNow); Mockito.when(auditLog.getReasonCode()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getUserName()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getUserToken()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getComment()).thenReturn(UUID.randomUUID().toString()); Mockito.when(auditLog.getChangeType()).thenReturn(ChangeType.DELETE); return auditLog; } }
@Test(groups = "fast") public void testEventsForCancelledSubscriptionBeforeTransfer() throws Exception { final DateTime subscriptionStartTime = clock.getUTCNow(); final DateTime subscriptionCancelTime = subscriptionStartTime.plusDays(1); final ImmutableList<ExistingEvent> existingEvents = ImmutableList.<ExistingEvent>of(createEvent(subscriptionStartTime, SubscriptionBaseTransitionType.CREATE), createEvent(subscriptionCancelTime, SubscriptionBaseTransitionType.CANCEL)); final SubscriptionBuilder subscriptionBuilder = new SubscriptionBuilder(); final DefaultSubscriptionBase subscription = new DefaultSubscriptionBase(subscriptionBuilder); final DateTime transferDate = subscriptionStartTime.plusDays(10); final List<SubscriptionBaseEvent> events = transferApi.toEvents(existingEvents, subscription, transferDate, catalog, internalCallContext); Assert.assertEquals(events.size(), 0); }
@Test(groups = "fast", description="Test Account event deserialization") public void testDefaultAccountChangeEvent() throws Exception { final List<ChangedField> changes = new ArrayList<ChangedField>(); changes.add(new DefaultChangedField("fieldXX", "valueX", "valueXXX", clock.getUTCNow())); changes.add(new DefaultChangedField("fieldYY", "valueY", "valueYYY", clock.getUTCNow())); final AccountChangeInternalEvent e = new DefaultAccountChangeEvent(changes, UUID.randomUUID(), 1L, 2L, null); final String json = mapper.writeValueAsString(e); final Class<?> claz = Class.forName("org.killbill.billing.account.api.user.DefaultAccountChangeEvent"); final Object obj = mapper.readValue(json, claz); Assert.assertTrue(obj.equals(e)); }
@Test(groups = "slow") public void testWithBCDOnOperations() throws Exception { final DateTime initialDate = new DateTime(2018, 6, 21, 0, 13, 42, 0, testTimeZone); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis()); final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(21)); assertNotNull(account); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial"); busHandler.pushExpectedEvents( NextEvent.CREATE, NextEvent.BLOCK, NextEvent.BCD_CHANGE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); // We will realign the BCD on the 15 as we create the subscription - ignoring the account setting on 21. final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec, 15, null), null, null, null, false, false, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2018, 6, 21), new LocalDate(2018, 7, 15), InvoiceItemType.RECURRING, new BigDecimal("15.96"))); // Verify next month busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addDays(24); // 2018-7-15 assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2018, 7, 15), new LocalDate(2018, 8, 15), InvoiceItemType.RECURRING, new BigDecimal("19.95"))); final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext); final PlanPhaseSpecifier spec2 = new PlanPhaseSpecifier("blowdart-monthly-notrial"); // Change plan EOT // We will now realign the BCD on the 21 as we change the plan for the subscription. entitlement.changePlan(new DefaultEntitlementSpecifier(spec2, 21, null), ImmutableList.<PluginProperty>of(), callContext); busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.BCD_CHANGE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addMonths(1); assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2018, 8, 15), new LocalDate(2018, 8, 21), InvoiceItemType.RECURRING, new BigDecimal("5.80"))); }
@Test(groups = "slow") public void testCreateBaseWithDifferentInThePast() throws AccountApiException, EntitlementApiException, SubscriptionApiException { final LocalDate initialDate = new LocalDate(2013, 8, 7); clock.setDay(initialDate); final Account account = createAccount(getAccountData(7)); final LocalDate entitlementDate = initialDate.minusDays(3); final LocalDate billingDate = initialDate.minusDays(5); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null); testListener.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE); final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), entitlementDate, billingDate, false, true, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext); assertEquals(entitlement.getState(), EntitlementState.ACTIVE); assertEquals(entitlement.getEffectiveStartDate(), entitlementDate); }
@Test(groups = "fast") public void testFutureEffectiveDateWithBCDEqualsComputedEffectiveDay() throws Exception { // Set by test as : 2012-05-07T00:03:42.000Z final DateTime now = clock.getUTCNow(); int newBCD = 3; // effectiveDate = 2012-05-07T00:03:42.000Z => ComputedEffectiveDay = 7 LocalDate effectiveDate = new LocalDate(2012, 7, 3); // newBCD == ComputedEffectiveDay final DateTime result = ((DefaultSubscriptionInternalApi) subscriptionInternalApi).getEffectiveDateForNewBCD(newBCD, effectiveDate, internalCallContext); Assert.assertEquals(result, internalCallContext.toUTCDateTime(new LocalDate("2012-07-03"))); }
@Test(groups = "slow") public void testCreateSubscriptionWithBCD() throws SubscriptionBaseApiException { final DateTime init = clock.getUTCNow(); this.internalCallContext.getTenantRecordId()); final Iterable<EntitlementSpecifier> specifiers = ImmutableList.<EntitlementSpecifier>of(new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("shotgun-monthly"), 18, null)); final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(bundle.getId(), bundle.getExternalKey(), specifiers, init.toLocalDate(), false); final List<SubscriptionBaseWithAddOns> subscriptionBaseWithAddOns = subscriptionInternalApi.createBaseSubscriptionsWithAddOns(ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), false, internalCallContext); testListener.assertListenerStatus(); assertEquals(subscriptionBaseWithAddOns.size(), 1); assertEquals(subscriptionBaseWithAddOns.get(0).getSubscriptionBaseList().size(), 1); assertNotNull(subscription); assertNotNull(subscription.getBillCycleDayLocal()); assertEquals(subscription.getBillCycleDayLocal().intValue(), 18);
@Test(groups = "slow", expectedExceptions = EntitlementApiException.class) public void testCreateBaseSubscriptionsWithAddOnsMissingBase() throws AccountApiException, EntitlementApiException, SubscriptionApiException { final LocalDate initialDate = new LocalDate(2013, 8, 7); clock.setDay(initialDate); final Account account = createAccount(getAccountData(7)); final String bundleKey2 = "bundleKey2"; final EntitlementSpecifier spec22 = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null)); final List<EntitlementSpecifier> specs2 = ImmutableList.of(spec22); final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier2 = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey2, specs2, null, null, false); final Iterable<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifiers = ImmutableList.of(baseEntitlementWithAddOnsSpecifier2); entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), baseEntitlementWithAddOnsSpecifiers, true, ImmutableList.<PluginProperty>of(), callContext); }
@Test(groups = "slow") public void testBundleExternalKeyReused() throws Exception { final DateTime startDate = clock.getUTCNow(); final DateTime createdDate = startDate.plusSeconds(10); assertEquals(result.size(), 1); assertEquals(result.get(0).getExternalKey(), bundle.getExternalKey()); assertEquals(result2.size(), 1); testListener.pushExpectedEvents(NextEvent.CREATE); final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = new DefaultSubscriptionBaseWithAddOns(bundle, ImmutableList.<SubscriptionBase>of(subscription)); dao.createSubscriptionsWithAddOns(ImmutableList.<SubscriptionBaseWithAddOns>of(subscriptionBaseWithAddOns), ImmutableMap.<UUID, List<SubscriptionBaseEvent>>of(subscription.getId(), ImmutableList.<SubscriptionBaseEvent>of(creationEvent)), catalog, internalCallContext);
@Test(groups = "slow") public void testSubscriptionCreationWithExternalKeyOverLimit() throws AccountApiException, SubscriptionApiException, EntitlementApiException { final String externalKey = "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 LocalDate initialDate = new LocalDate(2013, 8, 7); clock.setDay(initialDate); final Account account = createAccount(getAccountData(7)); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null); //2013-08-07 final LocalDate effectiveDate = initialDate.plusMonths(1); try { entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), externalKey, effectiveDate, effectiveDate, false, true, ImmutableList.<PluginProperty>of(), callContext); Assert.fail(); } catch (final EntitlementApiException e) { assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } }
@Test(groups = "slow") public void testCancelSubscription_START_OF_TERM() throws SubscriptionBaseApiException { // Set date in such a way that Phase align with the first of the month (and so matches our hardcoded accountData account BCD) final DateTime testStartDate = new DateTime(2016, 11, 1, 0, 3, 42, 0); clock.setDeltaFromReality(testStartDate.getMillis() - clock.getUTCNow().getMillis()); final String prod = "Shotgun"; final BillingPeriod term = BillingPeriod.MONTHLY; final String planSet = PriceListSet.DEFAULT_PRICELIST_NAME; // CREATE DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, prod, term, planSet); PlanPhase currentPhase = subscription.getCurrentPhase(); assertEquals(currentPhase.getPhaseType(), PhaseType.TRIAL); // Move out of TRIAL testListener.pushExpectedEvent(NextEvent.PHASE); clock.addDays(30); assertListenerStatus(); // Artificially set the CTD final Duration ctd = testUtil.getDurationMonth(1); final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(clock.getUTCNow(), ctd); subscriptionInternalApi.setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext); subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); // Move ahead a bit abd cancel START_OF_TERM clock.addDays(5); testListener.pushExpectedEvent(NextEvent.CANCEL); subscription.cancelWithPolicy(BillingActionPolicy.START_OF_TERM, callContext); assertListenerStatus(); subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); Assert.assertEquals(subscription.getAllTransitions().get(subscription.getAllTransitions().size() - 1).getTransitionType(), SubscriptionBaseTransitionType.CANCEL); Assert.assertEquals(new LocalDate(subscription.getAllTransitions().get(subscription.getAllTransitions().size() - 1).getEffectiveTransitionTime(), accountData.getTimeZone()), new LocalDate(2016, 12, 1)); }
@Test(groups = "slow") public void testTransferBPInTrialWithNoCTD() throws Exception { final String baseProduct = "Shotgun"; clock.addDays(20); final DateTime beforeTransferDate = clock.getUTCNow(); final DateTime transferRequestedDate = clock.getUTCNow(); transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, false, false, callContext); assertListenerStatus(); final DateTime afterTransferDate = clock.getUTCNow(); assertNotNull(oldBaseSubscription.getEndDate()); testUtil.assertDateWithin(oldBaseSubscription.getEndDate(), beforeTransferDate, afterTransferDate); assertTrue(oldBaseSubscription.getEndDate().compareTo(transferRequestedDate) == 0); assertEquals(bundlesForAccountAndKey.size(), 1); assertTrue(((DefaultSubscriptionBase) newBaseSubscription).getAlignStartDate().compareTo(((DefaultSubscriptionBase) oldBaseSubscription).getAlignStartDate()) == 0); assertEquals(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).size(), 2); assertTrue(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).get(1).getEffectiveTransitionTime().compareTo(evergreenPhaseDate) == 0);
clock.addDays(3); final String aoProduct1 = "Telescopic-Scope"; final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY; final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, aoProduct1, aoTerm1, basePriceList); assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE); clock.addDays(25); final String aoProduct2 = "Laser-Scope"; final BillingPeriod aoTerm2 = BillingPeriod.MONTHLY; final DefaultSubscriptionBase aoSubscription2 = testUtil.createSubscription(bundle, aoProduct2, aoTerm2, basePriceList); assertEquals(aoSubscription2.getState(), EntitlementState.ACTIVE); clock.addDays(5); assertListenerStatus(); final DateTime transferRequestedDate = clock.getUTCNow(); testListener.pushExpectedEvent(NextEvent.TRANSFER); testListener.pushExpectedEvent(NextEvent.TRANSFER); testListener.pushExpectedEvent(NextEvent.CANCEL); testListener.pushExpectedEvent(NextEvent.CANCEL); clock.addMonths(1); assertListenerStatus(); final DateTime newTransferRequestedDate = clock.getUTCNow(); testListener.pushExpectedEvent(NextEvent.CANCEL); testListener.pushExpectedEvent(NextEvent.TRANSFER);