-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Updated File Manager to have a fallback resource
Also updated tests to be independent on external files.
- Loading branch information
Showing
4 changed files
with
101 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
86 changes: 46 additions & 40 deletions
86
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/service/StockFileManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,66 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| 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; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class StockFileManagerTest { | ||
|
|
||
| private final String testStockDataPath = "/testStockData.txt"; | ||
|
|
||
| private final String absoluteTestStockDataPath = "src/main/resources/testStockData.txt"; | ||
| StockFileManager stockFileManager; | ||
|
|
||
| private final String validStockFromFile = "NVID, Nvidida Corporation, 241.591"; | ||
|
|
||
| private final String invalidStockFromFile = "COOLI, This is a cool name, 252.2"; | ||
| /** | ||
| * Path used to test file system. | ||
| * */ | ||
| @TempDir | ||
| private Path tempDir; | ||
|
|
||
| private final String commentFromFile = "#Above me are some valid formats."; | ||
|
|
||
| private List<String> allLines = new ArrayList<>(); | ||
| @Test | ||
| void readFileParsesValidStocksAndFiltersOutCommentsAndInvalidData() | ||
| throws IOException { | ||
| Path tempFile = tempDir.resolve("testStockData.txt"); | ||
| List<String> mockFileData = List.of( | ||
| "# This is a comment header line and should be skipped", | ||
| "NVID, Nvidida Corporation, 241.591", | ||
| "AAPL, Apple Inc, 175.50", | ||
| "", | ||
| "INVALID_ROW_MISSING_COLUMNS", | ||
| "COOLI, This is a cool name but missing price token" | ||
| ); | ||
| Files.write(tempFile, mockFileData); | ||
|
|
||
| private List<String> validStocks = new ArrayList<>(); | ||
| StockFileManager stockFileManager = | ||
| new StockFileManager(tempFile.toString()); | ||
| List<String> parsingResults = stockFileManager.readFile(); | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| stockFileManager = new StockFileManager(testStockDataPath); | ||
| Path path = Paths.get(absoluteTestStockDataPath); | ||
| allLines = Files.readAllLines(path); | ||
| try { | ||
| validStocks = stockFileManager.readFile(); | ||
| } catch (Exception _) { | ||
| throw new Exception("Test failed"); | ||
| } | ||
| assertEquals(2, parsingResults.size()); | ||
| assertTrue(parsingResults.contains("NVID, Nvidida Corporation, 241.591")); | ||
| assertTrue(parsingResults.contains("AAPL, Apple Inc, 175.50")); | ||
| assertFalse(parsingResults.stream().anyMatch(line -> line.startsWith("#"))); | ||
| } | ||
|
|
||
| @Test | ||
| void parser_gets_valid_stock_from_file() { | ||
| assertTrue(allLines.contains(validStockFromFile)); | ||
| assertTrue(validStocks.contains(validStockFromFile)); | ||
| } | ||
| void readFileHandlesEmptyFileGracefully() throws IOException { | ||
| Path emptyFile = tempDir.resolve("emptyStockData.txt"); | ||
| Files.createFile(emptyFile); | ||
|
|
||
| @Test | ||
| void parser_skips_comments_from_file() { | ||
| assertTrue(allLines.contains(commentFromFile)); | ||
| assertFalse(validStocks.contains(commentFromFile)); | ||
| StockFileManager stockFileManager = new StockFileManager(emptyFile.toString()); | ||
| List<String> results = stockFileManager.readFile(); | ||
|
|
||
| assertTrue(results.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void parser_skips_invalid_stock_from_file() { | ||
| assertTrue(allLines.contains(invalidStockFromFile)); | ||
| assertFalse(validStocks.contains(invalidStockFromFile)); | ||
| void constructorOrReadFileDoesNotThrowExceptionOnMissingFile() { | ||
| StockFileManager stockFileManager = | ||
| new StockFileManager("non_existent_directory/missing_file.txt"); | ||
|
|
||
| assertDoesNotThrow(stockFileManager::readFile); | ||
| } | ||
| } | ||
| } |