|
| 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 testInvoiceSubtotalWithTwoDifferentProducts() { |
| 41 | + Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10")); |
| 42 | + Product apples = new TaxFreeProduct("Owoce", new BigDecimal("10")); |
| 43 | + invoice.addProduct(onions); |
| 44 | + invoice.addProduct(apples); |
| 45 | + Assert.assertThat(new BigDecimal("20"), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testInvoiceSubtotalWithManySameProducts() { |
| 50 | + Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10")); |
| 51 | + invoice.addProduct(onions, 100); |
| 52 | + Assert.assertThat(new BigDecimal("1000"), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() { |
| 57 | + Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99")); |
| 58 | + invoice.addProduct(taxFreeProduct); |
| 59 | + Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testInvoiceHasProperSubtotalForManyProducts() { |
| 64 | + invoice.addProduct(new TaxFreeProduct("Owoce", new BigDecimal("200"))); |
| 65 | + invoice.addProduct(new DairyProduct("Maslanka", new BigDecimal("100"))); |
| 66 | + invoice.addProduct(new OtherProduct("Wino", new BigDecimal("10"))); |
| 67 | + Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testInvoiceHasProperTaxValueForManyProduct() { |
| 72 | + // tax: 0 |
| 73 | + invoice.addProduct(new TaxFreeProduct("Pampersy", new BigDecimal("200"))); |
| 74 | + // tax: 8 |
| 75 | + invoice.addProduct(new DairyProduct("Kefir", new BigDecimal("100"))); |
| 76 | + // tax: 2.30 |
| 77 | + invoice.addProduct(new OtherProduct("Piwko", new BigDecimal("10"))); |
| 78 | + Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTax())); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testInvoiceHasProperTotalValueForManyProduct() { |
| 83 | + // price with tax: 200 |
| 84 | + invoice.addProduct(new TaxFreeProduct("Maskotki", new BigDecimal("200"))); |
| 85 | + // price with tax: 108 |
| 86 | + invoice.addProduct(new DairyProduct("Maslo", new BigDecimal("100"))); |
| 87 | + // price with tax: 12.30 |
| 88 | + invoice.addProduct(new OtherProduct("Chipsy", new BigDecimal("10"))); |
| 89 | + Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getTotal())); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testInvoiceHasPropoerSubtotalWithQuantityMoreThanOne() { |
| 94 | + // 2x kubek - price: 10 |
| 95 | + invoice.addProduct(new TaxFreeProduct("Kubek", new BigDecimal("5")), 2); |
| 96 | + // 3x kozi serek - price: 30 |
| 97 | + invoice.addProduct(new DairyProduct("Kozi Serek", new BigDecimal("10")), 3); |
| 98 | + // 1000x pinezka - price: 10 |
| 99 | + invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000); |
| 100 | + Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getSubtotal())); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testInvoiceHasPropoerTotalWithQuantityMoreThanOne() { |
| 105 | + // 2x chleb - price with tax: 10 |
| 106 | + invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2); |
| 107 | + // 3x chedar - price with tax: 32.40 |
| 108 | + invoice.addProduct(new DairyProduct("Chedar", new BigDecimal("10")), 3); |
| 109 | + // 1000x pinezka - price with tax: 12.30 |
| 110 | + invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000); |
| 111 | + Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getTotal())); |
| 112 | + } |
| 113 | + |
| 114 | + @Test(expected = IllegalArgumentException.class) |
| 115 | + public void testInvoiceWithZeroQuantity() { |
| 116 | + invoice.addProduct(new TaxFreeProduct("Tablet", new BigDecimal("1678")), 0); |
| 117 | + } |
| 118 | + |
| 119 | + @Test(expected = IllegalArgumentException.class) |
| 120 | + public void testInvoiceWithNegativeQuantity() { |
| 121 | + invoice.addProduct(new DairyProduct("Zsiadle mleko", new BigDecimal("5.55")), -1); |
| 122 | + } |
| 123 | +} |
0 commit comments