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 = "slow") public void test() throws Exception { final UUID accountId = new UUID(0L, 1L); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(accountId); Mockito.when(accountApi.getImmutableAccountDataByRecordId(Mockito.eq(internalCallContext.getAccountRecordId()), Mockito.<InternalTenantContext>any())).thenReturn(account); final DateTime now = clock.getUTCNow(); final DateTime readyTime = now.plusMillis(2000); final OverdueCheckNotificationKey notificationKey = new OverdueCheckNotificationKey(accountId); checkPoster.insertOverdueNotification(accountId, readyTime, OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE, notificationKey, internalCallContext); // Move time in the future after the notification effectiveDate clock.setDeltaFromReality(3000); await().atMost(5, SECONDS).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return mockDispatcher.getEventCount() == 1; } }); Assert.assertEquals(mockDispatcher.getEventCount(), 1); Assert.assertEquals(mockDispatcher.getLatestAccountId(), accountId); } }
@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)); }
private void resetClockToStartOfTest(final ClockMock clock) { // 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()); }
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()); }
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
assertListenerStatus(); clock.setDeltaFromReality(1000 * 3600 * 24);
@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"))); }
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
@Test(groups = "slow") public void testApiHistory() throws Exception { final String overdueStateName = "WayPassedItMan"; final String service = "TEST"; final boolean blockChange = true; final boolean blockEntitlement = false; final boolean blockBilling = false; final Account account = createAccount(getAccountData(7)); testListener.pushExpectedEvent(NextEvent.BLOCK); final BlockingState state1 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, overdueStateName, service, blockChange, blockEntitlement, blockBilling, clock.getUTCNow()); blockingInternalApi.setBlockingState(state1, internalCallContext); assertListenerStatus(); clock.setDeltaFromReality(1000 * 3600 * 24); testListener.pushExpectedEvent(NextEvent.BLOCK); final String overdueStateName2 = "NoReallyThisCantGoOn"; final BlockingState state2 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, overdueStateName2, service, blockChange, blockEntitlement, blockBilling, clock.getUTCNow()); blockingInternalApi.setBlockingState(state2, internalCallContext); assertListenerStatus(); final List<BlockingState> blockingAll = blockingInternalApi.getBlockingAllForAccount(catalog, internalCallContext); final List<BlockingState> history = ImmutableList.<BlockingState>copyOf(Collections2.<BlockingState>filter(blockingAll, new Predicate<BlockingState>() { @Override public boolean apply(final BlockingState input) { return input.getService().equals(service); } })); Assert.assertEquals(history.size(), 2); Assert.assertEquals(history.get(0).getStateName(), overdueStateName); Assert.assertEquals(history.get(1).getStateName(), overdueStateName2); }
@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 testBlockingStatesV2() throws Exception { final DateTime initialDate = new DateTime(2017, 3, 1, 0, 1, 35, 0, DateTimeZone.UTC); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis()); final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(0)); assertNotNull(account); final BlockingState blockingState1 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "state1", "Service", false, false, true, null); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial", null); // Unlike the previous scenario, we create the subscription and set the blocking state at the same time busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK); subscriptionApi.addBlockingState(blockingState1, null, ImmutableList.<PluginProperty>of(), callContext); entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "bundleExternalKey", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); clock.addMonths(1); busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); final BlockingState blockingState2 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "state2", "Service", false, false, false, null); subscriptionApi.addBlockingState(blockingState2, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addMonths(1); assertListenerStatus(); }
@Test(groups = "slow") public void testApi() { final UUID uuid = UUID.randomUUID(); final String overdueStateName = "WayPassedItMan"; final String service = "TEST"; final boolean blockChange = true; final boolean blockEntitlement = false; final boolean blockBilling = false; testListener.pushExpectedEvent(NextEvent.BLOCK); final BlockingState state1 = new DefaultBlockingState(uuid, BlockingStateType.ACCOUNT, overdueStateName, service, blockChange, blockEntitlement, blockBilling, clock.getUTCNow()); blockingInternalApi.setBlockingState(state1, internalCallContext); assertListenerStatus(); clock.setDeltaFromReality(1000 * 3600 * 24); testListener.pushExpectedEvent(NextEvent.BLOCK); final String overdueStateName2 = "NoReallyThisCantGoOn"; final BlockingState state2 = new DefaultBlockingState(uuid, BlockingStateType.ACCOUNT, overdueStateName2, service, blockChange, blockEntitlement, blockBilling, clock.getUTCNow()); blockingInternalApi.setBlockingState(state2, internalCallContext); assertListenerStatus(); Assert.assertEquals(blockingInternalApi.getBlockingStateForService(uuid, BlockingStateType.ACCOUNT, service, internalCallContext).getStateName(), overdueStateName2); }
@Test(groups = "slow") public void testBlockingStatesV1() throws Exception { final DateTime initialDate = new DateTime(2017, 3, 1, 0, 1, 35, 0, DateTimeZone.UTC); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis()); final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(0)); assertNotNull(account); busHandler.pushExpectedEvents(NextEvent.BLOCK); final BlockingState blockingState1 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "state1", "Service", false, false, true, null); subscriptionApi.addBlockingState(blockingState1, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); clock.addDays(1); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial", null); busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK); entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "bundleExternalKey", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); clock.addMonths(1); busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); final BlockingState blockingState2 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "state2", "Service", false, false, false, null); subscriptionApi.addBlockingState(blockingState2, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addMonths(1); assertListenerStatus(); }
@Test(groups = "slow") public void testWithExceptionAndRetrySuccess() throws Exception { final NotificationQueue queueWithExceptionAndRetrySuccess = queueService.createNotificationQueue("ExceptionAndRetrySuccess", "svc", new NotificationQueueHandlerWithExceptions(1)); try { queueWithExceptionAndRetrySuccess.startQueue(); final DateTime now = new DateTime(); final DateTime readyTime = now.plusMillis(2000); final NotificationEvent eventJson = new TestNotificationKey("Foo"); queueWithExceptionAndRetrySuccess.recordFutureNotification(readyTime, eventJson, TOKEN_ID, SEARCH_KEY_1, SEARCH_KEY_2); // Move time in the future after the notification effectiveDate clock.setDeltaFromReality(3000); await().atMost(5, TimeUnit.SECONDS).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { final Integer retryCount = dbi.withHandle(new HandleCallback<Integer>() { @Override public Integer withHandle(final Handle handle) throws Exception { return handle.createQuery(String.format("select error_count from %s", notificationQueueConfig.getHistoryTableName())).map(IntegerMapper.FIRST).first(); } }); return retryCount != null && retryCount == 1 && eventsReceived == 1; } }); } finally { queueWithExceptionAndRetrySuccess.stopQueue(); } }
@Test(groups = "slow") public void testWithExceptionAndFailed() throws Exception { final NotificationQueue queueWithExceptionAndFailed = queueService.createNotificationQueue("ExceptionAndRetrySuccess", "svc", new NotificationQueueHandlerWithExceptions(3)); try { queueWithExceptionAndFailed.startQueue(); final DateTime now = new DateTime(); final DateTime readyTime = now.plusMillis(2000); final NotificationEvent eventJson = new TestNotificationKey("Foo"); queueWithExceptionAndFailed.recordFutureNotification(readyTime, eventJson, TOKEN_ID, SEARCH_KEY_1, SEARCH_KEY_2); // Move time in the future after the notification effectiveDate clock.setDeltaFromReality(3000); await().atMost(5, TimeUnit.SECONDS).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { final Integer retryCount = dbi.withHandle(new HandleCallback<Integer>() { @Override public Integer withHandle(final Handle handle) throws Exception { return handle.createQuery(String.format("select error_count from %s", notificationQueueConfig.getHistoryTableName())).map(IntegerMapper.FIRST).first(); } }); return retryCount != null && retryCount == 3; } }); } finally { queueWithExceptionAndFailed.stopQueue(); } }
@Test(groups = "slow") public void testInvoiceNotifier() throws Exception { final Account account = invoiceUtil.createAccount(callContext); final Long accountRecordId = nonEntityDao.retrieveAccountRecordIdFromObject(account.getId(), ObjectType.ACCOUNT, null); final SubscriptionBase subscription = invoiceUtil.createSubscription(); final UUID subscriptionId = subscription.getId(); final DateTime now = clock.getUTCNow(); final NotificationQueue nextBillingQueue = notificationQueueService.getNotificationQueue(KILLBILL_SERVICES.INVOICE_SERVICE.getServiceName(), DefaultNextBillingDateNotifier.NEXT_BILLING_DATE_NOTIFIER_QUEUE); nextBillingQueue.recordFutureNotification(now, new NextBillingDateNotificationKey(null, ImmutableList.<UUID>of(subscriptionId), now, Boolean.FALSE, Boolean.FALSE), internalCallContext.getUserToken(), accountRecordId, internalCallContext.getTenantRecordId()); // Move time in the future after the notification effectiveDate clock.setDeltaFromReality(3000); await().atMost(1, MINUTES).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return testInvoiceNotificationQListener.getEventCount() == 1; } }); Assert.assertEquals(testInvoiceNotificationQListener.getEventCount(), 1); Assert.assertEquals(testInvoiceNotificationQListener.getLatestSubscriptionId(), subscriptionId); } }
@Test(groups = "slow") public void testBlockingStatesV4() throws Exception { final DateTime initialDate = new DateTime(2017, 3, 1, 0, 1, 35, 0, DateTimeZone.UTC); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
@Test(groups = "slow") public void testAddonsWithMultipleAlignments() throws Exception { final DateTime initialDate = new DateTime(2012, 4, 25, 0, 13, 42, 0, testTimeZone); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());