// -------------------------------------------------------------------------
 public void coverage() {
   TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
   coverImmutableBean(test);
   TermDepositCurveNode test2 =
       TermDepositCurveNode.of(
           TermDepositTemplate.of(Period.ofMonths(1), CONVENTION),
           QuoteId.of(StandardId.of("OG-Ticker", "Deposit2")));
   coverBeanEquals(test, test2);
 }
  // -----------------------------------------------------------------------
  // create a vanilla fixed vs libor 3m swap
  private static Trade createVanillaFixedVsLibor3mSwap() {
    NotionalSchedule notional = NotionalSchedule.of(Currency.USD, 100_000_000);

    SwapLeg payLeg =
        RateCalculationSwapLeg.builder()
            .payReceive(PayReceive.PAY)
            .accrualSchedule(
                PeriodicSchedule.builder()
                    .startDate(LocalDate.of(2014, 9, 12))
                    .endDate(LocalDate.of(2021, 9, 12))
                    .frequency(Frequency.P6M)
                    .businessDayAdjustment(
                        BusinessDayAdjustment.of(MODIFIED_FOLLOWING, HolidayCalendarIds.USNY))
                    .build())
            .paymentSchedule(
                PaymentSchedule.builder()
                    .paymentFrequency(Frequency.P6M)
                    .paymentDateOffset(DaysAdjustment.NONE)
                    .build())
            .notionalSchedule(notional)
            .calculation(FixedRateCalculation.of(0.015, DayCounts.THIRTY_U_360))
            .build();

    SwapLeg receiveLeg =
        RateCalculationSwapLeg.builder()
            .payReceive(PayReceive.RECEIVE)
            .accrualSchedule(
                PeriodicSchedule.builder()
                    .startDate(LocalDate.of(2014, 9, 12))
                    .endDate(LocalDate.of(2021, 9, 12))
                    .frequency(Frequency.P3M)
                    .businessDayAdjustment(
                        BusinessDayAdjustment.of(MODIFIED_FOLLOWING, HolidayCalendarIds.USNY))
                    .build())
            .paymentSchedule(
                PaymentSchedule.builder()
                    .paymentFrequency(Frequency.P3M)
                    .paymentDateOffset(DaysAdjustment.NONE)
                    .build())
            .notionalSchedule(notional)
            .calculation(IborRateCalculation.of(IborIndices.USD_LIBOR_3M))
            .build();

    return SwapTrade.builder()
        .product(Swap.of(payLeg, receiveLeg))
        .info(
            TradeInfo.builder()
                .addAttribute(TradeAttributeType.DESCRIPTION, "Fixed vs Libor 3m")
                .counterparty(StandardId.of("example", "A"))
                .settlementDate(LocalDate.of(2014, 9, 12))
                .build())
        .build();
  }
/** Test {@link SecurityPosition}. */
@Test
public class SecurityPositionTest {

  private static final PositionInfo POSITION_INFO = PositionInfo.of(StandardId.of("A", "B"));
  private static final SecurityId SECURITY_ID = SecurityId.of("OG-Test", "Id");
  private static final SecurityId SECURITY_ID2 = SecurityId.of("OG-Test", "Id2");
  private static final double LONG_QUANTITY = 300;
  private static final double LONG_QUANTITY2 = 350;
  private static final double SHORT_QUANTITY = 200;
  private static final double SHORT_QUANTITY2 = 150;
  private static final double QUANTITY = 100;

