Skip to content

Commit

Permalink
Upgrade to junit 5 + JerseyLogbackFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
amanteaux committed Nov 21, 2024
1 parent c1c5019 commit 4865e55
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 100 deletions.
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,6 @@
</dependency>

<!-- unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-db-test</artifactId>
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/coreoz/jersey/JerseyLogbackFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.coreoz.jersey;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.turbo.TurboFilter;
import ch.qos.logback.core.spi.FilterReply;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.slf4j.Marker;

import java.util.Arrays;

/**
* Change the log level of Jersey logs in case of problem when writing HTTP responses.
* Indeed, it is not an application error, these errors do not concern the application,
* and no application solution can be provided.
*
* To avoid raising an alert, these error logs are rewritten as warning logs.
*
* This code should be removed once https://github.com/eclipse-ee4j/jersey/issues/4420#issuecomment-1332087576
* is accepted.
*/
public class JerseyLogbackFilter extends TurboFilter {
@Override
public FilterReply decide(Marker marker, ch.qos.logback.classic.Logger logger, Level level, String format, Object[] params, Throwable t) {
if (level == Level.ERROR
&& "org.glassfish.jersey.server.ServerRuntime$Responder".equals(logger.getName())
&& (LocalizationMessages.ERROR_WRITING_RESPONSE_ENTITY().equals(format)
|| LocalizationMessages.ERROR_COMMITTING_OUTPUT_STREAM().equals(format)
|| LocalizationMessages.ERROR_WRITING_RESPONSE_ENTITY_CHUNK().equals(format)
|| LocalizationMessages.ERROR_CLOSING_COMMIT_OUTPUT_STREAM().equals(format)
)) {
Object[] paramsWithThrowable = params;
if (t != null) {
if (params != null) {
paramsWithThrowable = Arrays.copyOf(params, params.length + 1);
paramsWithThrowable[params.length] = t;
} else {
paramsWithThrowable = new Object[] { t };
}
}
// rewrite error log as warning log
logger.warn(marker, format, paramsWithThrowable);
return FilterReply.DENY;
}
return FilterReply.NEUTRAL;
}
}
8 changes: 5 additions & 3 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<turboFilter class="com.coreoz.jersey.JerseyLogbackFilter" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.coreoz" level="DEBUG"/>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
</configuration>
4 changes: 2 additions & 2 deletions src/test/java/com/coreoz/SampleTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.coreoz;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* A unit test sample.
Expand All @@ -11,7 +11,7 @@
* - Testing a function that does not have a lot of dependencies
*
* To test something that has interactions with the database, or not only one function but a chain of services,
* integration tests are preferred. See {@link SampleIntegrationTest} for an example.
* integration tests are preferred. See {@link com.coreoz.integration.SampleIntegrationTest} for an example.
*
* Once there are other unit tests in the project, this sample should be deleted.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/coreoz/guice/TestModule.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.coreoz.guice;

import com.coreoz.plume.db.guice.GuiceDbTestModule;
import com.coreoz.plume.services.time.TimeProvider;
import com.coreoz.plume.mocks.MockedClock;
import com.google.inject.AbstractModule;
import com.google.inject.util.Modules;

import java.time.Clock;

/**
* The Guice module that will be used for integration tests.
*
* In this module, it is possible to override the behaviors of some services as it is shown with the {@link TimeProvider}
* In this module, it is possible to override the behaviors of some services as it is shown with the {@link MockedClock}
* module.
*/
public class TestModule extends AbstractModule {
Expand All @@ -17,7 +19,7 @@ protected void configure() {
install(Modules.override(new ApplicationModule()).with(new AbstractModule() {
@Override
protected void configure() {
bind(TimeProvider.class).to(TestableTimeProvider.class);
bind(Clock.class).to(MockedClock.class);
}
}));
install(new GuiceDbTestModule());
Expand Down
71 changes: 0 additions & 71 deletions src/test/java/com/coreoz/guice/TestableTimeProvider.java

This file was deleted.

18 changes: 7 additions & 11 deletions src/test/java/com/coreoz/integration/SampleIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package com.coreoz.integration;

import com.carlosbecker.guice.GuiceModules;
import com.carlosbecker.guice.GuiceTestRunner;
import com.coreoz.guice.TestModule;
import com.coreoz.test.GuiceTest;
import com.coreoz.webservices.api.ExampleWs;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;

import jakarta.inject.Inject;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* An integration test sample.
*
* This tests differs from an unit tests, cf {@link SampleTest}, because:
* This tests differs from an unit tests, cf {@link com.coreoz.SampleTest}, because:
* - It will initialize and rely on the dependency injection, see {@link TestModule} for tests specific overrides
* - Other services can be referenced for this tests
* - These other services can be altered for tests, see {@link TimeProviderForTest} for an example
* - These other services can be altered for tests, see {@link com.coreoz.plume.mocks.MockedClock} for an example
* - If a database is used in the project, an H2 in memory database will be available to run queries and verify that data is correctly being inserted/updated in the database
* - The H2 in memory database will be created by playing Flyway initialization scripts: these scripts must be correctly setup
*
* Integration tests are a great tool to test the whole chain of services with one automated test.
* Although, to test intensively a function, a unit test is preferred, see {@link TimeProviderForTest} for an example.
* Although, to test intensively a function, a unit test is preferred, see {@link com.coreoz.SampleTest} for an example.
*
* Once there are other integration tests in the project, this sample should be deleted.
*/
@RunWith(GuiceTestRunner.class)
@GuiceModules(TestModule.class)
@GuiceTest(TestModule.class)
public class SampleIntegrationTest {
@Inject
ExampleWs exampleWs;
Expand Down

0 comments on commit 4865e55

Please sign in to comment.