Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Karate-Tools] Sonar Issues Fix 2 #9

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.slf4j.LoggerFactory;

public abstract class AbstractClientTest {

protected ListAppender<ILoggingEvent> logWatcher;

protected Level defaultLogLevel;

@BeforeEach
void beforeEach() {
defaultLogLevel = ((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).getLevel();
logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).addAppender(logWatcher);
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).setLevel(Level.INFO);
}

@AfterEach
void afterEach() {
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).detachAppender(logWatcher);
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).setLevel(defaultLogLevel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -28,13 +29,21 @@ class KarateClientLoggerTest {

protected ListAppender<ILoggingEvent> logWatcher;

protected Level defaultLogLevel;

@BeforeEach
void setup() {
defaultLogLevel = ((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).getLevel();
logWatcher = new ListAppender<>();
logWatcher.start();
final Logger logger = (Logger) LoggerFactory.getLogger(KarateClientLogger.class);
logger.setLevel(Level.TRACE);
logger.addAppender(logWatcher);
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).setLevel(Level.TRACE);
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).addAppender(logWatcher);
}

@AfterEach
void afterEach() {
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).detachAppender(logWatcher);
((Logger) LoggerFactory.getLogger(KarateClientLogger.class)).setLevel(defaultLogLevel);
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.intuit.karate.core.ScenarioFileReader;
import com.intuit.karate.graal.JsValue;
import com.intuit.karate.http.HttpClientFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
Expand All @@ -24,14 +25,23 @@ class SystemPropertiesParserIT {

protected ListAppender<ILoggingEvent> logWatcher;

protected Level defaultLogLevel;

@BeforeEach
protected void beforeEach() {
defaultLogLevel = ((Logger) LoggerFactory.getLogger(SystemPropertiesParser.class)).getLevel();
logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(SystemPropertiesParser.class)).addAppender(logWatcher);
((Logger) LoggerFactory.getLogger(SystemPropertiesParser.class)).setLevel(Level.DEBUG);
}

@AfterEach
protected void afterEach() {
((Logger) LoggerFactory.getLogger(SystemPropertiesParser.class)).detachAppender(logWatcher);
((Logger) LoggerFactory.getLogger(SystemPropertiesParser.class)).setLevel(defaultLogLevel);
}

@Nested
class ParseConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The Class OpenApiGenerator.
Expand Down Expand Up @@ -71,9 +69,9 @@ public List<Path> execute() {
outputs.addAll(this.executeMockData());
}
} catch (final IllegalArgumentException e) {
ANSILogger.error(e.getMessage());
OpenApiGeneratorANSILogger.error(e.getMessage());
}
outputs.forEach(p -> ANSILogger.info("Generated " + rootPath.relativize(p)));
outputs.forEach(p -> OpenApiGeneratorANSILogger.info("Generated " + rootPath.relativize(p)));
return outputs;
}

