PortfolioWriter { public void write(Portfolio portfolio, Set<ManageableSecurity> securities) { persistSecurities(securities); persistPortfolio(portfolio); } PortfolioWriter(boolean write, PortfolioMaster portfolioMaster,
                         PositionMaster positionMaster, SecurityMaster securityMaster); void write(Portfolio portfolio, Set<ManageableSecurity> securities);  }
@Test public void testNewPortfolioInsertsSecurityPositionPortfolio() { PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(), mockPositionMaster(), mockSecurityMaster()); String portfolioName = "TestPortfolio"; SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); ExternalIdBundle securityKey = ExternalIdBundle.of("TEST", "1234"); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); security.setExternalIdBundle(securityKey); Set<ManageableSecurity> securities = ImmutableSet.of(security); root.addPosition(new SimplePosition(BigDecimal.valueOf(1000), securityKey)); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); assertThat(_recorder._securities.size(), is(1)); assertThat(_recorder._securities.get(0).getExternalIdBundle(), is(securityKey)); assertThat(_recorder._positions.size(), is(1)); assertThat(_recorder._positions.get(0).getSecurityLink().getExternalId(), is(securityKey)); assertThat(_recorder._positions.get(0).getQuantity(), is(BigDecimal.valueOf(1000))); assertThat(_recorder.countPortfolioAdditions(), is(1)); assertThat(_recorder._portfolios.get(0).getPortfolio().getName(), is(portfolioName)); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testCannotInsertSecurityWithoutIdBundle() { PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(), mockPositionMaster(), mockSecurityMaster()); String portfolioName = "TestPortfolio"; SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); Set<ManageableSecurity> securities = ImmutableSet.of(security); SimplePosition position = new SimplePosition(); position.setQuantity(BigDecimal.valueOf(1000)); root.addPosition(position); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testCannotInsertPositionWithEmptySecurityIdBundle() { PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(), mockPositionMaster(), mockSecurityMaster()); String portfolioName = "TestPortfolio"; SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); ExternalIdBundle securityKey = ExternalIdBundle.of("TEST", "1234"); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); security.setExternalIdBundle(securityKey); Set<ManageableSecurity> securities = ImmutableSet.of(security); root.addPosition(new SimplePosition(BigDecimal.valueOf(1000), ExternalIdBundle.EMPTY)); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testCannotInsertTradeWithEmptySecurityIdBundle() { PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(), mockPositionMaster(), mockSecurityMaster()); String portfolioName = "TestPortfolio"; SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); ExternalIdBundle securityKey = ExternalIdBundle.of("TEST", "1234"); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); security.setExternalIdBundle(securityKey); Set<ManageableSecurity> securities = ImmutableSet.of(security); SimplePosition position = new SimplePosition(BigDecimal.valueOf(1000), securityKey); position.addTrade(new SimpleTrade(new SimpleSecurityLink(ExternalIdBundle.EMPTY), BigDecimal.valueOf(1000), new SimpleCounterparty(ExternalId.of("CP", "123")), LocalDate.of(2014, 5, 1), OffsetTime.MAX)); root.addPosition(position); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); }
@Test(expectedExceptions = OpenGammaRuntimeException.class) public void testCannotInsertPositionWithNullSecurityIdBundle() { PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(), mockPositionMaster(), mockSecurityMaster()); String portfolioName = "TestPortfolio"; SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); ExternalIdBundle securityKey = ExternalIdBundle.of("TEST", "1234"); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); security.setExternalIdBundle(securityKey); Set<ManageableSecurity> securities = ImmutableSet.of(security); SimplePosition position = new SimplePosition(); position.setQuantity(BigDecimal.valueOf(1000)); root.addPosition(position); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); }
@Test public void testNewPortfolioWithExistingSecurity() { ExternalIdBundle securityKey = ExternalIdBundle.of("TEST", "1234"); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); security.setExternalIdBundle(securityKey); PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(), mockPositionMaster(), mockSecurityMaster(security)); String portfolioName = "TestPortfolio"; SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); Set<ManageableSecurity> securities = ImmutableSet.of(security); root.addPosition(new SimplePosition(BigDecimal.valueOf(1000), securityKey)); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); assertThat(_recorder._securities.size(), is(0)); assertThat(_recorder._positions.size(), is(1)); assertThat(_recorder._positions.get(0).getSecurityLink().getExternalId(), is(securityKey)); assertThat(_recorder._positions.get(0).getQuantity(), is(BigDecimal.valueOf(1000))); assertThat(_recorder.countPortfolioAdditions(), is(1)); assertThat(_recorder._portfolios.get(0).getPortfolio().getName(), is(portfolioName)); }
@Test public void testReplacementOfExistingPortfolio() { String portfolioName = "TestPortfolio"; ManageablePortfolio existing = new ManageablePortfolio(portfolioName); PortfolioWriter persister = new PortfolioWriter(true, mockPortfolioMaster(existing), mockPositionMaster(), mockSecurityMaster()); SimplePortfolioNode root = new SimplePortfolioNode(portfolioName); ExternalIdBundle securityKey = ExternalIdBundle.of("TEST", "1234"); ManageableSecurity security = new ManageableSecurity("SEC_TYPE_TEST"); security.setName("TestSec"); security.setExternalIdBundle(securityKey); Set<ManageableSecurity> securities = ImmutableSet.of(security); root.addPosition(new SimplePosition(BigDecimal.valueOf(1000), securityKey)); SimplePortfolio pf = new SimplePortfolio(portfolioName, root); persister.write(pf, securities); assertThat(_recorder._securities.size(), is(1)); assertThat(_recorder._securities.get(0).getExternalIdBundle(), is(securityKey)); assertThat(_recorder._positions.size(), is(1)); assertThat(_recorder._positions.get(0).getSecurityLink().getExternalId(), is(securityKey)); assertThat(_recorder._positions.get(0).getQuantity(), is(BigDecimal.valueOf(1000))); assertThat(_recorder.countPortfolioUpdates(), is(1)); assertThat(_recorder._portfolios.get(0).getPortfolio().getName(), is(portfolioName)); }