Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed TestableAggregateRoot to be explicit on commandhandling failu…
…res (#44) Changed TestableAggregateRoot to be explicit on commandhandling failures Ensured TestableAggregateRoot works properly based on a starting 'given' (the actor was created in when, that is now part of given that is actually class constructor work) Added tests to cover the functionality of TestableAggregateRoot
- Loading branch information
Showing
9 changed files
with
326 additions
and
47 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
63 changes: 63 additions & 0 deletions
63
bounded-test/src/test/scala/io/cafienne/bounded/test/SpecConfig.scala
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,63 @@ | ||
/* | ||
* Copyright (C) 2016-2018 Cafienne B.V. <https://www.cafienne.io/bounded> | ||
*/ | ||
|
||
package io.cafienne.bounded.test | ||
|
||
import com.typesafe.config.ConfigFactory | ||
|
||
object SpecConfig { | ||
|
||
/* | ||
PLEASE NOTE: | ||
Currently the https://github.com/dnvriend/akka-persistence-inmemory is NOT working for Aggregate Root tests | ||
because it is not possible to use a separate instance writing the events that should be in the event store | ||
before you actually create the aggregate root (should replay those stored events) to check execution of a new | ||
command. | ||
A new configuration that uses the akka bundled inmem storage is added to create a working situation. | ||
*/ | ||
val testConfig = ConfigFactory.parseString( | ||
""" | ||
| akka { | ||
| loglevel = "DEBUG" | ||
| stdout-loglevel = "DEBUG" | ||
| loggers = ["akka.testkit.TestEventListener"] | ||
| actor { | ||
| default-dispatcher { | ||
| executor = "fork-join-executor" | ||
| fork-join-executor { | ||
| parallelism-min = 8 | ||
| parallelism-factor = 2.0 | ||
| parallelism-max = 8 | ||
| } | ||
| } | ||
| serialize-creators = off | ||
| serialize-messages = off | ||
| } | ||
| persistence { | ||
| publish-confirmations = on | ||
| publish-plugin-commands = on | ||
| journal { | ||
| plugin = "inmemory-journal" | ||
| } | ||
| snapshot-store.plugin = "inmemory-snapshot-store" | ||
| } | ||
| test { | ||
| single-expect-default = 10s | ||
| timefactor = 1 | ||
| } | ||
| } | ||
| inmemory-read-journal { | ||
| refresh-interval = "10ms" | ||
| max-buffer-size = "1000" | ||
| } | ||
| | ||
| bounded.eventmaterializers.publish = true | ||
| | ||
| bounded.eventmaterializers.offsetstore { | ||
| type = "inmemory" | ||
| } | ||
""".stripMargin | ||
) | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
bounded-test/src/test/scala/io/cafienne/bounded/test/TestAggregateRoot.scala
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,88 @@ | ||
/* | ||
* Copyright (C) 2016-2018 Cafienne B.V. <https://www.cafienne.io/bounded> | ||
*/ | ||
|
||
package io.cafienne.bounded.test | ||
|
||
import akka.actor.{ActorSystem, Props} | ||
import io.cafienne.bounded.{BuildInfo, RuntimeInfo} | ||
import io.cafienne.bounded.aggregate._ | ||
import io.cafienne.bounded.test.DomainProtocol.StateUpdated | ||
import io.cafienne.bounded.test.TestAggregateRoot.TestAggregateRootState | ||
|
||
import scala.collection.immutable.Seq | ||
|
||
object DomainProtocol { | ||
case class TestAggregateRootId(id: String) extends AggregateRootId { | ||
override def idAsString: String = id | ||
} | ||
|
||
case class CreateInitialState(metaData: CommandMetaData, aggregateRootId: AggregateRootId, state: String) | ||
extends DomainCommand | ||
case class InitialStateCreated(metaData: MetaData, id: AggregateRootId, state: String) extends DomainEvent | ||
|
||
case class UpdateState(metaData: CommandMetaData, aggregateRootId: AggregateRootId, state: String) | ||
extends DomainCommand | ||
case class StateUpdated(metaData: MetaData, id: AggregateRootId, state: String) extends DomainEvent | ||
|
||
case class InvalidCommand(msg: String) extends HandlingFailure | ||
case class InvalidState(msg: String) extends HandlingFailure | ||
} | ||
|
||
class TestAggregateRoot(aggregateRootId: AggregateRootId, buildInfo: BuildInfo, runtimeInfo: RuntimeInfo) | ||
extends AggregateRootActor[TestAggregateRootState] { | ||
import DomainProtocol._ | ||
|
||
implicit val bi = buildInfo | ||
implicit val ri = runtimeInfo | ||
|
||
override def aggregateId: AggregateRootId = aggregateRootId | ||
|
||
override def handleCommand(command: DomainCommand, aggregateState: Option[TestAggregateRootState]): Reply = { | ||
command match { | ||
case CreateInitialState(metaData, aggregateRootId, state) => | ||
Ok(Seq[DomainEvent](InitialStateCreated(MetaData.fromCommand(metaData), aggregateRootId, state))) | ||
case UpdateState(metaData, aggregateRootId, state) => | ||
if (aggregateState.isDefined && aggregateState.get.state.equals("new")) { | ||
Ok(Seq(StateUpdated(MetaData.fromCommand(metaData), aggregateRootId, state))) | ||
} else { | ||
Ko(InvalidState(s"The current state $aggregateState does not allow an update to $state")) | ||
} | ||
case other => Ko(new UnexpectedCommand(other)) | ||
} | ||
} | ||
|
||
override def newState(evt: DomainEvent): Option[TestAggregateRootState] = { | ||
evt match { | ||
case InitialStateCreated(metaData, id, state) => Some(TestAggregateRootState(state)) | ||
case _ => | ||
log.error("Event {} is not valid to create a new TestAggregateRootState") | ||
throw new IllegalArgumentException(s"Event $evt is not valid to create a new TestAggregateRootState") | ||
} | ||
} | ||
} | ||
|
||
object TestAggregateRoot { | ||
|
||
case class TestAggregateRootState(state: String) extends AggregateState[TestAggregateRootState] { | ||
override def update(event: DomainEvent): Option[TestAggregateRootState] = { | ||
event match { | ||
case evt: StateUpdated => | ||
Some(this.copy(state = evt.state)) | ||
case other => throw new IllegalArgumentException(s"Cannot update state based on event $other") | ||
} | ||
} | ||
} | ||
|
||
val aggregateRootTag = "ar-test" | ||
} | ||
|
||
class TestAggregateRootCreator(system: ActorSystem)(implicit buildInfo: BuildInfo, runtimeInfo: RuntimeInfo) | ||
extends AggregateRootCreator { | ||
|
||
override def props(aggregateRootId: AggregateRootId): Props = { | ||
system.log.debug("Returning new Props for {}", aggregateRootId) | ||
Props(classOf[TestAggregateRoot], aggregateRootId, buildInfo, runtimeInfo) | ||
} | ||
|
||
} |
Oops, something went wrong.