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

Integrated Pipeline: refactored TreeParser test suite

parent 3fcbcfb2
......@@ -37,7 +37,7 @@ class CalculationParsingFlowSpec extends WordSpec with MockitoSugar with TestDat
val myMock = mock[CalculationParsingEngine]
when(myMock.processSignal(any())).thenAnswer(new Answer[FileParsingResult] {
override def answer(invocation: InvocationOnMock): FileParsingResult = {
Thread.sleep(3)
Thread.sleep(3) //pretend that each dummy task consumes a little time
anInMemoryResult().
withTask(invocation.getArgument[FileParsingTask](0)).
withParseResult(ParseResult.ParseSuccess).
......
......@@ -13,106 +13,102 @@ import org.scalatest.mockito.MockitoSugar
import scala.collection.JavaConverters._
class TreeParserSpec extends WordSpec
with MessageProcessorBehaviour[FileTreeScanTask, FileParsingTaskSignal]
with TestDataBuilders with MockitoSugar {
class TreeParserSpec extends WordSpec with TestDataBuilders with MockitoSugar
with MessageProcessorBehaviour[FileTreeScanTask, FileParsingTaskSignal] {
def generatorMock(task: FileTreeScanTask): ParsingTaskGenerator = {
val dummy = mock[ParsingTaskGenerator]
val data: Iterator[Either[FileScanningError, FileParsingTask]] = (1 to 3).map { i =>
Right(aFileParsingTask().withTreeTask(task).withRelativePath(s"file$i").build())
}.iterator
when(dummy.hasNext).thenAnswer {
private def argsCaptor(): ArgumentCaptor[TreeParserEvent] = ArgumentCaptor.forClass(classOf[TreeParserEvent])
private val sampleTreeTask = aFileTreeScanTask().withBasePath("/foo").build()
class Fixture(sampleData: Iterator[Either[FileScanningError, FileParsingTask]]) {
val events = mock[EventListener]
val taskGenerator = mock[ParsingTaskGenerator]
val treeParser = new TreeParser {
override val eventListener = events
override def createProcessor(task: FileTreeScanTask) = taskGenerator
override protected[this] val eventReporterName: Option[String] = Some("TreeParserSpec")
}
when(taskGenerator.hasNext).thenAnswer {
new Answer[Boolean] {
override def answer(invocation: InvocationOnMock): Boolean = data.hasNext
override def answer(invocation: InvocationOnMock): Boolean = sampleData.hasNext
}
}
when(dummy.next()).thenAnswer {
when(taskGenerator.next()).thenAnswer {
new Answer[Either[FileScanningError, FileParsingTask]] {
override def answer(invocation: InvocationOnMock): Either[FileScanningError, FileParsingTask] = {
data.next()
sampleData.next()
}
}
}
dummy
}
private def argsCaptor(): ArgumentCaptor[TreeParserEvent] = ArgumentCaptor.forClass(classOf[TreeParserEvent])
private val sampleTreeTask = aFileTreeScanTask().withBasePath("/foo").build()
def fixture(): (TreeParser, EventListener) = {
val eventListenerMock = mock[EventListener]
val test = new TreeParser {
override val eventListener = eventListenerMock
override def createProcessor(task: FileTreeScanTask) = generatorMock(task)
override protected[this] val eventReporterName: Option[String] = Some("TreeParserSpec")
}
(test, eventListenerMock)
}
def noTaskReceived(): (TreeParser, EventListener) = {
fixture()
def noTaskReceived(): Fixture = {
val data: Iterator[Either[FileScanningError, FileParsingTask]] = (1 to 3).map { i =>
Right(aFileParsingTask().withTreeTask(sampleTreeTask).withRelativePath(s"file$i").build())
}.iterator
new Fixture(data)
}
def taskReceivedNoSignalsFetched(): (TreeParser, EventListener) = {
val (f, mock) = fixture()
f.processSignal(sampleTreeTask)
(f, mock)
def taskReceivedNoSignalsFetched(): Fixture = {
val f = noTaskReceived()
f.treeParser.processSignal(sampleTreeTask)
f
}
def oneParsingTaskLeftToEmit(): (TreeParser, EventListener) = {
val (f, mock) = fixture()
f.processSignal(sampleTreeTask)
(1 to 2).foreach(_ => f.getNextSignalToEmit())
(f, mock)
def oneParsingTaskLeftToEmit(): Fixture = {
val f = taskReceivedNoSignalsFetched()
(1 to 2).foreach(_ => f.treeParser.getNextSignalToEmit())
f
}
def allParsingTasksFetched(): (TreeParser, EventListener) = {
val (f, mock) = fixture()
f.processSignal(sampleTreeTask)
(1 to 3).foreach(_ => f.getNextSignalToEmit())
(f, mock)
def allParsingTasksFetched(): Fixture = {
val f = taskReceivedNoSignalsFetched()
(1 to 3).foreach(_ => f.treeParser.getNextSignalToEmit())
f
}
def allSignalsFetched(): (TreeParser, EventListener) = {
val (f, mock) = fixture()
f.processSignal(sampleTreeTask)
(1 to 4).foreach(_ => f.getNextSignalToEmit())
(f, mock)
def allSignalsFetched(): Fixture = {
val f = taskReceivedNoSignalsFetched()
(1 to 4).foreach(_ => f.treeParser.getNextSignalToEmit())
f
}
"A TreeParser" when {
"not having received a file tree scan task" should {
behave like processorWithNoSignalReady(() => noTaskReceived()._1, Seq(aFileTreeScanTask()))
def createFixture = () => noTaskReceived()
behave like processorWithNoSignalReady(() => createFixture().treeParser, Seq(aFileTreeScanTask()))
"register with the provided EventListener when receiving the first message" in {
val (f, test) = noTaskReceived()
f.processSignal(sampleTreeTask)
verify(test).registerReporter(f, Some("TreeParserSpec"))
val f = createFixture()
f.treeParser.processSignal(sampleTreeTask)
verify(f.events).registerReporter(f.treeParser, Some("TreeParserSpec"))
}
"notify the provided EventListener when receiving a new file tree scan task" in {
import TreeParserEventMatchers._
val (f, test) = noTaskReceived()
f.processSignal(sampleTreeTask)
val f = createFixture()
f.treeParser.processSignal(sampleTreeTask)
val captor = argsCaptor()
verify(test).processEvent(any(), captor.capture())
verify(f.events).processEvent(any(), captor.capture())
captor.getValue should (be(a[TreeParserEventStart]) and have(basePath("/foo")))
}
}
"having received a new file tree scan task" should {
def createFixture = () => taskReceivedNoSignalsFetched()
behave like processorWithOutboundSignalReady(
() => taskReceivedNoSignalsFetched()._1,
() => createFixture().treeParser,
Seq(aFileTreeScanTask()),
be(a[FileParsingTask]) and have(FileParsingTaskMatchers.relativePath("file1"))
)
"emit parse requests for all potential calculations in the file tree" in {
import FileParsingTaskMatchers._
val (f, _) = taskReceivedNoSignalsFetched()
val signals = (1 to 3).map { _ => f.getNextSignalToEmit() }.collect { case x: FileParsingTask => x }
val f = createFixture()
val signals = (1 to 3).map { _ => f.treeParser.getNextSignalToEmit() }.collect { case x: FileParsingTask => x }
signals should have length 3
exactly(1, signals) should have(relativePath("file1"))
exactly(1, signals) should have(relativePath("file2"))
......@@ -121,10 +117,10 @@ class TreeParserSpec extends WordSpec
"notify the provided EventListener for each emitted candidate calculation" in {
import TreeParserEventMatchers._
val (f, test) = taskReceivedNoSignalsFetched()
(1 to 3).foreach { _ => f.getNextSignalToEmit() }
val f = createFixture()
(1 to 3).map { _ => f.treeParser.getNextSignalToEmit() }
val captor = argsCaptor()
verify(test, Mockito.atLeast(0)).processEvent(any(), captor.capture())
verify(f.events, Mockito.atLeast(0)).processEvent(any(), captor.capture())
val events = captor.getAllValues.asScala.collect { case x: TreeParserEventCandidate => x }
events should have length 3
exactly(1, events) should have(relativePath("file1"))
......@@ -134,34 +130,38 @@ class TreeParserSpec extends WordSpec
}
"having one parsing task left to emit" should {
def createFixture = () => oneParsingTaskLeftToEmit()
behave like processorWithOutboundSignalReady(
() => oneParsingTaskLeftToEmit()._1,
() => createFixture().treeParser,
Seq(aFileTreeScanTask()),
be(a[FileParsingTask]) and have(FileParsingTaskMatchers.relativePath("file3"))
)
}
"having emitted all parsing tasks, but not the end tree signal yet" should {
def createFixture = () => allParsingTasksFetched()
behave like processorWithOutboundSignalReady(
() => allParsingTasksFetched()._1,
() => createFixture().treeParser,
Seq(aFileTreeScanTask()),
be(a[FileParsingSignalEndTree]) and have(FileParsingTaskMatchers.numParsingTasks(3))
)
"emit an end tree signal with the correct number of entries" in {
import FileParsingTaskMatchers._
val (f, _) = allParsingTasksFetched()
val result = f.getNextSignalToEmit()
val f = createFixture()
val result = f.treeParser.getNextSignalToEmit()
result should be(a[FileParsingSignalEndTree])
result.asInstanceOf[FileParsingSignalEndTree] should have(treeTask(sampleTreeTask), numParsingTasks(3))
}
"notify the provided EventListener when emitting the end tree signal" in {
import TreeParserEventMatchers._
val (f, test) = allParsingTasksFetched()
f.getNextSignalToEmit()
val f = createFixture()
f.treeParser.getNextSignalToEmit()
val captor = argsCaptor()
verify(test, Mockito.atLeast(1)).processEvent(any(), captor.capture())
verify(f.events, Mockito.atLeast(1)).processEvent(any(), captor.capture())
val event = captor.getValue
event should be(a[TreeParserEventEnd])
event.asInstanceOf[TreeParserEventEnd] should have(basePath("/foo"), numCandidates(3))
......@@ -169,8 +169,10 @@ class TreeParserSpec extends WordSpec
}
"having emitted all tasks and the end tree signal" should {
def createFixture = () => allSignalsFetched()
behave like processorWithNoSignalReady(
() => allSignalsFetched()._1,
() => createFixture().treeParser,
Seq(aFileTreeScanTask())
)
}
......
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