PortofolioService { public OrderInfo getOrderInfo(String login, String symbolCode, double quantity) { Objects.requireNonNull(symbolCode, "symbolCode is null"); SymbolEntity selectedSymbol = symbolRepo.findById(symbolCode).get(); if (selectedSymbol == null) { throw SymbolException.UNKNOWN(symbolCode); } PortofolioEntity portofolio = portofolioRepo.findByUserUsername(login); AssetEntity selectedAssetEntity = portofolio.getAsset(selectedSymbol.getCode()); if (selectedAssetEntity == null) { selectedAssetEntity = new AssetEntity(portofolio, selectedSymbol, ZERO, portofolio.getCurrencyFor(selectedSymbol), ZERO); } String currencyCode = selectedAssetEntity.getCurrency().getCode(); Rate q = rateService.getLatest(selectedSymbol.getCode(), currencyCode); AssetInfo selectedAsset = new AssetInfo(selectedAssetEntity.getSymbol(), selectedAssetEntity.getCurrency(), selectedAssetEntity.getQuantity().doubleValue()); selectedAsset.setCurrentRate(q.getValue().doubleValue()); final AssetInfo selectedCurrency; if (selectedAssetEntity.getCurrency().equals(portofolio.getBaseCurrency())) { selectedCurrency = new AssetInfo(portofolio.getBaseCurrency(), null, portofolio.getAmount().doubleValue()); } else { AssetEntity currencyAE = portofolio.getAsset(selectedAssetEntity.getCurrency().getCode()); selectedCurrency = new AssetInfo(currencyAE.getSymbol(), currencyAE.getCurrency(), currencyAE.getQuantity().doubleValue()); } BigDecimal gainCost = null; if (quantity != 0) { gainCost = q.getValue().multiply(BigDecimal.valueOf(quantity)); } return new OrderInfo(selectedAsset, selectedCurrency, gainCost); }  Portofolio createPortofolio(String login, String baseCurrencyCode, int baseCurrencyAmount); AssetInfo getBaseCurrency(String login); Portofolio getPortofolio(String login); OrderInfo getOrderInfo(String login, String symbolCode, double quantity); List<Symbol> getAvailableSymbols(String login); Order buy(String login, String code, double quantity); Order sell(String login, String code, double quantity);  }
@Test public void testGetOrderInfo_NullSymbol() { String login = "seller"; assertThatThrownBy(() -> portofolioService.getOrderInfo(login, null, 0)) .isInstanceOf(NullPointerException.class) .hasMessageContaining("symbolCode"); }
@Test public void testGetOrderInfo_withSymbolOwned() { String login = "seller"; String symbol = "UBI"; OrderInfo info = portofolioService.getOrderInfo(login, symbol, 1); assertThat(info.getSelected().getSymbol().getCode()).isEqualTo(symbol); assertThat(info.getSelectedCurrency().getSymbol().getCode()).isEqualTo(EUR); assertThat(info.getGainCost()).isEqualByComparingTo("28.155001"); }
@Test public void testGetOrderInfo_withSymbolNotOwned() { String login = "seller"; String symbol = GBP; OrderInfo info = portofolioService.getOrderInfo(login, symbol, 1); assertThat(info.getSelected().getSymbol().getCode()).isEqualTo(symbol); assertThat(info.getSelectedCurrency().getSymbol().getCode()).isEqualTo(EUR); assertThat(info.getGainCost()).isEqualByComparingTo("1.2664838478"); }