Skip to content

Commit

Permalink
added support for euro as currency + fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sten Laane committed Mar 24, 2019
1 parent 6ff0a72 commit 44e59cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/main/java/stocktracker/CurrencyRateFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -30,16 +31,35 @@ private CurrencyRateFetcher(String currencyCode) {

public static void main(String[] args) throws IOException {
writeCurrencyInfo("USD", LocalDate.now().minusDays(365));
writeCurrencyInfo("EUR", LocalDate.now().minusDays(365));
}

static void writeCurrencyInfo(String currencyCode, LocalDate firstDate) throws IOException {
CurrencyRateFetcher fetcher = new CurrencyRateFetcher(currencyCode);
if (!currencyCode.equals("EUR")) {
CurrencyRateFetcher fetcher = new CurrencyRateFetcher(currencyCode);

String url_str = "https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D." + currencyCode +
".EUR.SP00.A?startPeriod=" + firstDate + "&detail=dataonly";
fetcher.xmlParser.downloadXMLFile(new URL(url_str));
List<String> dataList = fetcher.xmlParser.parse();
//inefficient but works
if (currencyCode.equals("EUR")) {
for (int i = 0; i < dataList.size(); i++) {
dataList.set(i, dataList.get(i).split(",")[0] + ",1.000");
}
}
FileManager.writeList(StockTracker.PATH + currencyCode + "_temp.csv", dataList);
}
else {
ArrayList<String> dataList = new ArrayList<>();
do {
if (firstDate.getDayOfWeek() != DayOfWeek.SATURDAY && firstDate.getDayOfWeek() != DayOfWeek.SUNDAY)
dataList.add(firstDate +",1.000");
firstDate = firstDate.plusDays(1);
} while (!firstDate.isEqual(LocalDate.now()));
FileManager.writeList(StockTracker.PATH + currencyCode + "_temp.csv", dataList);

String url_str = "https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D." + currencyCode +
".EUR.SP00.A?startPeriod=" + firstDate + "&detail=dataonly";
fetcher.xmlParser.downloadXMLFile(new URL(url_str));
List<String> dataList = fetcher.xmlParser.parse();
FileManager.writeList(StockTracker.PATH + currencyCode + "_temp.csv", dataList);
}
System.out.println("Fetching " + currencyCode + " done");
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/stocktracker/CurrencyRateFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -27,6 +28,22 @@ static void setUp() {
dataList = FileManager.readLines(PATH + "USD_temp.csv");
}

@Test
void testEuroFetching() {
try {
CurrencyRateFetcher.writeCurrencyInfo("EUR", LocalDate.now().minusDays(365));
} catch (IOException e) {
e.printStackTrace();
}
File dataFile = new File(PATH + "EUR_temp.csv");
assertTrue(dataFile.lastModified() > System.currentTimeMillis()-120000);
List<String> data = FileManager.readLines(PATH + "EUR_temp.csv");
String line = data.get(new Random().nextInt(data.size()-1));
assertEquals("1.000", line.split(",")[1]);
assertDoesNotThrow(() -> LocalDate.parse(line.split(",")[0]));

}

@Test
void testInvalidCurrency() {
assertThrows(FileNotFoundException.class, () -> CurrencyRateFetcher
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/stocktracker/StockTrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void runNewTest()
StockTracker.deleteTempFiles();
System.out.println("Files aggregated, money calculated");
System.out.println("Done");
assertTrue(new File(PATH + "money.csv").exists());
assertTrue(new File(PATH + "save_data.csv").exists());
assertTrue(new File(PATH + "save_config.csv").exists());
}

Expand Down

0 comments on commit 44e59cf

Please sign in to comment.