Skip to content

Commit

Permalink
updated maven dependency and plugin versions + fixed jUnit tests + fi…
Browse files Browse the repository at this point in the history
…le path fixes
  • Loading branch information
Sten Laane committed Mar 25, 2019
1 parent ffee00f commit 91f76e2
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 88 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ For functional usage navigate to "Help -> Getting Started" from the menu bar of

* [Java 8](https://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html) - The programming language used
* [Maven](https://maven.apache.org/) - Dependency Management, project building and deployment
* [Alpha Vantage API](https://www.alphavantage.co/) - API for fetching stock data
* [Alpha Vantage API](https://www.alphavantage.co/) - API for fetching stock price data
* [Quotes API for Yahoo Finance] (https://financequotes-api.com/) - API for fetching additional stock data (e.g. currency it's traded in)
* [ECB SDMX 2.1 RESTful web service](https://sdw-wsrest.ecb.europa.eu/help/) - API for fetching currency data

## Authors
Expand Down
23 changes: 10 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>Laane.Sten</groupId>
<artifactId>StockTracker</artifactId>
<version>1.2.3</version>
<version>1.3.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand All @@ -31,7 +31,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -44,17 +44,16 @@
<artifactId>jmetro</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.yahoofinance-api</groupId>
<artifactId>YahooFinanceAPI</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.26</version>
</dependency>
</dependencies>
<scm>
<connection>scm:git:https://github.com/StenAL/StockTracker</connection>
Expand All @@ -66,7 +65,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -76,11 +75,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.0</version>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -174,6 +173,4 @@
</plugin>
</plugins>
</build>


</project>
3 changes: 3 additions & 0 deletions src/main/java/stocktracker/CurrencyRateFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ private void downloadXMLFile(URL url) throws IOException {
connection.setRequestProperty("Accept", "text/xml");

System.out.println("Response code: " + connection.getResponseCode());
if (connection.getResponseCode() != 200) {
System.out.println("Error fetching '" +currencyCode + "'");
}
String readStream = readStream(connection.getInputStream());
try {
List<String> lines = Arrays.asList(readStream.split("\n"));
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/stocktracker/DataAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@ static void calculateMoney(List<String> tickers, List<Number> stockAmounts) thro
}

private static void aggregate(List<String> tickers) throws IOException {
String workingDir = StockTracker.PATH;
List<String> data;
try {
String dest = workingDir + "aggregated_temp.csv";
data = FileManager.readLines(workingDir + "/" + tickers.get(0) + "_currency_temp.csv");
data = FileManager.readLines(StockTracker.PATH + tickers.get(0) + "_currency_temp.csv");
for (int i = 1; i < tickers.size(); i++) {
List<String> fileLines = FileManager.readLines(workingDir + "\\" + tickers.get(i) + "_currency_temp.csv");
List<String> fileLines = FileManager.readLines(StockTracker.PATH + tickers.get(i) + "_currency_temp.csv");
for (int j = 0; j < fileLines.size(); j++) {
String stockPrice = fileLines.get(j).split(",")[1];
String currencyRate = fileLines.get(j).split(",")[2];
data.set(j, data.get(j) + "," + stockPrice + "," + currencyRate);
}
}
FileManager.writeList(dest, data);
FileManager.writeList(StockTracker.PATH + "aggregated_temp.csv", data);
}catch (Exception e) {
e.printStackTrace();
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/stocktracker/StockTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ private static void runExistingTest() {
updateSave();
}

/**
* @param nameList List of tickers of stocks.
* @param amountList List containing amounts of stocks specified in nameList owned.
*/
public static void createConfig(ArrayList<String> nameList, ArrayList<Number> amountList) {
boolean append = false;
for (int i = 0; i < nameList.size(); i++) {
String line = nameList.get(i) + "," + amountList.get(i) + ",1.0";
FileManager.writeLine(PATH + "save_config.csv", line, append);
append = true;
}
}

/**
* Writes data of a specified stock and its trading currency to a csv file.
* @param ticker Ticker of the stock to be recorded.
Expand Down Expand Up @@ -104,19 +117,6 @@ public static void calculateMoney(List<String> tickers, List<Number> stockAmount
}
}

/**
* @param nameList List of tickers of stocks.
* @param amountList List containing amounts of stocks specified in nameList owned.
*/
public static void createConfig(ArrayList<String> nameList, ArrayList<Number> amountList) {
boolean append = false;
for (int i = 0; i < nameList.size(); i++) {
String line = nameList.get(i) + "," + amountList.get(i) + ",1.0";
FileManager.writeLine(PATH + "save_config.csv", line, append);
append = true;
}
}

/**
* Creates a csv file that acts as a cache and stores fetched data
* as to not call the APIs too much and improve performance.
Expand Down
19 changes: 5 additions & 14 deletions src/test/java/stocktracker/CurrencyRateFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@ class CurrencyRateFetcherTest {
private static List<String> dataList;

@BeforeAll
static void setUp() {
try {
CurrencyRateFetcher.writeCurrencyInfo("USD", LocalDate.now().minusDays(365));
} catch (IOException e) {
e.printStackTrace();
}
static void setUp() throws IOException {
CurrencyRateFetcher.writeCurrencyInfo("USD", LocalDate.now().minusDays(365));
dataList = FileManager.readLines(PATH + "USD_temp.csv");
}

@Test
void testEuroFetching() {
try {
CurrencyRateFetcher.writeCurrencyInfo("EUR", LocalDate.now().minusDays(365));
} catch (IOException e) {
e.printStackTrace();
}
void testEuroFetching() throws IOException {
CurrencyRateFetcher.writeCurrencyInfo("EUR", LocalDate.now().minusDays(365));
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
Expand Down Expand Up @@ -72,6 +63,6 @@ void testFetchingNewData() {

@AfterAll
static void teardown() throws InterruptedException {
Thread.sleep(20000);
Thread.sleep(15000);
}
}
53 changes: 23 additions & 30 deletions src/test/java/stocktracker/DataAggregatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

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

Expand All @@ -19,37 +20,36 @@ class DataAggregatorTest {
private static List<String> dataList;

@BeforeAll
static void updateData() {
try {
StockInfoFetcher.getData("AAPL", LocalDate.now().minusDays(365));
StockInfoFetcher.getData("MSFT", LocalDate.now().minusDays(365));
CurrencyRateFetcher.writeCurrencyInfo("USD", LocalDate.now().minusDays(365));
} catch (Exception e) {
e.printStackTrace();
}
static void updateData() throws IOException {
StockInfoFetcher.getData("AAPL", LocalDate.now().minusDays(139));
StockInfoFetcher.getData("MSFT", LocalDate.now().minusDays(139));
CurrencyRateFetcher.writeCurrencyInfo("USD", LocalDate.now().minusDays(139));
DataAggregator.aggregate("AAPL_USD");
DataAggregator.aggregate("MSFT_USD");

ArrayList<String> testList = new ArrayList<>();
testList.add("AAPL_USD");
testList.add("MSFT_USD");
testList.add("AAPL");
testList.add("MSFT");
ArrayList<Number> testAmounts = new ArrayList<>();
testAmounts.add(5);
testAmounts.add(10);
try {
System.out.println(testList + " " + testAmounts);
DataAggregator.calculateMoney(testList, testAmounts);
} catch (IOException e) {
e.printStackTrace();
}
DataAggregator.calculateMoney(testList, testAmounts);
dataList = FileManager.readLines(PATH + "aggregated_temp.csv");
}

@Test
void testInvalidData() {
List<String> invalidList = new ArrayList<>();
invalidList.add("AAAPL_USD");
List<Number> amounts = new ArrayList<>();
amounts.add(1);
assertThrows(InvalidPathException.class, () -> DataAggregator.calculateMoney(invalidList, amounts));
assertThrows(InvalidPathException.class, () -> DataAggregator.aggregate("AAAPL_USD"));
}

@Test
void testSingleAggregation() {
File dataFile = new File(PATH + "AAPL_currency_temp.csv");
assertTrue(dataFile.lastModified() > System.currentTimeMillis()-120000);
List<String> data = FileManager.readLines(PATH + "AAPL_currency_temp.csv");
String line = data.get(new Random().nextInt(data.size()-1));
assertEquals(3, line.split(",").length);
assertDoesNotThrow(() -> LocalDate.parse(line.split(",")[0]));
}

@Test
Expand All @@ -63,7 +63,7 @@ void testDataValidity() {

@Test
void testDataSize() {
assertTrue(dataList.size() > 200);
assertTrue(dataList.size() > 80);
}

@Test
Expand All @@ -72,15 +72,8 @@ void testFetchingNewData() {
assertTrue(dataFile.lastModified() > System.currentTimeMillis()-120000);
}

@Test
void testCurrencyAggregation() {
List<String> data = FileManager.readLines(PATH + "AAPL_USD_temp.csv");
assertTrue(data.size() > 200);
assertTrue(new File(PATH + "AAPL_USD_temp.csv").lastModified() > System.currentTimeMillis()-120000);
}

@AfterAll
static void teardown() throws InterruptedException {
Thread.sleep(20000);
Thread.sleep(15000);
}
}
8 changes: 4 additions & 4 deletions src/test/java/stocktracker/StockInfoFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StockInfoFetcherTest {
private static List<String> dataList;
@BeforeAll
static synchronized void updateData() {
StockInfoFetcher.getData("TSLA", LocalDate.now().minusDays(365));
StockInfoFetcher.getData("TSLA", LocalDate.now().minusDays(139));
PATH = StockTracker.PATH;
dataList = FileManager.readLines(PATH + "TSLA_temp.csv");
}
Expand All @@ -26,7 +26,7 @@ class getDataTests {
@Test
void testInvalidTicker() {
assertThrows(AlphaVantageException.class, () -> StockInfoFetcher
.getData("STEN", LocalDate.now().minusDays(365)));
.getData("STEN", LocalDate.now().minusDays(139)));
}

@Test
Expand All @@ -40,7 +40,7 @@ void testDataValidity() {

@Test
void testDataSize() {
assertTrue(dataList.size() > 200);
assertTrue(dataList.size() > 80);
}

@Test
Expand Down Expand Up @@ -70,6 +70,6 @@ void testGetMostRecentDay() {

@AfterAll
static void teardown() throws InterruptedException {
Thread.sleep(20000);
Thread.sleep(15000);
}
}
12 changes: 4 additions & 8 deletions src/test/java/stocktracker/StockTrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ class StockTrackerTest {

@BeforeAll
static void setup() throws IOException {
// ?? some file permissions magic without which the program won't work :(
new FileWriter(new File(PATH + "USD_temp.csv")).write("broken");
new FileWriter(new File(PATH + "IVV_temp.csv")).write("broken");
new FileWriter(new File(PATH + "QQQ_temp.csv")).write("broken");

testList = new ArrayList<>();
testList.add("IVV_USD");
testList.add("QQQ_USD");
testList.add("IVV");
testList.add("QQQ");
testAmounts = new ArrayList<>();
testAmounts.add(5);
testAmounts.add(10);
Expand All @@ -39,7 +34,7 @@ void runNewTest()
{
StockTracker.createConfig(testList, testAmounts);
StockTracker.writeData("IVV", LocalDate.now().minusDays(139));
StockTracker.writeData("QQQ",LocalDate.now().minusDays(139));
StockTracker.writeData("QQQ", LocalDate.now().minusDays(139));

System.out.println("Data fetching done");
System.out.println("$$$");
Expand All @@ -50,6 +45,7 @@ void runNewTest()
System.out.println("Done");
assertTrue(new File(PATH + "save_data.csv").exists());
assertTrue(new File(PATH + "save_config.csv").exists());
assertFalse(StockTracker.updateSave());
}

//TODO: test actually updating the save
Expand Down

0 comments on commit 91f76e2

Please sign in to comment.