-
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 #308 from holixon/feature/upcasting_junit5
Upcaster testing with JUnit 5
- Loading branch information
Showing
58 changed files
with
1,386 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.holixon.axon.testing._</groupId> | ||
<artifactId>examples</artifactId> | ||
<version>4.7.4.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>io.holixon.axon.testing.examples</groupId> | ||
<artifactId>bankaccount-upcaster-junit5</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.holixon.axon.testing</groupId> | ||
<artifactId>axon-testing-upcaster-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.module</groupId> | ||
<artifactId>jackson-module-kotlin</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-jsr310</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>${junit5.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.holixon.axon.testing.lib</groupId> | ||
<artifactId>axon-testing-fixtures-bankaccount</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>2.0.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.24.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.28</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
112 changes: 112 additions & 0 deletions
112
...ixon/axon/testing/examples/upcaster/junit5/java/AccountCreatedEventUpcastingJavaTest.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,112 @@ | ||
package io.holixon.axon.testing.examples.upcaster.junit5.java; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.module.kotlin.KotlinModule; | ||
import com.thoughtworks.xstream.XStream; | ||
import com.thoughtworks.xstream.security.AnyTypePermission; | ||
import fixture.bankaccount.event.AccountCreatedEvent; | ||
import io.holixon.axon.testing.upcaster.MessageEncoding; | ||
import io.holixon.axon.testing.upcaster.UpcasterTest; | ||
import lombok.val; | ||
import org.axonframework.serialization.json.JacksonSerializer; | ||
import org.axonframework.serialization.upcasting.event.IntermediateEventRepresentation; | ||
import org.axonframework.serialization.upcasting.event.SingleEventUpcaster; | ||
import org.axonframework.serialization.xml.XStreamSerializer; | ||
import org.dom4j.Document; | ||
import org.dom4j.Element; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.holixon.axon.testing.upcaster.UpcasterTestSupport.*; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
public class AccountCreatedEventUpcastingJavaTest { | ||
private final static XStreamSerializer xmlSerializer = createXstreamSerializer(); | ||
private final static JacksonSerializer jacksonSerializer = createJacksonSerializer(); | ||
private final static String payloadType = AccountCreatedEvent.class.getName(); | ||
private final static SingleEventUpcaster xmlDocumentUpcaster = xmlDocumentUpcaster(payloadType, "0", "1", (document) -> { | ||
document.selectNodes("//" + payloadType).forEach(node -> { | ||
// replace bank account id with account id | ||
Element accountId = ((Element) node).addElement("accountId"); | ||
Element bankAccountId = ((Element) node).element("bankAccountId"); | ||
accountId.addText(bankAccountId.getText()); | ||
((Element) node).remove(bankAccountId); | ||
|
||
// add max balance | ||
Element maxBalance = ((Element) node).addElement("maximalBalance"); | ||
maxBalance.addText("1000"); | ||
}); | ||
return document; | ||
}); | ||
|
||
private final static SingleEventUpcaster jsonNodeUpcaster = jsonNodeUpcaster(payloadType, "12", "13", (node) -> { | ||
val root = (ObjectNode)node; | ||
root.set("accountId", root.get("bankAccountId")); | ||
root.remove("bankAccountId"); | ||
root.put("maximalBalance", 1000); | ||
return root; | ||
}); | ||
|
||
|
||
|
||
@UpcasterTest( | ||
messageEncoding = MessageEncoding.XSTREAM | ||
) | ||
public void upcasts_account_created_xstream(List<IntermediateEventRepresentation> events) { | ||
|
||
|
||
val upcastedStream = xmlDocumentUpcaster.upcast(events.stream()); | ||
|
||
// FIXME: build better assertions | ||
val upcastedEvents = upcastedStream.map((ier) -> xmlSerializer.deserialize(ier.getData(Document.class))).collect(Collectors.toList()); | ||
assertThat(upcastedEvents) | ||
.hasSize(1) | ||
.element(0) | ||
.isEqualTo( | ||
AccountCreatedEvent | ||
.builder() | ||
.accountId("4711") | ||
.customerId("Customer1") | ||
.initialBalance(100) | ||
.maximalBalance(1000) | ||
.build() | ||
); | ||
} | ||
|
||
@UpcasterTest( | ||
messageEncoding = MessageEncoding.JACKSON | ||
) | ||
public void upcasts_account_created_jackson(List<IntermediateEventRepresentation> events) { | ||
|
||
val upcastedStream = jsonNodeUpcaster.upcast(events.stream()); | ||
|
||
// FIXME: build better assertions | ||
val upcastedEvents = upcastedStream.map((ier) -> jacksonSerializer.deserialize(ier.getData())).collect(Collectors.toList()); | ||
|
||
assertThat(upcastedEvents) | ||
.hasSize(1) | ||
.element(0) | ||
.isEqualTo( | ||
AccountCreatedEvent | ||
.builder() | ||
.accountId("4711") | ||
.customerId("Customer1") | ||
.initialBalance(100) | ||
.maximalBalance(1000) | ||
.build() | ||
); | ||
} | ||
|
||
static XStreamSerializer createXstreamSerializer() { | ||
val xstream = new XStream(); | ||
xstream.addPermission(AnyTypePermission.ANY); | ||
return XStreamSerializer.builder().lenientDeserialization().xStream(xstream).build(); | ||
} | ||
|
||
static JacksonSerializer createJacksonSerializer() { | ||
val objectMapper = new ObjectMapper().registerModule(new KotlinModule.Builder().build()); | ||
return JacksonSerializer.builder().lenientDeserialization().objectMapper(objectMapper).build(); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...les/bankaccount-upcaster-junit5/src/test/kotlin/AccountCreatedEventUpcastingKotlinTest.kt
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,111 @@ | ||
package io.holixon.axon.testing.examples.upcaster.junit5.kotlin | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.thoughtworks.xstream.XStream | ||
import com.thoughtworks.xstream.security.AnyTypePermission | ||
import fixture.bankaccount.event.AccountCreatedEvent | ||
import io.holixon.axon.testing.upcaster.MessageEncoding | ||
import io.holixon.axon.testing.upcaster.UpcasterTest | ||
import io.holixon.axon.testing.upcaster.UpcasterTestSupport.Companion.jsonNodeUpcaster | ||
import io.holixon.axon.testing.upcaster.UpcasterTestSupport.Companion.xmlDocumentUpcaster | ||
import io.holixon.axon.testing.upcaster.content.DefaultStringMessageContentProvider | ||
import io.holixon.axon.testing.upcaster.payloadtype.FilenameBasedPayloadTypeAndRevisionProvider | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.axonframework.serialization.json.JacksonSerializer | ||
import org.axonframework.serialization.upcasting.event.IntermediateEventRepresentation | ||
import org.axonframework.serialization.upcasting.event.SingleEventUpcaster | ||
import org.axonframework.serialization.xml.XStreamSerializer | ||
import org.dom4j.Document | ||
import org.dom4j.Element | ||
import java.util.stream.Collectors | ||
import java.util.stream.Stream | ||
|
||
class AccountCreatedEventUpcastingKotlinTest { | ||
|
||
companion object { | ||
private val xmlSerializer: XStreamSerializer = | ||
XStreamSerializer.builder().lenientDeserialization().xStream(XStream().apply { addPermission(AnyTypePermission.ANY) }).build() | ||
private val jacksonSerializer: JacksonSerializer = JacksonSerializer.builder().lenientDeserialization().objectMapper(jacksonObjectMapper()).build() | ||
|
||
private val payloadType: String = AccountCreatedEvent::class.java.name | ||
private val xmlUpcaster = xmlDocumentUpcaster(payloadType, "0", "1") { | ||
it.apply { | ||
selectNodes("//$payloadType").forEach { node -> | ||
// replace bank account id with account id | ||
val accountId = (node as Element).addElement("accountId") | ||
val bankAccountId = node.element("bankAccountId") | ||
accountId.addText(bankAccountId.text) | ||
node.remove(bankAccountId) | ||
|
||
// add max balance | ||
val maxBalance: Element = node.addElement("maximalBalance") | ||
maxBalance.addText("1000") | ||
} | ||
} | ||
} | ||
|
||
private val jsonUpcaster = jsonNodeUpcaster(payloadType, "12", "13") { | ||
(it as ObjectNode).apply { | ||
put("accountId", get("bankAccountId").asText()) | ||
remove("bankAccountId") | ||
put("maximalBalance", 1000) | ||
} | ||
} | ||
|
||
private val accountEvent = AccountCreatedEvent | ||
.builder() | ||
.accountId("4711") | ||
.customerId("Customer1") | ||
.initialBalance(100) | ||
.maximalBalance(1000) | ||
.build() | ||
} | ||
|
||
|
||
@UpcasterTest( | ||
messageEncoding = MessageEncoding.XSTREAM | ||
) | ||
fun upcasts_account_created_xstream(events: List<IntermediateEventRepresentation>) { | ||
|
||
val upcastedStream: Stream<IntermediateEventRepresentation> = xmlUpcaster.upcast(events.stream()) | ||
|
||
// FIXME: build better assertions | ||
val upcastedEvents = upcastedStream.map { ier -> | ||
xmlSerializer.deserialize<Document, Any>( | ||
ier.getData( | ||
Document::class.java | ||
) | ||
) | ||
} | ||
assertThat(upcastedEvents) | ||
.hasSize(1) | ||
.element(0) | ||
.isEqualTo(accountEvent) | ||
} | ||
|
||
@UpcasterTest( | ||
messageEncoding = MessageEncoding.JACKSON | ||
) | ||
fun `upcasts account created jackson`(events: List<IntermediateEventRepresentation>, result: List<IntermediateEventRepresentation>) { | ||
|
||
val upcastedStream = jsonUpcaster.upcast(events.stream()).collect(Collectors.toList()) | ||
|
||
// FIXME: build better assertions | ||
val upcastedEvents = upcastedStream.map { ier -> | ||
val event: AccountCreatedEvent = jacksonSerializer.deserialize( | ||
ier.data | ||
) | ||
event | ||
} | ||
|
||
val deserializedResult = result.map { ier -> | ||
val event: AccountCreatedEvent = jacksonSerializer.deserialize( | ||
ier.data | ||
) | ||
event | ||
} | ||
|
||
assertThat(upcastedEvents).containsExactlyElementsOf(deserializedResult) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...upcasts_account_created_jackson/1__fixture.bankaccount.event.AccountCreatedEvent__12.json
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 @@ | ||
{ | ||
"bankAccountId": "4711", | ||
"customerId": "Customer1", | ||
"initialBalance": 100 | ||
} |
5 changes: 5 additions & 0 deletions
5
...t/upcasts_account_created_xstream/1__fixture.bankaccount.event.AccountCreatedEvent__0.xml
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 @@ | ||
<fixture.bankaccount.event.AccountCreatedEvent> | ||
<bankAccountId>4711</bankAccountId> | ||
<customerId>Customer1</customerId> | ||
<initialBalance>100</initialBalance> | ||
</fixture.bankaccount.event.AccountCreatedEvent> |
5 changes: 5 additions & 0 deletions
5
...upcasts_account_created_jackson/1__fixture.bankaccount.event.AccountCreatedEvent__12.json
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 @@ | ||
{ | ||
"bankAccountId": "4711", | ||
"customerId": "Customer1", | ||
"initialBalance": 100 | ||
} |
6 changes: 6 additions & 0 deletions
6
...account_created_jackson/1__fixture.bankaccount.event.AccountCreatedEvent__13__result.json
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,6 @@ | ||
{ | ||
"accountId": "4711", | ||
"customerId": "Customer1", | ||
"initialBalance": 100, | ||
"maximalBalance": 1000 | ||
} |
5 changes: 5 additions & 0 deletions
5
...t/upcasts_account_created_xstream/1__fixture.bankaccount.event.AccountCreatedEvent__0.xml
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 @@ | ||
<fixture.bankaccount.event.AccountCreatedEvent> | ||
<bankAccountId>4711</bankAccountId> | ||
<customerId>Customer1</customerId> | ||
<initialBalance>100</initialBalance> | ||
</fixture.bankaccount.event.AccountCreatedEvent> |
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
Oops, something went wrong.