Expand All @@ -93,7 +91,7 @@ public void configureOptions() {
protected List<Path> executeOperations() {
final Path rootPath = Paths.get(this.targetFolder);
final List<Path> outputs = new ArrayList<>();
ANSILogger.info("Generating Operations ...");
OpenApiGeneratorANSILogger.info("Generating Operations ...");
outputs.addAll(OpenApiGenerators.generateOperations(rootPath, this.options.getArtifact(), this.options.getOperations()));
KarateConfig.updateKarateUrls(this.targetFolder, this.options.getArtifact());
return outputs;
Expand All @@ -107,7 +105,7 @@ protected List<Path> executeOperations() {
protected List<Path> executeSmokeTests() {
final Path rootPath = Paths.get(this.targetFolder);
final List<Path> outputs = new ArrayList<>();
ANSILogger.info("Generating Smoke Tests ...");
OpenApiGeneratorANSILogger.info("Generating Smoke Tests ...");
outputs.addAll(OpenApiGenerators.generateSmokeTests(rootPath, this.options.getArtifact(), this.options.getOperations(),
this.options.getOpenApi()));
return outputs;
Expand All @@ -121,8 +119,8 @@ protected List<Path> executeSmokeTests() {
protected List<Path> executeFunctionalTest() {
final Path rootPath = Paths.get(this.targetFolder);
final List<Path> outputs = new ArrayList<>();
ANSILogger.info("Generating Functional Tests ...");
ANSILogger.info("Generating Functional Test Case [" + this.options.getTestName() + "] ...");
OpenApiGeneratorANSILogger.info("Generating Functional Tests ...");
OpenApiGeneratorANSILogger.info("Generating Functional Test Case [" + this.options.getTestName() + "] ...");
outputs.addAll(OpenApiGenerators.generateFunctionalTest(rootPath,
this.options.getArtifact(), this.options.getTestName(), this.options.getInlineMocks(), this.options.getOperationsResponses(),
this.options.getOpenApi()));
Expand All @@ -137,69 +135,11 @@ protected List<Path> executeFunctionalTest() {
protected List<Path> executeMockData() {
final Path rootPath = Paths.get(this.targetFolder);
final List<Path> outputs = new ArrayList<>();
ANSILogger.info("Generating Mock Data ...");
ANSILogger.info("Generating Mock Data ...");
OpenApiGeneratorANSILogger.info("Generating Mock Data ...");
OpenApiGeneratorANSILogger.info("Generating Mock Data ...");
outputs.addAll(OpenApiGenerators.generateMockData(rootPath,
this.options.getArtifact(), this.options.getInlineMocks(), this.options.getInlineMocksFunctionalArtifact(),
this.options.getTestName(), this.options.getOperationsResponses(), this.options.getOpenApi()));
return outputs;
}

/**
* The Class ANSILogger.
*/
protected static class ANSILogger {

/** The Constant OPEN_API_GENERATOR_STDOUT. */
protected static final Logger OPEN_API_GENERATOR_STDOUT = LoggerFactory.getLogger("OpenApiGenerator");

/** The Constant ANSI_RESET. */
protected static final String ANSI_RESET = "\u001B[0m";

/** The Constant ANSI_BLUE. */
protected static final String ANSI_BLUE = "\u001B[34m";

/** The Constant ANSI_YELLOW. */
protected static final String ANSI_YELLOW = "\u001B[33m";

/** The Constant ANSI_RED. */
protected static final String ANSI_RED = "\u001B[31m";

/** The Constant LOG_FORMAT. */
protected static final String LOG_FORMAT = "\n{}{}{}{}\n";

/**
* Instantiates a new ANSI logger.
*/
protected ANSILogger() {
// Empty
}

/**
* Info.
*
* @param message the message
*/
public static void info(final String message) {
OPEN_API_GENERATOR_STDOUT.info(LOG_FORMAT, ANSI_BLUE, "INFO - ", message, ANSI_RESET);
}

/**
* Warn.
*
* @param message the message
*/
public static void warn(final String message) {
OPEN_API_GENERATOR_STDOUT.debug(LOG_FORMAT, ANSI_YELLOW, "WARN - ", message, ANSI_RESET);
}

/**
* Error.
*
* @param message the message
*/
public static void error(final String message) {
OPEN_API_GENERATOR_STDOUT.error(LOG_FORMAT, ANSI_RED, "ERROR - ", message, ANSI_RESET);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev.inditex.karate.openapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The Class ANSILogger.
*/
public class OpenApiGeneratorANSILogger {

/** The Constant OPEN_API_GENERATOR_STDOUT. */
protected static final Logger OPEN_API_GENERATOR_STDOUT = LoggerFactory.getLogger("OpenApiGenerator");

/** The Constant ANSI_RESET. */
protected static final String ANSI_RESET = "\u001B[0m";

/** The Constant ANSI_BLUE. */
protected static final String ANSI_BLUE = "\u001B[34m";

/** The Constant ANSI_YELLOW. */
protected static final String ANSI_YELLOW = "\u001B[33m";

/** The Constant ANSI_RED. */
protected static final String ANSI_RED = "\u001B[31m";

/** The Constant LOG_FORMAT. */
protected static final String LOG_FORMAT = "\n{}{}{}{}\n";

/**
* Instantiates a new ANSI logger.
*/
protected OpenApiGeneratorANSILogger() {
// Empty
}

/**
* Info.
*
* @param message the message
*/
public static void info(final String message) {
OPEN_API_GENERATOR_STDOUT.info(LOG_FORMAT, ANSI_BLUE, "INFO - ", message, ANSI_RESET);
}

/**
* Warn.
*
* @param message the message
*/
public static void warn(final String message) {
OPEN_API_GENERATOR_STDOUT.warn(LOG_FORMAT, ANSI_YELLOW, "WARN - ", message, ANSI_RESET);
}

/**
* Error.
*
* @param message the message
*/
public static void error(final String message) {
OPEN_API_GENERATOR_STDOUT.error(LOG_FORMAT, ANSI_RED, "ERROR - ", message, ANSI_RESET);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import dev.inditex.karate.openapi.OpenApiGenerator.ANSILogger;
import dev.inditex.karate.openapi.data.MavenArtifact;
import dev.inditex.karate.openapi.data.MavenUtils;
import dev.inditex.karate.openapi.data.OpenApiParser;
Expand Down Expand Up @@ -320,7 +319,8 @@ protected boolean isOpenApi(final File dir, final String name) {
final Map<String, Object> yaml = yamlMapper.readValue(content, new TypeReference<Map<String, Object>>() {});
return yaml.containsKey("openapi");
} catch (final IOException e) {
ANSILogger.warn(String.format("Not an Open Api file %s in folder %s %n Exception [%s]", name, dir, e.getMessage()));
OpenApiGeneratorANSILogger.warn(
String.format("Not an Open Api file %s in folder %s %n Exception [%s]", name, dir, e.getMessage()));
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package dev.inditex.karate.openapi;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

class OpenApiGeneratorANSILoggerTest {

protected ListAppender<ILoggingEvent> logWatcher;

protected Level defaultLogLevel;

@BeforeEach
void beforeEach() {
defaultLogLevel = ((Logger) LoggerFactory.getLogger("OpenApiGenerator")).getLevel();
logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger("OpenApiGenerator")).addAppender(logWatcher);
((Logger) LoggerFactory.getLogger("OpenApiGenerator")).setLevel(Level.INFO);
}

@AfterEach
void afterEach() {
((Logger) LoggerFactory.getLogger("OpenApiGenerator")).detachAppender(logWatcher);
((Logger) LoggerFactory.getLogger("OpenApiGenerator")).setLevel(defaultLogLevel);
}

@Nested
class Constructor {
@Test
void when_instance_expect_no_exception() {
assertThatCode(OpenApiGeneratorANSILogger::new).doesNotThrowAnyException();
}
}

@Nested
class Info {
@Test
void when_message_expect_delegate_to_logger_with_color() {
final String message = "message";
OpenApiGeneratorANSILogger.info(message);

assertThat(logWatcher.list).hasSize(1).extracting("level").containsOnly(Level.INFO);
assertThat(logWatcher.list.get(0).getFormattedMessage())
.contains(OpenApiGeneratorANSILogger.ANSI_BLUE + "INFO - " + message + OpenApiGeneratorANSILogger.ANSI_RESET);

}
}

@Nested
class Warn {
@Test
void when_message_expect_delegate_to_logger_with_color() {
final String message = "message";
OpenApiGeneratorANSILogger.warn(message);

assertThat(logWatcher.list).hasSize(1).extracting("level").containsOnly(Level.WARN);
assertThat(logWatcher.list.get(0).getFormattedMessage())
.contains(OpenApiGeneratorANSILogger.ANSI_YELLOW + "WARN - " + message + OpenApiGeneratorANSILogger.ANSI_RESET);
}
}

@Nested
class Error {
@Test
void when_message_expect_delegate_to_logger_with_color() {
final String message = "message";
OpenApiGeneratorANSILogger.error(message);

assertThat(logWatcher.list).hasSize(1).extracting("level").containsOnly(Level.ERROR);
assertThat(logWatcher.list.get(0).getFormattedMessage())
.contains(OpenApiGeneratorANSILogger.ANSI_RED + "ERROR - " + message + OpenApiGeneratorANSILogger.ANSI_RESET);
}
}

}
Loading
Loading