-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
051b2d6
commit 7f27da2
Showing
56 changed files
with
446 additions
and
167 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
This file contains 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,34 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.spotify.confidence</groupId> | ||
<artifactId>confidence-sdk-java</artifactId> | ||
<version>0.0.16-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>openfeature-provider</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.spotify.confidence</groupId> | ||
<artifactId>sdk-java</artifactId> | ||
<version>0.0.16-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>dev.openfeature</groupId> | ||
<artifactId>sdk</artifactId> | ||
<version>1.6.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.protobuf</groupId> | ||
<artifactId>protobuf-java-util</artifactId> | ||
<version>${protobuf.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
This file contains 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
This file contains 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
This file contains 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
File renamed without changes.
48 changes: 48 additions & 0 deletions
48
openfeature-provider/src/test/java/com/spotify/confidence/ConfidenceFeatureProviderTest.java
This file contains 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,48 @@ | ||
package com.spotify.confidence; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import dev.openfeature.sdk.ErrorCode; | ||
import dev.openfeature.sdk.FlagEvaluationDetails; | ||
import dev.openfeature.sdk.ImmutableContext; | ||
import dev.openfeature.sdk.OpenFeatureAPI; | ||
import dev.openfeature.sdk.Reason; | ||
import dev.openfeature.sdk.Value; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ConfidenceFeatureProviderTest { | ||
|
||
private Confidence root; | ||
private FakeEventSenderEngine fakeEngine; | ||
private ResolverClientTestUtils.FakeFlagResolverClient fakeFlagResolverClient; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
fakeEngine = new FakeEventSenderEngine(new FakeClock()); | ||
fakeFlagResolverClient = new ResolverClientTestUtils.FakeFlagResolverClient(); | ||
root = Confidence.create(fakeEngine, fakeFlagResolverClient); | ||
} | ||
|
||
@Test | ||
public void testCloseChildShouldReturnDefaultsFromOpenFeatureApi() throws IOException { | ||
final Confidence child = root.withContext(Map.of("child-key", ConfidenceValue.of("child"))); | ||
OpenFeatureAPI.getInstance().setProvider(new ConfidenceFeatureProvider(child)); | ||
child.close(); | ||
final boolean defaultValue = false; | ||
final FlagEvaluationDetails<Boolean> booleanDetails = | ||
OpenFeatureAPI.getInstance() | ||
.getClient() | ||
.getBooleanDetails( | ||
"some-flag", | ||
defaultValue, | ||
new ImmutableContext("some-key", Map.of("some", new Value("value")))); | ||
assertThat(booleanDetails.getValue()).isEqualTo(defaultValue); | ||
assertThat(booleanDetails.getReason()).isEqualTo(Reason.ERROR.name()); | ||
assertThat(booleanDetails.getErrorCode()).isEqualTo(ErrorCode.GENERAL); | ||
assertThat(booleanDetails.getErrorMessage()).isEqualTo("Resource closed"); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
181 changes: 181 additions & 0 deletions
181
openfeature-provider/src/test/java/com/spotify/confidence/ResolverClientTestUtils.java
This file contains 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,181 @@ | ||
package com.spotify.confidence; | ||
|
||
import com.google.protobuf.Struct; | ||
import com.google.protobuf.util.Structs; | ||
import com.google.protobuf.util.Values; | ||
import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsResponse; | ||
import com.spotify.confidence.shaded.flags.resolver.v1.ResolveReason; | ||
import com.spotify.confidence.shaded.flags.resolver.v1.ResolvedFlag; | ||
import com.spotify.confidence.shaded.flags.types.v1.FlagSchema; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class ResolverClientTestUtils { | ||
|
||
public static class FakeFlagResolverClient implements FlagResolverClient { | ||
|
||
public boolean closed = false; | ||
private Map<String, ConfidenceValue.Struct> resolves = new HashMap<>(); | ||
|
||
public ResolveFlagsResponse response = generateSampleResponse(Collections.emptyList()); | ||
|
||
@Override | ||
public CompletableFuture<ResolveFlagsResponse> resolveFlags( | ||
String flag, ConfidenceValue.Struct context, Boolean isProvider) { | ||
resolves.put(flag, context); | ||
return CompletableFuture.completedFuture(response); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
closed = true; | ||
} | ||
} | ||
|
||
static ResolveFlagsResponse generateSampleResponse(List<ValueSchemaHolder> additionalProps) { | ||
return ResolveFlagsResponse.newBuilder() | ||
.addResolvedFlags(generateResolvedFlag(additionalProps)) | ||
.build(); | ||
} | ||
|
||
static class ValueSchemaHolder { | ||
public ValueSchemaHolder( | ||
String prop, com.google.protobuf.Value value, FlagSchema.SchemaTypeCase schemaTypeCase) { | ||
this.prop = prop; | ||
this.value = value; | ||
this.schemaTypeCase = schemaTypeCase; | ||
} | ||
|
||
String prop; | ||
com.google.protobuf.Value value; | ||
FlagSchema.SchemaTypeCase schemaTypeCase; | ||
} | ||
|
||
private static ResolvedFlag generateResolvedFlag(List<ValueSchemaHolder> additionalProps) { | ||
final Struct.Builder valueBuilder = | ||
Struct.newBuilder() | ||
.putAllFields( | ||
Map.of( | ||
"prop-A", | ||
Values.of(false), | ||
"prop-B", | ||
Values.of( | ||
Structs.of( | ||
"prop-C", Values.of("str-val"), | ||
"prop-D", Values.of(5.3))), | ||
"prop-E", | ||
Values.of(50), | ||
"prop-F", | ||
Values.of(List.of(Values.of("a"), Values.of("b"))), | ||
"prop-G", | ||
Values.of( | ||
Structs.of( | ||
"prop-H", Values.ofNull(), | ||
"prop-I", Values.ofNull())))); | ||
|
||
final FlagSchema.StructFlagSchema.Builder schemaBuilder = | ||
FlagSchema.StructFlagSchema.newBuilder() | ||
.putAllSchema( | ||
Map.of( | ||
"prop-A", | ||
FlagSchema.newBuilder() | ||
.setBoolSchema(FlagSchema.BoolFlagSchema.getDefaultInstance()) | ||
.build(), | ||
"prop-B", | ||
FlagSchema.newBuilder() | ||
.setStructSchema( | ||
FlagSchema.StructFlagSchema.newBuilder() | ||
.putAllSchema( | ||
Map.of( | ||
"prop-C", | ||
FlagSchema.newBuilder() | ||
.setStringSchema( | ||
FlagSchema.StringFlagSchema.getDefaultInstance()) | ||
.build(), | ||
"prop-D", | ||
FlagSchema.newBuilder() | ||
.setDoubleSchema( | ||
FlagSchema.DoubleFlagSchema.getDefaultInstance()) | ||
.build())) | ||
.build()) | ||
.build(), | ||
"prop-E", | ||
FlagSchema.newBuilder() | ||
.setIntSchema(FlagSchema.IntFlagSchema.getDefaultInstance()) | ||
.build(), | ||
"prop-F", | ||
FlagSchema.newBuilder() | ||
.setListSchema( | ||
FlagSchema.ListFlagSchema.newBuilder() | ||
.setElementSchema( | ||
FlagSchema.newBuilder() | ||
.setStringSchema( | ||
FlagSchema.StringFlagSchema.getDefaultInstance()) | ||
.build()) | ||
.build()) | ||
.build(), | ||
"prop-G", | ||
FlagSchema.newBuilder() | ||
.setStructSchema( | ||
FlagSchema.StructFlagSchema.newBuilder() | ||
.putAllSchema( | ||
Map.of( | ||
"prop-H", | ||
FlagSchema.newBuilder() | ||
.setStringSchema( | ||
FlagSchema.StringFlagSchema.getDefaultInstance()) | ||
.build(), | ||
"prop-I", | ||
FlagSchema.newBuilder() | ||
.setIntSchema( | ||
FlagSchema.IntFlagSchema.getDefaultInstance()) | ||
.build())) | ||
.build()) | ||
.build())); | ||
|
||
additionalProps.forEach( | ||
(valueSchemaHolder) -> { | ||
valueBuilder.putFields(valueSchemaHolder.prop, valueSchemaHolder.value); | ||
final FlagSchema.Builder builder = getSchemaBuilder(valueSchemaHolder); | ||
schemaBuilder.putSchema(valueSchemaHolder.prop, builder.build()); | ||
}); | ||
|
||
return ResolvedFlag.newBuilder() | ||
.setFlag("flags/flag") | ||
.setVariant("flags/flag/variants/var-A") | ||
.setReason(ResolveReason.RESOLVE_REASON_MATCH) | ||
.setValue(valueBuilder) | ||
.setFlagSchema(schemaBuilder) | ||
.build(); | ||
} | ||
|
||
private static FlagSchema.Builder getSchemaBuilder(ValueSchemaHolder valueSchemaHolder) { | ||
final FlagSchema.Builder builder = FlagSchema.newBuilder(); | ||
switch (valueSchemaHolder.schemaTypeCase) { | ||
case STRUCT_SCHEMA: | ||
builder.setStructSchema(FlagSchema.StructFlagSchema.getDefaultInstance()); | ||
break; | ||
case LIST_SCHEMA: | ||
builder.setListSchema(FlagSchema.ListFlagSchema.getDefaultInstance()); | ||
break; | ||
case INT_SCHEMA: | ||
builder.setIntSchema(FlagSchema.IntFlagSchema.getDefaultInstance()); | ||
break; | ||
case DOUBLE_SCHEMA: | ||
builder.setDoubleSchema(FlagSchema.DoubleFlagSchema.getDefaultInstance()); | ||
break; | ||
case STRING_SCHEMA: | ||
builder.setStringSchema(FlagSchema.StringFlagSchema.getDefaultInstance()); | ||
break; | ||
case BOOL_SCHEMA: | ||
builder.setBoolSchema(FlagSchema.BoolFlagSchema.getDefaultInstance()); | ||
break; | ||
case SCHEMATYPE_NOT_SET: | ||
break; | ||
} | ||
return builder; | ||
} | ||
} |
Oops, something went wrong.