Skip to content

Commit

Permalink
Merge pull request #308 from holixon/feature/upcasting_junit5
Browse files Browse the repository at this point in the history
Upcaster testing with JUnit 5
  • Loading branch information
zambrovski committed Jul 7, 2023
2 parents 7f3faab + dcf5eb3 commit a456aec
Show file tree
Hide file tree
Showing 58 changed files with 1,386 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.junit.Test
class BankAccountAggregateJgivenKotlinTest : AggregateFixtureScenarioTest<BankAccountAggregate>() {

@ProvidedScenarioState
private val fixture = aggregateTestFixtureBuilder(BankAccountAggregate::class).build()
private val fixture = aggregateTestFixtureBuilder<BankAccountAggregate>().build()

@Test
fun `create account`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.junit.Test
class MoneyTransferSagaJgivenKotlinTest : SagaFixtureScenarioTest<MoneyTransferSaga>() {

@ProvidedScenarioState
private val fixture = sagaTestFixtureBuilder(MoneyTransferSaga::class)
private val fixture = sagaTestFixtureBuilder<MoneyTransferSaga>()
.registerStartRecordingCallback({})
.build()

Expand Down
65 changes: 65 additions & 0 deletions examples/bankaccount-upcaster-junit5/pom.xml
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>
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();
}
}
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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bankAccountId": "4711",
"customerId": "Customer1",
"initialBalance": 100
}
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bankAccountId": "4711",
"customerId": "Customer1",
"initialBalance": 100
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"accountId": "4711",
"customerId": "Customer1",
"initialBalance": 100,
"maximalBalance": 1000
}
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>
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<modules>
<module>bankaccount-jgiven-junit4</module>
<module>bankaccount-jgiven-junit5</module>
<module>bankaccount-upcaster-junit5</module>
</modules>

<dependencyManagement>
Expand Down
14 changes: 0 additions & 14 deletions extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6 changes: 5 additions & 1 deletion extension/upcaster/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
<artifactId>dom4j</artifactId>
<version>${dom4j.version}</version>
</dependency>

<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>${jaxen.version}</version>
</dependency>
<!-- Local test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Loading

0 comments on commit a456aec

Please sign in to comment.