@Override
  public Timestamp toNonNullValue(Instant value) {

    final Timestamp timestamp = new Timestamp(value.toEpochMilli());
    timestamp.setNanos(value.getNano());
    return timestamp;
  }
  @Override
  public Instant fromNonNullValue(Timestamp value) {

    Instant instant = Instant.ofEpochMilli(value.getTime());
    instant = instant.with(ChronoField.NANO_OF_SECOND, value.getNanos());

    return instant;
  }
  @Override
  public PortfolioDocument update(PortfolioDocument document) {
    ArgumentChecker.notNull(document, "document");
    ArgumentChecker.notNull(document.getUniqueId(), "document.uniqueId");
    ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio");

    final UniqueId uniqueId = document.getUniqueId();
    final Instant now = Instant.now();
    final PortfolioDocument storedDocument = _store.get(uniqueId.getObjectId());
    if (storedDocument == null) {
      throw new DataNotFoundException("Portfolio not found: " + uniqueId);
    }

    final PortfolioDocument clonedDoc = clonePortfolioDocument(document);
    removeNodes(storedDocument.getPortfolio().getRootNode());

    setVersionTimes(document, clonedDoc, now, null, now, null);

    if (_store.replace(uniqueId.getObjectId(), storedDocument, clonedDoc) == false) {
      throw new IllegalArgumentException("Concurrent modification");
    }
    storeNodes(
        clonedDoc.getPortfolio().getRootNode(),
        document.getPortfolio().getRootNode(),
        uniqueId,
        null);
    _changeManager.entityChanged(
        ChangeType.CHANGED,
        document.getObjectId(),
        document.getVersionFromInstant(),
        document.getVersionToInstant(),
        now);
    updateCaches(storedDocument, clonedDoc);
    return document;
  }
  @Override
  public PortfolioDocument add(PortfolioDocument document) {
    ArgumentChecker.notNull(document, "document");
    ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio");

    final ObjectId objectId = _objectIdSupplier.get();
    final UniqueId uniqueId = objectId.atVersion("");
    final Instant now = Instant.now();

    final PortfolioDocument clonedDoc = clonePortfolioDocument(document);
    setDocumentId(document, clonedDoc, uniqueId);
    setVersionTimes(document, clonedDoc, now, null, now, null);
    _store.put(objectId, clonedDoc);
    storeNodes(
        clonedDoc.getPortfolio().getRootNode(),
        document.getPortfolio().getRootNode(),
        uniqueId,
        null);
    _changeManager.entityChanged(
        ChangeType.ADDED,
        objectId,
        document.getVersionFromInstant(),
        document.getVersionToInstant(),
        now);
    updateCaches(null, clonedDoc);
    return document;
  }
 /**
  * Sets the function initialization identifier.
  *
  * @param id the identifier to bind
  */
 public void setFunctionInitId(final long id) {
   put(FUNCTION_INIT_ID_NAME, id);
   // TODO: Note that the behaviour below is closely coupled to the implementation of
   // initialization identifiers in CompiledFunctionService
   final Instant instant = Instant.ofEpochMilli(id);
   put(FUNCTION_INIT_TIMESTAMP_NAME, VersionCorrection.of(instant, instant));
 }
  @BeforeClass
  public void setUpClass() {

    ImmutableMap<Class<?>, Object> components = generateComponents();
    VersionCorrectionProvider vcProvider = new FixedInstantVersionCorrectionProvider(Instant.now());
    ServiceContext serviceContext =
        ServiceContext.of(components).with(VersionCorrectionProvider.class, vcProvider);
    ThreadLocalServiceContext.init(serviceContext);

    ComponentMap componentMap = ComponentMap.of(components);
    MarketDataSource marketDataSource =
        InterestRateMockSources.createMarketDataSource(MARKET_DATA_DATE, true);
    TestMarketDataFactory marketDataFactory = new TestMarketDataFactory(marketDataSource);
    ConfigLink<CurrencyMatrix> currencyMatrixLink =
        ConfigLink.resolved(componentMap.getComponent(CurrencyMatrix.class));
    List<MarketDataBuilder> builders =
        MarketDataBuilders.standard(componentMap, "dataSource", currencyMatrixLink);

    MarketDataEnvironmentFactory environmentFactory =
        new MarketDataEnvironmentFactory(marketDataFactory, builders);

    _functionRunner = new FunctionRunner(environmentFactory);
    _normalIRFutureOptionFn =
        FunctionModel.build(IRFutureOptionFn.class, normalConfig(), componentMap);
    _blackIRFutureOptionFn =
        FunctionModel.build(IRFutureOptionFn.class, blackConfig(), componentMap);
  }
 @Override
 public void remove(ObjectIdentifiable objectIdentifiable) {
   ArgumentChecker.notNull(objectIdentifiable, "objectIdentifiable");
   PortfolioDocument storedDocument = _store.remove(objectIdentifiable.getObjectId());
   if (storedDocument == null) {
     throw new DataNotFoundException("Portfolio not found " + objectIdentifiable);
   }
   removeNodes(storedDocument.getPortfolio().getRootNode());
   _changeManager.entityChanged(
       ChangeType.REMOVED, objectIdentifiable.getObjectId(), null, null, Instant.now());
   updateCaches(storedDocument, null);
 }
 @Test
 public void masterSubscription() throws IOException, JSONException {
   String clientId = _webPushTestUtils.handshake();
   String restUrl = "/jax/test/positions";
   // this REST request should set up a subscription for any changes in the position master
   _webPushTestUtils.readFromPath(restUrl, clientId);
   // send a change event
   UniqueId uid = UniqueId.of("Tst", "101");
   _positionChangeManager.entityChanged(
       ChangeType.CHANGED, uid.getObjectId(), null, null, Instant.now());
   // connect to the long-polling URL to receive notification of the change
   String json = _webPushTestUtils.readFromPath("/updates/" + clientId);
   WebPushTestUtils.checkJsonResults(json, restUrl);
 }
