diff --git a/build.gradle b/build.gradle index be1d02c5..566eb558 100644 --- a/build.gradle +++ b/build.gradle @@ -49,6 +49,7 @@ subprojects { 'mockito': '2.27.0', 'requirejs': '2.3.6', 'slf4j': '1.7.21', + 'slf4j_mock': '2.3.0', 'wiremock': '2.33.2', 'xtext': '2.26.0' ] diff --git a/metafix/build.gradle b/metafix/build.gradle index e2e403e2..7aed709c 100644 --- a/metafix/build.gradle +++ b/metafix/build.gradle @@ -25,6 +25,7 @@ dependencies { testImplementation "org.eclipse.xtext:org.eclipse.xtext.xbase.testing:${versions.xtext}" testImplementation "org.junit.jupiter:junit-jupiter-api:${versions.junit_jupiter}" testImplementation "org.junit.platform:junit-platform-launcher:${versions.junit_platform}" + testImplementation "org.simplify4u:slf4j2-mock:${versions.slf4j_mock}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${versions.junit_jupiter}" diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 8eb9f4f0..aef080a0 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -24,6 +24,9 @@ import org.metafacture.metamorph.functions.URLEncode; import org.metafacture.metamorph.maps.FileMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -43,9 +46,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public enum FixMethod implements FixFunction { // checkstyle-disable-line ClassDataAbstractionCoupling|ClassFanOutComplexity // SCRIPT-LEVEL METHODS: @@ -68,18 +68,29 @@ public void apply(final Metafix metafix, final Record record, final List log { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { - final String logMessage = params.get(0); - final String logLevel = options.get("level"); // does not support Catmandu lop level option FATAL - - if (logLevel.equals("WARN")) { - LOG.warn(logMessage); - } - else if (logLevel.equals("ERROR")) { - LOG.error(logMessage); - } - else { - LOG.info(logMessage); + // does not support Catmandu log level option FATAL + + final String level = options.getOrDefault("level", "INFO"); + final Consumer consumer; + + switch (level) { + case "DEBUG": + consumer = LOG::debug; + break; + case "ERROR": + consumer = LOG::error; + break; + case "INFO": + consumer = LOG::info; + break; + case "WARN": + consumer = LOG::warn; + break; + default: + throw new IllegalArgumentException("Unsupported log level: " + level); } + + consumer.accept(params.get(0)); } }, nothing { diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java index 67f3d448..2141ed05 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java @@ -26,6 +26,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; import java.io.IOException; import java.util.Arrays; @@ -45,6 +46,9 @@ public class MetafixScriptTest { private static final String CSV_MAP = "src/test/resources/org/metafacture/metafix/maps/test.csv"; private static final String TSV_MAP = "src/test/resources/org/metafacture/metafix/maps/test.tsv"; + @Mock(name = "org.metafacture.metafix.FixMethod") + private Logger fixMethodLogger; + @Mock private StreamReceiver streamReceiver; @@ -193,6 +197,29 @@ public void shouldPutExternalFileMapWithNameAndOptions() { assertMap("put_filemap('" + TSV_MAP + "', '" + MAP_NAME + "', sep_char: '\t')", MAP_NAME); } + @Test + public void shouldLog() { + assertFix("log('test')", f -> { + Mockito.verify(fixMethodLogger).info("test"); + Mockito.verifyNoMoreInteractions(fixMethodLogger); + }); + } + + @Test + public void shouldLogWithLevel() { + assertFix("log('test', level: 'DEBUG')", f -> { + Mockito.verify(fixMethodLogger).debug("test"); + Mockito.verifyNoMoreInteractions(fixMethodLogger); + }); + } + + @Test + public void shouldNotLogWithUnsupportedLevel() { + MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "Unsupported log level: FATAL", () -> + assertFix("log('test', level: 'FATAL')", f -> { }) + ); + } + @Test public void shouldDoNothing() { assertFix("nothing()", f -> { });