Skip to content

Commit

Permalink
Allow more log levels in log() Fix function. (#366)
Browse files Browse the repository at this point in the history
* Disallow unsupported levels.
* Add tests.
  • Loading branch information
blackwinter committed Sep 24, 2024
1 parent f1405ea commit 5d15e0e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
Expand Down
1 change: 1 addition & 0 deletions metafix/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
39 changes: 25 additions & 14 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -68,18 +68,29 @@ public void apply(final Metafix metafix, final Record record, final List<String>
log {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> 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<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 -> { });
Expand Down

0 comments on commit 5d15e0e

Please sign in to comment.