Skip to content

Commit

Permalink
Merge pull request #64 from einaskoi/stockFileHandlerTests
Browse files Browse the repository at this point in the history
add tests for StockFileHandler and refactor getNetworthTest in player
  • Loading branch information
peretr authored Apr 10, 2026
2 parents 80359ff + a335692 commit fa6fb28
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void getTransactionArchiveTest() {
void getMoneyTest() {
assertEquals(new BigDecimal("10000"), player.getMoney());
}

@Test
void withdrawExactAmountTest() {
player.withdrawMoney(new BigDecimal("10000"));
Expand All @@ -72,11 +73,10 @@ void withdrawExactAmountTest() {

@Test
void getNetworthTest() {

// Starting with 10000 money and adding 5000 moneys worth of AAPL then calculating the networth
// with commissions taken account of will result in this:
// 10000 + 5000 - (5000 * 0.01) = 14950
player.getPortfolio().addShare(new Share(new Stock("AAPL", "Apple Inc.", new BigDecimal("5000")), new BigDecimal("1"), new BigDecimal("5000")));
assertEquals(new BigDecimal("14950"), player.getNetWorth());
assertEquals(new BigDecimal("14950.00"), player.getNetWorth());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;

public class StockFileHandlerTest {

@Test
void readFromFileTest() throws IOException {
Path testFile = Files.createTempFile("stocks", "csv");
Files.writeString(testFile, "AAPL,Apple Inc.,276.43\nMSFT,Microsoft,404.68");
List<Stock> stocks = StockFileHandler.readFromFile(testFile);
assertEquals(2, stocks.size());
assertEquals("AAPL", stocks.get(0).getSymbol());
}

@Test
void readFromFileWithCommentsAndEmptyLines() throws IOException {
Path testFile = Files.createTempFile("stocks", "csv");
Files.writeString(testFile, "#Comment\n\nAAPL,Apple Inc.,276.43");
List<Stock> stocks = StockFileHandler.readFromFile(testFile);
assertEquals(1, stocks.size());
}

@Test
void writeToFileTest() throws IOException {
Path testFile = Files.createTempFile("stocks", "csv");
List<Stock> stocks = List.of(new Stock("AAPL", "Apple Inc.", new BigDecimal("276.43")));
StockFileHandler.writeToFile(testFile, stocks);
List<String> lines = Files.readAllLines(testFile);
assertTrue(lines.get(2).startsWith("AAPL,Apple Inc.,276.43"));
}
}

0 comments on commit fa6fb28

Please sign in to comment.