-
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.
- Loading branch information
Showing
2 changed files
with
103 additions
and
36 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,155 @@ | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import Model.Portfolio; | ||
| import Model.Share; | ||
| import Model.Stock; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| public class PortfolioTest { | ||
|
|
||
| @Test | ||
| void testAddSharePortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
| private Portfolio portfolio; | ||
| private Stock stock; | ||
| private Share share; | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); | ||
| @BeforeEach | ||
| void setUp() { | ||
| portfolio = new Portfolio(); | ||
| stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); | ||
| } | ||
|
|
||
| boolean result = portfolio.addShare(share); | ||
| // ---- Positive tests ---- | ||
|
|
||
| @Test | ||
| void testAddShare() { | ||
| boolean result = portfolio.addShare(share); | ||
| assertTrue(result); | ||
| assertTrue(portfolio.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveSharePortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); | ||
| void testAddMultipleShares() { | ||
| Stock stock2 = new Stock("GOOGL", "Google", new BigDecimal("200")); | ||
| Share share2 = new Share(stock2, new BigDecimal("5"), new BigDecimal("190")); | ||
|
|
||
| portfolio.addShare(share); | ||
| portfolio.addShare(share2); | ||
|
|
||
| boolean result = portfolio.removeShare(share); | ||
| assertEquals(2, portfolio.getShares().size()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveShare() { | ||
| portfolio.addShare(share); | ||
| boolean result = portfolio.removeShare(share); | ||
| assertTrue(result); | ||
| assertFalse(portfolio.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSharesPortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); | ||
| void testRemoveShareNotInPortfolioReturnsFalse() { | ||
| boolean result = portfolio.removeShare(share); | ||
| assertFalse(result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSharesReturnsAll() { | ||
| portfolio.addShare(share); | ||
|
|
||
| var shares = portfolio.getShares(); | ||
|
|
||
| List<Share> shares = portfolio.getShares(); | ||
| assertEquals(1, shares.size()); | ||
| assertTrue(shares.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSharesBySymbolPortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
| void testGetSharesIsDefensiveCopy() { | ||
| portfolio.addShare(share); | ||
| List<Share> copy = portfolio.getShares(); | ||
| copy.clear(); | ||
| assertEquals(1, portfolio.getShares().size()); | ||
| } | ||
|
|
||
| Stock stock1 = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| @Test | ||
| void testGetSharesBySymbol() { | ||
| Stock stock2 = new Stock("GOOGL", "Google", new BigDecimal("200")); | ||
| Share share2 = new Share(stock2, new BigDecimal("5"), new BigDecimal("190")); | ||
|
|
||
| Share share1 = new Share(stock1, new BigDecimal("10"), new BigDecimal("140")); | ||
| Share share2 = new Share(stock2, new BigDecimal("10"), new BigDecimal("190")); | ||
|
|
||
| portfolio.addShare(share1); | ||
| portfolio.addShare(share); | ||
| portfolio.addShare(share2); | ||
|
|
||
| var result = portfolio.getShares("AAPL"); | ||
|
|
||
| List<Share> result = portfolio.getShares("AAPL"); | ||
| assertEquals(1, result.size()); | ||
| assertTrue(result.contains(share1)); | ||
| assertTrue(result.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSharesBySymbolNoMatch() { | ||
| portfolio.addShare(share); | ||
| List<Share> result = portfolio.getShares("MSFT"); | ||
| assertTrue(result.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void testContainsReturnsTrueWhenPresent() { | ||
| portfolio.addShare(share); | ||
| assertTrue(portfolio.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void testContainsPortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
| void testContainsReturnsFalseWhenAbsent() { | ||
| assertFalse(portfolio.contains(share)); | ||
| } | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("140")); | ||
| @Test | ||
| void testGetNetWorthEmptyPortfolio() { | ||
| assertEquals(BigDecimal.ZERO, portfolio.getNetWorth()); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetNetWorthWithShares() { | ||
| portfolio.addShare(share); | ||
| // net worth should be positive when portfolio has holdings | ||
| assertTrue(portfolio.getNetWorth().compareTo(BigDecimal.ZERO) > 0); | ||
| } | ||
|
|
||
| assertTrue(portfolio.contains(share)); | ||
| // ---- Negative tests ---- | ||
|
|
||
| @Test | ||
| void testAddNullShareThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| portfolio.addShare(null) | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void testRemoveNullShareThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| portfolio.removeShare(null) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testContainsNullShareThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| portfolio.contains(null) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSharesByNullSymbolThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| portfolio.getShares((String) null) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSharesByBlankSymbolThrows() { | ||
| assertThrows(IllegalArgumentException.class, () -> | ||
| portfolio.getShares(" ") | ||
| ); | ||
| } | ||
| } |
Binary file not shown.