Skip to content

Commit

Permalink
Added FileHandlerTest
Browse files Browse the repository at this point in the history
Added positive tests for the methods in FileHandler.
  • Loading branch information
roaraf committed Mar 26, 2026
1 parent 692a7a7 commit 0938229
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions millions/src/test/java/no/ntnu/gruppe53/FileHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package no.ntnu.gruppe53;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static no.ntnu.gruppe53.FileHandler.*;
import static org.junit.jupiter.api.Assertions.*;

class FileHandlerTest {
@Test
void writeTest() throws IOException {
String filePathString = System.getProperty("user.dir") + "\\target\\test-output\\";
String fileName = "stockTest.csv";


String symbol1 = "AAPL";
String company1 = "Apple Inc.";
BigDecimal price1 = new BigDecimal("100");

String symbol2 = "NVDA";
String company2 = "Nvidia";
BigDecimal price2 = new BigDecimal("191.27");

Stock testStock1 = new Stock(symbol1,company1, price1);
Stock testStock2 = new Stock(symbol2, company2, price2);

List<Stock> stockList = new ArrayList<>();
stockList.add(testStock1);
stockList.add(testStock2);

writeStocksToFile(stockList, filePathString, fileName);

List<String> lines = Files.readAllLines(Paths.get(filePathString + fileName));

assertTrue(Files.exists(Paths.get(filePathString + fileName)),
"File should have been created successfully.");
assertEquals("#Ticker,Name,Price",lines.getFirst(), "First line " +
"should contain correct headers.");
assertEquals("", lines.get(1), "Second line should be empty.");
assertEquals(symbol1 + "," + company1 + "," + price1, lines.get(2),
"Third line should contain correct values for first stock.");
assertEquals(symbol2 + "," + company2 + "," + price2, lines.getLast(),
"Last line should contain correct values for second stock.");
}

@Test
void readTest() {
String filePathString = System.getProperty("user.dir") + "\\target\\test-output\\";
String fileName = "stockTest.csv";


String symbol1 = "AAPL";
String company1 = "Apple Inc.";
BigDecimal price1 = new BigDecimal("100");

String symbol2 = "NVDA";
String company2 = "Nvidia";
BigDecimal price2 = new BigDecimal("191.27");

Stock testStock1 = new Stock(symbol1,company1, price1);
Stock testStock2 = new Stock(symbol2, company2, price2);

List<Stock> stockList = new ArrayList<>();
stockList.add(testStock1);
stockList.add(testStock2);

writeStocksToFile(stockList, filePathString, fileName);

List <Stock> readStocks = readStocksFromFile(filePathString, fileName);

assertEquals(symbol1, readStocks.getFirst().getSymbol(),
"First stock's symbol should be AAPL.");
assertEquals(company1, readStocks.getFirst().getCompany(),
"First stock's company name should be Apple Inc.");
assertEquals(0, price1.compareTo(readStocks.getFirst().getSalesPrice()),
"First stock's price should be 100.");
assertEquals(symbol2, readStocks.getLast().getSymbol(),
"Second stock's symbol should be NVDA.");
assertEquals(company2, readStocks.getLast().getCompany(),
"Second stock's company name should be Nvidia.");
assertEquals(0, price2.compareTo(readStocks.getLast().getSalesPrice()),
"Second stock's price should be 191.27");
}
}

0 comments on commit 0938229

Please sign in to comment.