-
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.
Merge pull request #39 from einaskoi/per/calculatorTest
Added PurchaseCalculatorTest & SaleCalculatorTest
- Loading branch information
Showing
3 changed files
with
141 additions
and
4 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
69 changes: 69 additions & 0 deletions
69
src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/PurchaseCalculatorTest.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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Model.Calculator; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Share; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; | ||
| import java.math.BigDecimal; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class PurchaseCalculatorTest { | ||
| private PurchaseCalculator calculator; | ||
| private BigDecimal purchasePrice; | ||
| private BigDecimal quantity; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| // Setup test data | ||
| purchasePrice = new BigDecimal("100"); | ||
| quantity = new BigDecimal("10"); | ||
| Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("150")); | ||
| Share share = new Share(stock, quantity, purchasePrice); | ||
|
|
||
| calculator = new PurchaseCalculator(share); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateGross() { | ||
| /** | ||
| * Gross = Sales Price * Quantity | ||
| * 1000 = 100 * 10 | ||
| */ | ||
| BigDecimal expectedGross = new BigDecimal("1000"); | ||
| assertEquals(0, expectedGross.compareTo(calculator.calculateGross()), | ||
| "Gross should be purchasePrice * quantity"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateCommission() { | ||
| /** | ||
| * Commission = Gross * Commission Rate (1%) | ||
| * Commission = 1000 * 0.01 = 10 | ||
| */ | ||
| BigDecimal expectedCommission = new BigDecimal("10"); | ||
| assertEquals(0, expectedCommission.compareTo(calculator.calculateCommission()), | ||
| "Commission should be 1% of Gross"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateTax() { | ||
| /** | ||
| * Purchase tax is 0 | ||
| */ | ||
| BigDecimal expectedTax = BigDecimal.ZERO; | ||
| assertEquals(0, expectedTax.compareTo(calculator.calculateTax()), | ||
| "Tax for purchase should be zero"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateTotal() { | ||
| /** | ||
| * Total = Gross + Commission | ||
| * Total = 1000 + 10 = 1010 | ||
| */ | ||
| BigDecimal expectedTotal = new BigDecimal("1010"); | ||
| assertEquals(0, expectedTotal.compareTo(calculator.calculateTotal()), | ||
| "Total should be Gross + Commission + Tax"); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/SaleCalculatorTest.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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Model.Calculator; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Share; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; | ||
| import java.math.BigDecimal; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SaleCalculatorTest { | ||
| private SaleCalculator calculator; | ||
| private BigDecimal purchasePrice; | ||
| private BigDecimal salesPrice; | ||
| private BigDecimal quantity; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| purchasePrice = new BigDecimal("100"); | ||
| salesPrice = new BigDecimal("150"); | ||
| quantity = new BigDecimal("10"); | ||
| Stock stock = new Stock("AAPL", "Apple Inc.", salesPrice); | ||
| Share share = new Share(stock, quantity, purchasePrice); | ||
|
|
||
| calculator = new SaleCalculator(share); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateGross() { | ||
| /** | ||
| * Gross = Sales Price * Quantity | ||
| * 1500 = 150 * 10 | ||
| */ | ||
| BigDecimal expectedGross = new BigDecimal("1500"); | ||
| assertEquals(0, expectedGross.compareTo(calculator.calculateGross()), | ||
| "Gross should be salesPrice * quantity"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateCommission() { | ||
| /** | ||
| * Commission = Gross * Commission Rate (1%) | ||
| * Commission = 1500 * 0.01 = 15 | ||
| */ | ||
| BigDecimal expectedCommission = new BigDecimal("15"); | ||
| assertEquals(0, expectedCommission.compareTo(calculator.calculateCommission()), | ||
| "Commission should be 1% of Gross"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateTax() { | ||
| /** | ||
| * Tax = (Sales Price - Purchase Price) * Quantity * Tax Rate (22%) | ||
| * (150 - 100) * 10 * 0.22 = 110 | ||
| */ | ||
| BigDecimal expectedTax = new BigDecimal("110"); | ||
| assertEquals(0, expectedTax.compareTo(calculator.calculateTax()), | ||
| "Tax should be 22% of profit"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCalculateTotal() { | ||
| /** | ||
| * Total = Gross - Commission - Tax | ||
| * Total = 1500 - 15 - 110 = 1375 | ||
| */ | ||
| BigDecimal expectedTotal = new BigDecimal("1375"); | ||
| assertEquals(0, expectedTotal.compareTo(calculator.calculateTotal()), | ||
| "Total should be Gross - Commission - Tax"); | ||
| } | ||
| } |