Skip to content

feat: Add datafile accessor #392

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 7 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2019, Optimizely and contributors
* Copyright 2016-2020, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,7 +43,6 @@
* Optimizely provides custom JSON parsers to extract objects from the JSON payload
* to populate the members of this class. {@link DefaultConfigParser} for details.
*/
@Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
public class DatafileProjectConfig implements ProjectConfig {

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

private String datafile;

// v2 constructor
public DatafileProjectConfig(String accountId, String projectId, String version, String revision, List<Group> groups,
List<Experiment> experiments, List<Attribute> attributes, List<EventType> eventType,
Expand Down Expand Up @@ -301,6 +302,11 @@ public String getAccountId() {
return accountId;
}

@Override
public String toDatafile() {
return datafile;
}

@Override
public String getProjectId() {
return projectId;
Expand Down Expand Up @@ -481,6 +487,9 @@ public ProjectConfig build() throws ConfigParseException {
}

ProjectConfig projectConfig = DefaultConfigParser.getInstance().parseProjectConfig(datafile);
if (projectConfig instanceof DatafileProjectConfig) {
((DatafileProjectConfig) projectConfig).datafile = datafile;
}

if (!supportedVersions.contains(projectConfig.getVersion())) {
throw new ConfigParseException("This version of the Java SDK does not support the given datafile version: " + projectConfig.getVersion());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2019, Optimizely and contributors
* Copyright 2016-2020, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,6 +47,8 @@ Experiment getExperimentForKey(@Nonnull String experimentKey,

String getAccountId();

String toDatafile();

String getProjectId();

String getVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
***************************************************************************/
package com.optimizely.ab.optimizelyconfig;


import java.util.*;

/**
Expand All @@ -25,13 +26,22 @@ public class OptimizelyConfig {
private Map<String, OptimizelyExperiment> experimentsMap;
private Map<String, OptimizelyFeature> featuresMap;
private String revision;
private String datafile;

public OptimizelyConfig(Map<String, OptimizelyExperiment> experimentsMap,
Map<String, OptimizelyFeature> featuresMap,
String revision) {
this(experimentsMap, featuresMap, revision, null);
}

public OptimizelyConfig(Map<String, OptimizelyExperiment> experimentsMap,
Map<String, OptimizelyFeature> featuresMap,
String revision,
String datafile) {
this.experimentsMap = experimentsMap;
this.featuresMap = featuresMap;
this.revision = revision;
this.revision = revision;
this.datafile = datafile;
}

public Map<String, OptimizelyExperiment> getExperimentsMap() {
Expand All @@ -46,6 +56,10 @@ public String getRevision() {
return revision;
}

public String getDatafile() {
return datafile;
}

@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public OptimizelyConfigService(ProjectConfig projectConfig) {
optimizelyConfig = new OptimizelyConfig(
experimentsMap,
getFeaturesMap(experimentsMap),
projectConfig.getRevision()
projectConfig.getRevision(),
projectConfig.toDatafile()
);
}

Expand Down
9 changes: 8 additions & 1 deletion core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2019, Optimizely, Inc. and contributors *
* Copyright 2016-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -4572,4 +4572,11 @@ public void getForcedVariationEmptyExperimentKey() {
Optimizely optimizely = optimizelyBuilder.build();
assertNull(optimizely.getForcedVariation("", "testUser1"));
}

@Test
public void getOptimizelyConfigValidDatafile() {
Optimizely optimizely = optimizelyBuilder.build();
assertEquals(optimizely.getOptimizelyConfig().getDatafile(), validDatafile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void withValidDatafile() throws Exception {
ProjectConfig projectConfig = new DatafileProjectConfig.Builder()
.withDatafile(validConfigJsonV4())
.build();

assertEquals(projectConfig.toDatafile(), validConfigJsonV4());
assertNotNull(projectConfig);
assertEquals("4", projectConfig.getVersion());
}
Expand Down