From a512456a48d2031ba067524816eac6049d41a09b Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 14 Nov 2024 16:17:11 +0100 Subject: [PATCH] Extracted test DataGenerator --- .../invoicing/creation/EInvoiceCreatorTest.kt | 42 +++--------- .../codinux/invoicing/test/DataGenerator.kt | 66 +++++++++++++++++++ 2 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/test/DataGenerator.kt diff --git a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt index 56c0a5b..b1791d1 100644 --- a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/creation/EInvoiceCreatorTest.kt @@ -1,12 +1,9 @@ package net.codinux.invoicing.creation import assertk.assertThat +import assertk.assertions.contains import assertk.assertions.isNotEmpty -import net.codinux.invoicing.model.Invoice -import net.codinux.invoicing.model.LineItem -import net.codinux.invoicing.model.Party -import java.math.BigDecimal -import java.time.LocalDate +import net.codinux.invoicing.test.DataGenerator import kotlin.test.Test class EInvoiceCreatorTest { @@ -20,7 +17,7 @@ class EInvoiceCreatorTest { val result = underTest.createXRechnungXml(invoice) - assertThat(result).isNotEmpty() + assertInvoiceXml(result) } @Test @@ -29,36 +26,17 @@ class EInvoiceCreatorTest { val result = underTest.createZugferdXml(invoice) - assertThat(result).isNotEmpty() + assertInvoiceXml(result) } - private fun createInvoice( - invoiceNumber: String = "12345", - invoicingDate: LocalDate = LocalDate.of(2015, 10, 21), - sender: Party = createParty("Hochwürdiger Leistungserbringer"), - recipient: Party = createParty("Untertänigster Leistungsempfänger"), - items: List = listOf(createItem()), - dueDate: LocalDate? = null - ) = Invoice(invoiceNumber, invoicingDate, sender, recipient, items, dueDate) + private fun createInvoice() = DataGenerator.createInvoice() - private fun createParty( - name: String, - streetName: String = "Fun Street 1", - postalCode: String = "12345", - city: String = "Glückstadt", - country: String? = null, - taxNumber: String? = "DE12345678", - email: String? = null, - ) = Party(name, streetName, postalCode, city, country, taxNumber, email) + private fun assertInvoiceXml(xml: String) { + assertThat(xml).isNotEmpty() - private fun createItem( - name: String = "Erbrachte Dienstleistungen", - unit: String = "", - quantity: BigDecimal = BigDecimal(1), - price: BigDecimal = BigDecimal(99), - vatPercentage: BigDecimal = BigDecimal(0.19), - description: String? = null, - ) = LineItem(name, unit, quantity, price, vatPercentage, description) + assertThat(xml).contains("${DataGenerator.InvoiceNumber}") + assertThat(xml).contains("""${DataGenerator.InvoicingDate.toString().replace("-", "")}""") + } } \ No newline at end of file diff --git a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/test/DataGenerator.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/test/DataGenerator.kt new file mode 100644 index 0000000..7870dba --- /dev/null +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/test/DataGenerator.kt @@ -0,0 +1,66 @@ +package net.codinux.invoicing.test + +import net.codinux.invoicing.model.Invoice +import net.codinux.invoicing.model.LineItem +import net.codinux.invoicing.model.Party +import java.math.BigDecimal +import java.time.LocalDate + +object DataGenerator { + + const val InvoiceNumber = "12345" + val InvoicingDate = LocalDate.of(2015, 10, 21) + + const val SenderName = "Hochwürdiger Leistungserbringer" + const val SenderStreet = "Fun Street 1" + const val SenderPostalCode = "12345" + const val SenderCity = "Glückstadt" + val SenderCountry: String? = null + const val SenderVatId = "DE12345678" + const val SenderEmail = "working-class-hero@rock.me" + + const val RecipientName = "Untertänigster Leistungsempfänger" + const val RecipientStreet = "Party Street 1" + const val RecipientPostalCode = SenderPostalCode + const val RecipientCity = SenderCity + val RecipientCountry: String? = SenderCountry + const val RecipientVatId = "DE87654321" + const val RecipientEmail = "exploiter@your.boss" + + const val ItemName = "Erbrachte Dienstleistungen" + const val ItemUnit = "HUR" // EN code for 'hour' + val ItemQuantity = BigDecimal(1) + val ItemPrice = BigDecimal(99) + val ItemVat = BigDecimal(0.19) + val ItemDescription: String? = null + + + fun createInvoice( + invoiceNumber: String = InvoiceNumber, + invoicingDate: LocalDate = InvoicingDate, + sender: Party = createParty(SenderName, SenderStreet, SenderPostalCode, SenderCity, SenderCountry, SenderVatId, SenderEmail), + recipient: Party = createParty(RecipientName, RecipientStreet, RecipientPostalCode, RecipientCity, RecipientCountry, RecipientVatId, RecipientEmail), + items: List = listOf(createItem()), + dueDate: LocalDate? = null + ) = Invoice(invoiceNumber, invoicingDate, sender, recipient, items, dueDate) + + fun createParty( + name: String, + streetName: String = SenderStreet, + postalCode: String = SenderPostalCode, + city: String = SenderCity, + country: String? = SenderCountry, + taxNumber: String? = SenderVatId, + email: String? = SenderEmail, + ) = Party(name, streetName, postalCode, city, country, taxNumber, email) + + fun createItem( + name: String = ItemName, + unit: String = ItemUnit, + quantity: BigDecimal = ItemQuantity, + price: BigDecimal = ItemPrice, + vatPercentage: BigDecimal = ItemVat, + description: String? = ItemDescription, + ) = LineItem(name, unit, quantity, price, vatPercentage, description) + +} \ No newline at end of file