-
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.
- Loading branch information
Showing
10 changed files
with
225 additions
and
9 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
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
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
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
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,5 @@ | ||
package application.business | ||
|
||
enum class OrderStatus { | ||
PLACED, PROCESSING, COMPLETED, CANCELLED; | ||
} |
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,23 @@ | ||
package application.config | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL | ||
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES | ||
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES | ||
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS | ||
import com.fasterxml.jackson.databind.SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
|
||
/** | ||
* Produces an [ObjectMapper] intended for usage when (de)serializing events. | ||
*/ | ||
fun objectMapperForEvents(): ObjectMapper = jacksonObjectMapper() | ||
.registerModule(JavaTimeModule()) | ||
.disable(FAIL_ON_UNKNOWN_PROPERTIES) | ||
.disable(FAIL_ON_IGNORED_PROPERTIES) | ||
.disable(WRITE_DATES_AS_TIMESTAMPS) | ||
.disable(WRITE_DURATIONS_AS_TIMESTAMPS) | ||
.enable(FAIL_ON_NULL_FOR_PRIMITIVES) | ||
.setDefaultPropertyInclusion(NON_NULL) |
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,76 @@ | ||
package application.business | ||
|
||
import application.business.OrderStatus.PLACED | ||
import application.business.OrderStatus.PROCESSING | ||
import application.documentation.ArchitectureDocumentation.createOrReplaceEvent | ||
import application.documentation.EventDescriptionSpec | ||
import application.documentation.eventDescription | ||
import org.junit.jupiter.api.Test | ||
import java.time.Instant.parse | ||
import java.util.UUID.fromString | ||
|
||
class OrderEventTests { | ||
|
||
@Test | ||
fun `OrderPlaced event`() { | ||
val description = eventDescription( | ||
example = OrderPlaced( | ||
id = fromString("3d6fd447-a311-4028-8248-356e3621d450"), | ||
timestamp = parse("2024-07-22T12:34:56.789Z"), | ||
order = OrderData( | ||
orderId = fromString("a64914f7-7404-4e85-8e1a-778068fae307"), | ||
customerId = fromString("ed2a43d7-e49b-408d-8b5f-e2e2305954c2"), | ||
status = PLACED | ||
) | ||
), | ||
description = "Emitted whenever a new order is placed.", | ||
fields = { | ||
orderDataFields("order") | ||
} | ||
) | ||
createOrReplaceEvent(description) | ||
} | ||
|
||
@Test | ||
fun `OrderCanceled event`() { | ||
val description = eventDescription( | ||
example = OrderCanceled( | ||
id = fromString("4c97c099-0c00-4e56-841d-fbfe81770936"), | ||
timestamp = parse("2024-07-22T12:34:56.789Z"), | ||
order = OrderData( | ||
orderId = fromString("a64914f7-7404-4e85-8e1a-778068fae307"), | ||
customerId = fromString("ed2a43d7-e49b-408d-8b5f-e2e2305954c2"), | ||
status = PROCESSING | ||
) | ||
), | ||
description = "Emitted whenever an order is canceled.", | ||
fields = { | ||
orderDataFields("order") | ||
} | ||
) | ||
createOrReplaceEvent(description) | ||
} | ||
|
||
private fun EventDescriptionSpec.orderDataFields(objectName: String) { | ||
field( | ||
property = objectName, | ||
type = "Object", | ||
) | ||
field( | ||
property = "$objectName.orderId", | ||
type = "UUID4", | ||
description = "The ID of the order." | ||
) | ||
field( | ||
property = "$objectName.customerId", | ||
type = "UUID4", | ||
description = "The ID of the customer that placed the order." | ||
) | ||
field( | ||
property = "$objectName.status", | ||
type = "Enumeration", | ||
description = "The status of the order. Might have one of the following values: " | ||
+ OrderStatus.entries.joinToString() | ||
) | ||
} | ||
} |
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
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,58 @@ | ||
package application.documentation | ||
|
||
import application.business.Event | ||
import application.config.objectMapperForEvents | ||
import com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT | ||
|
||
private val eventObjectMapper = objectMapperForEvents() | ||
.enable(INDENT_OUTPUT) // pretty format the JSON examples | ||
|
||
fun eventDescription(example: Event, description: String, fields: EventDescriptionSpec.() -> Unit): EventDescription = | ||
EventDescriptionSpec(example, description).apply(fields).build() | ||
|
||
class EventDescriptionSpec( | ||
private val example: Event, | ||
private val description: String | ||
) { | ||
private val fields = mutableListOf<EventDescription.Field>() | ||
|
||
init { | ||
field( | ||
property = "id", | ||
type = "UUID4", | ||
nullable = false, | ||
description = "The unique ID of the event." | ||
) | ||
field( | ||
property = "timestamp", | ||
type = "ISO-8601 Date+Time (UTC)", | ||
nullable = false, | ||
description = "The exact instant the event occurred at its source." | ||
) | ||
} | ||
|
||
fun field( | ||
property: String, | ||
type: String, | ||
nullable: Boolean = false, | ||
description: String? = null | ||
) { | ||
check(fields.none { it.property == property }) { "Field '$property' is already documented." } | ||
fields.add( | ||
EventDescription.Field( | ||
property = property, | ||
type = type, | ||
nullable = nullable, | ||
description = description, | ||
) | ||
) | ||
} | ||
|
||
internal fun build() = EventDescription( | ||
name = example.getEventName(), | ||
type = example.getEventType(), | ||
description = description, | ||
example = eventObjectMapper.writeValueAsString(example), | ||
fields = fields | ||
) | ||
} |
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