Skip to content

Commit

Permalink
expanded PostModule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Rolfs committed Sep 28, 2014
1 parent 7b41ceb commit 6c5c177
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
16 changes: 7 additions & 9 deletions examples/src/main/scala/blog/post/PostModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ object PostModule extends AggregateRootModuleCompanion { module =>
override var state: PostState = PostState()

override def transitionFor( state: PostState ): Transition = {
case _: PostAdded => context.become( around( created orElse publishProtocol orElse unhandled("CREATED") ) )
case _: PostPublished => context.become( around( published orElse publishProtocol orElse unhandled("PUBLISHED") ) )
case _: PostAdded => context.become( around( created orElse publishProtocol ) )
case _: PostPublished => context.become( around( published orElse publishProtocol ) )
}

override def receiveCommand: Receive = around( quiescent )

import peds.akka.envelope._

val quiescent: Receive = LoggingReceive {
case GetContent(_) => sender() ! state.content
case GetContent(_) => sender() send state.content
case AddPost( id, content ) => trace.block( s"quiescent(AddPost(${id}, ${content}))" ) {
if ( !content.isIncomplete ) {
persist( PostAdded( id, content ) ) { event =>
Expand All @@ -102,7 +104,7 @@ object PostModule extends AggregateRootModuleCompanion { module =>
}

val created: Receive = LoggingReceive {
case GetContent( id ) => sender() ! state.content
case GetContent( id ) => sender() send state.content

case ChangeBody( id, body ) => persist( BodyChanged( id, body ) ) { event =>
state = accept( event )
Expand All @@ -120,11 +122,7 @@ object PostModule extends AggregateRootModuleCompanion { module =>
}

val published: Receive = LoggingReceive {
case GetContent(_) => sender() ! state.content
}

def unhandled( label: String ): Receive = {
case x => log info s">>>>> POST[${label}] UNEXPECTED MESSAGE: $x"
case GetContent(_) => sender() send state.content
}
}
}
89 changes: 86 additions & 3 deletions examples/src/test/scala/blog/post/PostModuleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import akka.testkit.TestProbe
import demesne._
import demesne.testkit.AggregateRootSpec
import org.scalatest.Tag
import peds.akka.envelope.Envelope
import peds.akka.envelope.{WorkId, MessageNumber, Envelope}
import peds.akka.publish.ReliableMessage
import peds.commons.log.Trace
import sample.blog.post._
Expand Down Expand Up @@ -51,9 +51,7 @@ class PostModuleSpec extends AggregateRootSpec[PostModuleSpec] {
val content = PostContent( author = "Damon", title = "Add Content", body = "add body content" )
val post = PostModule aggregateOf id
post ! AddPost( id, content )
// probe.expectNoMsg()
probe.expectMsgPF( max = 800.millis, hint = "post added" ) {
// case x => fail( s"recd $x" )
case ReliableMessage( _, Envelope( payload: PostAdded, _ ) ) => payload.content mustBe content
}
}
Expand All @@ -68,6 +66,91 @@ class PostModuleSpec extends AggregateRootSpec[PostModuleSpec] {
probe.expectNoMsg( 200.millis )
}

"have empty contents before use" in { fixture: Fixture =>
import fixture._

val id = PostModule.nextId
val post = PostModule aggregateOf id
post.tell( GetContent( id ), probe.ref )
probe.expectMsgPF( max = 200.millis, hint = "empty contents" ){
case Envelope( payload: PostContent, h ) => {
payload mustBe PostContent( "", "", "" )
h.messageNumber mustBe MessageNumber( 2 )
h.workId must not be WorkId.unknown

}
}
}

"have contents after posting" in { fixture: Fixture =>
import fixture._

val id = PostModule.nextId
val post = PostModule aggregateOf id
val content = PostContent( author = "Damon", title = "Contents", body = "initial contents" )

val clientProbe = TestProbe()
post ! AddPost( id, content )
post.tell( GetContent( id ), clientProbe.ref )
clientProbe.expectMsgPF( max = 200.millis, hint = "initial contents" ){
case Envelope( payload: PostContent, h ) if payload == content => true
}
}

"have changed contents after change" in { fixture: Fixture =>
import fixture._

val id = PostModule.nextId
val post = PostModule aggregateOf id
val content = PostContent( author = "Damon", title = "Contents", body = "initial contents" )
val updated = "updated contents"

val clientProbe = TestProbe()
post ! AddPost( id, content )
post ! ChangeBody( id, updated )
post.tell( GetContent( id ), clientProbe.ref )
clientProbe.expectMsgPF( max = 200.millis, hint = "changed contents" ){
case Envelope( payload: PostContent, h ) => payload mustBe content.copy( body = updated )
}
}

"have changed contents after change and published" in { fixture: Fixture =>
import fixture._

val id = PostModule.nextId
val post = PostModule aggregateOf id
val content = PostContent( author = "Damon", title = "Contents", body = "initial contents" )
val updated = "updated contents"

val clientProbe = TestProbe()
post ! AddPost( id, content )
post ! ChangeBody( id, updated )
post ! Publish( id )
post.tell( GetContent( id ), clientProbe.ref )
clientProbe.expectMsgPF( max = 200.millis, hint = "changed contents" ){
case Envelope( payload: PostContent, h ) => payload mustBe content.copy( body = updated )
}
}

"dont change contents after published" in { fixture: Fixture =>
import fixture._

val id = PostModule.nextId
val post = PostModule aggregateOf id
val content = PostContent( author = "Damon", title = "Contents", body = "initial contents" )
val updated = "updated contents"

val clientProbe = TestProbe()
post ! AddPost( id, content )
post ! ChangeBody( id, updated )
post ! Publish( id )
post ! ChangeBody( id, "BAD CONTENT" )
post.tell( GetContent( id ), clientProbe.ref )
clientProbe.expectMsgPF( max = 200.millis, hint = "changed contents" ){
case Envelope( payload: PostContent, h ) => payload mustBe content.copy( body = updated )
}
}

"follow happy path" taggedAs( HAPPY ) in { fixture: Fixture =>
import fixture._

Expand Down

0 comments on commit 6c5c177

Please sign in to comment.