Skip to content

Commit

Permalink
chore: Update slf4j (#284)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
justinabrahms authored Feb 10, 2023
1 parent 24c237e commit 07ecea1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
19 changes: 10 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
<version>2.0.6</version>
</dependency>

<!-- test -->
Expand All @@ -70,13 +70,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>slf4j-test</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down Expand Up @@ -131,6 +124,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.simplify4u</groupId>
<artifactId>slf4j2-mock</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down Expand Up @@ -228,14 +228,15 @@
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>com.github.spotbugs:*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.junit*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>org.simplify4u:slf4j2-mock*</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
<ignoredDependencies>
<ignoredDependency>com.google.guava*</ignoredDependency>
<ignoredDependency>io.cucumber*</ignoredDependency>
<ignoredDependency>org.junit*</ignoredDependency>
<ignoredDependency>com.google.code.findbugs*</ignoredDependency>
<ignoredDependency>com.github.spotbugs*</ignoredDependency>
<ignoredDependency>uk.org.lidalia:lidalia-slf4j-ext:*</ignoredDependency>
<ignoredDependency> org.simplify4u:slf4j-mock-common:*</ignoredDependency>
</ignoredDependencies>
</configuration>
</plugin>
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/dev/openfeature/sdk/EvalContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -25,7 +27,7 @@ public class EvalContextTest {
"values of type `boolean | string | number | datetime | structure`.")
@Test void eval_context() {
Map<String, Value> attributes = new HashMap<>();
Instant dt = Instant.now();
Instant dt = Instant.now().truncatedTo(ChronoUnit.MILLIS);
attributes.put("str", new Value("test"));
attributes.put("bool", new Value(true));
attributes.put("int", new Value(4));
Expand All @@ -38,7 +40,7 @@ public class EvalContextTest {

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

assertEquals(dt, ec.getValue("dt").asInstant());
assertEquals(dt, ec.getValue("dt").asInstant().truncatedTo(ChronoUnit.MILLIS));
}

@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
Expand Down
38 changes: 23 additions & 15 deletions src/test/java/dev/openfeature/sdk/FlagEvaluationSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableList;

import dev.openfeature.sdk.exceptions.FlagNotFoundError;
import io.cucumber.java.hu.Ha;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import dev.openfeature.sdk.fixtures.HookFixtures;
import uk.org.lidalia.slf4jtest.LoggingEvent;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.simplify4u.slf4jmock.LoggerMock;
import org.slf4j.Logger;

class FlagEvaluationSpecTest implements HookFixtures {

private static final TestLogger TEST_LOGGER = TestLoggerFactory.getTestLogger(OpenFeatureClient.class);
private Logger logger;

private Client _client() {
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
Expand All @@ -42,6 +45,15 @@ private Client _client() {
api.setEvaluationContext(null);
}

@BeforeEach void set_logger() {
logger = Mockito.mock(Logger.class);
LoggerMock.setMock(OpenFeatureClient.class, logger);
}

@AfterEach void reset_logs() {
LoggerMock.setMock(OpenFeatureClient.class, logger);
}

@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.")
@Test void global_singleton() {
assertSame(OpenFeatureAPI.getInstance(), OpenFeatureAPI.getInstance());
Expand Down Expand Up @@ -203,20 +215,16 @@ private Client _client() {

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

ImmutableList<LoggingEvent> loggingEvents = TEST_LOGGER.getLoggingEvents();
assertThat(loggingEvents.size()).isGreaterThan(0);

LoggingEvent event = loggingEvents.get(0);
assertThat(event.getMessage()).isEqualTo("Unable to correctly evaluate flag with key '{}'");
assertThat(event.getThrowable().isPresent()).isTrue();
assertThat(event.getThrowable().get()).isInstanceOf(FlagNotFoundError.class);
assertEquals(Reason.ERROR.toString(), result.getReason());
Mockito.verify(logger).error(
ArgumentMatchers.contains("Unable to correctly evaluate flag with key"),
any(),
ArgumentMatchers.isA(FlagNotFoundError.class));
}

@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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ImmutableStructureTest {

@Test void MutatingGetInstantValueShouldNotChangeOriginalValue() {
String KEY = "key";
Instant now = Instant.now();
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
Map<String, Value> map = new HashMap<String, Value>() {
{
put(KEY, new Value(now));
Expand Down
25 changes: 18 additions & 7 deletions src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@

import dev.openfeature.sdk.fixtures.HookFixtures;
import org.junit.jupiter.api.*;
import uk.org.lidalia.slf4jext.Level;
import uk.org.lidalia.slf4jtest.*;
import org.mockito.Mockito;
import org.simplify4u.slf4jmock.LoggerMock;
import org.slf4j.Logger;

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

class OpenFeatureClientTest implements HookFixtures {

private static final TestLogger TEST_LOGGER = TestLoggerFactory.getTestLogger(OpenFeatureClient.class);
private Logger logger;

@BeforeEach void set_logger() {
logger = Mockito.mock(Logger.class);
LoggerMock.setMock(OpenFeatureClient.class, logger);
}

@AfterEach void reset_logs() {
LoggerMock.setMock(OpenFeatureClient.class, logger);
}
@Test
@DisplayName("should not throw exception if hook has different type argument than hookContext")
void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
TEST_LOGGER.clear();
OpenFeatureAPI api = mock(OpenFeatureAPI.class);
when(api.getProvider()).thenReturn(new DoSomethingProvider());
when(api.getHooks()).thenReturn(Arrays.asList(mockBooleanHook(), mockStringHook()));
Expand All @@ -27,13 +35,16 @@ void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
FlagEvaluationDetails<Boolean> actual = client.getBooleanDetails("feature key", Boolean.FALSE);

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

@Test
void mergeContextTest() {
TEST_LOGGER.clear();

String flag = "feature key";
boolean defaultValue = false;
String targetingKey = "targeting key";
Expand Down

0 comments on commit 07ecea1

Please sign in to comment.