AdminService { public SymbolInfo add(String code, String provider) throws IOException { Stopwatch sw = Stopwatch.createStarted(); Pair pair = pairService.getForCodeAndProvider(code, provider); if (pair != null) { throw PairException.alreadyExists(pair.getSymbol(), provider); } LookupDetails details = lookupService.getDetails(code, provider); String source = details.getSource(); String currency = details.getCurrency(); String name = details.getName(); String exchange = details.getExchange(); if (!symbolService.getForCodeWithNoCurrency(currency).isPresent()) { throw PairException.currencyNotFound(currency); } Optional<Symbol> sourceSymbol = symbolService.getForCode(source); if(sourceSymbol.isPresent()) { if(sourceSymbol.get().getCurrency() == null) { throw AdminException.currencyAlreadyExists(source); } } else { symbolService.addSymbol(source, name, null, currency); } pair = pairService.create(code, name, source, currency, exchange, provider); final LatestRateProvider rProvider = rateProviderService.getLatestProvider(ProviderType.STOCK); Quote latestQuote = rProvider.getLatestRate(code); if (latestQuote == null) { throw SymbolException.UNAVAILABLE(code); } latestQuote.setCode(source); latestQuote.setCurrency(currency); List<Quote> historicalQuotes = new ArrayList<>(); final HistoricalRateProvider hRProvider = rateProviderService.getHistoricalProvider(ProviderType.STOCK); try { historicalQuotes.addAll(hRProvider.getHistoricalRates(code)); } catch (IOException | RuntimeException e) { log.warn("Cannot get historical data for: {} (provider: {})", code, provider); } for (Quote q : historicalQuotes) { q.setCode(source); q.setCurrency(currency); } historicalQuotes.removeIf(q -> q.getTime().compareTo(latestQuote.getTime()) == 0); historicalQuotes.add(latestQuote); rateService.addHistoricalRates(historicalQuotes); rateService.addLatestRate(latestQuote); log.info("{} created in: {}", pair, sw); return new SymbolInfo(pair.getSymbol(), pair.getName(), !historicalQuotes.isEmpty(), latestQuote); }  SymbolInfo add(String code, String provider); void delete(String code, String provider); SearchResult search(String symbolOrName);  }
@Sql("/sql/insert_data_histo.sql") @Test public void testAdd() throws Exception { String symbol = "UBI.PA"; String name = "Ubi"; String provider = "Provider"; when(lookupService.search(provider)).thenReturn(asList(new LookupInfo(symbol, name, "Paris", "Titres", provider))); when(lookupService.getDetails(symbol, provider)).thenReturn(new LookupDetails(symbol, name, "UBI", "EUR", "EPA", provider)); Quote histoQuote = new Quote(symbol, new BigDecimal("25.2"), parse("2016-06-30T02:00:00+02:00")); Quote latestQuote = new Quote(symbol, new BigDecimal("25"), parse("2016-06-30T19:32:00+02:00")); when(rateProviderService.getHistoricalProvider(ProviderType.STOCK)).thenReturn(hrp); when(rateProviderService.getLatestProvider(ProviderType.STOCK)).thenReturn(lrp); when(hrp.getHistoricalRates(symbol)).thenReturn(asList(histoQuote)); when(lrp.getLatestRate(symbol)).thenReturn(latestQuote); SymbolInfo result = adminService.add(symbol, provider); Pair pairResult = pairService.getForCodeAndProvider(symbol, provider); assertThat(result.getCode()).isEqualTo(pairResult.getSymbol()); assertThat(result.getName()).isEqualTo(pairResult.getName()); assertThat(result.getQuote().getCode()).isEqualTo(pairResult.getSource().getCode()); assertThat(result.getQuote().getCurrency()).isEqualTo(pairResult.getTarget().getCode()); assertThat(result.getQuote().getPrice()).isEqualTo("25"); assertThat(result.getQuote().getTime()).isEqualTo(parse("2016-06-30T19:32:00+02:00")); BooleanExpression exp = historicalRate.fromcur.code.eq("UBI").and(historicalRate.tocur.code.eq("EUR")); assertThat(hRateRepo.count(exp)).isEqualTo(2); assertThat(rateService.getLatest("UBI", "EUR")).isNotNull(); }
@Sql("/sql/insert_data.sql") @Test public void testAdd_pairAlreadyExists() throws Exception { String symbol = "UBI.PA"; String provider = "YAHOO"; assertThatThrownBy(() -> adminService.add(symbol, provider)) .isInstanceOf(PairException.class) .hasMessage("pair.error.already_exists: [UBI.PA, YAHOO]"); }
@Test public void testAdd_currencyNotExist() throws Exception { String symbol = "UBI.PA"; String name = "Ubi"; String provider = "Provider"; when(lookupService.getDetails(symbol, provider)).thenReturn(new LookupDetails(symbol, name, "UBI", "XXX", "EPA", provider)); assertThatThrownBy(() -> adminService.add(symbol, provider)) .isInstanceOf(PairException.class) .hasMessage("pair.error.currency-not-found: [XXX]"); }
@Sql("/sql/insert_data.sql") @Test public void testAdd_alreadyExistsAsCurrency() throws Exception { String symbol = "EURUSD=X"; String name = "EUR/USD"; String provider = "Provider"; when(lookupService.getDetails(symbol, provider)).thenReturn(new LookupDetails(symbol, name, "EUR", "USD", "CY", provider)); assertThatThrownBy(() -> adminService.add(symbol, provider)) .isInstanceOf(AdminException.class) .hasMessage("admin.error.currency-already-exists: [EUR]"); }