Commit a83825f2 authored by Ihrig, Arvid Conrad (ari)'s avatar Ihrig, Arvid Conrad (ari)
Browse files

Integrated Pipeline: started to make tests more flexible by using builders and...

Integrated Pipeline: started to make tests more flexible by using builders and matchers instead of direct case class creation and comparison
parent 1d97397d
package integrated_pipeline_tests
import java.nio.file.{ Path, Paths }
import eu.nomad_lab.TreeType
import eu.nomad_lab.TreeType.TreeType
import eu.nomad_lab.integrated_pipeline.messages.{ FileParsingTask, FileParsingTaskEndTree, FileParsingTaskStartTree, FileTreeScanTask }
import integrated_pipeline_tests.Builders._
trait TestDataBuilders {
def aFileTreeScanTask() = BuilderFileTreeScanTask()
def aFileParsingTask() = BuilderFileParsingTask()
def aFileParsingTaskStartTree() = BuilderFileParsingTaskStartTree()
def aFileParsingTaskEndTree() = BuilderFileParsingTaskEndTree()
}
object Builders {
private def dummyFileTreeScanTask = FileTreeScanTask(Paths.get(""), TreeType.Directory)
case class BuilderFileTreeScanTask(
treeBasePath: Path = Paths.get(""),
treeType: TreeType = TreeType.Zip
) {
def withBasePath(path: Path) = copy(treeBasePath = path)
def withBasePath(path: String) = copy(treeBasePath = Paths.get(path))
def withTreeType(newType: TreeType) = copy(treeType = newType)
def build() = FileTreeScanTask(
treeBasePath = treeBasePath,
treeType = treeType
)
}
case class BuilderFileParsingTask(
private val treeTask: FileTreeScanTask = dummyFileTreeScanTask,
private val relativePath: Path = Paths.get(""),
private val parserName: String = "noParser"
) {
def withTreeTask(task: FileTreeScanTask) = copy(treeTask = task)
def withRelativePath(path: Path) = copy(relativePath = path)
def withRelativePath(path: String) = copy(relativePath = Paths.get(path))
def withParserName(name: String) = copy(parserName = name)
def build() = FileParsingTask(
treeTask = treeTask,
relativePath = relativePath,
parserName = parserName
)
}
case class BuilderFileParsingTaskStartTree(
private val treeTask: FileTreeScanTask = dummyFileTreeScanTask
) {
def withTreeTask(task: FileTreeScanTask) = copy(treeTask = task)
def build() = FileParsingTaskStartTree(treeTask = treeTask)
}
case class BuilderFileParsingTaskEndTree(
private val treeTask: FileTreeScanTask = dummyFileTreeScanTask
) {
def withTreeTask(task: FileTreeScanTask) = copy(treeTask = task)
def build() = FileParsingTaskEndTree(treeTask = treeTask)
}
}
package integrated_pipeline_tests
import java.nio.file.{ Path, Paths }
import eu.nomad_lab.integrated_pipeline.messages.{ FileParsingTask, FileParsingTaskSignal, FileTreeScanTask }
import org.scalatest.matchers.{ HavePropertyMatchResult, HavePropertyMatcher }
trait MessageMatchers extends FileTreeTaskMatchers with FileParsingTaskMatchers
object MessageMatchers extends MessageMatchers
trait FileTreeTaskMatchers {
def basePath(expectedValue: Path) =
new HavePropertyMatcher[FileTreeScanTask, Path] {
def apply(test: FileTreeScanTask) =
HavePropertyMatchResult(
test.treeBasePath == expectedValue,
"tree base path",
expectedValue,
test.treeBasePath
)
}
}
trait FileParsingTaskMatchers {
def treeTask(expectedValue: FileTreeScanTask) =
new HavePropertyMatcher[FileParsingTaskSignal, FileTreeScanTask] {
def apply(test: FileParsingTaskSignal) =
HavePropertyMatchResult(
test.treeTask == expectedValue,
"parent tree task",
expectedValue,
test.treeTask
)
}
def relativePath(expectedValue: Path) =
new HavePropertyMatcher[FileParsingTask, Path] {
def apply(test: FileParsingTask) =
HavePropertyMatchResult(
test.relativePath == expectedValue,
"relative file path",
expectedValue,
test.relativePath
)
}
def relativePath(expectedValue: String) =
new HavePropertyMatcher[FileParsingTask, Path] {
def apply(test: FileParsingTask) =
HavePropertyMatchResult(
test.relativePath == Paths.get(expectedValue),
"relative file path",
Paths.get(expectedValue),
test.relativePath
)
}
}
\ No newline at end of file
......@@ -11,12 +11,15 @@ import eu.nomad_lab.integrated_pipeline.stream_components.{ ParsingRequestGenera
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import org.scalatest.WordSpec
import org.scalatest.{ Matchers, WordSpec }
import org.scalatest.mockito.MockitoSugar
import scala.collection.immutable
import scala.concurrent.duration._
class TreeParserFlowSpec extends WordSpec with MockitoSugar {
class TreeParserFlowSpec extends WordSpec with MockitoSugar with Matchers with TestDataBuilders with MessageMatchers {
val timeOut: FiniteDuration = 1.seconds
/**
* Test Fixture
......@@ -26,10 +29,11 @@ class TreeParserFlowSpec extends WordSpec with MockitoSugar {
def getGeneratorDummy(request: FileTreeScanTask): ParsingRequestGenerator = {
val generator = mock[ParsingRequestGenerator]
var extracted = 0
val baseTask = aFileParsingTask().withTreeTask(request)
val dummyIterator = Iterator.apply(
FileParsingTask(request, Paths.get("file1"), "fooParser"),
FileParsingTask(request, Paths.get("file2"), "fooParser"),
FileParsingTask(request, Paths.get("file3"), "fooParser")
baseTask.withRelativePath("file1").build(),
baseTask.withRelativePath("file2").build(),
baseTask.withRelativePath("file3").build()
)
when(generator.hasNext).thenAnswer(new Answer[Boolean] {
def answer(x: InvocationOnMock) = dummyIterator.hasNext
......@@ -60,6 +64,11 @@ class TreeParserFlowSpec extends WordSpec with MockitoSugar {
}
)
val path1 = "/foo/archive1"
val path2 = "/foo/archive2"
val input1 = aFileTreeScanTask().withBasePath(path1).build()
val input2 = aFileTreeScanTask().withBasePath(path2).build()
def sampleInput(baseName: String) = FileTreeScanTask(
treeBasePath = Paths.get(s"/foo/$baseName"),
treeType = TreeType.Zip
......@@ -72,23 +81,21 @@ class TreeParserFlowSpec extends WordSpec with MockitoSugar {
"receiving only a single file tree parsing request from the source" should {
"emit a start tree signal before sending any file parsing tasks" in new GraphWithDummy {
val input = sampleInput("archive1")
probeOutput.ensureSubscription().request(1)
probeInput.sendNext(input)
probeOutput.expectNext(FileParsingTaskStartTree(input))
probeInput.sendNext(input1)
val result = probeOutput.expectNext()
result shouldBe a[FileParsingTaskStartTree]
result should have(treeTask(input1))
}
"emit parse requests for all potential calculations in the file tree" in new GraphWithDummy {
val input = sampleInput("archive1")
probeOutput.ensureSubscription().request(4)
probeInput.sendNext(input)
probeOutput.expectNext(FileParsingTaskStartTree(input))
val expectedRequests = immutable.Seq(
FileParsingTask(input, Paths.get("file1"), "fooParser"),
FileParsingTask(input, Paths.get("file2"), "fooParser"),
FileParsingTask(input, Paths.get("file3"), "fooParser")
)
probeOutput.expectNextUnorderedN(expectedRequests)
probeInput.sendNext(input1).sendComplete()
val results = probeOutput.ensureSubscription().toStrict(timeOut).
filter(_.isInstanceOf[FileParsingTask]).map(_.asInstanceOf[FileParsingTask])
results should have size 3
exactly(1, results) should have(relativePath("file1"))
exactly(1, results) should have(relativePath("file2"))
exactly(1, results) should have(relativePath("file3"))
}
"emit an end tree signal after sending all file parsing tasks" in new GraphWithDummy {
......@@ -127,7 +134,6 @@ class TreeParserFlowSpec extends WordSpec with MockitoSugar {
"receiving multiple file tree parsing requests from the source" should {
"emit parse requests grouped by file tree request" in new GraphWithDummy {
val (input1, input2) = (sampleInput("archive1"), sampleInput("archive2"))
probeOutput.ensureSubscription().request(10)
probeInput.sendNext(input1).sendNext(input2)
val responses = probeOutput.expectNextN(10)
......@@ -146,7 +152,6 @@ class TreeParserFlowSpec extends WordSpec with MockitoSugar {
}
"completes once all elements are emitted if the upstream completes before the trees are finished" in new GraphWithDummy {
val (input1, input2) = (sampleInput("archive1"), sampleInput("archive2"))
probeInput.sendNext(input1).sendNext(input2).sendComplete()
probeOutput.ensureSubscription().request(11)
probeOutput.expectNextN(10)
......@@ -154,7 +159,6 @@ class TreeParserFlowSpec extends WordSpec with MockitoSugar {
}
"completes once all elements are emitted if the upstream completes after the trees are finished" in new GraphWithDummy {
val (input1, input2) = (sampleInput("archive1"), sampleInput("archive2"))
probeInput.sendNext(input1).sendNext(input2)
probeOutput.ensureSubscription().request(11)
probeOutput.expectNextN(10)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment