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

Integrated Pipeline: reworked EventLogger tests, fixed incorrect fixtures in some tests

parent 9dbc4680
package eu.nomad_lab.integrated_pipeline_tests
import eu.nomad_lab.integrated_pipeline.messages.{ CalculationParserEvent, ResultWriterEvent, TreeParserEvent }
import eu.nomad_lab.integrated_pipeline._
import eu.nomad_lab.integrated_pipeline.messages.{ CalculationParserEvent, ResultWriterEvent, TreeParserEvent }
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{ Matchers, WordSpec }
import org.scalatest.{ Matchers, fixture }
trait EventListenerBehaviour extends Matchers with MockitoSugar with TestDataBuilders { this: WordSpec =>
trait EventListenerBehaviour extends Matchers with MockitoSugar with TestDataBuilders { this: fixture.WordSpec =>
abstract class EventListenerFixture(val listener: EventListener) {
val treeParser = mock[TreeParser]
val calculationParser = mock[CalculationParsingEngine]
val resultWriter = mock[ParsingResultsProcessingManager]
}
private val sampleTreeParserEvents: Seq[TreeParserEvent] = Seq(
aTreeParserEventStart(),
......@@ -22,58 +28,58 @@ trait EventListenerBehaviour extends Matchers with MockitoSugar with TestDataBui
aResultWriterEventEnd()
)
def eventListener(fixture: () => EventListener) {
def eventListener(fixture: () => EventListenerFixture) {
"accept (multiple) registrations for each EventReporterType" in {
val listener = fixture()
(1 to 3).foreach(_ => listener.registerReporter(mock[TreeParser], None))
(1 to 3).foreach(_ => listener.registerReporter(mock[CalculationParsingEngine], None))
(1 to 3).foreach(_ => listener.registerReporter(mock[ParsingResultsProcessingManager], None))
"accept (multiple) registrations for each EventReporterType" in { () =>
val f = fixture()
(1 to 3).foreach(_ => f.listener.registerReporter(mock[TreeParser], None))
(1 to 3).foreach(_ => f.listener.registerReporter(mock[CalculationParsingEngine], None))
(1 to 3).foreach(_ => f.listener.registerReporter(mock[ParsingResultsProcessingManager], None))
succeed
}
"throw an exception if the same event reporter is registered twice" in {
val listener = fixture()
"throw an exception if the same event reporter is registered twice" in { () =>
val f = fixture()
val treeParser = mock[TreeParser]
val calculationParser = mock[CalculationParsingEngine]
val resultWriter = mock[ParsingResultsProcessingManager]
listener.registerReporter(treeParser, None)
a[IllegalArgumentException] should be thrownBy listener.registerReporter(treeParser, None)
listener.registerReporter(calculationParser, None)
a[IllegalArgumentException] should be thrownBy listener.registerReporter(calculationParser, None)
listener.registerReporter(resultWriter, None)
a[IllegalArgumentException] should be thrownBy listener.registerReporter(resultWriter, None)
f.listener.registerReporter(treeParser, None)
a[IllegalArgumentException] should be thrownBy f.listener.registerReporter(treeParser, None)
f.listener.registerReporter(calculationParser, None)
a[IllegalArgumentException] should be thrownBy f.listener.registerReporter(calculationParser, None)
f.listener.registerReporter(resultWriter, None)
a[IllegalArgumentException] should be thrownBy f.listener.registerReporter(resultWriter, None)
}
}
def eventListenerWithRegisteredTreeParser(fixture: () => EventListener, reporter: TreeParser) {
def eventListenerWithRegisteredTreeParser(fixture: () => EventListenerFixture) {
"process incoming events from the registered file tree parser normally" in {
val listener = fixture()
val id = TreeParserId(reporter.toString, reporter)
sampleTreeParserEvents.foreach { message => listener.processEvent(id, message) }
"process incoming events from the registered file tree parser normally" in { () =>
val f = fixture()
val id = TreeParserId(f.treeParser.toString, f.treeParser)
sampleTreeParserEvents.foreach { message => f.listener.processEvent(id, message) }
succeed
}
}
def eventListenerWithRegisteredCalculationParser(fixture: () => EventListener, reporter: CalculationParsingEngine) {
def eventListenerWithRegisteredCalculationParser(fixture: () => EventListenerFixture) {
"process incoming events from the registered calculation parser normally" in {
val listener = fixture()
val id = CalculationParserId(reporter.toString, reporter)
sampleCalculationParserEvents.foreach { message => listener.processEvent(id, message) }
"process incoming events from the registered calculation parser normally" in { () =>
val f = fixture()
val id = CalculationParserId(f.calculationParser.toString, f.calculationParser)
sampleCalculationParserEvents.foreach { message => f.listener.processEvent(id, message) }
succeed
}
}
def eventListenerWithRegisteredResultWriter(fixture: () => EventListener, reporter: ParsingResultsProcessingManager) {
def eventListenerWithRegisteredResultWriter(fixture: () => EventListenerFixture) {
"process incoming events from the registered result writer normally" in {
val listener = fixture()
val id = ResultWriterId(reporter.toString, reporter)
sampleResultWriterEvents.foreach { message => listener.processEvent(id, message) }
"process incoming events from the registered result writer normally" in { () =>
val f = fixture()
val id = ResultWriterId(f.resultWriter.toString, f.resultWriter)
sampleResultWriterEvents.foreach { message => f.listener.processEvent(id, message) }
succeed
}
......
package eu.nomad_lab.integrated_pipeline_tests
import eu.nomad_lab.integrated_pipeline.{ CalculationParsingEngine, EventLogger, ParsingResultsProcessingManager, TreeParser }
import org.scalatest.WordSpec
import eu.nomad_lab.integrated_pipeline._
import eu.nomad_lab.integrated_pipeline_tests.helpers.LoggingTestWrapper
import org.scalatest.mockito.MockitoSugar
class EventLoggerSpec extends WordSpec with MockitoSugar with EventListenerBehaviour {
class EventLoggerSpec extends LoggingTestWrapper with MockitoSugar with EventListenerBehaviour {
def fixture(): EventLogger = {
new EventLogger {}
}
class EventLoggerFixture(val logger: EventLogger) extends EventListenerFixture(logger)
def noRegisteredReporters(): EventLogger = {
fixture()
def noRegisteredReporters(): EventLoggerFixture = {
new EventLoggerFixture(new EventLogger {})
}
def registeredTreeParser(toRegister: TreeParser): EventLogger = {
val test = fixture()
test.registerReporter(toRegister, None)
test
def registeredTreeParser(): EventLoggerFixture = {
val f = noRegisteredReporters()
f.logger.registerReporter(f.treeParser, None)
f
}
def registeredCalculationParser(toRegister: CalculationParsingEngine): EventLogger = {
val test = fixture()
test.registerReporter(toRegister, None)
test
def registeredCalculationParser(): EventLoggerFixture = {
val f = noRegisteredReporters()
f.logger.registerReporter(f.calculationParser, None)
f
}
def registeredResultWriter(toRegister: ParsingResultsProcessingManager): EventLogger = {
val test = fixture()
test.registerReporter(toRegister, None)
test
def registeredResultWriter(): EventLoggerFixture = {
val f = noRegisteredReporters()
f.logger.registerReporter(f.resultWriter, None)
f
}
//TODO: find a nice and flexible way to test logging calls, testing the underlying SLF4J class would be too fragile
"An EventLogger" when {
"having received no registrations from reporters" should {
behave like eventListener(() => noRegisteredReporters())
}
def createFixture = () => noRegisteredReporters()
"having received a registration from a CalculationParsingEngine" should {
val reporter = mock[CalculationParsingEngine]
behave like eventListener(() => noRegisteredReporters())
behave like eventListenerWithRegisteredCalculationParser(() => registeredCalculationParser(reporter), reporter)
behave like eventListener(createFixture)
}
"having received a registration from a TreeParser" should {
val reporter = mock[TreeParser]
behave like eventListener(() => noRegisteredReporters())
behave like eventListenerWithRegisteredTreeParser(() => registeredTreeParser(reporter), reporter)
def createFixture = () => registeredTreeParser()
behave like eventListener(createFixture)
behave like eventListenerWithRegisteredTreeParser(createFixture)
}
"having received a registration from a CalculationParsingEngine" should {
def createFixture = () => registeredCalculationParser()
behave like eventListener(createFixture)
behave like eventListenerWithRegisteredCalculationParser(createFixture)
}
"having received a registration from a ResultsWriter" should {
val reporter = mock[ParsingResultsProcessingManager]
behave like eventListener(() => noRegisteredReporters())
behave like eventListenerWithRegisteredResultWriter(() => registeredResultWriter(reporter), reporter)
def createFixture = () => registeredResultWriter()
behave like eventListener(createFixture)
behave like eventListenerWithRegisteredResultWriter(createFixture)
}
}
......
package eu.nomad_lab.integrated_pipeline_tests.helpers
import org.scalatest.{ Outcome, fixture }
trait LoggingTestWrapper extends fixture.WordSpec with LoggingTestBase {
override protected[this] val appenderName: String = "ParsingModule-EndToEndTests"
def withFixture(test: OneArgTest): Outcome = {
val logMessages = addAppender()
try {
withFixture(test.toNoArgTest(logMessages))
} finally {
purgeConfiguration()
}
}
}
\ No newline at end of file
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