Skip to content

Commit 3ea24c3

Browse files
committed
adding integration tests for flagd and inmemory provider
1 parent 20eabfa commit 3ea24c3

16 files changed

+309
-132
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Your Dropwizard application configuration class must implement `OpenFeatureBundl
4848

4949
## Configuring dropwizard-openfeature in the dropwizard config file
5050

51-
For a full overview see `OpenFeatureBundleFactory`, `OpenFeatureHealthCheckConfiguration`, and `FlagdConfiguration` a
51+
For a full overview see `OpenFeatureConfiguration`, `OpenFeatureHealthCheckConfiguration`, and `FlagdConfiguration` a
5252
minimal configuration for flagd runnining locally on the port 8013 would look as follows.
5353

5454
```yaml
@@ -63,15 +63,17 @@ For the bundle to have access to the configuration, your application configurati
6363
`OpenFeatureBundleConfiguration`.
6464

6565
```java
66+
import io.github.sideshowcoder.dropwizard_openfeature.OpenFeatureConfiguration;
67+
6668
public class ApplicationConfiguration implements OpenFeatureBundleConfiguration {
6769
6870
@Valid
6971
@NotNull
7072
@JsonProperty
71-
private OpenFeatureBundleFactory openfeature;
73+
private OpenFeatureConfiguration openfeature;
7274
7375
@Override
74-
public OpenFeatureBundleFactory getOpenFeatureBundleFactory() {
76+
public OpenFeatureConfiguration getOpenFeatureConfiguration() {
7577
return openfeature;
7678
}
7779
}

pom.xml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.sideshowcoder</groupId>
88
<artifactId>dropwizard-openfeature</artifactId>
9-
<version>0.0.1-SNAPSHOT</version>
9+
<version>0.0.2-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>dropwizard-openfeature</name>
@@ -28,6 +28,8 @@
2828
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
2929
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
3030
<maven-surefire-plugin.version>3.5.3</maven-surefire-plugin.version>
31+
<maven-failsafe-plugin.version>3.5.3</maven-failsafe-plugin.version>
32+
<mockito.version>5.17.0</mockito.version>
3133
</properties>
3234

3335
<developers>
@@ -61,11 +63,11 @@
6163
<scope>import</scope>
6264
</dependency>
6365
<dependency>
64-
<groupId>org.testcontainers</groupId>
65-
<artifactId>testcontainers-bom</artifactId>
66-
<version>1.21.0</version>
66+
<groupId>org.mockito</groupId>
67+
<artifactId>mockito-bom</artifactId>
68+
<version>${mockito.version}</version>
6769
<type>pom</type>
68-
<scope>import</scope>
70+
<scope>test</scope>
6971
</dependency>
7072
</dependencies>
7173
</dependencyManagement>
@@ -102,6 +104,16 @@
102104
<artifactId>dropwizard-testing</artifactId>
103105
<scope>test</scope>
104106
</dependency>
107+
<dependency>
108+
<groupId>org.mockito</groupId>
109+
<artifactId>mockito-core</artifactId>
110+
<scope>test</scope>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.mockito</groupId>
114+
<artifactId>mockito-junit-jupiter</artifactId>
115+
<scope>test</scope>
116+
</dependency>
105117
</dependencies>
106118

107119
<build>
@@ -113,10 +125,16 @@
113125
<plugin>
114126
<artifactId>maven-surefire-plugin</artifactId>
115127
<version>${maven-surefire-plugin.version}</version>
128+
<configuration>
129+
<argLine>
130+
-javaagent:${settings.localRepository}/org/mockito/mockito-core/${mockito.version}/mockito-core-${mockito.version}.jar
131+
-Xshare:off
132+
</argLine>
133+
</configuration>
116134
</plugin>
117135
<plugin>
118136
<artifactId>maven-failsafe-plugin</artifactId>
119-
<version>${maven-surefire-plugin.version}</version>
137+
<version>${maven-failsafe-plugin.version}</version>
120138
</plugin>
121139
<plugin>
122140
<groupId>org.apache.maven.plugins</groupId>

src/main/java/io/github/sideshowcoder/dropwizard_openfeature/OpenFeatureAPIManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import dev.openfeature.sdk.FeatureProvider;
44
import dev.openfeature.sdk.OpenFeatureAPI;
55
import io.dropwizard.lifecycle.Managed;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
public class OpenFeatureAPIManager implements Managed {
10+
private static final Logger log = LoggerFactory.getLogger(OpenFeatureAPIManager.class);
811
private final FeatureProvider featureProvider;
912

1013
public OpenFeatureAPIManager(FeatureProvider featureProvider) {
@@ -14,10 +17,12 @@ public OpenFeatureAPIManager(FeatureProvider featureProvider) {
1417
@Override
1518
public void start() throws Exception {
1619
OpenFeatureAPI.getInstance().setProviderAndWait(featureProvider);
20+
log.info("Started {}", this.getClass().getSimpleName());
1721
}
1822

1923
@Override
2024
public void stop() throws Exception {
2125
OpenFeatureAPI.getInstance().shutdown();
26+
log.info("Stopped {}", this.getClass().getSimpleName());
2227
}
2328
}
Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package io.github.sideshowcoder.dropwizard_openfeature;
22

3-
import dev.openfeature.sdk.providers.memory.Flag;
3+
import dev.openfeature.contrib.providers.flagd.FlagdProvider;
4+
import dev.openfeature.sdk.FeatureProvider;
5+
import dev.openfeature.sdk.OpenFeatureAPI;
6+
import dev.openfeature.sdk.providers.memory.InMemoryProvider;
47
import io.dropwizard.core.ConfiguredBundle;
58
import io.dropwizard.core.setup.Environment;
9+
import io.github.sideshowcoder.dropwizard_openfeature.health.OpenFeatureHealthCheck;
610

711
import java.util.Map;
812

@@ -18,20 +22,33 @@
1822
*/
1923
public class OpenFeatureBundle implements ConfiguredBundle<OpenFeatureBundleConfiguration> {
2024

21-
private final Map<String, Flag<?>> inMemoryFlags;
25+
private FeatureProvider featureProvider;
2226

23-
public OpenFeatureBundle() {
24-
this(Map.of());
27+
@Override
28+
public void run(OpenFeatureBundleConfiguration configuration, Environment environment) {
29+
var config = configuration.getOpenFeatureConfiguration();
30+
initializeFeatureProvider(config);
31+
var manager = new OpenFeatureAPIManager(getFeatureProvider());
32+
environment.lifecycle().manage(manager);
33+
34+
if (config.getHealthcheck().isEnabled()) {
35+
var client = OpenFeatureAPI.getInstance().getClient(config.getHealthcheck().getClientDomain());
36+
var healthcheck = new OpenFeatureHealthCheck(client);
37+
environment.healthChecks().register(config.getHealthcheck().getName(), healthcheck);
38+
}
2539
}
2640

27-
public OpenFeatureBundle(Map<String, Flag<?>> inMemoryFlags) {
28-
this.inMemoryFlags = inMemoryFlags;
41+
public FeatureProvider getFeatureProvider() {
42+
if (featureProvider == null) {
43+
throw new IllegalStateException("FeatureProvider has not been initialized");
44+
}
45+
return featureProvider;
2946
}
3047

31-
@Override
32-
public void run(OpenFeatureBundleConfiguration configuration, Environment environment) {
33-
configuration.getOpenFeatureBundleFactory().setFlags(inMemoryFlags);
34-
configuration.getOpenFeatureBundleFactory().startOpenFeatureAPIManager(environment);
35-
configuration.getOpenFeatureBundleFactory().registerOpenFeatureHealthCheck(environment);
48+
private synchronized void initializeFeatureProvider(OpenFeatureConfiguration config) {
49+
featureProvider = switch (config.getProviderType()) {
50+
case FLAGD -> new FlagdProvider(config.getFlagd().getFlagdOptions());
51+
case INMEMORY -> new InMemoryProvider(Map.of());
52+
};
3653
}
3754
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package io.github.sideshowcoder.dropwizard_openfeature;
22

33
public interface OpenFeatureBundleConfiguration {
4-
OpenFeatureBundleFactory getOpenFeatureBundleFactory();
4+
OpenFeatureConfiguration getOpenFeatureConfiguration();
55
}

src/main/java/io/github/sideshowcoder/dropwizard_openfeature/OpenFeatureBundleFactory.java

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.sideshowcoder.dropwizard_openfeature;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.github.sideshowcoder.dropwizard_openfeature.validation.ValidEnum;
5+
import jakarta.validation.Valid;
6+
import jakarta.validation.constraints.NotNull;
7+
8+
public class OpenFeatureConfiguration {
9+
10+
@Valid
11+
@JsonProperty
12+
private FlagdConfiguration flagd = new FlagdConfiguration();
13+
14+
@Valid
15+
@JsonProperty
16+
private OpenFeatureHealthCheckConfiguration healthcheck = new OpenFeatureHealthCheckConfiguration();
17+
18+
@JsonProperty
19+
@ValidEnum(enumClass = ProviderType.class)
20+
@NotNull
21+
private String provider;
22+
23+
public FlagdConfiguration getFlagd() {
24+
return flagd;
25+
}
26+
27+
public void setFlagd(FlagdConfiguration flagd) {
28+
this.flagd = flagd;
29+
}
30+
31+
public OpenFeatureHealthCheckConfiguration getHealthcheck() {
32+
return healthcheck;
33+
}
34+
35+
public void setHealthcheck(OpenFeatureHealthCheckConfiguration healthcheck) {
36+
this.healthcheck = healthcheck;
37+
}
38+
39+
public String getProvider() {
40+
return provider;
41+
}
42+
43+
public void setProvider(String provider) {
44+
this.provider = provider;
45+
}
46+
47+
public ProviderType getProviderType() {
48+
return ProviderType.valueOf(provider.toUpperCase());
49+
}
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.github.sideshowcoder.dropwizard_openfeature;
2+
3+
import dev.openfeature.sdk.OpenFeatureAPI;
4+
import io.dropwizard.testing.ResourceHelpers;
5+
import io.dropwizard.testing.junit5.DropwizardAppExtension;
6+
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
7+
import io.github.sideshowcoder.dropwizard_openfeature.helpers.App;
8+
import io.github.sideshowcoder.dropwizard_openfeature.helpers.Config;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
15+
@ExtendWith(DropwizardExtensionsSupport.class)
16+
public class OpenFeatureBundleFlagdProviderTest {
17+
18+
private static final DropwizardAppExtension<Config> APP = new DropwizardAppExtension<>(
19+
App.class,
20+
ResourceHelpers.resourceFilePath("flagd-provider-config.yml"));
21+
22+
@Test
23+
public void initializesHealthCheck() throws Exception {
24+
var healthcheckResult = APP.getEnvironment().healthChecks().runHealthCheck("openfeature-health-check");
25+
assertTrue(healthcheckResult.isHealthy());
26+
}
27+
28+
@Test
29+
public void providesFeatureFlagsViaInMemoryProvider() throws Exception {
30+
// See flagd-test-flags.json for flag definitions used!
31+
var client = OpenFeatureAPI.getInstance().getClient("flagd-client");
32+
assertEquals("red", client.getStringValue("static-string-flag", "not-expected-value"));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)