2015-06-08 12:26:19 +02:00
|
|
|
/*
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-06-08 12:26:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.persistence.query
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import akka.actor.ActorSystem
|
2015-09-10 19:48:57 +02:00
|
|
|
import akka.persistence.journal.EventSeq
|
|
|
|
|
import akka.persistence.journal.ReadEventAdapter
|
2015-06-08 12:26:19 +02:00
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike }
|
|
|
|
|
import scala.concurrent.Await
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
2015-06-08 12:26:19 +02:00
|
|
|
class PersistenceQuerySpec extends WordSpecLike with Matchers with BeforeAndAfterAll {
|
2015-06-08 12:26:19 +02:00
|
|
|
|
|
|
|
|
val eventAdaptersConfig =
|
|
|
|
|
s"""
|
2015-09-14 11:08:22 +02:00
|
|
|
|akka.persistence.query.journal.dummy {
|
2015-06-08 12:26:19 +02:00
|
|
|
| event-adapters {
|
|
|
|
|
| adapt = ${classOf[PrefixStringWithPAdapter].getCanonicalName}
|
|
|
|
|
| }
|
|
|
|
|
|}
|
|
|
|
|
""".stripMargin
|
|
|
|
|
|
|
|
|
|
"ReadJournal" must {
|
|
|
|
|
"be found by full config key" in {
|
|
|
|
|
withActorSystem() { system ⇒
|
2015-09-14 11:08:22 +02:00
|
|
|
PersistenceQuery.get(system).readJournalFor[DummyReadJournal](DummyReadJournal.Identifier)
|
2015-06-08 12:26:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"throw if unable to find query journal by config key" in {
|
|
|
|
|
withActorSystem() { system ⇒
|
|
|
|
|
intercept[IllegalArgumentException] {
|
2015-09-14 11:08:22 +02:00
|
|
|
PersistenceQuery.get(system).readJournalFor[DummyReadJournal](DummyReadJournal.Identifier + "-unknown")
|
2015-06-08 12:26:19 +02:00
|
|
|
}.getMessage should include("missing persistence read journal")
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-08 12:26:19 +02:00
|
|
|
|
2015-06-08 12:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val systemCounter = new AtomicInteger()
|
|
|
|
|
private def withActorSystem(conf: String = "")(block: ActorSystem ⇒ Unit): Unit = {
|
|
|
|
|
val config =
|
2015-09-14 11:08:22 +02:00
|
|
|
DummyReadJournalProvider.config
|
|
|
|
|
.withFallback(DummyJavaReadJournalProvider.config)
|
2015-06-08 12:26:19 +02:00
|
|
|
.withFallback(ConfigFactory.parseString(conf))
|
|
|
|
|
.withFallback(ConfigFactory.parseString(eventAdaptersConfig))
|
|
|
|
|
.withFallback(ConfigFactory.load())
|
|
|
|
|
|
|
|
|
|
val sys = ActorSystem(s"sys-${systemCounter.incrementAndGet()}", config)
|
|
|
|
|
try block(sys) finally Await.ready(sys.terminate(), 10.seconds)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object ExampleQueryModels {
|
|
|
|
|
case class OldModel(value: String) { def promote = NewModel(value) }
|
|
|
|
|
case class NewModel(value: String)
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-10 19:48:57 +02:00
|
|
|
class PrefixStringWithPAdapter extends ReadEventAdapter {
|
2015-06-08 12:26:19 +02:00
|
|
|
override def fromJournal(event: Any, manifest: String) = EventSeq.single("p-" + event)
|
2015-09-10 19:48:57 +02:00
|
|
|
}
|
2015-09-14 11:08:22 +02:00
|
|
|
|