### Question:
DiscountingKnownAmountPaymentPeriodPricer implements SwapPaymentPeriodPricer<KnownAmountSwapPaymentPeriod> { @Override public double accruedInterest(KnownAmountSwapPaymentPeriod period, RatesProvider provider) { LocalDate valDate = provider.getValuationDate(); if (valDate.compareTo(period.getStartDate()) <= 0 || valDate.compareTo(period.getEndDate()) > 0) { return 0d; } double fv = forecastValue(period, provider); double totalDays = period.getStartDate().until(period.getEndDate(), DAYS); double partialDays = period.getStartDate().until(valDate, DAYS); return fv * (partialDays / totalDays); } DiscountingKnownAmountPaymentPeriodPricer(DiscountingPaymentPricer paymentPricer); @Override double presentValue(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override double forecastValue(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override double accruedInterest(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override double pvbp(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override PointSensitivityBuilder presentValueSensitivity(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override PointSensitivityBuilder forecastValueSensitivity(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override PointSensitivityBuilder pvbpSensitivity(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override void explainPresentValue(KnownAmountSwapPaymentPeriod period, RatesProvider provider, ExplainMapBuilder builder); @Override MultiCurrencyAmount currencyExposure(KnownAmountSwapPaymentPeriod period, RatesProvider provider); @Override double currentCash(KnownAmountSwapPaymentPeriod period, RatesProvider provider); static final DiscountingKnownAmountPaymentPeriodPricer DEFAULT; }### Answer:
@Test public void test_accruedInterest() { LocalDate valDate = PERIOD.getStartDate().plusDays(7); SimpleRatesProvider prov = createProvider(valDate); double expected = AMOUNT_1000 * (7d / (7 + 28 + 31 + 25)); double computed = PRICER.accruedInterest(PERIOD, prov); assertThat(computed).isCloseTo(expected, offset(TOLERANCE_PV)); }
@Test public void test_accruedInterest_valDateBeforePeriod() { SimpleRatesProvider prov = createProvider(PERIOD.getStartDate()); double computed = PRICER.accruedInterest(PERIOD, prov); assertThat(computed).isCloseTo(0, offset(TOLERANCE_PV)); }
@Test public void test_accruedInterest_valDateAfterPeriod() { SimpleRatesProvider prov = createProvider(PERIOD.getEndDate().plusDays(1)); double computed = PRICER.accruedInterest(PERIOD, prov); assertThat(computed).isCloseTo(0, offset(TOLERANCE_PV)); }