Class TestData
- java.lang.Object
-
- com.launchdarkly.sdk.server.integrations.TestData
-
- All Implemented Interfaces:
ComponentConfigurer<DataSource>
public final class TestData extends java.lang.Object implements ComponentConfigurer<DataSource>
A mechanism for providing dynamically updatable feature flag state in a simplified form to an SDK client in test scenarios.Unlike
FileData, this mechanism does not use any external resources. It provides only the data that the application has put into it using theupdate(FlagBuilder)method.
The above example uses a simple boolean flag, but more complex configurations are possible using the methods of theTestData td = TestData.dataSource(); td.update(testData.flag("flag-key-1").booleanFlag().variationForAllUsers(true)); LDConfig config = new LDConfig.Builder() .dataSource(td) .build(); LDClient client = new LDClient(sdkKey, config); // flags can be updated at any time: td.update(testData.flag("flag-key-2") .variationForUser("some-user-key", true) .fallthroughVariation(false));TestData.FlagBuilderthat is returned byflag(String).TestData.FlagBuildersupports many of the ways a flag can be configured on the LaunchDarkly dashboard, but does not currently support 1. rule operators other than "in" and "not in", or 2. percentage rollouts.If the same
TestDatainstance is used to configure multipleLDClientinstances, any changes made to the data will propagate to all of theLDClients.- Since:
- 5.1.0
- See Also:
FileData
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTestData.FlagBuilderA builder for feature flag configurations to be used withTestData.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DataSourcebuild(ClientContext context)Called internally by the SDK to create an implementation instance.static TestDatadataSource()Creates a new instance of the test data source.TestData.FlagBuilderflag(java.lang.String key)Creates or copies aTestData.FlagBuilderfor building a test flag configuration.TestDataupdate(TestData.FlagBuilder flagBuilder)Updates the test data with the specified flag configuration.TestDataupdateStatus(DataSourceStatusProvider.State newState, DataSourceStatusProvider.ErrorInfo newError)Simulates a change in the data source status.
-
-
-
Method Detail
-
dataSource
public static TestData dataSource()
Creates a new instance of the test data source.See
TestDatafor details.- Returns:
- a new configurable test data source
-
flag
public TestData.FlagBuilder flag(java.lang.String key)
Creates or copies aTestData.FlagBuilderfor building a test flag configuration.If this flag key has already been defined in this
TestDatainstance, then the builder starts with the same configuration that was last provided for this flag.Otherwise, it starts with a new default configuration in which the flag has
trueandfalsevariations, istruefor all users when targeting is turned on andfalseotherwise, and currently has targeting turned on. You can change any of those properties, and provide more complex behavior, using theTestData.FlagBuildermethods.Once you have set the desired configuration, pass the builder to
update(FlagBuilder).- Parameters:
key- the flag key- Returns:
- a flag configuration builder
- See Also:
update(FlagBuilder)
-
update
public TestData update(TestData.FlagBuilder flagBuilder)
Updates the test data with the specified flag configuration.This has the same effect as if a flag were added or modified on the LaunchDarkly dashboard. It immediately propagates the flag change to any
LDClientinstance(s) that you have already configured to use thisTestData. If noLDClienthas been started yet, it simply adds this flag to the test data which will be provided to anyLDClientthat you subsequently configure.Any subsequent changes to this
TestData.FlagBuilderinstance do not affect the test data, unless you callupdate(FlagBuilder)again.- Parameters:
flagBuilder- a flag configuration builder- Returns:
- the same
TestDatainstance - See Also:
flag(String)
-
updateStatus
public TestData updateStatus(DataSourceStatusProvider.State newState, DataSourceStatusProvider.ErrorInfo newError)
Simulates a change in the data source status.Use this if you want to test the behavior of application code that uses
LDClient.getDataSourceStatusProvider()to track whether the data source is having problems (for example, a network failure interruptsingthe streaming connection). It does not actually stop theTestDatadata source from working, so even if you have simulated an outage, callingupdate(FlagBuilder)will still send updates.- Parameters:
newState- one of the constants defined byDataSourceStatusProvider.StatenewError- anDataSourceStatusProvider.ErrorInfoinstance, or null- Returns:
- the same
TestDatainstance
-
build
public DataSource build(ClientContext context)
Description copied from interface:ComponentConfigurerCalled internally by the SDK to create an implementation instance. Applications should not need to call this method.- Specified by:
buildin interfaceComponentConfigurer<DataSource>- Parameters:
context- provides configuration properties and other components from the current SDK client instance- Returns:
- a instance of the component type
-
-