RateUpdaterService { @Transactional public void updateRates(List<Quote> quotes) { List<Quote> quotesList = new ArrayList<>(quotes); Collections.sort(quotesList, (o1, o2) -> o1.getTime().compareTo(o2.getTime())); List<LatestRate> existingLatestRates = latestRepo.findAll(); List<LatestRate> newLatestRates = new ArrayList<>(); List<Quote> newHistoricalRates = new ArrayList<>(); List<SymbolEntity> symbols = symbolRepo.findAll(); for (Quote quote : quotesList) { LatestRate existingLatestRate = getFromList(existingLatestRates, quote); if (existingLatestRate == null) { SymbolEntity fromcur = getFromList(symbols, quote.getCode()); if(fromcur == null) { log.warn("Cannot find symbol '{}' in DB, skipping it.", quote.getCode()); continue; } SymbolEntity tocur = getFromList(symbols, quote.getCurrency()); if(tocur == null) { log.warn("Cannot find symbol '{}' in DB, skipping it.", quote.getCurrency()); continue; } LatestRate newLatestRate = new LatestRate(fromcur, tocur, quote.getPrice(), quote.getTime()); newLatestRates.add(newLatestRate); existingLatestRates.add(newLatestRate); } else if (quote.getTime().isAfter(existingLatestRate.getDate())) { existingLatestRate.setDate(quote.getTime()); existingLatestRate.setValue(quote.getPrice()); if (!newLatestRates.contains(existingLatestRate)) { newLatestRates.add(existingLatestRate); } } newHistoricalRates.add(quote); } latestRepo.saveAll(newLatestRates); rateService.addHistoricalRates(newHistoricalRates); if (!newLatestRates.isEmpty()) { bus.publishEvent(new RatesUpdatedEvent().setRates(rateMapper.mapToRates(newLatestRates))); } }  @Transactional void updateRates(List<Quote> quotes);  }
@Test public void testUpdateRates() { List<Quote> quotes = getBrentQuotes(); HistoricalRate expected1 = new HistoricalRate(BRE, USD, new BigDecimal("55.18"), parse("2015-04-02T02:00:00")); HistoricalRate expected2 = new HistoricalRate(BRE, USD, new BigDecimal("56.72"), parse("2015-04-03T02:00:00")); HistoricalRate expected3 = new HistoricalRate(BRE, USD, new BigDecimal("55.18"), parse("2015-04-07T02:00:00")); HistoricalRate expected4 = new HistoricalRate(BRE, USD, new BigDecimal("57.8"), parse("2015-04-08T02:00:00")); HistoricalRate expected5 = new HistoricalRate(BRE, USD, new BigDecimal("58.3"), parse("2015-04-09T02:00:00")); LatestRate expected6 = new LatestRate(BRE, USD, new BigDecimal("58.3"), parse("2015-04-09T02:00:00")); rateUpdaterService.updateRates(quotes); List<HistoricalRate> hRates = new ArrayList<>(histoRepo.findAll()); List<LatestRate> lRates = new ArrayList<>(latestRepo.findAll()); assertThat(hRates).containsExactlyInAnyOrder(expected1, expected2, expected3, expected4, expected5); assertThat(lRates).containsExactly(expected6); verify(activeMQConfig).send(any()); }
@Sql("/sql/insert_symbols_bre_usd.sql") @Sql("/sql/insert_rates_bre_usd.sql") @Test public void testUpdateRates_WithExistingData() { List<Quote> quotes = getBrentQuotes(); HistoricalRate expectedOldLatest = new HistoricalRate(BRE, USD, new BigDecimal("55.18"), parse("2015-04-06T02:00:00")); HistoricalRate expectedAdded = new HistoricalRate(BRE, USD, new BigDecimal("57.8"), parse("2015-04-08T02:00:00")); HistoricalRate expectedNewLatest = new HistoricalRate(BRE, USD, new BigDecimal("58.3"), parse("2015-04-09T02:00:00")); LatestRate expectedNewLatest1 = new LatestRate(BRE, USD, new BigDecimal("58.3"), parse("2015-04-09T02:00:00")); HistoricalRate olderButAdded = new HistoricalRate(BRE, USD, new BigDecimal("55.18"), parse("2015-04-02T02:00:00")); rateUpdaterService.updateRates(quotes); List<HistoricalRate> hRates = new ArrayList<>(histoRepo.findAll()); List<LatestRate> lRates = new ArrayList<>(latestRepo.findAll()); assertThat(hRates).hasSize(7); assertThat(hRates).containsOnlyOnce(expectedOldLatest, expectedAdded, expectedNewLatest, olderButAdded); assertThat(lRates).hasSize(1); assertThat(lRates).containsOnly(expectedNewLatest1); verify(activeMQConfig).send(any()); }