@Override public DateTime addToDateTime(final DateTime dateTime) { if ((number == null) && (unit != TimeUnit.UNLIMITED)) { return dateTime; } switch (unit) { case DAYS: return dateTime.plusDays(number); case WEEKS: return dateTime.plusWeeks(number); case MONTHS: return dateTime.plusMonths(number); case YEARS: return dateTime.plusYears(number); case UNLIMITED: default: throw new IllegalStateException("Unexpected duration unit " + unit); } }
@Override public DateTime addToDateTime(final DateTime dateTime) throws CatalogApiException { if ((number == null) && (unit != TimeUnit.UNLIMITED)) { return dateTime; } switch (unit) { case DAYS: return dateTime.plusDays(number); case WEEKS: return dateTime.plusWeeks(number); case MONTHS: return dateTime.plusMonths(number); case YEARS: return dateTime.plusYears(number); case UNLIMITED: default: throw new CatalogApiException(ErrorCode.CAT_UNDEFINED_DURATION, unit); } }
@Test public void testPatchedNameKeysSydney() throws Exception { // the tz database does not have unique name keys [1716305] DateTimeZone zone = DateTimeZone.forID("Australia/Sydney"); DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0); String str1 = zone.getName(now.getMillis()); String str2 = zone.getName(now.plusMonths(6).getMillis()); assertEquals(false, str1.equals(str2)); }
@Test public void testPatchedNameKeysSydneyHistoric() throws Exception { // the tz database does not have unique name keys [1716305] DateTimeZone zone = DateTimeZone.forID("Australia/Sydney"); DateTime now = new DateTime(1996, 1, 1, 0, 0, 0, 0); String str1 = zone.getName(now.getMillis()); String str2 = zone.getName(now.plusMonths(6).getMillis()); assertEquals(false, str1.equals(str2)); }
@Test public void testPatchedNameKeysGazaHistoric() throws Exception { // the tz database does not have unique name keys [1716305] DateTimeZone zone = DateTimeZone.forID("Africa/Johannesburg"); DateTime now = new DateTime(1943, 1, 1, 0, 0, 0, 0); String str1 = zone.getName(now.getMillis()); String str2 = zone.getName(now.plusMonths(6).getMillis()); assertEquals(false, str1.equals(str2)); }
@Test public void testPatchedNameKeysLondon() throws Exception { // the tz database does not have unique name keys [1716305] DateTimeZone zone = DateTimeZone.forID("Europe/London"); DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0); String str1 = zone.getName(now.getMillis()); String str2 = zone.getName(now.plusMonths(6).getMillis()); assertEquals(false, str1.equals(str2)); }
@Test public void testAddFieldToDate() { assertFunction("date_add('day', 0, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE)); assertFunction("date_add('day', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusDays(3))); assertFunction("date_add('week', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusWeeks(3))); assertFunction("date_add('month', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusMonths(3))); assertFunction("date_add('quarter', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusMonths(3 * 3))); assertFunction("date_add('year', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusYears(3))); }
@Test public void testDublin() { DateTimeZone zone = DateTimeZone.forID("Europe/Dublin"); DateTime winter = new DateTime(2018, 1, 1, 0, 0, 0, 0, zone); assertEquals(0, zone.getStandardOffset(winter.getMillis())); assertEquals(0, zone.getOffset(winter.getMillis())); assertEquals(true, zone.isStandardOffset(winter.getMillis())); assertEquals("Greenwich Mean Time", zone.getName(winter.getMillis())); assertEquals("GMT", zone.getNameKey(winter.getMillis())); DateTime summer = winter.plusMonths(6); assertEquals(0, zone.getStandardOffset(summer.getMillis())); assertEquals(3600000, zone.getOffset(summer.getMillis())); assertEquals(false, zone.isStandardOffset(summer.getMillis())); assertEquals(true, zone.getName(summer.getMillis()).startsWith("Irish ")); assertEquals("IST", zone.getNameKey(summer.getMillis())); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType2() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "shotgun-monthly"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType1() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "pistol-monthly-notrial"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
public static DateTime addOrRemoveDuration(final DateTime input, final List<Duration> durations, final boolean add) { DateTime result = input; for (final Duration cur : durations) { switch (cur.getUnit()) { case DAYS: result = add ? result.plusDays(cur.getNumber()) : result.minusDays(cur.getNumber()); break; case MONTHS: result = add ? result.plusMonths(cur.getNumber()) : result.minusMonths(cur.getNumber()); break; case YEARS: result = add ? result.plusYears(cur.getNumber()) : result.minusYears(cur.getNumber()); break; case UNLIMITED: default: throw new RuntimeException("Trying to move to unlimited time period"); } } return result; }
@Test(groups = "fast") public void testCreateWithTargetPhaseType4() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "pistol-annual-gunclub-discount-notrial"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
Assert.assertEquals(phases[1].getStartPhase(), defaultSubscriptionBase.getStartDate().plusMonths(1)); Assert.assertEquals(nextTimePhase.getStartPhase(), defaultSubscriptionBase.getStartDate().plusMonths(1)); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(1); changeSubscription(effectiveChangeDate, defaultSubscriptionBase, productName, newProductName, initialPhase);
@Test(groups = "fast") public void testChangeWithTargetPhaseType1() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, null, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.TRIAL); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertEquals(phases[1].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[1].getStartPhase(), now.plusDays(30)); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.TRIAL); final String newProductName = "pistol-annual-gunclub-discount"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusDays(5); final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.DISCOUNT, catalog, internalCallContext); // We end up straight on DISCOUNT but because we are using START_OF_SUBSCRIPTION alignment, such Phase starts with beginning of subscription Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.DISCOUNT); final TimedPhase nextPhase = planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.DISCOUNT, catalog, internalCallContext); Assert.assertEquals(nextPhase.getStartPhase(), alignStartDate.plusMonths(6)); Assert.assertEquals(nextPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
@Test(groups = "fast") public void testCreateWithTargetPhaseType3() throws Exception { final String productName = "pistol-monthly"; final DateTime now = clock.getUTCNow(); final DateTime bundleStartDate = now; final DateTime alignStartDate = bundleStartDate; final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow()); final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext); Assert.assertEquals(phases.length, 2); Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN); Assert.assertEquals(phases[0].getStartPhase(), now); Assert.assertNull(phases[1]); final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN); final String newProductName = "assault-rifle-annual-gunclub-discount"; final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow()); final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15); // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext); Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate); Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN); }
final DateTime expectedNextPhaseDate = subscription.getStartDate().plusDays(30).plusMonths(6); final SubscriptionBaseTransition nextPhase = subscription.getPendingTransition();
final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1); subscriptionInternalApi.setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1); subscriptionInternalApi.setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
@Test public void testAddFieldToTimestamp() { assertFunction("date_add('millisecond', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMillis(3), session)); assertFunction("date_add('second', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusSeconds(3), session)); assertFunction("date_add('minute', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMinutes(3), session)); assertFunction("date_add('hour', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusHours(3), session)); assertFunction("date_add('hour', 23, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusHours(23), session)); assertFunction("date_add('hour', -4, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.minusHours(4), session)); assertFunction("date_add('hour', -23, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.minusHours(23), session)); assertFunction("date_add('day', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusDays(3), session)); assertFunction("date_add('week', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusWeeks(3), session)); assertFunction("date_add('month', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMonths(3), session)); assertFunction("date_add('quarter', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMonths(3 * 3), session)); assertFunction("date_add('year', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusYears(3), session)); assertFunction("date_add('millisecond', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMillis(3))); assertFunction("date_add('second', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusSeconds(3))); assertFunction("date_add('minute', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMinutes(3))); assertFunction("date_add('hour', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusHours(3))); assertFunction("date_add('day', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusDays(3))); assertFunction("date_add('week', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusWeeks(3))); assertFunction("date_add('month', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMonths(3))); assertFunction("date_add('quarter', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMonths(3 * 3))); assertFunction("date_add('year', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusYears(3))); }
Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(2)); clock.addDeltaFromReality(it.toDurationMillis()); assertListenerStatus(); testListener.pushExpectedEvent(NextEvent.CANCEL); it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1)); clock.addDeltaFromReality(it.toDurationMillis()); assertListenerStatus();