|
| 1 | +package pl.edu.agh.mwo.invoice; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | + |
| 5 | +import org.hamcrest.Matchers; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import pl.edu.agh.mwo.invoice.Invoice; |
| 11 | +import pl.edu.agh.mwo.invoice.product.DairyProduct; |
| 12 | +import pl.edu.agh.mwo.invoice.product.OtherProduct; |
| 13 | +import pl.edu.agh.mwo.invoice.product.Product; |
| 14 | +import pl.edu.agh.mwo.invoice.product.TaxFreeProduct; |
| 15 | + |
| 16 | +public class InvoiceTest { |
| 17 | + private Invoice invoice; |
| 18 | + |
| 19 | + @Before |
| 20 | + public void createEmptyInvoiceForTheTest() { |
| 21 | + invoice = new Invoice(); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testEmptyInvoiceHasEmptySubtotal() { |
| 26 | + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testEmptyInvoiceHasEmptyTaxAmount() { |
| 31 | + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTax())); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testEmptyInvoiceHasEmptyTotal() { |
| 36 | + Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal())); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() { |
| 41 | + Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99")); |
| 42 | + invoice.addProduct(taxFreeProduct); |
| 43 | + Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testInvoiceHasProperSubtotalForManyProducts() { |
| 48 | + invoice.addProduct(new TaxFreeProduct("Owoce", new BigDecimal("200"))); |
| 49 | + invoice.addProduct(new DairyProduct("Maslanka", new BigDecimal("100"))); |
| 50 | + invoice.addProduct(new OtherProduct("Wino", new BigDecimal("10"))); |
| 51 | + Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testInvoiceHasProperTaxValueForManyProduct() { |
| 56 | + // tax: 0 |
| 57 | + invoice.addProduct(new TaxFreeProduct("Pampersy", new BigDecimal("200"))); |
| 58 | + // tax: 8 |
| 59 | + invoice.addProduct(new DairyProduct("Kefir", new BigDecimal("100"))); |
| 60 | + // tax: 2.30 |
| 61 | + invoice.addProduct(new OtherProduct("Piwko", new BigDecimal("10"))); |
| 62 | + Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTax())); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testInvoiceHasProperTotalValueForManyProduct() { |
| 67 | + // price with tax: 200 |
| 68 | + invoice.addProduct(new TaxFreeProduct("Maskotki", new BigDecimal("200"))); |
| 69 | + // price with tax: 108 |
| 70 | + invoice.addProduct(new DairyProduct("Maslo", new BigDecimal("100"))); |
| 71 | + // price with tax: 12.30 |
| 72 | + invoice.addProduct(new OtherProduct("Chipsy", new BigDecimal("10"))); |
| 73 | + Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getTotal())); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testInvoiceHasPropoerSubtotalWithQuantityMoreThanOne() { |
| 78 | + // 2x kubek - price: 10 |
| 79 | + invoice.addProduct(new TaxFreeProduct("Kubek", new BigDecimal("5")), 2); |
| 80 | + // 3x kozi serek - price: 30 |
| 81 | + invoice.addProduct(new DairyProduct("Kozi Serek", new BigDecimal("10")), 3); |
| 82 | + // 1000x pinezka - price: 10 |
| 83 | + invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000); |
| 84 | + Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testInvoiceHasPropoerTotalWithQuantityMoreThanOne() { |
| 89 | + // 2x chleb - price with tax: 10 |
| 90 | + invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2); |
| 91 | + // 3x chedar - price with tax: 32.40 |
| 92 | + invoice.addProduct(new DairyProduct("Chedar", new BigDecimal("10")), 3); |
| 93 | + // 1000x pinezka - price with tax: 12.30 |
| 94 | + invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000); |
| 95 | + Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getTotal())); |
| 96 | + } |
| 97 | + |
| 98 | + @Test(expected = IllegalArgumentException.class) |
| 99 | + public void testInvoiceWithZeroQuantity() { |
| 100 | + invoice.addProduct(new TaxFreeProduct("Tablet", new BigDecimal("1678")), 0); |
| 101 | + } |
| 102 | + |
| 103 | + @Test(expected = IllegalArgumentException.class) |
| 104 | + public void testInvoiceWithNegativeQuantity() { |
| 105 | + invoice.addProduct(new DairyProduct("Zsiadle mleko", new BigDecimal("5.55")), -1); |
| 106 | + } |
| 107 | +} |
0 commit comments