@Test
 /** Tests the getters. */
 public void testGetters() {
   assertEquals(COUPON.getPaymentDate(), PAYMENT_DATE);
   assertEquals(COUPON.getAccrualStartDate(), ACCRUAL_START_DATES[0]);
   assertEquals(COUPON.getAccrualEndDate(), ACCRUAL_END_DATES[NB_SUB_PERIOD - 1]);
   assertEquals(COUPON.getPaymentYearFraction(), PAYMENT_ACCRUAL_FACTOR, 1E-10);
   assertEquals(COUPON.getNotional(), NOTIONAL, 1E-2);
   assertEquals(COUPON.getRate(), FIXED_RATE, 1E-10);
   assertArrayEquals(COUPON.getAccrualStartDates(), ACCRUAL_START_DATES);
   assertArrayEquals(COUPON.getAccrualEndDates(), ACCRUAL_END_DATES);
   assertArrayEquals(COUPON.getPaymentAccrualFactors(), PAYMENT_ACCRUAL_FACTORS, 0);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testNegativePaymentAccrualFactor() {
   CouponFixedCompoundingDefinition.from(
       CURRENCY,
       PAYMENT_DATE,
       ACCRUAL_START_DATES[0],
       ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
       -PAYMENT_ACCRUAL_FACTOR,
       NOTIONAL,
       FIXED_RATE,
       ACCRUAL_START_DATES,
       ACCRUAL_END_DATES,
       PAYMENT_ACCRUAL_FACTORS);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void nullCurrency() {
   CouponFixedCompoundingDefinition.from(
       null,
       PAYMENT_DATE,
       ACCRUAL_START_DATES[0],
       ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
       PAYMENT_ACCRUAL_FACTOR,
       NOTIONAL,
       FIXED_RATE,
       ACCRUAL_START_DATES,
       ACCRUAL_END_DATES,
       PAYMENT_ACCRUAL_FACTORS);
 }
public class CouponFixedCompoundingDefinitionTest {

  private static final Calendar NYC = new MondayToFridayCalendar("NYC");
  private static final IndexIborMaster MASTER_IBOR = IndexIborMaster.getInstance();
  private static final IborIndex USDLIBOR1M = MASTER_IBOR.getIndex("USDLIBOR1M");
  private static final Currency CURRENCY = USDLIBOR1M.getCurrency();

  private static final Period TENOR_3M = Period.ofMonths(3);
  private static final ZonedDateTime START_DATE = DateUtils.getUTCDate(2012, 8, 24);
  private static final double NOTIONAL = 123454321;
  private static final double FIXED_RATE = .02;

  private static final ZonedDateTime[] ACCRUAL_END_DATES =
      ScheduleCalculator.getAdjustedDateSchedule(
          START_DATE, TENOR_3M, true, false, USDLIBOR1M, NYC);
  private static final int NB_SUB_PERIOD = ACCRUAL_END_DATES.length;
  private static final ZonedDateTime[] ACCRUAL_START_DATES = new ZonedDateTime[NB_SUB_PERIOD];
  private static final double[] PAYMENT_ACCRUAL_FACTORS = new double[NB_SUB_PERIOD];
  private static final double PAYMENT_ACCRUAL_FACTOR;

  static {
    ACCRUAL_START_DATES[0] = START_DATE;
    for (int loopsub = 1; loopsub < NB_SUB_PERIOD; loopsub++) {
      ACCRUAL_START_DATES[loopsub] = ACCRUAL_END_DATES[loopsub - 1];
    }
    double af = 0.0;
    for (int loopsub = 0; loopsub < NB_SUB_PERIOD; loopsub++) {
      PAYMENT_ACCRUAL_FACTORS[loopsub] =
          USDLIBOR1M
              .getDayCount()
              .getDayCountFraction(ACCRUAL_START_DATES[loopsub], ACCRUAL_END_DATES[loopsub]);
      af += PAYMENT_ACCRUAL_FACTORS[loopsub];
    }
    PAYMENT_ACCRUAL_FACTOR = af;
  }

  private static final ZonedDateTime[] FIXING_DATES =
      ScheduleCalculator.getAdjustedDate(ACCRUAL_START_DATES, -USDLIBOR1M.getSpotLag(), NYC);
  private static final ZonedDateTime[] FIXING_PERIOD_END_DATES =
      ScheduleCalculator.getAdjustedDate(ACCRUAL_START_DATES, USDLIBOR1M, NYC);
  private static final double[] FIXING_ACCRUAL_FACTORS = new double[NB_SUB_PERIOD];

  static {
    for (int loopsub = 0; loopsub < NB_SUB_PERIOD; loopsub++) {
      FIXING_ACCRUAL_FACTORS[loopsub] =
          USDLIBOR1M
              .getDayCount()
              .getDayCountFraction(ACCRUAL_START_DATES[loopsub], FIXING_PERIOD_END_DATES[loopsub]);
    }
  }

  private static final ZonedDateTime PAYMENT_DATE = ACCRUAL_END_DATES[NB_SUB_PERIOD - 1];
  private static final ZonedDateTime REFERENCE_DATE = DateUtils.getUTCDate(2012, 8, 17);
  private static final double[] FIXING_TIMES =
      TimeCalculator.getTimeBetween(REFERENCE_DATE, FIXING_DATES);
  private static final double[] FIXING_PERIOD_END_TIMES =
      TimeCalculator.getTimeBetween(REFERENCE_DATE, FIXING_PERIOD_END_DATES);
  private static final double[] ACCRUAL_START_TIMES =
      TimeCalculator.getTimeBetween(REFERENCE_DATE, ACCRUAL_START_DATES);
  private static final double[] ACCRUAL_END_TIMES =
      TimeCalculator.getTimeBetween(REFERENCE_DATE, ACCRUAL_END_DATES);
  private static final double PAYMENT_TIME = ACCRUAL_END_TIMES[NB_SUB_PERIOD - 1];
  private static final String DSC_NAME = "Dsc_USD";
  private static final String FWD_NAME = "Forward1M_USD";

  private static final CouponFixedCompoundingDefinition COUPON =
      CouponFixedCompoundingDefinition.from(
          CURRENCY,
          PAYMENT_DATE,
          ACCRUAL_START_DATES[0],
          ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
          PAYMENT_ACCRUAL_FACTOR,
          NOTIONAL,
          FIXED_RATE,
          ACCRUAL_START_DATES,
          ACCRUAL_END_DATES,
          PAYMENT_ACCRUAL_FACTORS);

  @Test
  /** Tests the getters. */
  public void testGetters() {
    assertEquals(COUPON.getPaymentDate(), PAYMENT_DATE);
    assertEquals(COUPON.getAccrualStartDate(), ACCRUAL_START_DATES[0]);
    assertEquals(COUPON.getAccrualEndDate(), ACCRUAL_END_DATES[NB_SUB_PERIOD - 1]);
    assertEquals(COUPON.getPaymentYearFraction(), PAYMENT_ACCRUAL_FACTOR, 1E-10);
    assertEquals(COUPON.getNotional(), NOTIONAL, 1E-2);
    assertEquals(COUPON.getRate(), FIXED_RATE, 1E-10);
    assertArrayEquals(COUPON.getAccrualStartDates(), ACCRUAL_START_DATES);
    assertArrayEquals(COUPON.getAccrualEndDates(), ACCRUAL_END_DATES);
    assertArrayEquals(COUPON.getPaymentAccrualFactors(), PAYMENT_ACCRUAL_FACTORS, 0);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void nullCurrency() {
    CouponFixedCompoundingDefinition.from(
        null,
        PAYMENT_DATE,
        ACCRUAL_START_DATES[0],
        ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
        PAYMENT_ACCRUAL_FACTOR,
        NOTIONAL,
        FIXED_RATE,
        ACCRUAL_START_DATES,
        ACCRUAL_END_DATES,
        PAYMENT_ACCRUAL_FACTORS);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void testNullPaymentDate() {
    CouponFixedCompoundingDefinition.from(
        CURRENCY,
        null,
        ACCRUAL_START_DATES[0],
        ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
        PAYMENT_ACCRUAL_FACTOR,
        NOTIONAL,
        FIXED_RATE,
        ACCRUAL_START_DATES,
        ACCRUAL_END_DATES,
        PAYMENT_ACCRUAL_FACTORS);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void testNegativePaymentAccrualFactor() {
    CouponFixedCompoundingDefinition.from(
        CURRENCY,
        PAYMENT_DATE,
        ACCRUAL_START_DATES[0],
        ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
        -PAYMENT_ACCRUAL_FACTOR,
        NOTIONAL,
        FIXED_RATE,
        ACCRUAL_START_DATES,
        ACCRUAL_END_DATES,
        PAYMENT_ACCRUAL_FACTORS);
  }

  @Test
  /** Tests the equal and hash code. */
  public void testEqualHash() {
    assertEquals("CouponIbor: equal-hash", COUPON, COUPON);
    final CouponFixedCompoundingDefinition other =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE,
            ACCRUAL_START_DATES[0],
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
            PAYMENT_ACCRUAL_FACTOR,
            NOTIONAL,
            FIXED_RATE,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertEquals("CouponFixedCompoundingDefinition: equal-hash", other, COUPON);
    assertEquals(
        "CouponFixedCompoundingDefinition: equal-hash", other.hashCode(), COUPON.hashCode());
    CouponFixedCompoundingDefinition modified;
    modified =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE.plusDays(1),
            ACCRUAL_START_DATES[0],
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
            PAYMENT_ACCRUAL_FACTOR,
            NOTIONAL,
            FIXED_RATE,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
    modified =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE,
            ACCRUAL_START_DATES[0].plusDays(1),
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
            PAYMENT_ACCRUAL_FACTOR,
            NOTIONAL,
            FIXED_RATE,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
    modified =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE,
            ACCRUAL_START_DATES[0],
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1].plusDays(1),
            PAYMENT_ACCRUAL_FACTOR,
            NOTIONAL,
            FIXED_RATE,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
    modified =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE,
            ACCRUAL_START_DATES[0],
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
            PAYMENT_ACCRUAL_FACTOR + 1,
            NOTIONAL,
            FIXED_RATE,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
    modified =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE,
            ACCRUAL_START_DATES[0],
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
            PAYMENT_ACCRUAL_FACTOR,
            NOTIONAL + 1,
            FIXED_RATE,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
    modified =
        CouponFixedCompoundingDefinition.from(
            CURRENCY,
            PAYMENT_DATE,
            ACCRUAL_START_DATES[0],
            ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
            PAYMENT_ACCRUAL_FACTOR,
            NOTIONAL,
            FIXED_RATE + 1,
            ACCRUAL_START_DATES,
            ACCRUAL_END_DATES,
            PAYMENT_ACCRUAL_FACTORS);
    assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
  }
}
 @Test
 /** Tests the equal and hash code. */
 public void testEqualHash() {
   assertEquals("CouponIbor: equal-hash", COUPON, COUPON);
   final CouponFixedCompoundingDefinition other =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE,
           ACCRUAL_START_DATES[0],
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
           PAYMENT_ACCRUAL_FACTOR,
           NOTIONAL,
           FIXED_RATE,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertEquals("CouponFixedCompoundingDefinition: equal-hash", other, COUPON);
   assertEquals(
       "CouponFixedCompoundingDefinition: equal-hash", other.hashCode(), COUPON.hashCode());
   CouponFixedCompoundingDefinition modified;
   modified =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE.plusDays(1),
           ACCRUAL_START_DATES[0],
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
           PAYMENT_ACCRUAL_FACTOR,
           NOTIONAL,
           FIXED_RATE,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
   modified =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE,
           ACCRUAL_START_DATES[0].plusDays(1),
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
           PAYMENT_ACCRUAL_FACTOR,
           NOTIONAL,
           FIXED_RATE,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
   modified =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE,
           ACCRUAL_START_DATES[0],
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1].plusDays(1),
           PAYMENT_ACCRUAL_FACTOR,
           NOTIONAL,
           FIXED_RATE,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
   modified =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE,
           ACCRUAL_START_DATES[0],
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
           PAYMENT_ACCRUAL_FACTOR + 1,
           NOTIONAL,
           FIXED_RATE,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
   modified =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE,
           ACCRUAL_START_DATES[0],
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
           PAYMENT_ACCRUAL_FACTOR,
           NOTIONAL + 1,
           FIXED_RATE,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
   modified =
       CouponFixedCompoundingDefinition.from(
           CURRENCY,
           PAYMENT_DATE,
           ACCRUAL_START_DATES[0],
           ACCRUAL_END_DATES[NB_SUB_PERIOD - 1],
           PAYMENT_ACCRUAL_FACTOR,
           NOTIONAL,
           FIXED_RATE + 1,
           ACCRUAL_START_DATES,
           ACCRUAL_END_DATES,
           PAYMENT_ACCRUAL_FACTORS);
   assertFalse("CouponIbor: equal-hash", COUPON.equals(modified));
 }