Skip to content

Commit a256a2e

Browse files
authored
feat: Add datafile accessor (#392)
1 parent 66d42c3 commit a256a2e

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2019, Optimizely and contributors
3+
* Copyright 2016-2020, 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.
@@ -43,7 +43,6 @@
4343
* Optimizely provides custom JSON parsers to extract objects from the JSON payload
4444
* to populate the members of this class. {@link DefaultConfigParser} for details.
4545
*/
46-
@Immutable
4746
@JsonIgnoreProperties(ignoreUnknown = true)
4847
public class DatafileProjectConfig implements ProjectConfig {
4948

@@ -88,6 +87,8 @@ public class DatafileProjectConfig implements ProjectConfig {
8887
// other mappings
8988
private final Map<String, Experiment> variationIdToExperimentMapping;
9089

90+
private String datafile;
91+
9192
// v2 constructor
9293
public DatafileProjectConfig(String accountId, String projectId, String version, String revision, List<Group> groups,
9394
List<Experiment> experiments, List<Attribute> attributes, List<EventType> eventType,
@@ -301,6 +302,11 @@ public String getAccountId() {
301302
return accountId;
302303
}
303304

305+
@Override
306+
public String toDatafile() {
307+
return datafile;
308+
}
309+
304310
@Override
305311
public String getProjectId() {
306312
return projectId;
@@ -481,6 +487,9 @@ public ProjectConfig build() throws ConfigParseException {
481487
}
482488

483489
ProjectConfig projectConfig = DefaultConfigParser.getInstance().parseProjectConfig(datafile);
490+
if (projectConfig instanceof DatafileProjectConfig) {
491+
((DatafileProjectConfig) projectConfig).datafile = datafile;
492+
}
484493

485494
if (!supportedVersions.contains(projectConfig.getVersion())) {
486495
throw new ConfigParseException("This version of the Java SDK does not support the given datafile version: " + projectConfig.getVersion());

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2016-2019, Optimizely and contributors
3+
* Copyright 2016-2020, 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.
@@ -47,6 +47,8 @@ Experiment getExperimentForKey(@Nonnull String experimentKey,
4747

4848
String getAccountId();
4949

50+
String toDatafile();
51+
5052
String getProjectId();
5153

5254
String getVersion();

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***************************************************************************/
1616
package com.optimizely.ab.optimizelyconfig;
1717

18+
1819
import java.util.*;
1920

2021
/**
@@ -25,13 +26,22 @@ public class OptimizelyConfig {
2526
private Map<String, OptimizelyExperiment> experimentsMap;
2627
private Map<String, OptimizelyFeature> featuresMap;
2728
private String revision;
29+
private String datafile;
2830

2931
public OptimizelyConfig(Map<String, OptimizelyExperiment> experimentsMap,
3032
Map<String, OptimizelyFeature> featuresMap,
3133
String revision) {
34+
this(experimentsMap, featuresMap, revision, null);
35+
}
36+
37+
public OptimizelyConfig(Map<String, OptimizelyExperiment> experimentsMap,
38+
Map<String, OptimizelyFeature> featuresMap,
39+
String revision,
40+
String datafile) {
3241
this.experimentsMap = experimentsMap;
3342
this.featuresMap = featuresMap;
34-
this.revision = revision;
43+
this.revision = revision;
44+
this.datafile = datafile;
3545
}
3646

3747
public Map<String, OptimizelyExperiment> getExperimentsMap() {
@@ -46,6 +56,10 @@ public String getRevision() {
4656
return revision;
4757
}
4858

59+
public String getDatafile() {
60+
return datafile;
61+
}
62+
4963
@Override
5064
public boolean equals(Object obj) {
5165
if (obj == null || getClass() != obj.getClass()) return false;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public OptimizelyConfigService(ProjectConfig projectConfig) {
3131
optimizelyConfig = new OptimizelyConfig(
3232
experimentsMap,
3333
getFeaturesMap(experimentsMap),
34-
projectConfig.getRevision()
34+
projectConfig.getRevision(),
35+
projectConfig.toDatafile()
3536
);
3637
}
3738

core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2019, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2020, 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. *
@@ -4572,4 +4572,11 @@ public void getForcedVariationEmptyExperimentKey() {
45724572
Optimizely optimizely = optimizelyBuilder.build();
45734573
assertNull(optimizely.getForcedVariation("", "testUser1"));
45744574
}
4575+
4576+
@Test
4577+
public void getOptimizelyConfigValidDatafile() {
4578+
Optimizely optimizely = optimizelyBuilder.build();
4579+
assertEquals(optimizely.getOptimizelyConfig().getDatafile(), validDatafile);
4580+
}
4581+
45754582
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public void withValidDatafile() throws Exception {
5555
ProjectConfig projectConfig = new DatafileProjectConfig.Builder()
5656
.withDatafile(validConfigJsonV4())
5757
.build();
58+
59+
assertEquals(projectConfig.toDatafile(), validConfigJsonV4());
5860
assertNotNull(projectConfig);
5961
assertEquals("4", projectConfig.getVersion());
6062
}

0 commit comments

Comments
 (0)