-
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.
Added unit tests for new methods added in previous commits
- Loading branch information
Showing
4 changed files
with
339 additions
and
188 deletions.
There are no files selected for viewing
188 changes: 122 additions & 66 deletions
188
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.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,92 +1,148 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| final class ExchangeTest { | ||
|
|
||
| private Stock appleStock; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| appleStock = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorSetsNameWeekAndStocksCorrectly() { | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
|
|
||
| Exchange exchange = new Exchange("NASDAQ", List.of(appleStock, tesla)); | ||
|
|
||
| assertEquals("NASDAQ", exchange.getName()); | ||
| assertEquals(1, exchange.getWeek()); | ||
| assertTrue(exchange.hasStock("AAPL")); | ||
| assertTrue(exchange.hasStock("TSLA")); | ||
| } | ||
|
|
||
| @Test | ||
| void getStockReturnsCorrectStock() { | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(appleStock)); | ||
|
|
||
| Stock result = exchange.getStock("AAPL"); | ||
|
|
||
| assertSame(appleStock, result); | ||
| } | ||
|
|
||
| @Test | ||
| void findStocksReturnsMatchingStocksBySymbolOrCompany() { | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(appleStock, tesla)); | ||
|
|
||
| List<Stock> resultBySymbol = exchange.findStocks("AAP"); | ||
| List<Stock> resultByCompany = exchange.findStocks("tes"); | ||
|
|
||
| assertEquals(1, resultBySymbol.size()); | ||
| assertTrue(resultBySymbol.contains(appleStock)); | ||
|
|
||
| assertEquals(1, resultByCompany.size()); | ||
| assertTrue(resultByCompany.contains(tesla)); | ||
| } | ||
|
|
||
| @Test | ||
| void buyReturnsPurchaseAndWithdrawsMoneyFromPlayer() { | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(appleStock)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
|
|
||
| Transaction transaction = exchange.buy("AAPL", new BigDecimal("2"), player); | ||
|
|
||
| assertInstanceOf(Purchase.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertEquals(new BigDecimal("2"), transaction.getShare().getQuantity()); | ||
| assertEquals(new BigDecimal("100"), transaction.getShare() | ||
| .getPurchasePrice()); | ||
| assertEquals(new BigDecimal("799.000"), player.getMoney()); | ||
| } | ||
|
|
||
| class ExchangeTest { | ||
| @Test | ||
| void sellReturnsSaleAndAddsMoneyToPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
| Share share = new Share(apple, new BigDecimal("2"), new BigDecimal("100")); | ||
|
|
||
| @Test | ||
| void constructorSetsNameWeekAndStocksCorrectly() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
| Transaction transaction = exchange.sell(share, player); | ||
|
|
||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); | ||
| assertInstanceOf(Sale.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertSame(share, transaction.getShare()); | ||
| assertEquals(new BigDecimal("1267.9000"), player.getMoney()); | ||
| } | ||
|
|
||
| assertEquals("NASDAQ", exchange.getName()); | ||
| assertEquals(1, exchange.getWeek()); | ||
| assertTrue(exchange.hasStock("AAPL")); | ||
| assertTrue(exchange.hasStock("TSLA")); | ||
| } | ||
| @Test | ||
| void advanceIncreasesWeekAndUpdatesStockPrice() { | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(appleStock)); | ||
|
|
||
| @Test | ||
| void getStockReturnsCorrectStock() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| BigDecimal oldPrice = appleStock.getSalesPrice(); | ||
| exchange.advance(); | ||
|
|
||
| Stock result = exchange.getStock("AAPL"); | ||
| assertEquals(2, exchange.getWeek()); | ||
| assertNotEquals(oldPrice, appleStock.getSalesPrice()); | ||
| } | ||
|
|
||
| assertSame(apple, result); | ||
| } | ||
| @Test | ||
| void getGainersActuallyReturnsProperGainers() { | ||
| Stock teslaStock = new Stock("TSLA", "Tesla", new BigDecimal("200.00")); | ||
| Stock pearStock = new Stock("PEAR", "Pear inc.", new BigDecimal("97.00")); | ||
|
|
||
| @Test | ||
| void findStocksReturnsMatchingStocksBySymbolOrCompany() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); | ||
| appleStock.addNewSalesPrice(new BigDecimal("150.00")); | ||
| teslaStock.addNewSalesPrice(new BigDecimal("230.00")); | ||
| pearStock.addNewSalesPrice(new BigDecimal("112.00")); | ||
|
|
||
| List<Stock> resultBySymbol = exchange.findStocks("AAP"); | ||
| List<Stock> resultByCompany = exchange.findStocks("tes"); | ||
| Exchange exchange = new Exchange("Exchange", List.of(appleStock, teslaStock, pearStock)); | ||
|
|
||
| assertEquals(1, resultBySymbol.size()); | ||
| assertTrue(resultBySymbol.contains(apple)); | ||
| List<Stock> actualGainers = exchange.getGainers(2); | ||
|
|
||
| assertEquals(1, resultByCompany.size()); | ||
| assertTrue(resultByCompany.contains(tesla)); | ||
| } | ||
| boolean actualGainersContainLimitedGainers = actualGainers.contains(teslaStock) | ||
| && actualGainers.contains(appleStock); | ||
| assertTrue(actualGainersContainLimitedGainers); | ||
|
|
||
| @Test | ||
| void buyReturnsPurchaseAndWithdrawsMoneyFromPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
| boolean actualGainersInCorrectOrder = actualGainers.getFirst() == appleStock; | ||
| assertTrue(actualGainersInCorrectOrder); | ||
|
|
||
| Transaction transaction = exchange.buy("AAPL", new BigDecimal("2"), player); | ||
| boolean actualGainersNotContainsGainerOutsideOfLimit = !actualGainers.contains(pearStock); | ||
| assertTrue(actualGainersNotContainsGainerOutsideOfLimit); | ||
| } | ||
|
|
||
| assertInstanceOf(Purchase.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertEquals(new BigDecimal("2"), transaction.getShare().getQuantity()); | ||
| assertEquals(new BigDecimal("100"), transaction.getShare().getPurchasePrice()); | ||
| assertEquals(new BigDecimal("799.000"), player.getMoney()); | ||
| } | ||
| @Test | ||
| void getLosersActuallyReturnsProperLosers() { | ||
| Stock teslaStock = new Stock("TSLA", "Tesla", new BigDecimal("200.00")); | ||
| Stock pearStock = new Stock("PEAR", "Pear inc.", new BigDecimal("97.00")); | ||
|
|
||
| @Test | ||
| void sellReturnsSaleAndAddsMoneyToPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
| Share share = new Share(apple, new BigDecimal("2"), new BigDecimal("100")); | ||
| appleStock.addNewSalesPrice(new BigDecimal("50.00")); | ||
| teslaStock.addNewSalesPrice(new BigDecimal("170.00")); | ||
| pearStock.addNewSalesPrice(new BigDecimal("82.00")); | ||
|
|
||
| Transaction transaction = exchange.sell(share, player); | ||
| Exchange exchange = new Exchange("Exchange", List.of(appleStock, teslaStock, pearStock)); | ||
|
|
||
| assertInstanceOf(Sale.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertSame(share, transaction.getShare()); | ||
| assertEquals(new BigDecimal("1267.9000"), player.getMoney()); | ||
| } | ||
| List<Stock> actualLosers = exchange.getLosers(2); | ||
|
|
||
| @Test | ||
| void advanceIncreasesWeekAndUpdatesStockPrice() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| boolean actualLosersContainsValidLosers = actualLosers.contains(teslaStock) | ||
| && actualLosers.contains(appleStock); | ||
| assertTrue(actualLosersContainsValidLosers); | ||
|
|
||
| BigDecimal oldPrice = apple.getSalesPrice(); | ||
| exchange.advance(); | ||
| boolean actualLosersInCorrectOrder = actualLosers.getFirst() == appleStock; | ||
| assertTrue(actualLosersInCorrectOrder); | ||
|
|
||
| assertEquals(2, exchange.getWeek()); | ||
| assertNotEquals(oldPrice, apple.getSalesPrice()); | ||
| } | ||
| } | ||
| boolean actualLosersNotContainingLoserOutsideOfLimit = !actualLosers.contains(pearStock); | ||
| assertTrue(actualLosersNotContainingLoserOutsideOfLimit); | ||
| } | ||
| } |
77 changes: 46 additions & 31 deletions
77
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.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,48 +1,63 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| final class PlayerTest { | ||
|
|
||
| @Test | ||
| void constructorSetsNameMoneyPortfolioAndArchive() { | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
|
|
||
| assertEquals("Alice", player.getName()); | ||
| assertEquals(new BigDecimal("1000"), player.getMoney()); | ||
| assertNotNull(player.getPortfolio()); | ||
| assertNotNull(player.getTransactionArchive()); | ||
| } | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| @Test | ||
| void addMoneyIncreasesBalance() { | ||
| Player player = new Player("Bob", new BigDecimal("500")); | ||
|
|
||
| class PlayerTest { | ||
| player.addMoney(new BigDecimal("200")); | ||
|
|
||
| @Test | ||
| void constructorSetsNameMoneyPortfolioAndArchive() { | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
| assertEquals(new BigDecimal("700"), player.getMoney()); | ||
| } | ||
|
|
||
| assertEquals("Alice", player.getName()); | ||
| assertEquals(new BigDecimal("1000"), player.getMoney()); | ||
| assertNotNull(player.getPortfolio()); | ||
| assertNotNull(player.getTransactionArchive()); | ||
| } | ||
| @Test | ||
| void withdrawMoneyDecreasesBalance() { | ||
| Player player = new Player("Charlie", new BigDecimal("500")); | ||
|
|
||
| @Test | ||
| void addMoneyIncreasesBalance() { | ||
| Player player = new Player("Bob", new BigDecimal("500")); | ||
| player.withdrawMoney(new BigDecimal("150")); | ||
|
|
||
| player.addMoney(new BigDecimal("200")); | ||
| assertEquals(new BigDecimal("350"), player.getMoney()); | ||
| } | ||
|
|
||
| assertEquals(new BigDecimal("700"), player.getMoney()); | ||
| } | ||
| @Test | ||
| void addAndWithdrawMoneyUpdateBalanceCorrectly() { | ||
| Player player = new Player("Dana", new BigDecimal("1000")); | ||
|
|
||
| @Test | ||
| void withdrawMoneyDecreasesBalance() { | ||
| Player player = new Player("Charlie", new BigDecimal("500")); | ||
| player.addMoney(new BigDecimal("250")); | ||
| player.withdrawMoney(new BigDecimal("300")); | ||
|
|
||
| player.withdrawMoney(new BigDecimal("150")); | ||
| assertEquals(new BigDecimal("950"), player.getMoney()); | ||
| } | ||
|
|
||
| assertEquals(new BigDecimal("350"), player.getMoney()); | ||
| } | ||
| @Test | ||
| void getNetWorthCalculatesCorrectly() { | ||
| Stock stock = new Stock("AAPL", "Apple inc.,", new BigDecimal("100.00")); | ||
| Player player = new Player("Bob", new BigDecimal("900")); | ||
| Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("100.00")); | ||
|
|
||
| @Test | ||
| void addAndWithdrawMoneyUpdateBalanceCorrectly() { | ||
| Player player = new Player("Dana", new BigDecimal("1000")); | ||
| player.getPortfolio().addShare(share); | ||
| SaleCalculator saleCalculator = new SaleCalculator(share); | ||
|
|
||
| player.addMoney(new BigDecimal("250")); | ||
| player.withdrawMoney(new BigDecimal("300")); | ||
| BigDecimal calculatedNetWorth = player.getMoney().add(saleCalculator.calculateTotal()); | ||
| BigDecimal actualNetWorth = player.getNetWorth(); | ||
|
|
||
| assertEquals(new BigDecimal("950"), player.getMoney()); | ||
| } | ||
| } | ||
| assertEquals(calculatedNetWorth, actualNetWorth); | ||
| } | ||
| } |
Oops, something went wrong.