private ExchangeAggregateRate createExchangeAggregateRate(Date creationDate, Long blockNrEth, Long blockNrBtc) { return new ExchangeAggregateRate( creationDate, blockNrEth, blockNrBtc ); }
private ExchangeAggregateRate createExchangeAggregateRate(Date creationDate) { return new ExchangeAggregateRate(creationDate, null, null); }
@Test(expected = RateNotFoundException.class) public void testGetRate_ClosestRate_Nor_HistoricalDate_Are_Available() throws Exception { Instant now = Instant.now(); ExchangeAggregateRate r1 = new ExchangeAggregateRate(Date.from(now.minus(30, ChronoUnit.MINUTES)), null, null); ExchangeAggregateRate r2 = new ExchangeAggregateRate(Date.from(now.minus(5, ChronoUnit.MINUTES)), null, null); ExchangeAggregateRate r3 = new ExchangeAggregateRate(Date.from(now.plus(10, ChronoUnit.MINUTES)), null, null); when(exchangeAggregateRateRepository.findAllByCreationDateBetweenOrderByCreationDateDesc(any(), any())) .thenReturn(Arrays.asList(r1, r2, r3)); when(cryptoCompareClientService.getHistorical(CryptoCompareCurrency.ETH, Arrays.asList(CryptoCompareCurrency.USD), now.getEpochSecond())) .thenThrow(new Exception("Historical API not available.")); ratesProviderService.getRate(CurrencyType.ETH, now); }
ExchangeAggregateRate exchangeAggregateRate = new ExchangeAggregateRate(new Date(), blockNrETH, blockNrBTC);
@Test public void testGetRate_ClosestRate_Not_Available() throws Exception { Instant now = Instant.now(); ExchangeAggregateRate r1 = new ExchangeAggregateRate(Date.from(now.minus(30, ChronoUnit.MINUTES)), null, null); ExchangeAggregateRate r2 = new ExchangeAggregateRate(Date.from(now.minus(5, ChronoUnit.MINUTES)), null, null); ExchangeAggregateRate r3 = new ExchangeAggregateRate(Date.from(now.plus(10, ChronoUnit.MINUTES)), null, null); when(exchangeAggregateRateRepository.findAllByCreationDateBetweenOrderByCreationDateDesc(any(), any())) .thenReturn(Arrays.asList(r1, r2, r3)); List<CryptoCompareConversionRateResponse> rateResponses = Arrays.asList( new CryptoCompareConversionRateResponse(CryptoCompareCurrency.USD, new BigDecimal("9.2345")) ); CryptoCompareConversionRates conversionRates = new CryptoCompareConversionRates(rateResponses); List<CryptoCompareResponse> responses = Arrays.asList(new CryptoCompareResponse(CryptoCompareCurrency.ETH, conversionRates)); CryptoCompareResponseDTO dto = new CryptoCompareResponseDTO(responses); when(cryptoCompareClientService.getHistorical(CryptoCompareCurrency.ETH, Arrays.asList(CryptoCompareCurrency.USD), now.getEpochSecond())) .thenReturn(dto); BigDecimal rate = ratesProviderService.getRate(CurrencyType.ETH, now); assertNotNull(rate); assertEquals(new BigDecimal("9.2345"), rate); }
@Test public void testGetRate_ClosestRate_Available() throws RateNotFoundException { Instant now = Instant.now(); ExchangeAggregateRate r1 = new ExchangeAggregateRate(Date.from(now.minus(30, ChronoUnit.MINUTES)), null, null); ExchangeAggregateRate r2 = new ExchangeAggregateRate(Date.from(now.minus(5, ChronoUnit.MINUTES)), null, null); ExchangeAggregateRate r3 = new ExchangeAggregateRate(Date.from(now.plus(10, ChronoUnit.MINUTES)), null, null); r1.addExchangeAggregateCurrencyRate(new ExchangeAggregateCurrencyRate(CurrencyType.ETH, new BigDecimal("1.23456789"))); r1.addExchangeAggregateCurrencyRate(new ExchangeAggregateCurrencyRate(CurrencyType.BTC, new BigDecimal("0.123456789"))); r2.addExchangeAggregateCurrencyRate(new ExchangeAggregateCurrencyRate(CurrencyType.ETH, new BigDecimal("2.23456789"))); r2.addExchangeAggregateCurrencyRate(new ExchangeAggregateCurrencyRate(CurrencyType.BTC, new BigDecimal("1.123456789"))); r3.addExchangeAggregateCurrencyRate(new ExchangeAggregateCurrencyRate(CurrencyType.ETH, new BigDecimal("3.23456789"))); r3.addExchangeAggregateCurrencyRate(new ExchangeAggregateCurrencyRate(CurrencyType.BTC, new BigDecimal("2.123456789"))); when(exchangeAggregateRateRepository.findAllByCreationDateBetweenOrderByCreationDateDesc(any(), any())) .thenReturn(Arrays.asList(r1, r2, r3)); BigDecimal rate = ratesProviderService.getRate(CurrencyType.ETH, now); assertNotNull(rate); assertEquals(new BigDecimal("2.23456789"), rate); }
protected Optional<BigDecimal> getRateFromHistoricalProviderAndSaveToDB(CurrencyType currencyType, Instant timestamp) { ExchangeEntryRate exchangeEntryRate = getRateFromHistoricalProviderFromAllSupportedCurrencies(timestamp); // create the aggregation object ExchangeAggregateRate aggregateRate = new ExchangeAggregateRate(Date.from(Instant.now()), blockNrProviderService.getCurrentBlockNrEthereum(), blockNrProviderService.getCurrentBlockNrBitcoin()); aggregateRate.addExchangeEntry(exchangeEntryRate); if (exchangeEntryRate.getExchangeCurrencyRates() != null && !exchangeEntryRate.getExchangeCurrencyRates().isEmpty()) { exchangeEntryRate.getExchangeCurrencyRates().stream() .filter(distinctByKey(ExchangeCurrencyRate::getCurrencyType)) .forEach((entry) -> { // get all "ExchangeCurrencyRate" objects of distinct crypto currencies List<ExchangeCurrencyRate> allExchangeCurrencyRates = aggregateRate.getAllExchangeCurrencyRates(entry.getCurrencyType()); // remove outliers and get the mean BigDecimal mean = aggregationService.removeOutliersAndGetMean(allExchangeCurrencyRates); // create a new aggregate currency ExchangeAggregateCurrencyRate aggCurrencyRate = new ExchangeAggregateCurrencyRate(entry.getCurrencyType(), mean); // add to the top-level "ExchangeAggregateRate" aggregateRate.addExchangeAggregateCurrencyRate(aggCurrencyRate); }); // save to DB exchangeAggregateRateRepository.save(aggregateRate); } return aggregateRate.getExchangeAggregateCurrencyRates(currencyType) .map((aggregateObj) -> aggregateObj.getAggregateExchangeRate()); }