/** * Converts this object to a <code>LocalDate</code> with the * same date and chronology. * * @return a LocalDate with the same date and chronology * @since 1.3 */ public LocalDate toLocalDate() { return new LocalDate(getMillis(), getChronology()); }
@Override protected void parse(String value) throws Throwable { if (value == null || value.equalsIgnoreCase("today") || value.isEmpty()) { value = new LocalDate().toString(); // eeew } String[] fields = value.split("-"); year = Integer.parseInt(fields[0]); month = Integer.parseInt(fields[1]); day = Integer.parseInt(fields[2]); checkRangeInclusive(year, 2000, 2100); checkRangeInclusive(month, 1, 12); checkRangeInclusive(day, 1, daysInMonth[month - 1]); }
@Override public void removedExpiredAccounts(LocalDate reference) { template.update("DELETE Account AS a WHERE a.expiryDate < ?", reference.toDateTimeAtStartOfDay().toDate()); } }
/** * Returns a copy of this datetime with the specified date, retaining the time fields. * <p> * If the time is invalid on the new date due to the time-zone, the time will be adjusted. * <p> * This instance is immutable and unaffected by this method call. * * @param date the local date * @return a copy of this datetime with a different date * @throws IllegalArgumentException if the time-of-day is invalid for the date * @throws NullPointerException if the date is null */ public DateTime withDate(LocalDate date) { return withDate( date.getYear(), date.getMonthOfYear(), date.getDayOfMonth()); }
/** * Returns a copy of this date with different local millis. * <p> * The returned object will be a new instance of the same type. * Only the millis will change, the chronology is kept. * The returned object will be either be a new instance or <code>this</code>. * * @param newMillis the new millis, from 1970-01-01T00:00:00 * @return a copy of this date with different millis */ LocalDate withLocalMillis(long newMillis) { newMillis = iChronology.dayOfMonth().roundFloor(newMillis); return (newMillis == getLocalMillis() ? this : new LocalDate(newMillis, getChronology())); }
@Override public java.sql.Date getPrimitiveJavaObject(Object o) { final DateHolder h = (DateHolder) o; org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(h.value, org.joda.time.DateTimeZone.UTC); // Use "toDate()" to get java.util.Date object with exactly the same year the same year, month and day as Joda date. // See more in Javadoc for "LocalDate#toDate()" return new java.sql.Date(localDate.toDate().getTime()); }
@Test(groups = "fast") public void testBalanceFormatting() throws Exception { final BillingState billingState = new BillingState(UUID.randomUUID(), 2, BigDecimal.TEN, new LocalDate(), UUID.randomUUID(), null, null); final DefaultBillingStateFormatter formatter = new DefaultBillingStateFormatter(billingState); Assert.assertEquals(formatter.getFormattedBalanceOfUnpaidInvoices(), "10.00"); } }
@Test(groups = "fast") public void testTotalUnpaidInvoiceBalanceEqualsOrExceeds() throws Exception { final String xml = "<condition>" + " <totalUnpaidInvoiceBalanceEqualsOrExceeds>100.00</totalUnpaidInvoiceBalanceEqualsOrExceeds>" + "</condition>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final MockCondition c = XMLLoader.getObjectFromStreamNoValidation(is, MockCondition.class); final UUID unpaidInvoiceId = UUID.randomUUID(); final BillingState state0 = new BillingState(new UUID(0L, 1L), 0, BigDecimal.ZERO, new LocalDate(), unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state1 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("100.00"), new LocalDate(), unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state2 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("200.00"), new LocalDate(), unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); Assert.assertTrue(!c.evaluate(state0, new LocalDate())); Assert.assertTrue(c.evaluate(state1, new LocalDate())); Assert.assertTrue(c.evaluate(state2, new LocalDate())); }
@Test(groups = "slow") public void testAccountBalanceWithNoInvoiceItems() throws EntityPersistenceException { final UUID accountId = account.getId(); final LocalDate targetDate1 = new LocalDate(2011, 10, 6); final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD); invoiceUtil.createInvoice(invoice1, context); final BigDecimal payment1 = new BigDecimal("48.0"); final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true); invoiceUtil.createPayment(payment, context); final BigDecimal balance = invoiceDao.getAccountBalance(accountId, context); assertEquals(balance.compareTo(BigDecimal.ZERO.subtract(payment1)), 0); }
@Test(groups = "slow") public void testDuplicateRecords() { final UUID subscriptionId = UUID.randomUUID(); final String unitType1 = "foo"; final String unitType2 = "bar"; final LocalDate startDate = new LocalDate(2013, 1, 1); final LocalDate endDate = new LocalDate(2013, 2, 1); final Long amount1 = 10L; final Long amount2 = 5L; final Long amount3 = 13L; RolledUpUsageModelDao usage1 = new RolledUpUsageModelDao(subscriptionId, unitType1, startDate, amount1, UUID.randomUUID().toString()); RolledUpUsageModelDao usage2 = new RolledUpUsageModelDao(subscriptionId, unitType1, startDate.plusDays(1), amount2, UUID.randomUUID().toString()); RolledUpUsageModelDao usage3 = new RolledUpUsageModelDao(subscriptionId, unitType2, endDate.minusDays(1), amount3, UUID.randomUUID().toString()); List<RolledUpUsageModelDao> usages = new ArrayList<RolledUpUsageModelDao>(); usages.add(usage1); usages.add(usage2); usages.add(usage3); rolledUpUsageDao.record(usages, internalCallContext); final List<RolledUpUsageModelDao> result = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, startDate, endDate, internalCallContext); assertEquals(result.size(), 3); try { rolledUpUsageDao.record(usages, internalCallContext); fail("duplicate records accepted"); } catch (UnableToExecuteStatementException e) { assertEquals(result.size(), 3); } }
@Test(groups = "slow") public void testAddInvoiceItemAdjustmentForPartialAmount() throws Exception { final Invoice invoice = new DefaultInvoice(UUID.randomUUID(), clock.getUTCToday(), clock.getUTCToday(), Currency.USD); final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoice.getId(), invoice.getAccountId(), UUID.randomUUID(), UUID.randomUUID(), "test plan", "test phase", new LocalDate(2010, 1, 1), new LocalDate(2010, 4, 1), INVOICE_ITEM_AMOUNT, new BigDecimal("7.00"), Currency.USD); invoice.addInvoiceItem(invoiceItem); invoiceUtil.createInvoice(invoice, true, internalCallContext); final InvoiceItemModelDao adjustedInvoiceItem = createAndCheckAdjustment(invoice, invoiceItem, BigDecimal.TEN); Assert.assertEquals(adjustedInvoiceItem.getAmount().compareTo(BigDecimal.TEN.negate()), 0); }
@Test(groups = "slow") public void testSimple() { final UUID subscriptionId = UUID.randomUUID(); final String unitType = "foo"; final LocalDate startDate = new LocalDate(2013, 1, 1); final LocalDate endDate = new LocalDate(2013, 2, 1); final Long amount1 = 10L; final Long amount2 = 5L; RolledUpUsageModelDao usage1 = new RolledUpUsageModelDao(subscriptionId, unitType, startDate, amount1, UUID.randomUUID().toString()); RolledUpUsageModelDao usage2 = new RolledUpUsageModelDao(subscriptionId, unitType, endDate.minusDays(1), amount2, UUID.randomUUID().toString()); List<RolledUpUsageModelDao> usages = new ArrayList<RolledUpUsageModelDao>(); usages.add(usage1); usages.add(usage2); rolledUpUsageDao.record(usages, internalCallContext); final List<RolledUpUsageModelDao> result = rolledUpUsageDao.getUsageForSubscription(subscriptionId, startDate, endDate, unitType, internalCallContext); assertEquals(result.size(), 2); assertEquals(result.get(0).getSubscriptionId(), subscriptionId); assertEquals(result.get(0).getRecordDate().compareTo(startDate), 0); assertEquals(result.get(0).getUnitType(), unitType); assertEquals(result.get(0).getAmount().compareTo(amount1), 0); assertEquals(result.get(1).getSubscriptionId(), subscriptionId); assertEquals(result.get(1).getRecordDate().compareTo(endDate.minusDays(1)), 0); assertEquals(result.get(1).getUnitType(), unitType); assertEquals(result.get(1).getAmount().compareTo(amount2), 0); }
java.time.LocalTime.now().truncatedTo(ChronoUnit.MILLIS), java.time.Instant.now().truncatedTo(ChronoUnit.MILLIS), new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN) ); Assert.assertThat(withJoda.getS(), is(withJsr310.getS())); Assert.assertThat(withJoda.getD().toString(), is(withJsr310.getD().toString())); Assert.assertThat(withJoda.getT().toString(), is(withJsr310.getT().toString())); Assert.assertThat(withJoda.getTs().toString(), is(withJsr310.getTs().toString())); Assert.assertThat(withJoda.getDec(), comparesEqualTo(withJsr310.getDec()));
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDate3() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime3); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 20); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
@Test(groups = "slow", enabled = false) public void testWithGMTMinus20() throws SQLException { final LocalDate date1_1 = new LocalDate(2014, 10, 1, GregorianChronology.getInstance(DATE_TZ_MINUS_20_GMT)); // We chose a time such that it moves to next day final DateTime date2_1 = new DateTime(2014, 10, 1, 16, 48, 56, DATE_TZ_MINUS_20_GMT); insertData(date1_1, date2_1, date2_1); final FullOfDates result = readData(); assertEquals(result.getDate1().compareTo(date1_1), 0); assertEquals(result.getDate2().compareTo(date2_1), 0); assertEquals(result.getDate2().getZone().toString(), "UTC"); }
public BillingStateCalculator createBSCalc() { now = new LocalDate(); final Collection<Invoice> invoices = new ArrayList<Invoice>(); invoices.add(createInvoice(now, BigDecimal.ZERO, null)); invoices.add(createInvoice(now.plusDays(1), BigDecimal.TEN, null)); invoices.add(createInvoice(now.plusDays(2), new BigDecimal("100.0"), null)); Mockito.when(invoiceApi.getUnpaidInvoicesByAccountId(Mockito.<UUID>any(), Mockito.<LocalDate>any(), Mockito.<InternalTenantContext>any())).thenReturn(invoices); return new BillingStateCalculator(invoiceApi, clock, tagInternalApi) { @Override public BillingState calculateBillingState(final ImmutableAccountData overdueable, final InternalCallContext context) { return null; } }; }
/** * Converts this LocalDate to a full datetime at midnight using the * specified time zone. * <p> * This method will throw an exception if the time zone switches * to Daylight Savings Time at midnight and this LocalDate represents * that switchover date. The problem is that there is no such time as * midnight on the required date, and as such an exception is thrown. * <p> * This method uses the chronology from this instance plus the time zone * specified. * <p> * This instance is immutable and unaffected by this method call. * * @param zone the zone to use, null means default zone * @return this date as a datetime at midnight * @deprecated Use {@link #toDateTimeAtStartOfDay(DateTimeZone)} which won't throw an exception */ @Deprecated public DateTime toDateTimeAtMidnight(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = getChronology().withZone(zone); return new DateTime(getYear(), getMonthOfYear(), getDayOfMonth(), 0, 0, 0, 0, chrono); }
@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()); }
@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") public void testCreateSubscriptionInTheFuture() throws SubscriptionBaseApiException { final LocalDate init = clock.getUTCToday(); final String productName = "Shotgun"; final BillingPeriod term = BillingPeriod.MONTHLY; final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME; final LocalDate futureCreationDate = init.plusDays(10); DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, productName, term, planSetName, futureCreationDate); assertListenerStatus(); assertNotNull(subscription); assertEquals(subscription.getState(), EntitlementState.PENDING); testListener.pushExpectedEvent(NextEvent.CREATE); clock.addDays(10); assertListenerStatus(); subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); assertEquals(subscription.getState(), EntitlementState.ACTIVE); }