1
0

use @Slf4j (+logback) for logging instead of System.out/err.println (#165)

Co-authored-by: Michael Hoennig <michael@hoennig.de>
Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/165
Reviewed-by: Marc Sandlus <marc.sandlus@hostsharing.net>
This commit is contained in:
Michael Hoennig
2025-03-19 16:21:55 +01:00
parent eb9edf1cb1
commit 4994bac101
13 changed files with 60 additions and 39 deletions

View File

@@ -1,11 +1,16 @@
package net.hostsharing.hsadminng.config;
import com.github.tomakehurst.wiremock.WireMockServer;
import net.hostsharing.hsadminng.test.LogbackLogPattern;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
@@ -13,15 +18,23 @@ import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static net.hostsharing.hsadminng.config.HttpHeadersBuilder.headers;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"server.port=0", "hsadminng.cas.server=http://localhost:8088"})
@TestPropertySource(properties = {
"server.port=0",
"hsadminng.cas.server=http://localhost:8088",
"logging.level.root=DEBUG"
})
@ActiveProfiles({"wiremock", "realCasAuthenticator"}) // IMPORTANT: To test prod config, do NOT use test profile!
@Tag("generalIntegrationTest")
@ExtendWith(OutputCaptureExtension.class)
class CasAuthenticationFilterIntegrationTest {
@Value("${local.server.port}")
@@ -37,7 +50,7 @@ class CasAuthenticationFilterIntegrationTest {
private WireMockServer wireMockServer;
@Test
public void shouldAcceptRequestWithValidCasTicket() {
public void shouldAcceptRequestWithValidCasTicket(final CapturedOutput capturedOutput) {
// given
final var username = "test-user-" + randomAlphanumeric(4);
wireMockServer.stubFor(get(urlEqualTo("/cas/p3/serviceValidate?service=" + serviceUrl + "&ticket=ST-valid"))
@@ -63,6 +76,9 @@ class CasAuthenticationFilterIntegrationTest {
// then
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("pong " + username + "\n");
// HOWTO assert log messages
assertThat(capturedOutput.getOut()).containsPattern(
LogbackLogPattern.of(LogLevel.DEBUG, RealCasAuthenticator.class, "CAS-user: " + username));
}
@Test

View File

@@ -39,8 +39,8 @@ class RestResponseEntityExceptionHandlerUnitTest {
final var errorResponse = exceptionHandler.handleConflict(givenException, givenWebRequest);
// then
assertThat(errorResponse.getBody().getStatusCode()).isEqualTo(409);
assertThat(errorResponse.getBody().getMessage()).isEqualTo("ERROR: [409] First Line");
assertThat(errorResponse.getBody()).extracting(CustomErrorResponse::getStatusCode).isEqualTo(409);
assertThat(errorResponse.getBody()).extracting(CustomErrorResponse::getMessage).isEqualTo("ERROR: [409] First Line");
}
@Test

View File

@@ -0,0 +1,21 @@
package net.hostsharing.hsadminng.test;
import ch.qos.logback.classic.pattern.TargetLengthBasedClassNameAbbreviator;
import org.springframework.boot.logging.LogLevel;
public class LogbackLogPattern {
private static final int LOGBACK_MAX_CLASSNAME_LENGTH = 36;
/**
* @return a regular expression for a log message
*/
public static CharSequence of(
final LogLevel logLevel,
final Class<?> loggingClass,
final String message) {
final var abbreviator = new TargetLengthBasedClassNameAbbreviator(LOGBACK_MAX_CLASSNAME_LENGTH);
final var shortenedClassName = abbreviator.abbreviate(loggingClass.getName());
return logLevel + " [0-9]+ .* " + shortenedClassName + " *: " + message;
}
}