diff --git a/build.sbt b/build.sbt index 3ca3e7a..9f21568 100644 --- a/build.sbt +++ b/build.sbt @@ -6,5 +6,6 @@ scalaVersion := "2.12.4" libraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-actor" % "2.5.11", - "com.typesafe.akka" %% "akka-testkit" % "2.5.11" % Test + "com.typesafe.akka" %% "akka-testkit" % "2.5.11" % Test, + "org.scalatest" % "scalatest_2.12" % "3.0.5" % "test" ) \ No newline at end of file diff --git a/src/test/scala/com/cosmin/pipeline/PipelineSpec.scala b/src/test/scala/com/cosmin/pipeline/PipelineSpec.scala new file mode 100644 index 0000000..7719d60 --- /dev/null +++ b/src/test/scala/com/cosmin/pipeline/PipelineSpec.scala @@ -0,0 +1,41 @@ +package com.cosmin.pipeline + +import scala.util.{Failure, Success} + +class PipelineSpec extends UnitSpec { + test("Test building pipeline") { + val pipeline: Pipeline[String, String] = createDefaultPipeline + + assert(2 == pipeline.stages.size) + assertResult("input->f2") { + val stage = pipeline.stages.head + stage.execute("input".asInstanceOf[stage.In]) + } + } + + test("Test successful execution of a pipeline") { + val pipeline: Pipeline[String, String] = createDefaultPipeline + + pipeline.execute("msg") { + case Success(out) => assert("msg->f1->f2".equals(out)) + case Failure(_) => fail("Pipeline execution expected be successful") + } + } + + test("Test failed execution of a pipeline") { + val pipeline: Pipeline[String, String] = createDefaultPipeline | (s => throw new RuntimeException) + + pipeline.execute("msg") { + case Success(out) => fail("Pipeline execution expected fail") + case Failure(e) => assert(e.isInstanceOf[RuntimeException]) + } + } + + + private def createDefaultPipeline = { + val firstFilter: String => String = s => s"$s->f1" + val secondFilter: String => String = s => s"$s->f2" + + Pipeline[String, String]() | firstFilter | secondFilter + } +} diff --git a/src/test/scala/com/cosmin/pipeline/StageSpec.scala b/src/test/scala/com/cosmin/pipeline/StageSpec.scala new file mode 100644 index 0000000..adbc983 --- /dev/null +++ b/src/test/scala/com/cosmin/pipeline/StageSpec.scala @@ -0,0 +1,12 @@ +package com.cosmin.pipeline + +class StageSpec extends UnitSpec { + test("Text execute stage filter") { + val intToStringFilter: Filter[Int, String] = new Filter[Int, String] { + override def execute = i => i.toString + } + val stage = Stage(intToStringFilter) + + assertResult("1") (stage.execute(1.asInstanceOf[stage.In])) + } +} diff --git a/src/test/scala/com/cosmin/pipeline/UnitSpec.scala b/src/test/scala/com/cosmin/pipeline/UnitSpec.scala new file mode 100644 index 0000000..04cfa09 --- /dev/null +++ b/src/test/scala/com/cosmin/pipeline/UnitSpec.scala @@ -0,0 +1,6 @@ +package com.cosmin.pipeline + +import org.scalatest._ + +abstract class UnitSpec extends FunSuite with Matchers with + OptionValues with Inside with Inspectors