Skip to content

Commit

Permalink
feat: added interception of parameterized tests to Junit OpenFeature …
Browse files Browse the repository at this point in the history
…Extension (#1093)

Signed-off-by: sebastian.zahrhuber <[email protected]>
  • Loading branch information
sebastian-zahrhuber authored Dec 3, 2024
1 parent 4808d44 commit a78c906
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 4 deletions.
3 changes: 2 additions & 1 deletion tools/junit-openfeature/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ A JUnit5 extension to reduce boilerplate code for testing code which utilizes Op

## Getting Started

We are supporting two different flavors for testing, a [simple](#simple-configuration) and an [extended](#extended-configuration) configuration.
- We are supporting two different flavors for testing, a [simple](#simple-configuration) and an [extended](#extended-configuration) configuration.
- Both the [`@Test`](https://junit.org/junit5/docs/5.9.0/api/org.junit.jupiter.api/org/junit/jupiter/api/Test.html) annotation and the [`@ParameterizedTest`](https://junit.org/junit5/docs/5.9.0/api/org.junit.jupiter.params/org/junit/jupiter/params/ParameterizedTest.html) annotation are supported.

Notice: We are most likely not multithread compatible!
### Simple Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ public void interceptTestMethod(
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext
) throws Throwable {
TestProvider.setCurrentNamespace(getNamespace(extensionContext));
invocation.proceed();
TestProvider.clearCurrentNamespace();
executeWithNamespace(invocation, extensionContext);
}

@Override
public void interceptTestTemplateMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
executeWithNamespace(invocation, extensionContext);
}

@Override
Expand Down Expand Up @@ -150,4 +156,15 @@ private ExtensionContext.Namespace getNamespace(ExtensionContext extensionContex
private ExtensionContext.Store getStore(ExtensionContext context) {
return context.getStore(ExtensionContext.Namespace.create(getClass()));
}

private void executeWithNamespace(
Invocation<Void> invocation,
ExtensionContext extensionContext) throws Throwable {
TestProvider.setCurrentNamespace(getNamespace(extensionContext));
try {
invocation.proceed();
} finally {
TestProvider.clearCurrentNamespace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.openfeature.sdk.OpenFeatureAPI;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -45,6 +47,14 @@ void multipleFlagsSimple() {
assertThat(client.getBooleanValue(FLAG + "2", false)).isTrue();
assertThat(client.getBooleanValue(FLAG + "3", false)).isTrue();
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = "true")
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getBooleanValue(FLAG, false)).isTrue();
}
}

@Nested
Expand Down Expand Up @@ -81,6 +91,14 @@ void multipleFlagsSimple() {
assertThat(client.getBooleanValue(FLAG + "2", false)).isTrue();
assertThat(client.getBooleanValue(FLAG + "3", false)).isTrue();
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = "true")
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient("testSpecific");
assertThat(client.getBooleanValue(FLAG, false)).isTrue();
}
}

@Nested
Expand Down Expand Up @@ -125,6 +143,16 @@ void multipleFlags() {
assertThat(client.getBooleanValue(FLAG + "3", false)).isTrue();
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@OpenFeature({
@Flag(name = FLAG, value = "true")
})
void existingFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getBooleanValue(FLAG, false)).isTrue();
}

@Nested
@OpenFeature({
@Flag(name = FLAG, value = "true"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.openfeature.sdk.OpenFeatureAPI;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -52,6 +54,14 @@ void multipleFlagsSimple() {
assertThat(client.getDoubleValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Double.class)
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getDoubleValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@Nested
Expand Down Expand Up @@ -88,6 +98,14 @@ void multipleFlagsSimple() {
assertThat(client.getDoubleValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Double.class)
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient(SPECIFIC_DOMAIN);
assertThat(client.getDoubleValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@Nested
Expand Down Expand Up @@ -132,6 +150,16 @@ void multipleFlags() {
assertThat(client.getDoubleValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@OpenFeature({
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Double.class)
})
void existingFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getDoubleValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}

@Nested
@OpenFeature({
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Double.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.openfeature.sdk.OpenFeatureAPI;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -52,6 +54,14 @@ void multipleFlagsSimple() {
assertThat(client.getIntegerValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Integer.class)
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getIntegerValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@Nested
Expand Down Expand Up @@ -88,6 +98,14 @@ void multipleFlagsSimple() {
assertThat(client.getIntegerValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Integer.class)
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient(SPECIFIC_DOMAIN);
assertThat(client.getIntegerValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@Nested
Expand Down Expand Up @@ -132,6 +150,16 @@ void multipleFlags() {
assertThat(client.getIntegerValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@OpenFeature({
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Integer.class)
})
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getIntegerValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}

@Nested
@OpenFeature({
@Flag(name = FLAG, value = FLAG_VALUE_STRING, valueType = Integer.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.openfeature.sdk.OpenFeatureAPI;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -50,6 +52,14 @@ void multipleFlagsSimple() {
assertThat(client.getStringValue(FLAG + "2", FALLBACK)).isEqualTo(FLAG_VALUE);
assertThat(client.getStringValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = FLAG_VALUE, valueType = String.class)
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getStringValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@Nested
Expand Down Expand Up @@ -86,6 +96,14 @@ void multipleFlagsSimple() {
assertThat(client.getStringValue(FLAG + "2", FALLBACK)).isEqualTo(FLAG_VALUE);
assertThat(client.getStringValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@Flag(name = FLAG, value = FLAG_VALUE, valueType = String.class)
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient(SPECIFIC_DOMAIN);
assertThat(client.getStringValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}
}

@Nested
Expand Down Expand Up @@ -130,6 +148,16 @@ void multipleFlags() {
assertThat(client.getStringValue(FLAG + "3", FALLBACK)).isEqualTo(FLAG_VALUE);
}

@ParameterizedTest
@ValueSource(ints = {1, 2})
@OpenFeature({
@Flag(name = FLAG , value = FLAG_VALUE, valueType = String.class)
})
void existingSimpleFlagIsRetrievedOnParameterizedTest() {
Client client = OpenFeatureAPI.getInstance().getClient();
assertThat(client.getStringValue(FLAG, FALLBACK)).isEqualTo(FLAG_VALUE);
}

@Nested
@OpenFeature({
@Flag(name = FLAG , value = FLAG_VALUE, valueType = String.class),
Expand Down

0 comments on commit a78c906

Please sign in to comment.