Skip to content
Snippets Groups Projects
Commit b4fd9381 authored by Ihrig, Arvid Conrad (ari)'s avatar Ihrig, Arvid Conrad (ari)
Browse files

Integrated Pipeline: CalculationParsingEngine test tweaks

parent 2239775a
No related branches found
No related tags found
No related merge requests found
......@@ -2,22 +2,20 @@ package eu.nomad_lab.integrated_pipeline_tests
import java.nio.file.Paths
import eu.nomad_lab.TreeType
import eu.nomad_lab.integrated_pipeline.messages.{ FileParsingTask, FileTreeScanTask, InMemoryResult }
import eu.nomad_lab.integrated_pipeline.messages.InMemoryResult
import eu.nomad_lab.integrated_pipeline.stream_components.CalculationParsingEngine
import eu.nomad_lab.meta.KnownMetaInfoEnvs
import eu.nomad_lab.parsers.ParseResult.ParseResult
import eu.nomad_lab.parsers._
import org.json4s.JsonAST.{ JInt, JObject, JString }
import org.mockito.ArgumentMatchers._
import org.mockito.ArgumentMatchers.{ eq => raw }
import org.mockito.ArgumentMatchers.{ eq => raw, _ }
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import org.scalatest.WordSpec
import org.scalatest.mockito.MockitoSugar
class CalculationParsingEngineSpec extends WordSpec with MockitoSugar {
class CalculationParsingEngineSpec extends WordSpec with MockitoSugar with TestDataBuilders with MessageMatchers {
val parserInfo: JObject = JObject(
"name" -> JString("dummyParser")
......@@ -32,16 +30,8 @@ class CalculationParsingEngineSpec extends WordSpec with MockitoSugar {
)
implicit val metaInfo = KnownMetaInfoEnvs.all
val sampleTreeTask = FileTreeScanTask(
treeBasePath = Paths.get("s/foo"),
treeType = TreeType.Directory
)
val sampleParseRequest = FileParsingTask(
treeTask = sampleTreeTask,
relativePath = Paths.get("bar/gus.out"),
parserName = "dummyParser"
)
val unusedPath = Paths.get(".")
val sampleParseRequest = aFileParsingTask().withParserName("dummyParser")
class Fixture {
val dummyParserCollection = mock[ParserCollection]
......@@ -54,54 +44,62 @@ class CalculationParsingEngineSpec extends WordSpec with MockitoSugar {
when(dummyParserGenerator.name).thenReturn("dummyParser")
val worker = new CalculationParsingEngine(dummyParserCollection)
val unusedPath = Paths.get(".")
def prepareParserMockInvocation(): Unit = {
def prepareParserStandardInvocation(): Unit = {
when(dummyParser.parseExternal(any(), any(), any(), any())).thenAnswer(new Answer[ParseResult] {
override def answer(invocation: InvocationOnMock): ParseResult = {
val backend = invocation.getArgument[ParserBackendExternal](2)
backend.startedParsingSession(Some(sampleParseRequest.mainFileUri), parserInfo)
val mainFileUri = invocation.getArgument[String](0)
backend.startedParsingSession(Some(mainFileUri), parserInfo)
sampleEvents.foreach(x => x.emitOnBackend(backend))
backend.finishedParsingSession(
Some(ParseResult.ParseSkipped),
mainFileUri = Some(sampleParseRequest.mainFileUri)
mainFileUri = Some(mainFileUri)
)
ParseResult.ParseSkipped
}
})
}
def prepareParserFailureInvocation(): Unit = {
when(dummyParser.parseExternal(any(), any(), any(), any())).thenAnswer(new Answer[ParseResult] {
override def answer(invocation: InvocationOnMock): ParseResult = {
val backend = invocation.getArgument[ParserBackendExternal](2)
val mainFileUri = invocation.getArgument[String](0)
backend.startedParsingSession(Some(mainFileUri), parserInfo)
sampleEvents.foreach(x => x.emitOnBackend(backend))
throw new UnsupportedOperationException("parsing process just crashed!")
}
})
}
}
"a CalculationParser" when {
"parsing an already extracted valid calculation" should {
"record starting and finishing parse events" in new Fixture {
prepareParserMockInvocation()
val result = worker.processRequest(sampleParseRequest, unusedPath)
"record starting and finishing parse events" in {
val f = new Fixture
f.prepareParserStandardInvocation()
val result = f.worker.processRequest(sampleParseRequest, unusedPath)
assert(result.isInstanceOf[InMemoryResult], "results should have been kept in memory")
val inMemory = result.asInstanceOf[InMemoryResult]
assert(inMemory.start.nonEmpty, "expected a start event to be emitted")
assert(inMemory.end.nonEmpty, "expected a finish event to be emitted")
}
"record all emitted events in the correct order" in new Fixture {
prepareParserMockInvocation()
val result = worker.processRequest(sampleParseRequest, unusedPath)
"record all emitted events in the correct order" in {
val f = new Fixture
f.prepareParserStandardInvocation()
val result = f.worker.processRequest(sampleParseRequest, unusedPath)
assert(result.isInstanceOf[InMemoryResult], "results should have been kept in memory")
val inMemory = result.asInstanceOf[InMemoryResult]
inMemory.events.zip(sampleEvents).foreach(x => assert(x._1 == x._2))
assert(inMemory.events === sampleEvents, "emitted and recorded events do not match")
}
"handle parsing failures and return an appropriate result" in new Fixture {
when(dummyParser.parseExternal(any(), any(), any(), any())).thenAnswer(new Answer[ParseResult] {
override def answer(invocation: InvocationOnMock): ParseResult = {
val backend = invocation.getArgument[ParserBackendExternal](2)
backend.startedParsingSession(Some(sampleParseRequest.mainFileUri), parserInfo)
sampleEvents.foreach(x => x.emitOnBackend(backend))
throw new UnsupportedOperationException("parsing process just crashed!")
}
})
val result = worker.processRequest(sampleParseRequest, unusedPath)
"handle parsing failures and return an appropriate result" in {
val f = new Fixture
f.prepareParserFailureInvocation()
val result = f.worker.processRequest(sampleParseRequest, unusedPath)
assert(result.isInstanceOf[InMemoryResult], "results should have been kept in memory")
val inMemory = result.asInstanceOf[InMemoryResult]
assert(inMemory.result == ParseResult.ParseFailure)
......@@ -109,9 +107,10 @@ class CalculationParsingEngineSpec extends WordSpec with MockitoSugar {
assert(inMemory.end.get.parserErrors.children.nonEmpty, "should have error messages")
}
"gracefully fail parsing requests with unknown parsers" in new Fixture {
val anotherRequest = sampleParseRequest.copy(parserName = "nonSenseParser")
val result = worker.processRequest(anotherRequest, unusedPath)
"gracefully fail parsing requests with unknown parsers" in {
val f = new Fixture
val anotherRequest = sampleParseRequest.withParserName("nonSenseParser")
val result = f.worker.processRequest(anotherRequest, unusedPath)
assert(result.isInstanceOf[InMemoryResult], "results should have been kept in memory")
val inMemory = result.asInstanceOf[InMemoryResult]
assert(inMemory.result == ParseResult.ParseFailure, "should have a failed status")
......@@ -119,11 +118,12 @@ class CalculationParsingEngineSpec extends WordSpec with MockitoSugar {
assert(inMemory.end.get.parserErrors.children.nonEmpty, "should have error messages")
}
"specify the correct main file to the parser" in new Fixture {
when(dummyParser.parseExternal(any(), any(), any(), any())).thenReturn(ParseResult.ParseSkipped)
"specify the correct main file to the parser" in {
val f = new Fixture
when(f.dummyParser.parseExternal(any(), any(), any(), any())).thenReturn(ParseResult.ParseSkipped)
val targetPath = sampleParseRequest.treeTask.treeBasePath.resolve(sampleParseRequest.relativePath)
val result = worker.processRequest(sampleParseRequest, targetPath)
verify(dummyParser).parseExternal(any(), raw(targetPath), any(), any())
f.worker.processRequest(sampleParseRequest, targetPath)
verify(f.dummyParser).parseExternal(any(), raw(targetPath), any(), any())
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment