-
Notifications
You must be signed in to change notification settings - Fork 51
feat: Add JUnit5 extension for OpenFeature #888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
beeme1mr
merged 10 commits into
open-feature:main
from
open-feature-forking:feat/junit-extension
Jul 26, 2024
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b177459
feat: Add JUnit5 extension for OpenFeature
aepfli 61a7bc0
chore: checkstyle issues
aepfli 5787a4a
chore: more types and tests
aepfli b9dbf75
chore: adding own testProvider with some Thread magic
aepfli 01b9e6f
chore: changing to inmemory provider wrapper for testprovider and pr …
aepfli 49b9db0
Merge branch 'main' into feat/junit-extension
aepfli 74a9ff8
chore: changing to inmemory provider wrapper for testprovider and pr …
aepfli 93f10b9
chore: fix release-please files etc
aepfli 27dcd9a
Apply suggestions from code review
aepfli 05f94cc
Merge branch 'main' into feat/junit-extension
aepfli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule test-harness
updated
from ed7e0b to 25544e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# JUnit Open Feature extension | ||
|
||
A JUnit5 extension to reduce boilerplate code for testing code which utilizes OpenFeature. | ||
|
||
## Getting Started | ||
|
||
We are supporting two different flavors for testing, a [simple](#simple-configuration) and an [extended](#extended-configuration) configuration. | ||
|
||
### Simple Configuration | ||
|
||
Choose the simple configuration if you are only testing in one domain. | ||
Per default, it will be used in the global domain. | ||
|
||
```java | ||
@Test | ||
@Flag(name = "BOOLEAN_FLAG", value = "true") | ||
void test() { | ||
// your test code | ||
} | ||
``` | ||
|
||
#### Multiple flags | ||
|
||
The `@Flag` annotation can be also repeated multiple times. | ||
|
||
```java | ||
@Test | ||
@Flag(name = "BOOLEAN_FLAG", value = "true") | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") | ||
void test() { | ||
// your test code | ||
} | ||
``` | ||
|
||
#### Defining Flags for a whole test-class | ||
|
||
`@Flags` can be defined on the class-level too, but method-level | ||
annotations will superseded class-level annotations. | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```java | ||
@Flag(name = "BOOLEAN_FLAG", value = "true") | ||
@Flag(name = "BOOLEAN_FLAG2", value = "false") | ||
class Test { | ||
@Test | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used | ||
void test() { | ||
// your test code | ||
} | ||
} | ||
``` | ||
|
||
#### Setting a different domain | ||
|
||
You can define an own domain on the test-class-level with `@OpenFeatureDefaultDomain` like: | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```java | ||
@OpenFeatureDefaultDomain("domain") | ||
class Test { | ||
@Test | ||
@Flag(name = "BOOLEAN_FLAG", value = "true") | ||
// this flag will be available in the `domain` domain | ||
void test() { | ||
// your test code | ||
} | ||
} | ||
``` | ||
|
||
### Extended Configuration | ||
|
||
Use the extended configuration when your code needs to use multiple domains. | ||
|
||
```java | ||
@Test | ||
@OpenFeature({ | ||
@Flag(name = "BOOLEAN_FLAG", value = "true") | ||
}) | ||
void test() { | ||
// your test code | ||
} | ||
``` | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
#### Multiple flags | ||
|
||
The `@Flag` annotation can be also repeated multiple times. | ||
|
||
```java | ||
@Test | ||
@OpenFeature({ | ||
@Flag(name = "BOOLEAN_FLAG", value = "true"), | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") | ||
}) | ||
void test() { | ||
// your test code | ||
} | ||
``` | ||
|
||
#### Defining Flags for a whole test-class | ||
|
||
`@Flags` can be defined on the class-level too, but method-level | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
annotations will superseded class-level annotations. | ||
|
||
```java | ||
@OpenFeature({ | ||
@Flag(name = "BOOLEAN_FLAG", value = "true"), | ||
@Flag(name = "BOOLEAN_FLAG2", value = "false") | ||
}) | ||
class Test { | ||
@Test | ||
@OpenFeature({ | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used | ||
}) | ||
void test() { | ||
// your test code | ||
} | ||
} | ||
``` | ||
|
||
#### Setting a different domain | ||
|
||
You can define an own domain for each usage of the `@OpenFeature` annotation with the `domain` property: | ||
|
||
```java | ||
@Test | ||
@OpenFeature( | ||
domain = "domain", | ||
value = { | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used | ||
}) | ||
// this flag will be available in the `domain` domain | ||
void test() { | ||
// your test code | ||
} | ||
``` | ||
|
||
#### Multiple Configurations for multiple domains | ||
|
||
Following testcode will generate two providers, with different flag configurations for a test. | ||
|
||
```java | ||
@Test | ||
@OpenFeature({ | ||
@Flag(name = "BOOLEAN_FLAG", value = "true"), | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") | ||
}) | ||
@OpenFeature( | ||
domain = "domain", | ||
value = { | ||
@Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used | ||
}) | ||
void test() { | ||
// your test code | ||
} | ||
``` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>dev.openfeature.contrib</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>0.1.0</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<groupId>dev.openfeature.contrib.tools</groupId> | ||
<artifactId>junitopenfeature</artifactId> | ||
<version>3.1.2</version> <!--x-release-please-version --> | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<name>junit-openfeature-extension</name> | ||
<description>JUnit5 Extension for OpenFeature</description> | ||
<url>https://openfeature.dev</url> | ||
|
||
<developers> | ||
<developer> | ||
<id>aepfli</id> | ||
<name>Simon Schrottner</name> | ||
<organization>OpenFeature</organization> | ||
<url>https://openfeature.dev/</url> | ||
</developer> | ||
</developers> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>dev.openfeature</groupId> | ||
<artifactId>sdk</artifactId> | ||
<version>[1.4,2.0)</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit.jupiter.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit-pioneer</groupId> | ||
<artifactId>junit-pioneer</artifactId> | ||
<version>1.9.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
36 changes: 36 additions & 0 deletions
36
.../junit-openfeature/src/main/java/dev/openfeature/contrib/tools/junitopenfeature/Flag.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dev.openfeature.contrib.tools.junitopenfeature; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation for Flag Configuration for the default domain. | ||
* Can be used as a standalone flag configuration but also within {@link OpenFeature}. | ||
*/ | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Repeatable(Flags.class) | ||
@ExtendWith(OpenFeatureExtension.class) | ||
public @interface Flag { | ||
/** | ||
* The key of the FeatureFlag. | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* The value of the FeatureFlag. | ||
*/ | ||
String value(); | ||
|
||
/** | ||
* The type of the FeatureFlag. | ||
*/ | ||
Class<?> valueType() default Boolean.class; | ||
} | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
...junit-openfeature/src/main/java/dev/openfeature/contrib/tools/junitopenfeature/Flags.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.openfeature.contrib.tools.junitopenfeature; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Collection of {@link Flag} configurations. | ||
*/ | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(OpenFeatureExtension.class) | ||
public @interface Flags { | ||
/** | ||
* Collection of {@link Flag} configurations. | ||
*/ | ||
Flag[] value() default {}; | ||
} | ||
|
||
|
30 changes: 30 additions & 0 deletions
30
...openfeature/src/main/java/dev/openfeature/contrib/tools/junitopenfeature/OpenFeature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dev.openfeature.contrib.tools.junitopenfeature; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation for generating an extended configuration for OpenFeature. | ||
* This annotation allows you to specify a list of flags for a specific domain. | ||
*/ | ||
@Target({ ElementType.METHOD, ElementType.TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Repeatable(value = OpenFeatures.class) | ||
@ExtendWith(OpenFeatureExtension.class) | ||
public @interface OpenFeature { | ||
/** | ||
* the provider domain used for this configuration. | ||
*/ | ||
String domain() default ""; | ||
/** | ||
* Collection of {@link Flag} configurations for this domain. | ||
*/ | ||
Flag[] value(); | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
...rc/main/java/dev/openfeature/contrib/tools/junitopenfeature/OpenFeatureDefaultDomain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.openfeature.contrib.tools.junitopenfeature; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Configuration of a default domain for standalone {@link Flag} configurations. | ||
*/ | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(OpenFeatureExtension.class) | ||
public @interface OpenFeatureDefaultDomain { | ||
/** | ||
* the default domain. | ||
*/ | ||
String value() default ""; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.