diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java index c3696dd..6e6c405 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java @@ -64,6 +64,7 @@ void getTransactionArchiveTest() { void getMoneyTest() { assertEquals(new BigDecimal("10000"), player.getMoney()); } + @Test void withdrawExactAmountTest() { player.withdrawMoney(new BigDecimal("10000")); @@ -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()); } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java new file mode 100644 index 0000000..cd6d20b --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java @@ -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 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 stocks = StockFileHandler.readFromFile(testFile); + assertEquals(1, stocks.size()); + } + + @Test + void writeToFileTest() throws IOException { + Path testFile = Files.createTempFile("stocks", "csv"); + List stocks = List.of(new Stock("AAPL", "Apple Inc.", new BigDecimal("276.43"))); + StockFileHandler.writeToFile(testFile, stocks); + List lines = Files.readAllLines(testFile); + assertTrue(lines.get(2).startsWith("AAPL,Apple Inc.,276.43")); + } +}