FungibleTradeBuilder extends AbstractTradeBuilder { void updatePosition(BeanDataSource tradeData, UniqueId positionId) { ManageableTrade trade = buildTrade(tradeData); ManageablePosition position = getPositionMaster().get(positionId).getPosition(); trade.setUniqueId(null); if (position.getTrades().size() != 0) { throw new IllegalArgumentException("Cannot directly update a position that contains trade. Update the trades"); } position.setTrades(Lists.newArrayList(trade)); position.setQuantity(trade.getQuantity()); getPositionMaster().update(new PositionDocument(position)).getPosition(); } FungibleTradeBuilder(PositionMaster positionMaster,
                                     PortfolioMaster portfolioMaster,
                                     SecurityMaster securityMaster,
                                     Set<MetaBean> metaBeans,
                                     StringConvert stringConvert);   }
@Test public void updatePositionWithNoTrades() { ManageablePosition position = new ManageablePosition(BigDecimal.valueOf(42), APPLE_SECURITY.getExternalIdBundle()); ManageablePosition savedPosition = _positionMaster.add(new PositionDocument(position)).getPosition(); assertEquals(BigDecimal.valueOf(42), savedPosition.getQuantity()); _tradeBuilder.updatePosition(createTradeData("AAPL US Equity", null), savedPosition.getUniqueId()); ManageablePosition updatedPosition = _positionMaster.get(savedPosition.getUniqueId().getObjectId(), VersionCorrection.LATEST).getPosition(); assertEquals(BigDecimal.valueOf(30), updatedPosition.getQuantity()); assertEquals(1, updatedPosition.getTrades().size()); ManageableTrade trade = updatedPosition.getTrades().get(0); assertEquals(LocalDate.of(2012, 12, 21), trade.getTradeDate()); assertEquals(OffsetTime.of(LocalTime.of(14, 25), ZoneOffset.UTC), trade.getTradeTime()); assertEquals(APPLE_BUNDLE, trade.getSecurityLink().getExternalId()); assertEquals(1234d, trade.getPremium()); assertEquals(Currency.USD, trade.getPremiumCurrency()); assertEquals(LocalDate.of(2012, 12, 22), trade.getPremiumDate()); assertEquals(OffsetTime.of(LocalTime.of(13, 30), ZoneOffset.UTC), trade.getPremiumTime()); assertEquals(BigDecimal.valueOf(30), trade.getQuantity()); assertEquals(ExternalId.of(AbstractTradeBuilder.CPTY_SCHEME, "cptyName"), trade.getCounterpartyExternalId()); assertTrue(trade.getAttributes().isEmpty()); }