Skip to content

Commit

Permalink
Abstract the data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
donfiguerres committed Jun 4, 2023
1 parent 60006a2 commit 5d8ef27
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.europeanexchangerates.exchangeapi.provider;

import java.time.LocalDate;
import java.util.TreeMap;

import com.europeanexchangerates.exchangeapi.dto.ExchangeRate;

public interface ExchangeRateProvider {
TreeMap<LocalDate, ExchangeRate> getExchangeRates() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.europeanexchangerates.exchangeapi.provider;

import java.time.LocalDate;
import java.util.TreeMap;

import com.europeanexchangerates.exchangeapi.dto.ExchangeRate;
import com.europeanexchangerates.exchangeapi.util.DataDownloader;
import com.europeanexchangerates.exchangeapi.util.UrlCsvZipDataDownloader;

public class UrlCsvZipExchangeRateProvider implements ExchangeRateProvider {
private DataDownloader downloader;

public UrlCsvZipExchangeRateProvider() {
this.downloader = new UrlCsvZipDataDownloader();
}

public UrlCsvZipExchangeRateProvider(DataDownloader downloader) {
this.downloader = downloader;
}

public TreeMap<LocalDate, ExchangeRate> getExchangeRates() throws Exception {
return downloader.downloadData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import com.europeanexchangerates.exchangeapi.dto.CurrencyHighestRate;
import com.europeanexchangerates.exchangeapi.dto.ExchangeRate;
import com.europeanexchangerates.exchangeapi.exception.InvalidDateRangeException;
import com.europeanexchangerates.exchangeapi.util.UrlCsvZipDataDownloader;
import com.europeanexchangerates.exchangeapi.util.DataDownloader;
import com.europeanexchangerates.exchangeapi.provider.ExchangeRateProvider;
import com.europeanexchangerates.exchangeapi.provider.UrlCsvZipExchangeRateProvider;

@Service
public class ExchangeRateService {
Expand All @@ -25,12 +25,12 @@ public class ExchangeRateService {
private TreeMap<LocalDate, ExchangeRate> exchangeRates;

public ExchangeRateService() throws Exception {
DataDownloader downloader = new UrlCsvZipDataDownloader();
this.exchangeRates = downloader.downloadData();
ExchangeRateProvider provider = new UrlCsvZipExchangeRateProvider();
this.exchangeRates = provider.getExchangeRates();
}

public ExchangeRateService(DataDownloader downloader) throws Exception {
this.exchangeRates = downloader.downloadData();
public ExchangeRateService(ExchangeRateProvider provider) throws Exception {
this.exchangeRates = provider.getExchangeRates();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.europeanexchangerates.exchangeapi.dto.CurrencyHighestRate;
import com.europeanexchangerates.exchangeapi.dto.ExchangeRate;
import com.europeanexchangerates.exchangeapi.exception.InvalidDateRangeException;
import com.europeanexchangerates.exchangeapi.util.DataDownloader;
import com.europeanexchangerates.exchangeapi.provider.UrlCsvZipExchangeRateProvider;
import com.europeanexchangerates.exchangeapi.util.NullableConverter;

@SpringBootTest
Expand All @@ -35,7 +35,7 @@ public class ExchangeRateServiceTest {
private ExchangeRateService exchangeRateService;

@Mock
DataDownloader dataDownloader;
UrlCsvZipExchangeRateProvider exchangeRateProvider;

@BeforeEach
void setUp() throws Exception {
Expand Down Expand Up @@ -82,9 +82,9 @@ void setUp() throws Exception {
}));
}
};
when(dataDownloader.downloadData()).thenReturn(dummyData);
when(exchangeRateProvider.getExchangeRates()).thenReturn(dummyData);

exchangeRateService = new ExchangeRateService(dataDownloader);
exchangeRateService = new ExchangeRateService(exchangeRateProvider);
}

@ParameterizedTest
Expand Down

0 comments on commit 5d8ef27

Please sign in to comment.