-
Notifications
You must be signed in to change notification settings - Fork 51
chore: javadoc and tests for api, context #942
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -7,8 +7,6 @@ class DoSomethingProvider implements FeatureProvider { | |
| static final ImmutableMetadata DEFAULT_METADATA = ImmutableMetadata.builder().build(); | ||
| private ImmutableMetadata flagMetadata; | ||
|
|
||
| private EvaluationContext savedContext; | ||
|
|
||
| public DoSomethingProvider() { | ||
| this.flagMetadata = DEFAULT_METADATA; | ||
| } | ||
|
|
@@ -17,18 +15,13 @@ public DoSomethingProvider(ImmutableMetadata flagMetadata) { | |
| this.flagMetadata = flagMetadata; | ||
| } | ||
|
|
||
| EvaluationContext getMergedContext() { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is no longer needed, spies work better. |
||
| return savedContext; | ||
| } | ||
|
|
||
| @Override | ||
| public Metadata getMetadata() { | ||
| return () -> name; | ||
| } | ||
|
|
||
| @Override | ||
| public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { | ||
| savedContext = ctx; | ||
| return ProviderEvaluation.<Boolean>builder() | ||
| .value(!defaultValue) | ||
| .flagMetadata(flagMetadata) | ||
|
|
@@ -45,7 +38,6 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default | |
|
|
||
| @Override | ||
| public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { | ||
| savedContext = ctx; | ||
| return ProviderEvaluation.<Integer>builder() | ||
| .value(defaultValue * 100) | ||
| .flagMetadata(flagMetadata) | ||
|
|
@@ -54,7 +46,6 @@ public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defa | |
|
|
||
| @Override | ||
| public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { | ||
| savedContext = ctx; | ||
| return ProviderEvaluation.<Double>builder() | ||
| .value(defaultValue * 100) | ||
| .flagMetadata(flagMetadata) | ||
|
|
@@ -63,7 +54,6 @@ public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double default | |
|
|
||
| @Override | ||
| public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext invocationContext) { | ||
| savedContext = invocationContext; | ||
| return ProviderEvaluation.<Value>builder() | ||
| .value(null) | ||
| .flagMetadata(flagMetadata) | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -11,7 +11,9 @@ | |
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.argThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
|
|
@@ -307,11 +309,30 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { | |
| assertNotNull(result.getFlagMetadata()); | ||
| } | ||
|
|
||
| @Specification(number="3.2.1.1", text="The API, Client and invocation MUST have a method for supplying evaluation context.") | ||
| @Specification(number="3.2.2.1", text="The API MUST have a method for setting the global evaluation context.") | ||
| @Test void api_context() { | ||
| String contextKey = "some-key"; | ||
| String contextValue = "some-value"; | ||
| DoSomethingProvider provider = spy( new DoSomethingProvider()); | ||
| FeatureProviderTestUtils.setFeatureProvider(provider); | ||
|
|
||
| Map<String, Value> attributes = new HashMap<>(); | ||
| attributes.put(contextKey, new Value(contextValue)); | ||
| EvaluationContext apiCtx = new ImmutableContext(attributes); | ||
|
|
||
| // set the global context | ||
| api.setEvaluationContext(apiCtx); | ||
| Client client = api.getClient(); | ||
| client.getBooleanValue("any-flag", false); | ||
|
|
||
| // assert that the value from the global context was passed to the provider | ||
| verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> arg.getValue(contextKey).asString().equals(contextValue))); | ||
| } | ||
|
|
||
| @Specification(number="3.2.1.1", text="The API, Client and invocation MUST have a method for supplying evaluation context.") | ||
| @Specification(number="3.2.3", text="Evaluation context MUST be merged in the order: API (global; lowest precedence) -> transaction -> client -> invocation -> before hooks (highest precedence), with duplicate values being overwritten.") | ||
| @Test void multi_layer_context_merges_correctly() { | ||
| DoSomethingProvider provider = new DoSomethingProvider(); | ||
| DoSomethingProvider provider = spy(new DoSomethingProvider()); | ||
| FeatureProviderTestUtils.setFeatureProvider(provider); | ||
| TransactionContextPropagator transactionContextPropagator = new ThreadLocalTransactionContextPropagator(); | ||
| api.setTransactionContextPropagator(transactionContextPropagator); | ||
|
|
@@ -356,21 +377,21 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { | |
| invocationAttributes.put("invocation", new Value("4")); | ||
| EvaluationContext invocationCtx = new ImmutableContext(invocationAttributes); | ||
|
|
||
| // dosomethingprovider inverts this value. | ||
| assertTrue(c.getBooleanValue("key", false, invocationCtx)); | ||
|
|
||
| EvaluationContext merged = provider.getMergedContext(); | ||
| assertEquals("1", merged.getValue("api").asString()); | ||
| assertEquals("2", merged.getValue("transaction").asString()); | ||
| assertEquals("3", merged.getValue("client").asString()); | ||
| assertEquals("4", merged.getValue("invocation").asString()); | ||
| assertEquals("2", merged.getValue("common1").asString(), "transaction merge is incorrect"); | ||
| assertEquals("3", merged.getValue("common2").asString(), "api client merge is incorrect"); | ||
| assertEquals("4", merged.getValue("common3").asString(), "invocation merge is incorrect"); | ||
| assertEquals("3", merged.getValue("common4").asString(), "api client merge is incorrect"); | ||
| assertEquals("4", merged.getValue("common5").asString(), "invocation merge is incorrect"); | ||
| assertEquals("4", merged.getValue("common6").asString(), "invocation merge is incorrect"); | ||
|
|
||
| c.getBooleanValue("key", false, invocationCtx); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of the specific "getMergedContext" method, I simply used a spy here. |
||
|
|
||
| // assert the connect overrides | ||
| verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> { | ||
| return arg.getValue("api").asString().equals("1") && | ||
| arg.getValue("transaction").asString().equals("2") && | ||
| arg.getValue("client").asString().equals("3") && | ||
| arg.getValue("invocation").asString().equals("4") && | ||
| arg.getValue("common1").asString().equals("2") && | ||
| arg.getValue("common2").asString().equals("3") && | ||
| arg.getValue("common3").asString().equals("4") && | ||
| arg.getValue("common4").asString().equals("3") && | ||
| arg.getValue("common5").asString().equals("4") && | ||
| arg.getValue("common6").asString().equals("4"); | ||
| })); | ||
| } | ||
|
|
||
| @Specification(number="3.3.1.1", text="The API SHOULD have a method for setting a transaction context propagator.") | ||
|
|
||
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.