Skip to content

Commit 07ecea1

Browse files
chore: Update slf4j (#284)
* Update slf4j Signed-off-by: Justin Abrahms <[email protected]> * Move to a different log testing method Signed-off-by: Justin Abrahms <[email protected]> * Fix logger in another place too Signed-off-by: Justin Abrahms <[email protected]> --------- Signed-off-by: Justin Abrahms <[email protected]>
1 parent 24c237e commit 07ecea1

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

Diff for: pom.xml

+10-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<dependency>
6060
<groupId>org.slf4j</groupId>
6161
<artifactId>slf4j-api</artifactId>
62-
<version>1.7.36</version>
62+
<version>2.0.6</version>
6363
</dependency>
6464

6565
<!-- test -->
@@ -70,13 +70,6 @@
7070
<scope>test</scope>
7171
</dependency>
7272

73-
<dependency>
74-
<groupId>uk.org.lidalia</groupId>
75-
<artifactId>slf4j-test</artifactId>
76-
<version>1.2.0</version>
77-
<scope>test</scope>
78-
</dependency>
79-
8073
<dependency>
8174
<groupId>org.assertj</groupId>
8275
<artifactId>assertj-core</artifactId>
@@ -131,6 +124,13 @@
131124
<scope>test</scope>
132125
</dependency>
133126

127+
<dependency>
128+
<groupId>org.simplify4u</groupId>
129+
<artifactId>slf4j2-mock</artifactId>
130+
<version>2.3.0</version>
131+
<scope>test</scope>
132+
</dependency>
133+
134134
<dependency>
135135
<groupId>com.google.guava</groupId>
136136
<artifactId>guava</artifactId>
@@ -228,14 +228,15 @@
228228
<ignoredUnusedDeclaredDependencies>
229229
<ignoredUnusedDeclaredDependency>com.github.spotbugs:*</ignoredUnusedDeclaredDependency>
230230
<ignoredUnusedDeclaredDependency>org.junit*</ignoredUnusedDeclaredDependency>
231+
<ignoredUnusedDeclaredDependency>org.simplify4u:slf4j2-mock*</ignoredUnusedDeclaredDependency>
231232
</ignoredUnusedDeclaredDependencies>
232233
<ignoredDependencies>
233234
<ignoredDependency>com.google.guava*</ignoredDependency>
234235
<ignoredDependency>io.cucumber*</ignoredDependency>
235236
<ignoredDependency>org.junit*</ignoredDependency>
236237
<ignoredDependency>com.google.code.findbugs*</ignoredDependency>
237238
<ignoredDependency>com.github.spotbugs*</ignoredDependency>
238-
<ignoredDependency>uk.org.lidalia:lidalia-slf4j-ext:*</ignoredDependency>
239+
<ignoredDependency> org.simplify4u:slf4j-mock-common:*</ignoredDependency>
239240
</ignoredDependencies>
240241
</configuration>
241242
</plugin>

Diff for: src/test/java/dev/openfeature/sdk/EvalContextTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import java.time.Instant;
6+
import java.time.temporal.ChronoUnit;
7+
import java.time.temporal.TemporalUnit;
68
import java.util.ArrayList;
79
import java.util.HashMap;
810
import java.util.List;
@@ -25,7 +27,7 @@ public class EvalContextTest {
2527
"values of type `boolean | string | number | datetime | structure`.")
2628
@Test void eval_context() {
2729
Map<String, Value> attributes = new HashMap<>();
28-
Instant dt = Instant.now();
30+
Instant dt = Instant.now().truncatedTo(ChronoUnit.MILLIS);
2931
attributes.put("str", new Value("test"));
3032
attributes.put("bool", new Value(true));
3133
attributes.put("int", new Value(4));
@@ -38,7 +40,7 @@ public class EvalContextTest {
3840

3941
assertEquals(4, ec.getValue("int").asInteger());
4042

41-
assertEquals(dt, ec.getValue("dt").asInstant());
43+
assertEquals(dt, ec.getValue("dt").asInstant().truncatedTo(ChronoUnit.MILLIS));
4244
}
4345

4446
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +

Diff for: src/test/java/dev/openfeature/sdk/FlagEvaluationSpecTest.java

+23-15
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,27 @@
1212
import static org.mockito.Mockito.times;
1313
import static org.mockito.Mockito.verify;
1414

15+
import java.io.Serializable;
16+
import java.util.ArrayList;
1517
import java.util.HashMap;
1618
import java.util.List;
1719
import java.util.Map;
1820

19-
import com.google.common.collect.ImmutableList;
21+
2022
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
21-
import io.cucumber.java.hu.Ha;
2223
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
2325
import org.junit.jupiter.api.Test;
2426

2527
import dev.openfeature.sdk.fixtures.HookFixtures;
26-
import uk.org.lidalia.slf4jtest.LoggingEvent;
27-
import uk.org.lidalia.slf4jtest.TestLogger;
28-
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
28+
import org.mockito.ArgumentMatchers;
29+
import org.mockito.Mockito;
30+
import org.simplify4u.slf4jmock.LoggerMock;
31+
import org.slf4j.Logger;
2932

3033
class FlagEvaluationSpecTest implements HookFixtures {
3134

32-
private static final TestLogger TEST_LOGGER = TestLoggerFactory.getTestLogger(OpenFeatureClient.class);
35+
private Logger logger;
3336

3437
private Client _client() {
3538
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
@@ -42,6 +45,15 @@ private Client _client() {
4245
api.setEvaluationContext(null);
4346
}
4447

48+
@BeforeEach void set_logger() {
49+
logger = Mockito.mock(Logger.class);
50+
LoggerMock.setMock(OpenFeatureClient.class, logger);
51+
}
52+
53+
@AfterEach void reset_logs() {
54+
LoggerMock.setMock(OpenFeatureClient.class, logger);
55+
}
56+
4557
@Specification(number="1.1.1", text="The API, and any state it maintains SHOULD exist as a global singleton, even in cases wherein multiple versions of the API are present at runtime.")
4658
@Test void global_singleton() {
4759
assertSame(OpenFeatureAPI.getInstance(), OpenFeatureAPI.getInstance());
@@ -203,20 +215,16 @@ private Client _client() {
203215

204216
@Specification(number="1.4.10", text="In the case of abnormal execution, the client SHOULD log an informative error message.")
205217
@Test void log_on_error() throws NotImplementedException {
206-
TEST_LOGGER.clear();
207218
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
208219
api.setProvider(new AlwaysBrokenProvider());
209220
Client c = api.getClient();
210221
FlagEvaluationDetails<Boolean> result = c.getBooleanDetails("test", false);
211-
assertEquals(Reason.ERROR.toString(), result.getReason());
212222

213-
ImmutableList<LoggingEvent> loggingEvents = TEST_LOGGER.getLoggingEvents();
214-
assertThat(loggingEvents.size()).isGreaterThan(0);
215-
216-
LoggingEvent event = loggingEvents.get(0);
217-
assertThat(event.getMessage()).isEqualTo("Unable to correctly evaluate flag with key '{}'");
218-
assertThat(event.getThrowable().isPresent()).isTrue();
219-
assertThat(event.getThrowable().get()).isInstanceOf(FlagNotFoundError.class);
223+
assertEquals(Reason.ERROR.toString(), result.getReason());
224+
Mockito.verify(logger).error(
225+
ArgumentMatchers.contains("Unable to correctly evaluate flag with key"),
226+
any(),
227+
ArgumentMatchers.isA(FlagNotFoundError.class));
220228
}
221229

222230
@Specification(number="1.2.2", text="The client interface MUST define a metadata member or accessor, containing an immutable name field or accessor of type string, which corresponds to the name value supplied during client creation.")

Diff for: src/test/java/dev/openfeature/sdk/ImmutableStructureTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ImmutableStructureTest {
5353

5454
@Test void MutatingGetInstantValueShouldNotChangeOriginalValue() {
5555
String KEY = "key";
56-
Instant now = Instant.now();
56+
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
5757
Map<String, Value> map = new HashMap<String, Value>() {
5858
{
5959
put(KEY, new Value(now));

Diff for: src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44

55
import dev.openfeature.sdk.fixtures.HookFixtures;
66
import org.junit.jupiter.api.*;
7-
import uk.org.lidalia.slf4jext.Level;
8-
import uk.org.lidalia.slf4jtest.*;
7+
import org.mockito.Mockito;
8+
import org.simplify4u.slf4jmock.LoggerMock;
9+
import org.slf4j.Logger;
910

1011
import static org.assertj.core.api.Assertions.assertThat;
1112
import static org.mockito.Mockito.*;
1213

1314
class OpenFeatureClientTest implements HookFixtures {
1415

15-
private static final TestLogger TEST_LOGGER = TestLoggerFactory.getTestLogger(OpenFeatureClient.class);
16+
private Logger logger;
1617

18+
@BeforeEach void set_logger() {
19+
logger = Mockito.mock(Logger.class);
20+
LoggerMock.setMock(OpenFeatureClient.class, logger);
21+
}
22+
23+
@AfterEach void reset_logs() {
24+
LoggerMock.setMock(OpenFeatureClient.class, logger);
25+
}
1726
@Test
1827
@DisplayName("should not throw exception if hook has different type argument than hookContext")
1928
void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
20-
TEST_LOGGER.clear();
2129
OpenFeatureAPI api = mock(OpenFeatureAPI.class);
2230
when(api.getProvider()).thenReturn(new DoSomethingProvider());
2331
when(api.getHooks()).thenReturn(Arrays.asList(mockBooleanHook(), mockStringHook()));
@@ -27,13 +35,16 @@ void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
2735
FlagEvaluationDetails<Boolean> actual = client.getBooleanDetails("feature key", Boolean.FALSE);
2836

2937
assertThat(actual.getValue()).isTrue();
30-
assertThat(TEST_LOGGER.getLoggingEvents()).filteredOn(event -> event.getLevel().equals(Level.ERROR)).isEmpty();
38+
// I dislike this, but given the mocking tools available, there's no way that I know of to say "no errors were logged"
39+
Mockito.verify(logger, never()).error(any());
40+
Mockito.verify(logger, never()).error(anyString(), any(Throwable.class));
41+
Mockito.verify(logger, never()).error(anyString(), any(Object.class));
42+
Mockito.verify(logger, never()).error(anyString(), any(), any());
43+
Mockito.verify(logger, never()).error(anyString(), any(), any());
3144
}
3245

3346
@Test
3447
void mergeContextTest() {
35-
TEST_LOGGER.clear();
36-
3748
String flag = "feature key";
3849
boolean defaultValue = false;
3950
String targetingKey = "targeting key";

0 commit comments

Comments
 (0)