  // -------------------------------------------------------------------------
  public void test_ofNet_noInfo() {
    SecurityPosition test = SecurityPosition.ofNet(SECURITY_ID, QUANTITY);
    assertEquals(test.getInfo(), PositionInfo.empty());
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), QUANTITY);
    assertEquals(test.getShortQuantity(), 0d);
    assertEquals(test.getQuantity(), QUANTITY);
  }

  public void test_ofNet_withInfo_positive() {
    SecurityPosition test = SecurityPosition.ofNet(POSITION_INFO, SECURITY_ID, 100d);
    assertEquals(test.getInfo(), POSITION_INFO);
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), 100d);
    assertEquals(test.getShortQuantity(), 0d);
    assertEquals(test.getQuantity(), 100d);
  }

  public void test_ofNet_withInfo_zero() {
    SecurityPosition test = SecurityPosition.ofNet(POSITION_INFO, SECURITY_ID, 0d);
    assertEquals(test.getInfo(), POSITION_INFO);
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), 0d);
    assertEquals(test.getShortQuantity(), 0d);
    assertEquals(test.getQuantity(), 0d);
  }

  public void test_ofNet_withInfo_negative() {
    SecurityPosition test = SecurityPosition.ofNet(POSITION_INFO, SECURITY_ID, -100d);
    assertEquals(test.getInfo(), POSITION_INFO);
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), 0d);
    assertEquals(test.getShortQuantity(), 100d);
    assertEquals(test.getQuantity(), -100d);
  }

  public void test_ofLongShort_noInfo() {
    SecurityPosition test =
        SecurityPosition.ofLongShort(SECURITY_ID, LONG_QUANTITY, SHORT_QUANTITY);
    assertEquals(test.getInfo(), PositionInfo.empty());
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), LONG_QUANTITY);
    assertEquals(test.getShortQuantity(), SHORT_QUANTITY);
    assertEquals(test.getQuantity(), QUANTITY);
  }

  public void test_ofLongShort_withInfo() {
    SecurityPosition test =
        SecurityPosition.ofLongShort(POSITION_INFO, SECURITY_ID, LONG_QUANTITY, SHORT_QUANTITY);
    assertEquals(test.getInfo(), POSITION_INFO);
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), LONG_QUANTITY);
    assertEquals(test.getShortQuantity(), SHORT_QUANTITY);
    assertEquals(test.getQuantity(), QUANTITY);
  }

  public void test_builder() {
    SecurityPosition test = sut();
    assertEquals(test.getInfo(), POSITION_INFO);
    assertEquals(test.getSecurityId(), SECURITY_ID);
    assertEquals(test.getLongQuantity(), LONG_QUANTITY);
    assertEquals(test.getShortQuantity(), SHORT_QUANTITY);
    assertEquals(test.getQuantity(), QUANTITY);
  }

  // -------------------------------------------------------------------------
  public void coverage() {
    coverImmutableBean(sut());
    coverBeanEquals(sut(), sut2());
  }

  public void test_serialization() {
    assertSerialization(sut());
  }

  // -------------------------------------------------------------------------
  static SecurityPosition sut() {
    return SecurityPosition.builder()
        .info(POSITION_INFO)
        .securityId(SECURITY_ID)
        .longQuantity(LONG_QUANTITY)
        .shortQuantity(SHORT_QUANTITY)
        .build();
  }

  static SecurityPosition sut2() {
    return SecurityPosition.builder()
        .info(PositionInfo.empty())
        .securityId(SECURITY_ID2)
        .longQuantity(LONG_QUANTITY2)
        .shortQuantity(SHORT_QUANTITY2)
        .build();
  }
}
/** Test {@link TermDepositCurveNode}. */
@Test
public class TermDepositCurveNodeTest {

  private static final ReferenceData REF_DATA = ReferenceData.standard();
  private static final LocalDate VAL_DATE = date(2015, 6, 30);
  private static final BusinessDayAdjustment BDA_MOD_FOLLOW =
      BusinessDayAdjustment.of(MODIFIED_FOLLOWING, EUTA);
  private static final DaysAdjustment PLUS_TWO_DAYS = DaysAdjustment.ofBusinessDays(2, EUTA);
  private static final TermDepositConvention CONVENTION = TermDepositConventions.EUR_DEPOSIT_T2;
  private static final Period DEPOSIT_PERIOD = Period.ofMonths(3);
  private static final TermDepositTemplate TEMPLATE =
      TermDepositTemplate.of(DEPOSIT_PERIOD, CONVENTION);
  private static final QuoteId QUOTE_ID = QuoteId.of(StandardId.of("OG-Ticker", "Deposit1"));
  private static final double SPREAD = 0.0015;
  private static final String LABEL = "Label";
  private static final String LABEL_AUTO = "3M";

  public void test_builder() {
    TermDepositCurveNode test =
        TermDepositCurveNode.builder()
            .label(LABEL)
            .template(TEMPLATE)
            .rateId(QUOTE_ID)
            .additionalSpread(SPREAD)
            .date(CurveNodeDate.LAST_FIXING)
            .build();
    assertEquals(test.getLabel(), LABEL);
    assertEquals(test.getRateId(), QUOTE_ID);
    assertEquals(test.getAdditionalSpread(), SPREAD);
    assertEquals(test.getTemplate(), TEMPLATE);
    assertEquals(test.getDate(), CurveNodeDate.LAST_FIXING);
  }

  public void test_builder_defaults() {
    TermDepositCurveNode test =
        TermDepositCurveNode.builder()
            .label(LABEL)
            .template(TEMPLATE)
            .rateId(QUOTE_ID)
            .additionalSpread(SPREAD)
            .build();
    assertEquals(test.getLabel(), LABEL);
    assertEquals(test.getRateId(), QUOTE_ID);
    assertEquals(test.getAdditionalSpread(), SPREAD);
    assertEquals(test.getTemplate(), TEMPLATE);
    assertEquals(test.getDate(), CurveNodeDate.END);
  }

