Skip to content

Commit

Permalink
Feat: Refactor FileManager and FileParser, unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 25, 2026
1 parent 0bc91d8 commit 93969e0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Converts stock objects to/from string format for file handling.
Expand Down Expand Up @@ -42,29 +44,29 @@ public List<Stock> getStocksFromStrings(final List<String> validStocks)
throws IllegalArgumentException {
if (validStocks == null || validStocks.isEmpty()) {
throw new IllegalArgumentException("Empty or null stock list!");
} else {
List<Stock> stocksFromFile = new ArrayList<>();
List<String> stockSymbols = new ArrayList<>();
}
List<Stock> stocksFromFile = new ArrayList<>();
Set<String> stockSymbols = new HashSet<>();

validStocks.forEach(s -> {
String[] lineElements = s.split(",");
String stockSymbol = lineElements[0].trim();
String stockName = lineElements[1].trim();
BigDecimal stockPrice = new BigDecimal(lineElements[2].trim());
validStocks.forEach(s -> {
String[] lineElements = s.split(",");
String stockSymbol = lineElements[0].trim();
String stockName = lineElements[1].trim();
BigDecimal stockPrice = new BigDecimal(lineElements[2].trim());

try {
Stock stockObject = new Stock(stockSymbol, stockName, stockPrice);
if (!stockSymbols.contains(stockSymbol)) {
stockSymbols.add(stockSymbol);
stocksFromFile.add(stockObject);
}
} catch (IllegalArgumentException e) {
System.err.println("(" + s + ") is not a valid stock! Skipping...");
try {
Stock stockObject = new Stock(stockSymbol, stockName, stockPrice);
if (stockSymbols.add(stockSymbol)) {
stocksFromFile.add(stockObject);
}

});
return stocksFromFile;
} catch (IllegalArgumentException _) {
// Ignore invalid strings.
}
});
if (stocksFromFile.isEmpty()) {
throw new IllegalArgumentException("No stocks parsed succesfully!");
}
return stocksFromFile;
}

/**
Expand All @@ -81,13 +83,21 @@ public List<Stock> getStocksFromStrings(final List<String> validStocks)
public List<String> stocksToStrings(final List<Stock> stocks) {
if (stocks == null || stocks.isEmpty()) {
throw new IllegalArgumentException("Empty or null stock list!");
} else {
ArrayList<String> stringList = new ArrayList<>();
stocks.forEach(s ->
stringList.add(s.getSymbol().trim() + "," + s.getCompany().trim()
+ "," + s.getSalesPrice().toString())
}

List<String> stringList = new ArrayList<>();
for (Stock s : stocks) {
if (s == null) {
continue;
}

String csvRow = String.format("%s, %s, %s",
s.getSymbol().trim(),
s.getCompany().trim(),
s.getSalesPrice().toPlainString()
);
return stringList;
stringList.add(csvRow);
}
return stringList;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package edu.ntnu.idi.idatt2003.g40.mappe.service;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package edu.ntnu.idi.idatt2003.g40.mappe.service;

import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class StockFileParserTest {

private StockFileParser converter;
/**
* Test stock parser to use.
* */
private StockFileParser testParser;

private String validStockAsString1;
private String validStockAsString2;
private String validStockAsString3;
/**
* List of strings containing valid and invalid representations
* of stocks.
* */
private ArrayList<String> allStocks;
private String invalidStockAsString1;

@BeforeEach
void setUp() {
validStockAsString1 = "AAPL, Apple inc., 251.42";
validStockAsString2 = "NVID, Nvidia corp., 100.25";
validStockAsString3 = "SAMS, Samsung corporation, 103.21";
String validStockAsString1 = "AAPL, Apple inc., 251.42";
String validStockAsString2 = "NVID, Nvidia corp., 100.25";
String validStockAsString3 = "SAMS, Samsung corporation, 103.21";

invalidStockAsString1 = "INVALID, This stock has an invalid code!, 100.21";
String invalidStockAsString1 = "INVALID, This stock has an invalid code!, 100.21";

allStocks = new ArrayList<>();

Expand All @@ -33,15 +40,15 @@ void setUp() {
allStocks.add(validStockAsString3);
allStocks.add(invalidStockAsString1);

converter = new StockFileParser();
testParser = new StockFileParser();
}

@Test
void converter_returns_valid_stock_apple() {
void getStocksFromStringsReturnsValidStocks() {

boolean stockIncluded = false;

List<Stock> stocksFromConverter = converter.getStocksFromStrings(allStocks);
List<Stock> stocksFromConverter = testParser.getStocksFromStrings(allStocks);

for (Stock s : stocksFromConverter) {
if (s.getSymbol().equals("AAPL")) {
Expand All @@ -54,11 +61,11 @@ void converter_returns_valid_stock_apple() {
}

@Test
void converter_ignores_invalid_stock_representation() {
void getStocksFromStringsIgnoresInvalidStocks() {

boolean stockIncluded = false;

List<Stock> stocksFromConverter = converter.getStocksFromStrings(allStocks);
List<Stock> stocksFromConverter = testParser.getStocksFromStrings(allStocks);

for (Stock s : stocksFromConverter) {
if (s.getSymbol().equals("INVALID")) {
Expand All @@ -69,4 +76,30 @@ void converter_ignores_invalid_stock_representation() {

Assertions.assertFalse(stockIncluded);
}

@Test
void stocksToStringsConvertsValidStocksToCsvFormat() {
Stock apple = new Stock("AAPL", "Apple Inc", new BigDecimal("175.50"));
Stock tesla = new Stock("TSLA", "Tesla Inc", new BigDecimal("200.00"));
List<Stock> stocks = List.of(apple, tesla);

List<String> result = testParser.stocksToStrings(stocks);

assertEquals(2, result.size());
assertEquals("AAPL, Apple Inc, 175.50", result.get(0));
assertEquals("TSLA, Tesla Inc, 200.00", result.get(1));
}

@Test
void stocksToStringsThrowsExceptionOnNullOrEmptyList() {
List<Stock> emptyList = new ArrayList<>();

assertThrows(IllegalArgumentException.class,
() -> testParser.stocksToStrings(null)
);

assertThrows(IllegalArgumentException.class,
() -> testParser.stocksToStrings(emptyList)
);
}
}

0 comments on commit 93969e0

Please sign in to comment.