Skip to content

Commit

Permalink
Merge branch 'release/0.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
zambrovski committed Apr 12, 2021
2 parents 92a2537 + 7d58501 commit f238a2a
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 51 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
${{ runner.os }}-maven
# Get GPG private key into GPG
- name: Import GPG Owner Trust
run: echo ${{ secrets.GPG_OWNERTRUST }} | base64 --decode | gpg --import-ownertrust
# - name: Import GPG Owner Trust
# run: echo ${{ secrets.GPG_OWNERTRUST }} | base64 --decode | gpg --import-ownertrust

- name: Import GPG key
run: echo ${{ secrets.GPG_SECRET_KEYS }} | base64 --decode | gpg --import --no-tty --batch --yes
# - name: Import GPG key
# run: echo ${{ secrets.GPG_SECRET_KEYS }} | base64 --decode | gpg --import --no-tty --batch --yes

# Setup JDK and Maven
- name: Set up JDK 11
Expand Down
4 changes: 2 additions & 2 deletions 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>0.0.1</version>
<version>0.0.2</version>
</parent>

<groupId>io.holixon.axon.testing.examples</groupId>
Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.7.7</version>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@
import fixture.bankaccount.exception.InsufficientBalanceException;
import fixture.bankaccount.exception.MaximalBalanceExceededException;
import fixture.bankaccount.exception.MaximumActiveMoneyTransfersReachedException;
import io.holixon.axon.testing.jgiven.AxonJGiven;
import io.holixon.axon.testing.jgiven.junit.AggregateFixtureScenarioTest;
import org.axonframework.test.aggregate.AggregateTestFixture;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.Serializable;
import java.util.UUID;

import static fixture.bankaccount.AccountAggregateTestHelper.*;
import static fixture.bankaccount.AccountAggregateTestHelper.ACCOUNT_ID_1;
import static fixture.bankaccount.AccountAggregateTestHelper.ACCOUNT_ID_2;
import static fixture.bankaccount.AccountAggregateTestHelper.CUSTOMER_ID_1;
import static fixture.bankaccount.AccountAggregateTestHelper.accountAggregate;
import static fixture.bankaccount.AccountAggregateTestHelper.accountCreatedEvent;
import static fixture.bankaccount.AccountAggregateTestHelper.createAccountCommand;
import static fixture.bankaccount.BankAccountAggregate.Configuration.DEFAULT_INITIAL_BALANCE;
import static fixture.bankaccount.BankAccountAggregate.Configuration.DEFAULT_MAXIMAL_BALANCE;
import static io.holixon.axon.testing.jgiven.AxonJGiven.aggregateTestFixtureBuilder;
import static org.mockito.Mockito.mock;

