Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/vararg support #337

Merged
merged 7 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/bankaccount-jgiven-junit4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.holixon.axon.testing._</groupId>
<artifactId>examples</artifactId>
<version>4.8.3.0-SNAPSHOT</version>
<version>4.7.6.0-SNAPSHOT</version>
</parent>

<groupId>io.holixon.axon.testing.examples</groupId>
Expand Down
2 changes: 1 addition & 1 deletion examples/bankaccount-jgiven-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.holixon.axon.testing._</groupId>
<artifactId>examples</artifactId>
<version>4.8.3.0-SNAPSHOT</version>
<version>4.7.6.0-SNAPSHOT</version>
</parent>

<groupId>io.holixon.axon.testing.examples</groupId>
Expand Down
7 changes: 6 additions & 1 deletion examples/bankaccount-upcaster-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.holixon.axon.testing._</groupId>
<artifactId>examples</artifactId>
<version>4.8.3.0-SNAPSHOT</version>
<version>4.7.6.0-SNAPSHOT</version>
</parent>

<groupId>io.holixon.axon.testing.examples</groupId>
Expand All @@ -18,6 +18,11 @@
<artifactId>axon-testing-upcaster-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.holixon.axon.testing</groupId>
<artifactId>axon-testing-assert</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ 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.assert.AxonAssertions
import io.holixon.axon.testing.upcaster.MessageEncoding
import io.holixon.axon.testing.upcaster.UpcasterTest
import io.holixon.axon.testing.upcaster.UpcasterTestSupport.Companion.deserializeEvents
import io.holixon.axon.testing.upcaster.UpcasterTestSupport.Companion.jsonNodeUpcaster
import io.holixon.axon.testing.upcaster.UpcasterTestSupport.Companion.xmlDocumentUpcaster
import org.assertj.core.api.Assertions.assertThat
import org.axonframework.serialization.json.JacksonSerializer
import org.axonframework.serialization.upcasting.event.IntermediateEventRepresentation
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 {

Expand Down Expand Up @@ -61,33 +58,35 @@ class AccountCreatedEventUpcastingKotlinTest {
}


/**
* This method demonstrates usage of input events provided to the test method,
* usage of the list assert and element assert for intermediate representation.
*/
@UpcasterTest(
messageEncoding = MessageEncoding.XSTREAM
)
fun upcasts_account_created_xstream(events: List<IntermediateEventRepresentation>) {

val upcastedStream = xmlUpcaster.upcast(events.stream())
val upcastedEvents = deserializeEvents<AccountCreatedEvent>(upcastedStream, xmlSerializer)

// FIXME: build better assertions

assertThat(upcastedEvents)
AxonAssertions
.assertThat(upcastedStream.collect(Collectors.toList()), xmlSerializer)
.hasSize(1)
.element(0)
.isEqualTo(accountEvent)
.hasDeserializedData(accountEvent)
}

/**
* This method demonstrates usage of input events and resulting provided to the test method,
* and usage of the stream assert.
*/
@UpcasterTest(
messageEncoding = MessageEncoding.JACKSON
)
fun `upcasts account created jackson`(events: List<IntermediateEventRepresentation>, result: List<IntermediateEventRepresentation>) {

val upcastedStream = jsonUpcaster.upcast(events.stream())

// FIXME: build better assertions
val upcastedEvents = deserializeEvents<AccountCreatedEvent>(stream = upcastedStream, serializer = jacksonSerializer).collect(Collectors.toList())
val deserializedResult = deserializeEvents<AccountCreatedEvent>(list = result, serializer = jacksonSerializer)

assertThat(upcastedEvents).containsExactlyElementsOf(deserializedResult)
AxonAssertions.assertThat(upcastedStream, jacksonSerializer)
.containsExactlyDeserializedElementsOf(result.stream(), AccountCreatedEvent::class.java)
}
}

