Skip to content

Commit e99f26b

Browse files
Feat: added SDKkey and environment in datafile (#434)
* added SDKkey and environment in datafile added tests to verify changes * ignore key of sdk key and environment if null * resolved spotbugs issue * Changed environment to environmentKey Co-authored-by: mnoman09 <m.nomanshoaib09@gmail.com>
1 parent 5637fb5 commit e99f26b

File tree

14 files changed

+118
-15
lines changed

14 files changed

+118
-15
lines changed

core-api/src/main/java/com/optimizely/ab/config/DatafileProjectConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class DatafileProjectConfig implements ProjectConfig {
5959
private final String accountId;
6060
private final String projectId;
6161
private final String revision;
62+
private final String sdkKey;
63+
private final String environmentKey;
6264
private final String version;
6365
private final boolean anonymizeIP;
6466
private final boolean sendFlagDecisions;
@@ -108,6 +110,8 @@ public DatafileProjectConfig(String accountId, String projectId, String version,
108110
null,
109111
projectId,
110112
revision,
113+
null,
114+
null,
111115
version,
112116
attributes,
113117
audiences,
@@ -127,6 +131,8 @@ public DatafileProjectConfig(String accountId,
127131
Boolean botFiltering,
128132
String projectId,
129133
String revision,
134+
String sdkKey,
135+
String environmentKey,
130136
String version,
131137
List<Attribute> attributes,
132138
List<Audience> audiences,
@@ -141,6 +147,8 @@ public DatafileProjectConfig(String accountId,
141147
this.projectId = projectId;
142148
this.version = version;
143149
this.revision = revision;
150+
this.sdkKey = sdkKey;
151+
this.environmentKey = environmentKey;
144152
this.anonymizeIP = anonymizeIP;
145153
this.sendFlagDecisions = sendFlagDecisions;
146154
this.botFiltering = botFiltering;
@@ -326,6 +334,16 @@ public String getRevision() {
326334
return revision;
327335
}
328336

337+
@Override
338+
public String getSdkKey() {
339+
return sdkKey;
340+
}
341+
342+
@Override
343+
public String getEnvironmentKey() {
344+
return environmentKey;
345+
}
346+
329347
@Override
330348
public boolean getSendFlagDecisions() { return sendFlagDecisions; }
331349

@@ -451,6 +469,8 @@ public String toString() {
451469
"accountId='" + accountId + '\'' +
452470
", projectId='" + projectId + '\'' +
453471
", revision='" + revision + '\'' +
472+
", sdkKey='" + sdkKey + '\'' +
473+
", environmentKey='" + environmentKey + '\'' +
454474
", version='" + version + '\'' +
455475
", anonymizeIP=" + anonymizeIP +
456476
", botFiltering=" + botFiltering +

core-api/src/main/java/com/optimizely/ab/config/ProjectConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2020, Optimizely and contributors
3+
* Copyright 2016-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -55,6 +55,10 @@ Experiment getExperimentForKey(@Nonnull String experimentKey,
5555

5656
String getRevision();
5757

58+
String getSdkKey();
59+
60+
String getEnvironmentKey();
61+
5862
boolean getSendFlagDecisions();
5963

6064
boolean getAnonymizeIP();

core-api/src/main/java/com/optimizely/ab/config/parser/DatafileGsonDeserializer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2020, Optimizely and contributors
3+
* Copyright 2016-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -87,6 +87,8 @@ public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializa
8787
List<FeatureFlag> featureFlags = null;
8888
List<Rollout> rollouts = null;
8989
Boolean botFiltering = null;
90+
String sdkKey = null;
91+
String environmentKey = null;
9092
boolean sendFlagDecisions = false;
9193
if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V4.toString())) {
9294
Type featureFlagsType = new TypeToken<List<FeatureFlag>>() {
@@ -95,6 +97,10 @@ public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializa
9597
Type rolloutsType = new TypeToken<List<Rollout>>() {
9698
}.getType();
9799
rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType);
100+
if (jsonObject.has("sdkKey"))
101+
sdkKey = jsonObject.get("sdkKey").getAsString();
102+
if (jsonObject.has("environmentKey"))
103+
environmentKey = jsonObject.get("environmentKey").getAsString();
98104
if (jsonObject.has("botFiltering"))
99105
botFiltering = jsonObject.get("botFiltering").getAsBoolean();
100106
if (jsonObject.has("sendFlagDecisions"))
@@ -108,6 +114,8 @@ public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializa
108114
botFiltering,
109115
projectId,
110116
revision,
117+
sdkKey,
118+
environmentKey,
111119
version,
112120
attributes,
113121
audiences,

core-api/src/main/java/com/optimizely/ab/config/parser/DatafileJacksonDeserializer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2020, Optimizely and contributors
3+
* Copyright 2016-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -63,11 +63,19 @@ public DatafileProjectConfig deserialize(JsonParser parser, DeserializationConte
6363

6464
List<FeatureFlag> featureFlags = null;
6565
List<Rollout> rollouts = null;
66+
String sdkKey = null;
67+
String environmentKey = null;
6668
Boolean botFiltering = null;
6769
boolean sendFlagDecisions = false;
6870
if (datafileVersion >= Integer.parseInt(DatafileProjectConfig.Version.V4.toString())) {
6971
featureFlags = JacksonHelpers.arrayNodeToList(node.get("featureFlags"), FeatureFlag.class, codec);
7072
rollouts = JacksonHelpers.arrayNodeToList(node.get("rollouts"), Rollout.class, codec);
73+
if (node.hasNonNull("sdkKey")) {
74+
sdkKey = node.get("sdkKey").textValue();
75+
}
76+
if (node.hasNonNull("environmentKey")) {
77+
environmentKey = node.get("environmentKey").textValue();
78+
}
7179
if (node.hasNonNull("botFiltering")) {
7280
botFiltering = node.get("botFiltering").asBoolean();
7381
}
@@ -83,6 +91,8 @@ public DatafileProjectConfig deserialize(JsonParser parser, DeserializationConte
8391
botFiltering,
8492
projectId,
8593
revision,
94+
sdkKey,
95+
environmentKey,
8696
version,
8797
attributes,
8898
audiences,

core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2019, 2020, Optimizely and contributors
3+
* Copyright 2016-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -72,11 +72,17 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
7272

7373
List<FeatureFlag> featureFlags = null;
7474
List<Rollout> rollouts = null;
75+
String sdkKey = null;
76+
String environmentKey = null;
7577
Boolean botFiltering = null;
7678
boolean sendFlagDecisions = false;
7779
if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
7880
featureFlags = parseFeatureFlags(rootObject.getJSONArray("featureFlags"));
7981
rollouts = parseRollouts(rootObject.getJSONArray("rollouts"));
82+
if (rootObject.has("sdkKey"))
83+
sdkKey = rootObject.getString("sdkKey");
84+
if (rootObject.has("environmentKey"))
85+
environmentKey = rootObject.getString("environmentKey");
8086
if (rootObject.has("botFiltering"))
8187
botFiltering = rootObject.getBoolean("botFiltering");
8288
if (rootObject.has("sendFlagDecisions"))
@@ -90,6 +96,8 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
9096
botFiltering,
9197
projectId,
9298
revision,
99+
sdkKey,
100+
environmentKey,
93101
version,
94102
attributes,
95103
audiences,
@@ -100,6 +108,8 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
100108
groups,
101109
rollouts
102110
);
111+
} catch (RuntimeException e) {
112+
throw new ConfigParseException("Unable to parse datafile: " + json, e);
103113
} catch (Exception e) {
104114
throw new ConfigParseException("Unable to parse datafile: " + json, e);
105115
}

core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2019, 2020, Optimizely and contributors
3+
* Copyright 2016-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -50,6 +50,8 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
5050
String accountId = (String) rootObject.get("accountId");
5151
String projectId = (String) rootObject.get("projectId");
5252
String revision = (String) rootObject.get("revision");
53+
String sdkKey = (String) rootObject.get("sdkKey");
54+
String environmentKey = (String) rootObject.get("environmentKey");
5355
String version = (String) rootObject.get("version");
5456
int datafileVersion = Integer.parseInt(version);
5557

@@ -97,6 +99,8 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
9799
botFiltering,
98100
projectId,
99101
revision,
102+
sdkKey,
103+
environmentKey,
100104
version,
101105
attributes,
102106
audiences,

core-api/src/main/java/com/optimizely/ab/optimizelyconfig/OptimizelyConfig.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2020, Optimizely, Inc. and contributors *
2+
* Copyright 2020-2021, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -16,31 +16,40 @@
1616
package com.optimizely.ab.optimizelyconfig;
1717

1818

19+
import com.fasterxml.jackson.annotation.JsonInclude;
20+
1921
import java.util.*;
2022

2123
/**
2224
* Interface for OptimizleyConfig
2325
*/
26+
@JsonInclude(JsonInclude.Include.NON_NULL)
2427
public class OptimizelyConfig {
2528

2629
private Map<String, OptimizelyExperiment> experimentsMap;
2730
private Map<String, OptimizelyFeature> featuresMap;
2831
private String revision;
32+
private String sdkKey;
33+
private String environmentKey;
2934
private String datafile;
3035

3136
public OptimizelyConfig(Map<String, OptimizelyExperiment> experimentsMap,
3237
Map<String, OptimizelyFeature> featuresMap,
33-
String revision) {
34-
this(experimentsMap, featuresMap, revision, null);
38+
String revision, String sdkKey, String environmentKey) {
39+
this(experimentsMap, featuresMap, revision, sdkKey, environmentKey, null);
3540
}
3641

3742
public OptimizelyConfig(Map<String, OptimizelyExperiment> experimentsMap,
3843
Map<String, OptimizelyFeature> featuresMap,
3944
String revision,
45+
String sdkKey,
46+
String environmentKey,
4047
String datafile) {
4148
this.experimentsMap = experimentsMap;
4249
this.featuresMap = featuresMap;
4350
this.revision = revision;
51+
this.sdkKey = sdkKey;
52+
this.environmentKey = environmentKey;
4453
this.datafile = datafile;
4554
}
4655

@@ -56,6 +65,12 @@ public String getRevision() {
5665
return revision;
5766
}
5867

68+
public String getSdkKey() { return sdkKey; }
69+
70+
public String getEnvironmentKey() {
71+
return environmentKey;
72+
}
73+
5974
public String getDatafile() {
6075
return datafile;
6176
}

core-api/src/main/java/com/optimizely/ab/optimizelyconfig/OptimizelyConfigService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2020, Optimizely, Inc. and contributors *
2+
* Copyright 2020-2021, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -32,6 +32,8 @@ public OptimizelyConfigService(ProjectConfig projectConfig) {
3232
experimentsMap,
3333
getFeaturesMap(experimentsMap),
3434
projectConfig.getRevision(),
35+
projectConfig.getSdkKey(),
36+
projectConfig.getEnvironmentKey(),
3537
projectConfig.toDatafile()
3638
);
3739
}

core-api/src/test/java/com/optimizely/ab/config/PollingProjectConfigManagerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2019-2020, Optimizely and contributors
3+
* Copyright 2019-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -156,6 +156,8 @@ public void testSetOptimizelyConfig(){
156156

157157
testProjectConfigManager.setConfig(projectConfig);
158158
assertEquals("1480511547", testProjectConfigManager.getOptimizelyConfig().getRevision());
159+
assertEquals("ValidProjectConfigV4", testProjectConfigManager.getOptimizelyConfig().getSdkKey());
160+
assertEquals("production", testProjectConfigManager.getOptimizelyConfig().getEnvironmentKey());
159161

160162
// cached config because project config is null
161163
testProjectConfigManager.setConfig(null);

core-api/src/test/java/com/optimizely/ab/config/ValidProjectConfigV4.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2017-2020, Optimizely and contributors
3+
* Copyright 2017-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@ public class ValidProjectConfigV4 {
3636
private static final boolean BOT_FILTERING = true;
3737
private static final String PROJECT_ID = "3918735994";
3838
private static final String REVISION = "1480511547";
39+
private static final String SDK_KEY = "ValidProjectConfigV4";
40+
private static final String ENVIRONMENT_KEY = "production";
3941
private static final String VERSION = "4";
4042
private static final Boolean SEND_FLAG_DECISIONS = true;
4143

@@ -1434,6 +1436,8 @@ public static ProjectConfig generateValidProjectConfigV4() {
14341436
BOT_FILTERING,
14351437
PROJECT_ID,
14361438
REVISION,
1439+
SDK_KEY,
1440+
ENVIRONMENT_KEY,
14371441
VERSION,
14381442
attributes,
14391443
audiences,

0 commit comments

Comments
 (0)