  public void test_of_noSpread() {
    TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID);
    assertEquals(test.getLabel(), LABEL_AUTO);
    assertEquals(test.getRateId(), QUOTE_ID);
    assertEquals(test.getAdditionalSpread(), 0.0d);
    assertEquals(test.getTemplate(), TEMPLATE);
  }

  public void test_of_withSpread() {
    TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    assertEquals(test.getLabel(), LABEL_AUTO);
    assertEquals(test.getRateId(), QUOTE_ID);
    assertEquals(test.getAdditionalSpread(), SPREAD);
    assertEquals(test.getTemplate(), TEMPLATE);
  }

  public void test_of_withSpreadAndLabel() {
    TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD, LABEL);
    assertEquals(test.getLabel(), LABEL);
    assertEquals(test.getRateId(), QUOTE_ID);
    assertEquals(test.getAdditionalSpread(), SPREAD);
    assertEquals(test.getTemplate(), TEMPLATE);
  }

  public void test_requirements() {
    TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    Set<ObservableId> set = test.requirements();
    Iterator<ObservableId> itr = set.iterator();
    assertEquals(itr.next(), QUOTE_ID);
    assertFalse(itr.hasNext());
  }

  public void test_trade() {
    TermDepositCurveNode node = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    double rate = 0.035;
    MarketData marketData = ImmutableMarketData.builder(VAL_DATE).addValue(QUOTE_ID, rate).build();
    TermDepositTrade trade = node.trade(1d, marketData, REF_DATA);
    LocalDate startDateExpected = PLUS_TWO_DAYS.adjust(VAL_DATE, REF_DATA);
    LocalDate endDateExpected = startDateExpected.plus(DEPOSIT_PERIOD);
    TermDeposit depositExpected =
        TermDeposit.builder()
            .buySell(BuySell.BUY)
            .currency(EUR)
            .dayCount(ACT_360)
            .startDate(startDateExpected)
            .endDate(endDateExpected)
            .notional(1.0d)
            .businessDayAdjustment(BDA_MOD_FOLLOW)
            .rate(rate + SPREAD)
            .build();
    TradeInfo tradeInfoExpected = TradeInfo.builder().tradeDate(VAL_DATE).build();
    assertEquals(trade.getProduct(), depositExpected);
    assertEquals(trade.getInfo(), tradeInfoExpected);
  }

  public void test_trade_noMarketData() {
    TermDepositCurveNode node = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    LocalDate valuationDate = LocalDate.of(2015, 1, 22);
    MarketData marketData = MarketData.empty(valuationDate);
    assertThrows(() -> node.trade(1d, marketData, REF_DATA), MarketDataNotFoundException.class);
  }

  public void test_initialGuess() {
    TermDepositCurveNode node = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    double rate = 0.035;
    MarketData marketData = ImmutableMarketData.builder(VAL_DATE).addValue(QUOTE_ID, rate).build();
    assertEquals(node.initialGuess(marketData, ValueType.ZERO_RATE), rate);
    assertEquals(node.initialGuess(marketData, ValueType.FORWARD_RATE), rate);
    assertEquals(
        node.initialGuess(marketData, ValueType.DISCOUNT_FACTOR), Math.exp(-rate * 0.25), 1.0e-12);
  }

  public void test_metadata_end() {
    TermDepositCurveNode node = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    LocalDate valuationDate = LocalDate.of(2015, 1, 22);
    ParameterMetadata metadata = node.metadata(valuationDate, REF_DATA);
    assertEquals(((TenorDateParameterMetadata) metadata).getDate(), LocalDate.of(2015, 4, 27));
    assertEquals(((TenorDateParameterMetadata) metadata).getTenor(), Tenor.TENOR_3M);
  }

  public void test_metadata_fixed() {
    LocalDate nodeDate = VAL_DATE.plusMonths(1);
    TermDepositCurveNode node =
        TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD).withDate(CurveNodeDate.of(nodeDate));
    LocalDate valuationDate = LocalDate.of(2015, 1, 22);
    DatedParameterMetadata metadata = node.metadata(valuationDate, REF_DATA);
    assertEquals(metadata.getDate(), nodeDate);
    assertEquals(metadata.getLabel(), node.getLabel());
  }

  public void test_metadata_last_fixing() {
    TermDepositCurveNode node =
        TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD).withDate(CurveNodeDate.LAST_FIXING);
    assertThrowsWithCause(
        () -> node.metadata(VAL_DATE, REF_DATA), UnsupportedOperationException.class);
  }

  // -------------------------------------------------------------------------
  public void coverage() {
    TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    coverImmutableBean(test);
    TermDepositCurveNode test2 =
        TermDepositCurveNode.of(
            TermDepositTemplate.of(Period.ofMonths(1), CONVENTION),
            QuoteId.of(StandardId.of("OG-Ticker", "Deposit2")));
    coverBeanEquals(test, test2);
  }

  public void test_serialization() {
    TermDepositCurveNode test = TermDepositCurveNode.of(TEMPLATE, QUOTE_ID, SPREAD);
    assertSerialization(test);
  }
}