public class BankAccountAggregateJgivenJavaTest extends AggregateFixtureScenarioTest<BankAccountAggregate> {

private interface MyService {}

@ProvidedScenarioState
private final AggregateTestFixture<BankAccountAggregate> fixture = new AggregateTestFixture<>(BankAccountAggregate.class);
private final AggregateTestFixture<BankAccountAggregate> fixture = aggregateTestFixtureBuilder(BankAccountAggregate.class)
.registerInjectableResource(mock(MyService.class))
.build();

@Test
public void create_account() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import org.junit.Test;

import static fixture.bankaccount.AccountAggregateTestHelper.*;
import static io.holixon.axon.testing.jgiven.AxonJGiven.sagaTestFixtureBuilder;

public class MoneyTransferSagaJgivenJavaTest extends SagaFixtureScenarioTest<MoneyTransferSaga> {

@ProvidedScenarioState
private final SagaTestFixture<MoneyTransferSaga> fixture = new SagaTestFixture<>(MoneyTransferSaga.class);
private final SagaTestFixture<MoneyTransferSaga> fixture = sagaTestFixtureBuilder(MoneyTransferSaga.class)
.registerStartRecordingCallback(() -> {
})
.build();

@Test
public void initialize_transfer_money() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fixture.bankaccount.BankAccountAggregate
import fixture.bankaccount.BankAccountAggregate.Configuration.DEFAULT_MAXIMAL_BALANCE
import fixture.bankaccount.command.CreateAccountCommand
import fixture.bankaccount.event.AccountCreatedEvent
import io.holixon.axon.testing.jgiven.AxonJGiven
import io.holixon.axon.testing.jgiven.AxonJGiven.aggregateTestFixtureBuilder
import io.holixon.axon.testing.jgiven.junit.AggregateFixtureScenarioTest
import io.toolisticon.testing.jgiven.AND
import io.toolisticon.testing.jgiven.GIVEN
Expand All @@ -13,37 +15,41 @@ import io.toolisticon.testing.jgiven.WHEN
import org.axonframework.test.aggregate.AggregateTestFixture
import org.junit.Test


class BankAccountAggregateJgivenKotlinTest : AggregateFixtureScenarioTest<BankAccountAggregate>() {

@ProvidedScenarioState
private val fixture = AggregateTestFixture(BankAccountAggregate::class.java)
private val fixture = aggregateTestFixtureBuilder(BankAccountAggregate::class).build()

@Test
fun `create account`() {
GIVEN
.noPriorActivity()

WHEN
.command(CreateAccountCommand.builder()
.accountId("1")
.customerId("1")
.initialBalance(100)
.build()
.command(
CreateAccountCommand.builder()
.accountId("1")
.customerId("1")
.initialBalance(100)
.build()
)

THEN
.expectEvent(AccountCreatedEvent.builder()
.accountId("1")
.customerId("1")
.initialBalance(100)
.maximalBalance(DEFAULT_MAXIMAL_BALANCE)
.build())
.expectEvent(
AccountCreatedEvent.builder()
.accountId("1")
.customerId("1")
.initialBalance(100)
.maximalBalance(DEFAULT_MAXIMAL_BALANCE)
.build()
)
.AND
.expectState(BankAccountAggregate.builder()
.accountId("1")
.currentBalance(100)
.maximalBalance(DEFAULT_MAXIMAL_BALANCE)
.build())
.expectState(
BankAccountAggregate.builder()
.accountId("1")
.currentBalance(100)
.maximalBalance(DEFAULT_MAXIMAL_BALANCE)
.build()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package io.holixon.axon.testing.examples.jgiven.junit4.kotlin

import com.tngtech.jgiven.annotation.ProvidedScenarioState
import fixture.bankaccount.AccountAggregateTestHelper
import fixture.bankaccount.AccountAggregateTestHelper.*
import fixture.bankaccount.MoneyTransferSaga
import fixture.bankaccount.event.MoneyTransferInitializedEvent
import io.holixon.axon.testing.jgiven.AxonJGiven
import io.holixon.axon.testing.jgiven.AxonJGiven.sagaTestFixtureBuilder
import io.holixon.axon.testing.jgiven.junit.SagaFixtureScenarioTest
import io.toolisticon.testing.jgiven.GIVEN
import io.toolisticon.testing.jgiven.THEN
import io.toolisticon.testing.jgiven.WHEN
import org.axonframework.test.saga.SagaTestFixture
import org.junit.Test

class MoneyTransferSagaJgivenKotlinTest : SagaFixtureScenarioTest<MoneyTransferSaga>() {

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

@Test
internal fun `initialize money transfer`() {
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>0.0.1</version>
<version>0.0.2</version>
</parent>

<groupId>io.holixon.axon.testing.examples</groupId>
Expand Down
2 changes: 1 addition & 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>0.0.1</version>
<version>0.0.2</version>
</parent>

<artifactId>examples</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extension/jgiven-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.holixon.axon.testing._</groupId>
<artifactId>extension</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</parent>

<groupId>io.holixon.axon.testing</groupId>
Expand Down
18 changes: 18 additions & 0 deletions extension/jgiven-core/src/main/kotlin/AxonJGiven.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import com.tngtech.jgiven.base.ScenarioTestBase
import io.holixon.axon.testing.jgiven.aggregate.AggregateFixtureGiven
import io.holixon.axon.testing.jgiven.aggregate.AggregateFixtureThen
import io.holixon.axon.testing.jgiven.aggregate.AggregateFixtureWhen
import io.holixon.axon.testing.jgiven.aggregate.AggregateTestFixtureBuilder
import io.holixon.axon.testing.jgiven.saga.SagaFixtureGiven
import io.holixon.axon.testing.jgiven.saga.SagaFixtureThen
import io.holixon.axon.testing.jgiven.saga.SagaFixtureWhen
import io.holixon.axon.testing.jgiven.saga.SagaTestFixtureBuilder
import org.axonframework.test.aggregate.AggregateTestFixture
import org.axonframework.test.saga.SagaTestFixture
import kotlin.reflect.KClass

/**
* Base class for scenario aggregate tests.
Expand All @@ -17,3 +22,16 @@ abstract class AggregateFixtureScenarioTestBase<T> : ScenarioTestBase<AggregateF
* Base class for scenario saga tests.
*/
abstract class SagaFixtureScenarioTestBase<T> : ScenarioTestBase<SagaFixtureGiven<T>, SagaFixtureWhen<T>, SagaFixtureThen<T>>()

object AxonJGiven {

@JvmStatic
fun <T : Any> aggregateTestFixtureBuilder(aggregateType: Class<T>) = AggregateTestFixtureBuilder(aggregateType)

@JvmStatic
fun <T : Any> sagaTestFixtureBuilder(aggregateType: Class<T>) = SagaTestFixtureBuilder(aggregateType)

inline fun <reified T : Any> aggregateTestFixtureBuilder(aggregateType: KClass<T>) = AggregateTestFixtureBuilder(T::class.java)
inline fun <reified T : Any> sagaTestFixtureBuilder(aggregateType: KClass<T>) = SagaTestFixtureBuilder(T::class.java)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package io.holixon.axon.testing.jgiven.aggregate

import org.axonframework.commandhandling.CommandMessage
import org.axonframework.deadline.DeadlineMessage
import org.axonframework.eventsourcing.AggregateFactory
import org.axonframework.messaging.MessageDispatchInterceptor
import org.axonframework.messaging.MessageHandler
import org.axonframework.messaging.MessageHandlerInterceptor
import org.axonframework.messaging.annotation.HandlerDefinition
import org.axonframework.messaging.annotation.HandlerEnhancerDefinition
import org.axonframework.messaging.annotation.ParameterResolverFactory
import org.axonframework.modelling.command.CommandTargetResolver
import org.axonframework.modelling.command.Repository
import org.axonframework.modelling.command.RepositoryProvider
import org.axonframework.test.aggregate.AggregateTestFixture
import org.axonframework.test.matchers.FieldFilter
import kotlin.reflect.KClass

class AggregateTestFixtureBuilder<T>(private val aggregateType: Class<T>) {

private lateinit var aggregateFactory: AggregateFactory<T>
private val annotatedCommandHandler: MutableList<Any> = mutableListOf()
private val commandHandlers: MutableMap<String, MessageHandler<CommandMessage<*>>> = mutableMapOf()
private val commandDispatchInterceptors: MutableList<MessageDispatchInterceptor<in CommandMessage<*>>> = mutableListOf()
private val commandHandlerInterceptors: MutableList<MessageHandlerInterceptor<in CommandMessage<*>>> = mutableListOf()
private lateinit var commandTargetResolver: CommandTargetResolver
private val deadlineDispatchInterceptors: MutableList<MessageDispatchInterceptor<in DeadlineMessage<*>>> = mutableListOf()
private val deadlineHandlerInterceptors: MutableList<MessageHandlerInterceptor<in DeadlineMessage<*>>> = mutableListOf()
private val fieldFilters: MutableList<FieldFilter> = mutableListOf()
private val ignoredFields: MutableList<Pair<Class<*>, String>> = mutableListOf()
private val handlerDefinitions: MutableList<HandlerDefinition> = mutableListOf()
private val handlerEnhancerDefinitions: MutableList<HandlerEnhancerDefinition> = mutableListOf()
private val injectableResources: MutableList<Any> = mutableListOf()
private val parameterResolverFactories: MutableList<ParameterResolverFactory> = mutableListOf()
private var reportIllegalStateChange: Boolean? = null
private lateinit var repository: Repository<T>
private lateinit var repositoryProvider: RepositoryProvider
private lateinit var subtypes: Array<Class<out T>>

fun registerAggregateFactory(aggregateFactory: AggregateFactory<T>) = apply { this.aggregateFactory = aggregateFactory }
fun registerAnnotatedCommandHandler(annotatedCommandHandler: Any) = apply { this.annotatedCommandHandler.add(annotatedCommandHandler) }

fun registerCommandHandler(commandName: String, commandHandler: MessageHandler<CommandMessage<*>>): AggregateTestFixtureBuilder<T> =
apply { this.commandHandlers[commandName] = commandHandler }

fun registerCommandHandler(payloadType: Class<*>, commandHandler: MessageHandler<CommandMessage<*>>): AggregateTestFixtureBuilder<T> =
registerCommandHandler(payloadType.name, commandHandler)

fun registerCommandDispatchInterceptor(vararg commandDispatchInterceptors: MessageDispatchInterceptor<in CommandMessage<*>>) =
apply { this.commandDispatchInterceptors.addAll(commandDispatchInterceptors) }

fun registerCommandHandlerInterceptor(vararg commandHandlerInterceptors: MessageHandlerInterceptor<in CommandMessage<*>>) =
apply { this.commandHandlerInterceptors.addAll(commandHandlerInterceptors) }

fun registerCommandTargetResolver(commandTargetResolver: CommandTargetResolver) = apply { this.commandTargetResolver = commandTargetResolver }

fun registerDeadlineDispatchInterceptor(vararg deadlineDispatchInterceptors: MessageDispatchInterceptor<in DeadlineMessage<*>>) =
apply { this.deadlineDispatchInterceptors.addAll(deadlineDispatchInterceptors) }

fun registerDeadlineHandlerInterceptor(vararg deadlineHandlerInterceptors: MessageHandlerInterceptor<in DeadlineMessage<*>>) =
apply { this.deadlineHandlerInterceptors.addAll(deadlineHandlerInterceptors) }

fun registerFieldFilter(vararg fieldFilters: FieldFilter) = apply { this.fieldFilters.addAll(fieldFilters) }

fun registerHandlerDefinition(vararg handlerDefinitions: HandlerDefinition) = apply { this.handlerDefinitions.addAll(handlerDefinitions) }
fun registerHandlerEnhancerDefinition(vararg handlerEnhancerDefinitions: HandlerEnhancerDefinition) =
apply { this.handlerEnhancerDefinitions.addAll(handlerEnhancerDefinitions) }


fun registerIgnoredField(declaringClass: KClass<*>, fieldName: String) = apply { ignoredFields.add(declaringClass.java to fieldName) }
fun registerIgnoredField(declaringClass: Class<*>, fieldName: String) = apply { ignoredFields.add(declaringClass to fieldName) }


fun registerInjectableResource(vararg resources: Any) = apply { this.injectableResources.addAll(resources) }

fun registerParameterResolverFactory(vararg parameterResolverFactories: ParameterResolverFactory) =
apply { this.parameterResolverFactories.addAll(parameterResolverFactories) }

fun registerRepository(repository: Repository<T>) = apply { this.repository = repository }
fun registerRepositoryProvider(repositoryProvider: RepositoryProvider) = apply { this.repositoryProvider = repositoryProvider }


fun setReportIllegalStateChange(reportIllegalStateChange: Boolean) = apply { this.reportIllegalStateChange = reportIllegalStateChange }

fun withSubtypes(vararg subtypes: Class<out T>) = withSubtypes(subtypes.asList())
fun withSubtypes(subtypes: Collection<Class<out T>>) = apply { this.subtypes = subtypes.toTypedArray() }

fun build(): AggregateTestFixture<T> {
val fixture = AggregateTestFixture<T>(aggregateType)

if (this::aggregateFactory.isInitialized) fixture.registerAggregateFactory(aggregateFactory)

annotatedCommandHandler.forEach { fixture.registerAnnotatedCommandHandler(it) }
commandHandlers.forEach { fixture.registerCommandHandler(it.key, it.value) }
commandDispatchInterceptors.forEach { fixture.registerCommandDispatchInterceptor(it) }
commandHandlerInterceptors.forEach { fixture.registerCommandHandlerInterceptor(it) }
if (this::commandTargetResolver.isInitialized) fixture.registerCommandTargetResolver(commandTargetResolver)
deadlineDispatchInterceptors.forEach { fixture.registerDeadlineDispatchInterceptor(it) }
deadlineHandlerInterceptors.forEach { fixture.registerDeadlineHandlerInterceptor(it) }
fieldFilters.forEach { fixture.registerFieldFilter(it) }

handlerEnhancerDefinitions.forEach { fixture.registerHandlerEnhancerDefinition(it) }
handlerDefinitions.forEach { fixture.registerHandlerDefinition(it) }

injectableResources.forEach { fixture.registerInjectableResource(it) }
ignoredFields.forEach { fixture.registerIgnoredField(it.first, it.second) }

parameterResolverFactories.forEach { fixture.registerParameterResolverFactory(it) }

if (this::repository.isInitialized) fixture.registerRepository(repository)
if (this::repositoryProvider.isInitialized) fixture.registerRepositoryProvider(repositoryProvider)
if (this::subtypes.isInitialized) fixture.withSubtypes(*this.subtypes)
reportIllegalStateChange?.let { fixture.setReportIllegalStateChange(it) }

return fixture
}
}
Loading

0 comments on commit f238a2a

Please sign in to comment.