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

Integrated Pipeline: added unit-tests for logging TreeParserEvents

parent 654f6740
......@@ -11,7 +11,7 @@
</Console>
</Appenders>
<Loggers>
<Logger name="eu.nomad_lab">
<Logger name="eu.nomad_lab" level="debug">
<MarkerFilter marker="parsing-manager" onMatch="NEUTRAL" onMismatch="DENY"/>
<AppenderRef ref="Console"/>
</Logger>
......
......@@ -2,8 +2,8 @@ package eu.nomad_lab.integrated_pipeline_end_to_end_tests
import eu.nomad_lab.TreeType
import eu.nomad_lab.integrated_pipeline.{ Main, OutputType }
import eu.nomad_lab.integrated_pipeline_tests.helpers.CustomMatchers
import eu.nomad_lab.integrated_pipeline_tests.{ EndToEnd, PendingEndToEnd }
import org.scalatest.Matchers
/**
* This Test Suite contains the End-to-End tests for the Integrated Pipeline that are not yet
......@@ -12,7 +12,7 @@ import org.scalatest.Matchers
* These tests validate some basic assumptions on the generated output, but the queries made are
* simple enough to assume that the tests are independent of the parser versions.
*/
class UnderDevelopment extends LoggingTestAsyncWrapper with Matchers {
class UnderDevelopment extends LoggingTestAsyncWrapper with CustomMatchers {
"The Integrated Pipeline" when {
"invoked in console mode" should {
......@@ -20,8 +20,8 @@ class UnderDevelopment extends LoggingTestAsyncWrapper with Matchers {
val f = new NonExistingTreeFixture(TreeType.Directory, OutputType.Json)
val finished = Main.mainWithFuture(f.generateConsoleArgumentList(Some(info)))
finished.map { _ =>
atLeast(1, log.getLogMessages()) should (include("purely-imaginary")
and include("not exist") and include("skipping file tree"))
atLeast(1, log.messages()) should (startWith("ERROR") and
includeAll("purely-imaginary", "not exist", "skipping file tree"))
}
}
}
......
package eu.nomad_lab.integrated_pipeline_tests
import eu.nomad_lab.TreeType
import eu.nomad_lab.integrated_pipeline._
import eu.nomad_lab.integrated_pipeline_tests.helpers.LoggingTestWrapper
import eu.nomad_lab.integrated_pipeline_tests.helpers.{ CustomMatchers, LoggingTestWrapper }
import org.scalatest.mockito.MockitoSugar
class EventLoggerSpec extends LoggingTestWrapper with MockitoSugar with EventListenerBehaviour {
class EventLoggerSpec extends LoggingTestWrapper with MockitoSugar with EventListenerBehaviour
with CustomMatchers with TestDataBuilders {
class EventLoggerFixture(val logger: EventLogger) extends EventListenerFixture(logger)
class EventLoggerFixture(val logger: EventLogger) extends EventListenerFixture(logger) {
var treeParserId: Option[TreeParserId] = None
var calculationParserId: Option[CalculationParserId] = None
var resultWriterId: Option[ResultWriterId] = None
}
def noRegisteredReporters(): EventLoggerFixture = {
new EventLoggerFixture(new EventLogger {})
......@@ -14,24 +20,22 @@ class EventLoggerSpec extends LoggingTestWrapper with MockitoSugar with EventLis
def registeredTreeParser(): EventLoggerFixture = {
val f = noRegisteredReporters()
f.logger.registerReporter(f.treeParser, None)
f.treeParserId = Some(f.logger.registerReporter(f.treeParser, None))
f
}
def registeredCalculationParser(): EventLoggerFixture = {
val f = noRegisteredReporters()
f.logger.registerReporter(f.calculationParser, None)
f.calculationParserId = Some(f.logger.registerReporter(f.calculationParser, None))
f
}
def registeredResultWriter(): EventLoggerFixture = {
val f = noRegisteredReporters()
f.logger.registerReporter(f.resultWriter, None)
f.resultWriterId = Some(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 {
def createFixture = () => noRegisteredReporters()
......@@ -44,6 +48,41 @@ class EventLoggerSpec extends LoggingTestWrapper with MockitoSugar with EventLis
behave like eventListener(createFixture)
behave like eventListenerWithRegisteredTreeParser(createFixture)
"log file tree started events as info messages" in { log =>
val f = createFixture()
val message = aTreeParserEventStart().withBasePath("/foo/bar").withTreeType(TreeType.Zip)
f.logger.processEvent(f.treeParserId.get, message)
exactly(1, log.messages()) should (startWith("INFO") and
includeAll("/foo/bar", "start", "Zip"))
}
"log file tree candidate events as debug messages" in { log =>
val f = createFixture()
val message = aTreeParserEventCandidate().withBasePath("/foo/bar").
withRelativePath("magic/file.out").withParser("magicTestParser")
f.logger.processEvent(f.treeParserId.get, message)
exactly(1, log.messages()) should (startWith("DEBUG") and
includeAll("/foo/bar", "magic/file.out", "magicTestParser", "found candidate"))
}
"log file scan error events as warning messages" in { log =>
val f = createFixture()
val message = aTreeParserEventScanError().withBasePath("/foo/bar").
withRelativePath("magic/file.out").withError(new IllegalArgumentException("test crash"))
f.logger.processEvent(f.treeParserId.get, message)
exactly(1, log.messages()) should (startWith("WARN") and
includeAll("/foo/bar", "magic/file.out", "test crash", "scanning failure"))
}
"log file tree finished events as info messages" in { log =>
val f = createFixture()
val message = aTreeParserEventEnd().withBasePath("/foo/bar").
withTreeType(TreeType.Directory).withNumCandidates(42)
f.logger.processEvent(f.treeParserId.get, message)
exactly(1, log.messages()) should (startWith("INFO") and
includeAll("/foo/bar", "Directory", "42 candidate files", "finished"))
}
}
"having received a registration from a CalculationParsingEngine" should {
......
package eu.nomad_lab.integrated_pipeline_tests.helpers
import org.scalatest.Matchers
import org.scalatest.matchers.Matcher
trait CustomMatchers extends Matchers {
def includeAll(first: String, other: String*): Matcher[String] = {
other.foldLeft(include(first)) { (condition, next) => condition and include(next) }
}
}
......@@ -26,7 +26,7 @@ trait LoggingTestBase {
val rawBytes = new ByteArrayOutputStream()
val context = LoggerContext.getContext(false)
val config = context.getConfiguration
val layout = PatternLayout.newBuilder().withPattern("%d{HH:mm:ss.SSS} %-5level - %msg%n").build()
val layout = PatternLayout.newBuilder().withPattern("%-5level - %msg%n").build()
val appender = OutputStreamAppender.createAppender(layout, null, rawBytes, appenderName, false, true)
appender.start()
config.addAppender(appender)
......@@ -43,7 +43,7 @@ trait LoggingTestBase {
class LoggingOutput(private val byteStream: ByteArrayOutputStream) {
def getLogMessages(): Seq[String] = {
def messages(): Seq[String] = {
val output = byteStream.toString
byteStream.reset()
output.split(Properties.lineSeparator)
......
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