public DefaultBlockingState(final BlockingState input, final DateTime effectiveDate) { this(input.getBlockedId(), input.getType(), input.getStateName(), input.getService(), input.isBlockChange(), input.isBlockEntitlement(), input.isBlockBilling(), effectiveDate); }
@Override public int compareTo(final BlockingState arg0) { // effective_date column NOT NULL final int comparison = effectiveDate.compareTo(arg0.getEffectiveDate()); if (comparison == 0) { // Keep a stable ordering for ties final int comparison2 = createdDate.compareTo(arg0.getCreatedDate()); if (comparison2 == 0 && arg0 instanceof DefaultBlockingState) { final DefaultBlockingState other = (DefaultBlockingState) arg0; // New element is last if (totalOrdering == null) { return 1; } else if (other.getTotalOrdering() == null) { return -1; } else { return totalOrdering.compareTo(other.getTotalOrdering()); } } else { return comparison2; } } else { return comparison; } }
public void checkStateApplied(final BlockingState result, final OverdueState state) { Assert.assertEquals(result.getStateName(), state.getName()); Assert.assertEquals(result.isBlockChange(), state.isBlockChanges()); Assert.assertEquals(result.isBlockEntitlement(), state.isDisableEntitlementAndChangesBlocked()); Assert.assertEquals(result.isBlockBilling(), state.isDisableEntitlementAndChangesBlocked()); }
private void doCheckFutureBlockingStatesToCancel(final DefaultEntitlement addOnEntitlement, final DateTime effectiveCancellationDateTime, final Collection<BlockingState> blockingStatesForCancellation) { if (addOnEntitlement == null || effectiveCancellationDateTime == null) { Assert.assertEquals(blockingStatesForCancellation.size(), 0); } else { Assert.assertEquals(blockingStatesForCancellation.size(), 1); final BlockingState blockingState = blockingStatesForCancellation.iterator().next(); Assert.assertEquals(blockingState.getBlockedId(), addOnEntitlement.getId()); Assert.assertEquals(blockingState.getEffectiveDate(), effectiveCancellationDateTime); Assert.assertEquals(blockingState.getType(), BlockingStateType.SUBSCRIPTION); Assert.assertEquals(blockingState.getService(), KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName()); Assert.assertEquals(blockingState.getStateName(), DefaultEntitlementApi.ENT_STATE_CANCELLED); } }
public BlockingStateJson(final BlockingState input, final AccountAuditLogs accountAuditLogs) { this(input.getBlockedId(), input.getStateName(), input.getService(), input.isBlockChange(), input.isBlockEntitlement(), input.isBlockBilling(), input.getEffectiveDate(), input.getType(), toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForBlockingState(input.getId()))); }
private void verifyBlockingStates(final Iterable<BlockingState> result, final List<BlockingState> expected) { int i = 0; final Iterator<BlockingState> iterator = result.iterator(); while (iterator.hasNext()) { final BlockingState cur = iterator.next(); final BlockingState expectedItem = expected.get(i); assertEquals(cur.isBlockBilling(), expectedItem.isBlockBilling()); assertEquals(cur.isBlockEntitlement(), expectedItem.isBlockEntitlement()); assertEquals(cur.isBlockChange(), expectedItem.isBlockChange()); assertEquals(cur.getService(), expectedItem.getService()); assertEquals(cur.getStateName(), expectedItem.getStateName()); assertEquals(cur.getBlockedId(), expectedItem.getBlockedId()); assertEquals(internalCallContext.toLocalDate(cur.getEffectiveDate()).compareTo(internalCallContext.toLocalDate(expectedItem.getEffectiveDate())), 0); i++; } assertEquals(i, expected.size()); }
private void clearWithLock(final DateTime effectiveDate, final InternalCallContext context) throws OverdueException, OverdueApiException { final BlockingState blockingStateForService = api.getBlockingStateForService(overdueable.getId(), BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, context); final String previousOverdueStateName = blockingStateForService != null ? blockingStateForService.getStateName() : OverdueWrapper.CLEAR_STATE_NAME; final OverdueState previousOverdueState = overdueStateSet.findState(previousOverdueStateName); overdueStateApplicator.clear(effectiveDate, overdueable, previousOverdueState, overdueStateSet.getClearState(), context); }
public void addBlockingState(final BlockingState currentBlockingState) { final BlockingState firstBlockingState = perObjectTypeFirstBlockingState.get(currentBlockingState.getBlockedId()); if (currentBlockingState.isBlockBilling() && firstBlockingState == null) { perObjectTypeFirstBlockingState.put(currentBlockingState.getBlockedId(), currentBlockingState); } else if (!currentBlockingState.isBlockBilling() && firstBlockingState != null) { addDisabledDuration(firstBlockingState, currentBlockingState.getEffectiveDate()); perObjectTypeFirstBlockingState.put(currentBlockingState.getBlockedId(), null); } }
@Override public boolean apply(final BlockingState input) { return subscriptionEndDate == null || input.getEffectiveDate().compareTo(subscriptionEndDate) <= 0; } });
assertTrue(blockingState.isBlockBilling()); assertTrue(blockingState.isBlockChange()); assertTrue(blockingState.isBlockEntitlement());
@Override public BlockingState getBlockingStateForService(final UUID blockableId, final BlockingStateType blockingStateType, final String serviceName, final InternalTenantContext context) { if (blockingState != null && blockingState.getBlockedId().equals(blockableId)) { return blockingState; } else { return new DefaultBlockingState(null, blockingStateType, OverdueWrapper.CLEAR_STATE_NAME, serviceName, false, false, false, null); } }
@Override public String apply(final BlockingState input) { return input.getService(); } }));
private void computeEvents(final LinkedList<SubscriptionEvent> allEvents, final Collection<BlockingState> blockingStates) { final Collection<UUID> allEntitlementUUIDs = new HashSet<UUID>(); for (final SubscriptionEvent subscriptionEvent : allEvents) { allEntitlementUUIDs.add(subscriptionEvent.getEntitlementId()); } for (final BlockingState blockingState : blockingStates) { if (blockingState.getType() == BlockingStateType.SUBSCRIPTION) { allEntitlementUUIDs.add(blockingState.getBlockedId()); } } BlockingStateOrdering.INSTANCE.computeEvents(new LinkedList<UUID>(allEntitlementUUIDs), blockingStates, internalCallContext, allEvents); } }
@Override public List<BlockingState> getBlockingState(final UUID blockableId, final BlockingStateType blockingStateType, final DateTime upToDate, final InternalTenantContext context) { final List<BlockingState> blockingStatesForId = blockingStates.get(blockableId); if (blockingStatesForId == null) { return new ArrayList<BlockingState>(); } final Map<String, BlockingState> tmp = new HashMap<String, BlockingState>(); for (BlockingState cur : blockingStatesForId) { final BlockingState curStateForService = tmp.get(cur.getService()); if (curStateForService == null || curStateForService.getEffectiveDate().compareTo(cur.getEffectiveDate()) < 0) { tmp.put(cur.getService(), cur); } } return new ArrayList<BlockingState>(tmp.values()); }
@Test(groups = "slow", description = "Verify behavior with or without ENT_STARTED event works as expected") public void testRegressionForNew_ENT_STARTED_event() throws Exception { final LocalDate initialDate = new LocalDate(2013, 8, 7); clock.setDay(initialDate); // Start the entitlement yesterday (does not m,ake sense, but we want to check later different of behavior) final LocalDate entitlementEffectiveDate = initialDate.minusDays(1); final Account account = createAccount(getAccountData(7)); final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null); final EntitlementSpecifier spec = new DefaultEntitlementSpecifier(planPhaseSpecifier); testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK); final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), spec, UUID.randomUUID().toString(), entitlementEffectiveDate, null, false, true, ImmutableList.<PluginProperty>of(), callContext); final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext); // Because of the BlockingState event ENT_STARTED, the entitlement date should be correctly set Assert.assertEquals(entitlement.getEffectiveStartDate(), entitlementEffectiveDate); final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForAccountId(account.getId(), callContext); Assert.assertEquals(bundles.size(), 1); subscriptionBundleChecker(bundles, initialDate, entitlement, 0); // Let's do some surgery and inactivate the ENT_STARTED BlockingState final List<BlockingState> blockingStates = blockingStateDao.getBlockingState(entitlement.getId(), BlockingStateType.SUBSCRIPTION, clock.getUTCNow(), internalCallContext); assertEquals(blockingStates.size(), 1); assertEquals(blockingStates.get(0).getStateName(), DefaultEntitlementApi.ENT_STATE_START); blockingStateDao.unactiveBlockingState(blockingStates.get(0).getId(), internalCallContext); final Entitlement oldSchoolEntitlement = entitlementApi.getEntitlementForId(entitlement.getId(), callContext); // Because the ENT_STARTED BlockingState has been invalidated, the startDate should now default to the billingDate Assert.assertEquals(oldSchoolEntitlement.getEffectiveStartDate(), initialDate); final List<SubscriptionBundle> oldSchoolBundles = subscriptionApi.getSubscriptionBundlesForAccountId(account.getId(), callContext); Assert.assertEquals(oldSchoolBundles.size(), 1); subscriptionBundleChecker(oldSchoolBundles, initialDate, oldSchoolEntitlement, 0); }
@Override public boolean apply(final BlockingState input) { return BlockingStateType.ACCOUNT == input.getType(); } });
private void doCheckActualBlockingStatesToCancel(final DefaultEntitlement addOnEntitlement, final DateTime effectiveCancellationDateTime, final boolean approximateDateCheck, final Collection<BlockingState> blockingStatesForCancellation) { if (effectiveCancellationDateTime == null) { Assert.assertEquals(blockingStatesForCancellation.size(), 0); } else { Assert.assertEquals(blockingStatesForCancellation.size(), 1); final BlockingState blockingState = blockingStatesForCancellation.iterator().next(); Assert.assertEquals(blockingState.getBlockedId(), addOnEntitlement.getId()); if (approximateDateCheck) { Assert.assertEquals(blockingState.getEffectiveDate().toLocalDate(), effectiveCancellationDateTime.toLocalDate()); Assert.assertEquals(blockingState.getEffectiveDate().getMinuteOfDay(), effectiveCancellationDateTime.getMinuteOfDay()); } else { Assert.assertEquals(blockingState.getEffectiveDate(), effectiveCancellationDateTime); } Assert.assertEquals(blockingState.getType(), BlockingStateType.SUBSCRIPTION); Assert.assertEquals(blockingState.getService(), KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName()); Assert.assertEquals(blockingState.getStateName(), DefaultEntitlementApi.ENT_STATE_CANCELLED); } }
@Override public OverdueState getOverdueStateFor(final UUID accountId, final TenantContext tenantContext) throws OverdueApiException { final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext); final BlockingState blockingStateForService = blockingInternalApi.getBlockingStateForService(accountId, BlockingStateType.ACCOUNT, OverdueService.OVERDUE_SERVICE_NAME, internalTenantContext); final String stateName = blockingStateForService != null ? blockingStateForService.getStateName() : OverdueWrapper.CLEAR_STATE_NAME; final OverdueConfig overdueConfig = overdueConfigCache.getOverdueConfig(internalTenantContext); final OverdueStateSet states = ((DefaultOverdueConfig) overdueConfig).getOverdueStatesAccount(); return states.findState(stateName); }
private void addDisabledDuration(final BlockingState firstBlockingState, @Nullable final DateTime disableDurationEndDate) { if (disableDurationEndDate == null || Days.daysBetween(firstBlockingState.getEffectiveDate(), disableDurationEndDate).getDays() >= 1) { // Don't disable for periods less than a day (see https://github.com/killbill/killbill/issues/267) result.add(new DisabledDuration(firstBlockingState.getEffectiveDate(), disableDurationEndDate)); } } }
final Map<UUID, List<BlockingState>> getPerTypeBlockingEvents(final BlockingStateType type, final List<BlockingState> blockingEvents) { final Iterable<BlockingState> bundleBlockingEvents = Iterables.filter(blockingEvents, new Predicate<BlockingState>() { @Override public boolean apply(final BlockingState input) { return type == input.getType(); } }); final Map<UUID, List<BlockingState>> perTypeBlockingEvents = new HashMap<UUID, List<BlockingState>>(); for (final BlockingState cur : bundleBlockingEvents) { if (!perTypeBlockingEvents.containsKey(cur.getBlockedId())) { perTypeBlockingEvents.put(cur.getBlockedId(), new ArrayList<BlockingState>()); } perTypeBlockingEvents.get(cur.getBlockedId()).add(cur); } return perTypeBlockingEvents; }