/** Test {@link MasterExchangeSource}. */
@Test(groups = TestGroup.UNIT)
public class MasterExchangeSourceTest {

  private static final ObjectId OID = ObjectId.of("A", "B");
  private static final UniqueId UID = UniqueId.of("A", "B", "V");
  private static final ExternalId ID = ExternalId.of("C", "D");
  private static final ExternalIdBundle BUNDLE = ExternalIdBundle.of(ID);
  private static final Instant NOW = Instant.now();
  private static final VersionCorrection VC =
      VersionCorrection.of(NOW.minusSeconds(2), NOW.minusSeconds(1));

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void test_constructor_1arg_nullMaster() throws Exception {
    new MasterExchangeSource(null);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void test_constructor_2arg_nullMaster() throws Exception {
    new MasterExchangeSource(null, null);
  }

  // -------------------------------------------------------------------------
  public void test_getExchange_UniqueId_noOverride_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    ExchangeDocument doc = new ExchangeDocument(example());
    when(mock.get(UID)).thenReturn(doc);
    MasterExchangeSource test = new MasterExchangeSource(mock);
    Exchange testResult = test.get(UID);
    verify(mock, times(1)).get(UID);

    assertEquals(example(), testResult);
  }

  public void test_getExchange_UniqueId_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    ExchangeDocument doc = new ExchangeDocument(example());
    when(mock.get(OID, VC)).thenReturn(doc);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.get(UID);
    verify(mock, times(1)).get(OID, VC);

    assertEquals(example(), testResult);
  }

  @Test(expectedExceptions = DataNotFoundException.class)
  public void test_getExchange_UniqueId_notFound() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    when(mock.get(OID, VC)).thenThrow(new DataNotFoundException(""));
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    try {
      test.get(UID);
    } finally {
      verify(mock, times(1)).get(OID, VC);
    }
  }

  // -------------------------------------------------------------------------
  public void test_getExchange_ObjectId_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    ExchangeDocument doc = new ExchangeDocument(example());
    when(mock.get(OID, VC)).thenReturn(doc);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.get(OID, VC);
    verify(mock, times(1)).get(OID, VC);

    assertEquals(example(), testResult);
  }

  @Test(expectedExceptions = DataNotFoundException.class)
  public void test_getExchange_ObjectId_notFound() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);

    when(mock.get(OID, VC)).thenThrow(new DataNotFoundException(""));
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    try {
      test.get(OID, VC);
    } finally {
      verify(mock, times(1)).get(OID, VC);
    }
  }

  // -------------------------------------------------------------------------
  public void test_getSingleExchange_ExternalId_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);
    ExchangeSearchRequest request = new ExchangeSearchRequest(ID);
    request.setPagingRequest(PagingRequest.ONE);
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VC);

    ExchangeSearchResult result = new ExchangeSearchResult();
    result.getDocuments().add(new ExchangeDocument(example()));

    when(mock.search(request)).thenReturn(result);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.getSingle(ID);
    verify(mock, times(1)).search(request);

    assertEquals(example(), testResult);
  }

  public void test_getSingleExchange_ExternalId_notFound() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);
    ExchangeSearchRequest request = new ExchangeSearchRequest(ID);
    request.setPagingRequest(PagingRequest.ONE);
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VC);

    ExchangeSearchResult result = new ExchangeSearchResult();

    when(mock.search(request)).thenReturn(result);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.getSingle(ID);
    verify(mock, times(1)).search(request);

    assertEquals(null, testResult);
  }

  // -------------------------------------------------------------------------
  public void test_getSingleExchange_ExternalIdBundle_found() throws Exception {
    ExchangeMaster mock = mock(ExchangeMaster.class);
    ExchangeSearchRequest request = new ExchangeSearchRequest(BUNDLE);
    request.setPagingRequest(PagingRequest.ONE);
    request.setVersionCorrection(VC);

    ExchangeSearchResult result = new ExchangeSearchResult();
    result.getDocuments().add(new ExchangeDocument(example()));

    when(mock.search(request)).thenReturn(result);
    MasterExchangeSource test = new MasterExchangeSource(mock, VC);
    Exchange testResult = test.getSingle(BUNDLE);
    verify(mock, times(1)).search(request);

    assertEquals(example(), testResult);
  }

  // -------------------------------------------------------------------------
  protected ManageableExchange example() {
    ManageableExchange exchange = new ManageableExchange();
    exchange.setUniqueId(UID);
    exchange.setName("NYSE");
    exchange.setRegionIdBundle(ExternalIdBundle.of(ExternalSchemes.countryRegionId(Country.US)));
    return exchange;
  }
}
 @Override
 public String toNonNullValue(Instant value) {
   return value.toString();
 }
 @Override
 public Instant fromNonNullValue(String s) {
   return Instant.parse(s);
 }
 protected ZonedDateTime convertJavaDate(Date d) {
   return ZonedDateTime.ofInstant(Instant.ofEpochMilli(d.getTime()), ZoneId.systemDefault());
 }