11 changes: 10 additions & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.holixon.axon.testing._</groupId>
<artifactId>axon-testing_</artifactId>
<version>4.8.3.0-SNAPSHOT</version>
<version>4.7.6.0-SNAPSHOT</version>
</parent>

<artifactId>examples</artifactId>
Expand All @@ -33,4 +33,13 @@
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>com.tngtech.jgiven</groupId>
<artifactId>jgiven-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
39 changes: 39 additions & 0 deletions extension/assert/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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>extension</artifactId>
<version>4.7.6.0-SNAPSHOT</version>
</parent>

<groupId>io.holixon.axon.testing</groupId>
<artifactId>axon-testing-assert</artifactId>
<name>Axon Testing - Assert</name>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-messaging</artifactId>
</dependency>
</dependencies>
</project>
39 changes: 39 additions & 0 deletions extension/assert/src/main/kotlin/AxonAssertions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.holixon.axon.testing.assert

import org.axonframework.serialization.Serializer
import org.axonframework.serialization.upcasting.event.IntermediateEventRepresentation
import java.util.stream.Stream

class AxonAssertions private constructor() {
companion object {
/**
* Creates the assert for given intermediate representation serialized using given serializer.
* @param actual current intermediate representation.
* @param serializer serializer in use.
* @return the asserting anchor for checks.
*/
@JvmStatic
fun assertThat(actual: IntermediateEventRepresentation, serializer: Serializer) = IntermediateEventRepresentationAssert.assertThat(actual, serializer)

/**
* Creates the assert for given stream of intermediate representations serialized using given serializer.
* @param actual current intermediate representation.
* @param serializer serializer in use.
* @return the asserting anchor for checks.
*/
@JvmStatic
fun assertThat(actual: Stream<IntermediateEventRepresentation>, serializer: Serializer) =
IntermediateEventRepresentationStreamAssert.assertThat(actual, serializer)

/**
* Creates the assert for given list of intermediate representation serialized using given serializer.
* @param actual list of current intermediate representations.
* @param serializer serializer in use.
* @return the asserting anchor for checks.
*/
@JvmStatic
fun assertThat(actual: List<IntermediateEventRepresentation>, serializer: Serializer) =
IntermediateEventRepresentationListAssert.assertThat(actual, serializer)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package io.holixon.axon.testing.assert

import com.fasterxml.jackson.databind.JsonNode
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.ObjectAssert
import org.axonframework.serialization.SerializedObject
import org.axonframework.serialization.SerializedType
import org.axonframework.serialization.Serializer
import org.axonframework.serialization.json.JacksonSerializer
import org.axonframework.serialization.upcasting.event.IntermediateEventRepresentation
import org.axonframework.serialization.xml.XStreamSerializer
import org.dom4j.Document


/**
* Asserts intermediate representation.
*/
class IntermediateEventRepresentationAssert(
actual: IntermediateEventRepresentation,
private val serializer: Serializer,
private val intermediateRepresentationTypeResolver: (serializer: Serializer) -> Class<*> =
{
when (it) {
is XStreamSerializer -> Document::class.java
is JacksonSerializer -> JsonNode::class.java
else -> throw IllegalArgumentException("Unknown serializer type ${serializer::class.java}")
}
}
) : AbstractAssert<IntermediateEventRepresentationAssert, IntermediateEventRepresentation>(actual, IntermediateEventRepresentationAssert::class.java) {


companion object {
/**
* Creates the assert for given intermediate representation serialized using given serializer.
* @param actual current intermediate representation.
* @param serializer serializer in use.
* @return the asserting anchor for checks.
*/
@JvmStatic
fun assertThat(actual: IntermediateEventRepresentation, serializer: Serializer) = IntermediateEventRepresentationAssert(actual, serializer)
}

/**
* Asserts that the de-serialized data is equals to the given.
* @param expected expected data.
* @param T type of the data.
* @return object assert.
*/
fun <T : Any> hasDeserializedData(expected: T): ObjectAssert<T> {
isNotNull
val intermediateRepresentationType = intermediateRepresentationTypeResolver.invoke(serializer)
val data = actual.getData(intermediateRepresentationType)
val event: T = serializer.deserialize(data)
if (event != expected) {
failWithMessage("Expected the event to be <%s> but it was <%s>", expected, event)
}
return ObjectAssert(event)
}

/**
* Asserts that the serialized data is equals to the given.
* @param expected expected data.
* @return object assert.
*/
fun hasData(expected: SerializedObject<*>): ObjectAssert<SerializedObject<*>> {
isNotNull
val so: SerializedObject<*> = actual.data
if (so != expected) {
failWithMessage("Expected the serialized object to be <%s> but it was <%s>", expected, so)
}
return ObjectAssert(so)
}

/**
* Asserts that the aggregate identifier is equals to the given.
* @param expected aggregate identifier.
*/
fun hasAggregateIdentifier(expected: String): IntermediateEventRepresentationAssert {
isNotNull
val aggregateIdentifier = actual.aggregateIdentifier.orElse(null)
if (aggregateIdentifier != expected) {
failWithMessage("Expected the aggregate identifier to be <%s> but it was <%s>", expected, aggregateIdentifier)
}
return this
}

/**
* Asserts that the message identifier is equals to the given.
* @param expected message identifier.
*/
fun hasMessageIdentifier(expected: String): IntermediateEventRepresentationAssert {
isNotNull
val messageIdentifier = actual.messageIdentifier
if (messageIdentifier != expected) {
failWithMessage("Expected the message identifier to be <%s> but it was <%s>", expected, messageIdentifier)
}
return this
}

/**
* Asserts that the aggregate type is equals to the given.
* @param expected aggregate type.
*/
fun hasAggregateType(expected: String): IntermediateEventRepresentationAssert {
isNotNull
val aggregateType = actual.aggregateType.orElseGet(null)
if (aggregateType != expected) {
failWithMessage("Expected the aggregate type to be <%s> but it was <%s>", expected, aggregateType)
}
return this
}

/**
* Asserts that the content type is equals to the given.
* @param expected content type.
*/
fun hasAggregateType(expected: Class<*>): IntermediateEventRepresentationAssert {
isNotNull
val contentType = actual.contentType
if (contentType != expected) {
failWithMessage("Expected the content type to be <%s> but it was <%s>", expected, contentType)
}
return this
}

/**
* Asserts that the type is equals to the given.
* @param expected type.
*/
fun hasType(expected: SerializedType): IntermediateEventRepresentationAssert {
isNotNull
val type = actual.type
if (type != expected) {
failWithMessage("Expected the content type to be <%s> but it was <%s>", expected, type)
}
return this
}

/**
* Asserts that the type name is equals to the given.
* @param expected type.
*/
fun hasTypeName(expected: String): IntermediateEventRepresentationAssert {
isNotNull
val typeName = actual.type.name
if (typeName != expected) {
failWithMessage("Expected the content type name to be <%s> but it was <%s>", expected, typeName)
}
return this
}

/**
* Asserts that the type name is equals to the given.
* @param expected type.
*/
fun hasTypeRevision(expected: String?): IntermediateEventRepresentationAssert {
isNotNull
val typeName = actual.type.revision
if (typeName != expected) {
failWithMessage("Expected the content type name to be <%s> but it was <%s>", expected, typeName)
}
return this
}

/**
* Asserts that the deserialized version of data is equals to deserialized version of given.
* @param expected intermediate representation.
* @param T type of the payload.
*/
fun <T : Any> isEqualDeserializedTo(expected: IntermediateEventRepresentation): IntermediateEventRepresentationAssert {
isNotNull
val deserialized: T = serializer.deserialize(actual.data)
val deserializedExpected: T = serializer.deserialize(expected.data)
if (deserialized != deserializedExpected) {
failWithMessage("Expected the content type name to be <%s> but it was <%s>", deserializedExpected, deserialized)
}
return this
}


}
Loading