From 2640eaa289824a34bc60c33c90ab79dd68d65feb Mon Sep 17 00:00:00 2001 From: FarhanAnjum-opti Date: Mon, 11 Aug 2025 22:00:17 +0600 Subject: [PATCH 1/4] Cmab datafile parsed --- .../java/com/optimizely/ab/config/Cmab.java | 72 +++++++++++++++++++ .../com/optimizely/ab/config/Experiment.java | 35 +++++++-- .../java/com/optimizely/ab/config/Group.java | 3 +- .../ab/config/parser/GsonHelpers.java | 38 ++++++++-- .../ab/config/parser/JsonConfigParser.java | 27 ++++++- .../config/parser/JsonSimpleConfigParser.java | 33 ++++++++- 6 files changed, 193 insertions(+), 15 deletions(-) create mode 100644 core-api/src/main/java/com/optimizely/ab/config/Cmab.java diff --git a/core-api/src/main/java/com/optimizely/ab/config/Cmab.java b/core-api/src/main/java/com/optimizely/ab/config/Cmab.java new file mode 100644 index 000000000..738864e58 --- /dev/null +++ b/core-api/src/main/java/com/optimizely/ab/config/Cmab.java @@ -0,0 +1,72 @@ +/** + * + * Copyright 2025 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.optimizely.ab.config; + +import java.util.List; +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +/** + * Represents the Optimizely Traffic Allocation configuration. + * + * @see Project JSON + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class Cmab { + + private final List attributeIds; + private final int trafficAllocation; + + @JsonCreator + public Cmab(@JsonProperty("attributeIds") List attributeIds, + @JsonProperty("trafficAllocation") int trafficAllocation) { + this.attributeIds = attributeIds; + this.trafficAllocation = trafficAllocation; + } + + public List getAttributeIds() { + return attributeIds; + } + + public int getTrafficAllocation() { + return trafficAllocation; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Cmab cmab = (Cmab) obj; + return trafficAllocation == cmab.trafficAllocation && + Objects.equals(attributeIds, cmab.attributeIds); + } + + @Override + public int hashCode() { + return Objects.hash(attributeIds, trafficAllocation); + } + + @Override + public String toString() { + return "Cmab{" + + "attributeIds=" + attributeIds + + ", trafficAllocation=" + trafficAllocation + + '}'; + } +} \ No newline at end of file diff --git a/core-api/src/main/java/com/optimizely/ab/config/Experiment.java b/core-api/src/main/java/com/optimizely/ab/config/Experiment.java index 11530735c..1a61ce9d9 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/Experiment.java +++ b/core-api/src/main/java/com/optimizely/ab/config/Experiment.java @@ -41,6 +41,7 @@ public class Experiment implements IdKeyMapped { private final String status; private final String layerId; private final String groupId; + private final Cmab cmab; private final String AND = "AND"; private final String OR = "OR"; @@ -75,7 +76,25 @@ public String toString() { @VisibleForTesting public Experiment(String id, String key, String layerId) { - this(id, key, null, layerId, Collections.emptyList(), null, Collections.emptyList(), Collections.emptyMap(), Collections.emptyList(), ""); + this(id, key, null, layerId, Collections.emptyList(), null, Collections.emptyList(), Collections.emptyMap(), Collections.emptyList(), "", null); + } + + @VisibleForTesting + public Experiment(String id, String key, String status, String layerId, + List audienceIds, Condition audienceConditions, + List variations, Map userIdToVariationKeyMap, + List trafficAllocation, String groupId) { + this(id, key, status, layerId, audienceIds, audienceConditions, variations, + userIdToVariationKeyMap, trafficAllocation, groupId, null); // Default cmab=null + } + + @VisibleForTesting + public Experiment(String id, String key, String status, String layerId, + List audienceIds, Condition audienceConditions, + List variations, Map userIdToVariationKeyMap, + List trafficAllocation) { + this(id, key, status, layerId, audienceIds, audienceConditions, variations, + userIdToVariationKeyMap, trafficAllocation, "", null); // Default groupId="" and cmab=null } @JsonCreator @@ -87,8 +106,9 @@ public Experiment(@JsonProperty("id") String id, @JsonProperty("audienceConditions") Condition audienceConditions, @JsonProperty("variations") List variations, @JsonProperty("forcedVariations") Map userIdToVariationKeyMap, - @JsonProperty("trafficAllocation") List trafficAllocation) { - this(id, key, status, layerId, audienceIds, audienceConditions, variations, userIdToVariationKeyMap, trafficAllocation, ""); + @JsonProperty("trafficAllocation") List trafficAllocation, + @JsonProperty("cmab") Cmab cmab) { + this(id, key, status, layerId, audienceIds, audienceConditions, variations, userIdToVariationKeyMap, trafficAllocation, "", cmab); } public Experiment(@Nonnull String id, @@ -100,7 +120,8 @@ public Experiment(@Nonnull String id, @Nonnull List variations, @Nonnull Map userIdToVariationKeyMap, @Nonnull List trafficAllocation, - @Nonnull String groupId) { + @Nonnull String groupId, + @Nullable Cmab cmab) { this.id = id; this.key = key; this.status = status == null ? ExperimentStatus.NOT_STARTED.toString() : status; @@ -113,6 +134,7 @@ public Experiment(@Nonnull String id, this.userIdToVariationKeyMap = userIdToVariationKeyMap; this.variationKeyToVariationMap = ProjectConfigUtils.generateNameMapping(variations); this.variationIdToVariationMap = ProjectConfigUtils.generateIdMapping(variations); + this.cmab = cmab; } public String getId() { @@ -163,6 +185,10 @@ public String getGroupId() { return groupId; } + public Cmab getCmab() { + return cmab; + } + public boolean isActive() { return status.equals(ExperimentStatus.RUNNING.toString()) || status.equals(ExperimentStatus.LAUNCHED.toString()); @@ -281,6 +307,7 @@ public String toString() { ", variationKeyToVariationMap=" + variationKeyToVariationMap + ", userIdToVariationKeyMap=" + userIdToVariationKeyMap + ", trafficAllocation=" + trafficAllocation + + ", cmab=" + cmab + '}'; } } diff --git a/core-api/src/main/java/com/optimizely/ab/config/Group.java b/core-api/src/main/java/com/optimizely/ab/config/Group.java index afb068be4..d0d9ff364 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/Group.java +++ b/core-api/src/main/java/com/optimizely/ab/config/Group.java @@ -62,7 +62,8 @@ public Group(@JsonProperty("id") String id, experiment.getVariations(), experiment.getUserIdToVariationKeyMap(), experiment.getTrafficAllocation(), - id + id, + experiment.getCmab() ); } this.experiments.add(experiment); diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java b/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java index 1399497b2..184196fc6 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java @@ -24,13 +24,8 @@ import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; import com.optimizely.ab.bucketing.DecisionService; -import com.optimizely.ab.config.Experiment; +import com.optimizely.ab.config.*; import com.optimizely.ab.config.Experiment.ExperimentStatus; -import com.optimizely.ab.config.FeatureFlag; -import com.optimizely.ab.config.FeatureVariable; -import com.optimizely.ab.config.FeatureVariableUsageInstance; -import com.optimizely.ab.config.TrafficAllocation; -import com.optimizely.ab.config.Variation; import com.optimizely.ab.config.audience.AudienceIdCondition; import com.optimizely.ab.config.audience.Condition; import com.optimizely.ab.internal.ConditionUtils; @@ -118,6 +113,27 @@ static Condition parseAudienceConditions(JsonObject experimentJson) { } + static Cmab parseCmab(JsonObject cmabJson, JsonDeserializationContext context) { + if (cmabJson == null) { + return null; + } + + JsonArray attributeIdsJson = cmabJson.getAsJsonArray("attributeIds"); + List attributeIds = new ArrayList<>(); + if (attributeIdsJson != null) { + for (JsonElement attributeIdElement : attributeIdsJson) { + attributeIds.add(attributeIdElement.getAsString()); + } + } + + int trafficAllocation = 0; + if (cmabJson.has("trafficAllocation")) { + trafficAllocation = cmabJson.get("trafficAllocation").getAsInt(); + } + + return new Cmab(attributeIds, trafficAllocation); + } + static Experiment parseExperiment(JsonObject experimentJson, String groupId, JsonDeserializationContext context) { String id = experimentJson.get("id").getAsString(); String key = experimentJson.get("key").getAsString(); @@ -143,8 +159,16 @@ static Experiment parseExperiment(JsonObject experimentJson, String groupId, Jso List trafficAllocations = parseTrafficAllocation(experimentJson.getAsJsonArray("trafficAllocation")); + Cmab cmab = null; + if (experimentJson.has("cmab")) { + JsonObject cmabJson = experimentJson.getAsJsonObject("cmab"); + if (cmabJson != null) { + cmab = parseCmab(cmabJson, context); + } + } + return new Experiment(id, key, status, layerId, audienceIds, conditions, variations, userIdToVariationKeyMap, - trafficAllocations, groupId); + trafficAllocations, groupId, cmab); } static Experiment parseExperiment(JsonObject experimentJson, JsonDeserializationContext context) { diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java b/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java index ea5101054..71d446f72 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java @@ -159,8 +159,16 @@ private List parseExperiments(JSONArray experimentJson, String group List trafficAllocations = parseTrafficAllocation(experimentObject.getJSONArray("trafficAllocation")); + Cmab cmab = null; + if (experimentObject.has("cmab")) { + JSONObject cmabObject = experimentObject.optJSONObject("cmab"); + if (cmabObject != null) { + cmab = parseCmab(cmabObject); + } + } + experiments.add(new Experiment(id, key, status, layerId, audienceIds, conditions, variations, userIdToVariationKeyMap, - trafficAllocations, groupId)); + trafficAllocations, groupId, cmab)); } return experiments; @@ -255,6 +263,23 @@ private List parseTrafficAllocation(JSONArray trafficAllocati return trafficAllocation; } + private Cmab parseCmab(JSONObject cmabObject) { + if (cmabObject == null) { + return null; + } + + JSONArray attributeIdsJson = cmabObject.optJSONArray("attributeIds"); + List attributeIds = new ArrayList(); + if (attributeIdsJson != null) { + for (int i = 0; i < attributeIdsJson.length(); i++) { + attributeIds.add(attributeIdsJson.getString(i)); + } + } + + int trafficAllocation = cmabObject.optInt("trafficAllocation", 0); + return new Cmab(attributeIds, trafficAllocation); + } + private List parseAttributes(JSONArray attributeJson) { List attributes = new ArrayList(attributeJson.length()); diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java b/core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java index c65eb6213..a45f922b5 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java @@ -166,8 +166,17 @@ private List parseExperiments(JSONArray experimentJson, String group List trafficAllocations = parseTrafficAllocation((JSONArray) experimentObject.get("trafficAllocation")); - experiments.add(new Experiment(id, key, status, layerId, audienceIds, conditions, variations, userIdToVariationKeyMap, - trafficAllocations, groupId)); + // Add cmab parsing + Cmab cmab = null; + if (experimentObject.containsKey("cmab")) { + JSONObject cmabObject = (JSONObject) experimentObject.get("cmab"); + if (cmabObject != null) { + cmab = parseCmab(cmabObject); + } + } + + experiments.add(new Experiment(id, key, status, layerId, audienceIds, conditions, variations, + userIdToVariationKeyMap, trafficAllocations, groupId, cmab)); } return experiments; @@ -398,6 +407,26 @@ private List parseIntegrations(JSONArray integrationsJson) { return integrations; } + private Cmab parseCmab(JSONObject cmabObject) { + if (cmabObject == null) { + return null; + } + + JSONArray attributeIdsJson = (JSONArray) cmabObject.get("attributeIds"); + List attributeIds = new ArrayList<>(); + if (attributeIdsJson != null) { + for (Object idObj : attributeIdsJson) { + attributeIds.add((String) idObj); + } + } + + Object trafficAllocationObj = cmabObject.get("trafficAllocation"); + int trafficAllocation = trafficAllocationObj != null ? + ((Long) trafficAllocationObj).intValue() : 0; + + return new Cmab(attributeIds, trafficAllocation); + } + @Override public String toJson(Object src) { return JSONValue.toJSONString(src); From 2d21ee2c55f6d7220206972f0dfc4b72436ec680 Mon Sep 17 00:00:00 2001 From: FarhanAnjum-opti Date: Tue, 12 Aug 2025 22:19:42 +0600 Subject: [PATCH 2/4] Add CMAB configuration and parsing tests with cmab datafile --- .../ab/config/parser/GsonHelpers.java | 5 +- .../java/com/optimizely/ab/cmab/CmabTest.java | 160 ++++++++++++ .../ab/cmab/parser/CmabParsingTest.java | 233 ++++++++++++++++++ .../test/resources/config/cmab-config.json | 226 +++++++++++++++++ 4 files changed, 622 insertions(+), 2 deletions(-) create mode 100644 core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java create mode 100644 core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java create mode 100644 core-api/src/test/resources/config/cmab-config.json diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java b/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java index 74e9eecef..624f9f159 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java @@ -161,8 +161,9 @@ static Experiment parseExperiment(JsonObject experimentJson, String groupId, Jso Cmab cmab = null; if (experimentJson.has("cmab")) { - JsonObject cmabJson = experimentJson.getAsJsonObject("cmab"); - if (cmabJson != null) { + JsonElement cmabElement = experimentJson.get("cmab"); + if (!cmabElement.isJsonNull()) { + JsonObject cmabJson = cmabElement.getAsJsonObject(); cmab = parseCmab(cmabJson, context); } } diff --git a/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java b/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java new file mode 100644 index 000000000..665f015a1 --- /dev/null +++ b/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java @@ -0,0 +1,160 @@ +package com.optimizely.ab.cmab; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +import com.optimizely.ab.config.Cmab; + +/** + * Tests for {@link Cmab} configuration object. + */ +public class CmabTest { + + @Test + public void testCmabConstructorWithValidData() { + List attributeIds = Arrays.asList("attr1", "attr2", "attr3"); + int trafficAllocation = 4000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should match", attributeIds, cmab.getAttributeIds()); + assertEquals("TrafficAllocation should match", trafficAllocation, cmab.getTrafficAllocation()); + } + + @Test + public void testCmabConstructorWithEmptyAttributeIds() { + List attributeIds = Collections.emptyList(); + int trafficAllocation = 2000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should be empty", attributeIds, cmab.getAttributeIds()); + assertTrue("AttributeIds should be empty list", cmab.getAttributeIds().isEmpty()); + assertEquals("TrafficAllocation should match", trafficAllocation, cmab.getTrafficAllocation()); + } + + @Test + public void testCmabConstructorWithSingleAttributeId() { + List attributeIds = Collections.singletonList("single_attr"); + int trafficAllocation = 3000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should match", attributeIds, cmab.getAttributeIds()); + assertEquals("Should have one attribute", 1, cmab.getAttributeIds().size()); + assertEquals("Single attribute should match", "single_attr", cmab.getAttributeIds().get(0)); + assertEquals("TrafficAllocation should match", trafficAllocation, cmab.getTrafficAllocation()); + } + + @Test + public void testCmabConstructorWithZeroTrafficAllocation() { + List attributeIds = Arrays.asList("attr1", "attr2"); + int trafficAllocation = 0; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should match", attributeIds, cmab.getAttributeIds()); + assertEquals("TrafficAllocation should be zero", 0, cmab.getTrafficAllocation()); + } + + @Test + public void testCmabConstructorWithMaxTrafficAllocation() { + List attributeIds = Arrays.asList("attr1"); + int trafficAllocation = 10000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should match", attributeIds, cmab.getAttributeIds()); + assertEquals("TrafficAllocation should be 10000", 10000, cmab.getTrafficAllocation()); + } + + @Test + public void testCmabEqualsAndHashCode() { + List attributeIds1 = Arrays.asList("attr1", "attr2"); + List attributeIds2 = Arrays.asList("attr1", "attr2"); + List attributeIds3 = Arrays.asList("attr1", "attr3"); + + Cmab cmab1 = new Cmab(attributeIds1, 4000); + Cmab cmab2 = new Cmab(attributeIds2, 4000); + Cmab cmab3 = new Cmab(attributeIds3, 4000); + Cmab cmab4 = new Cmab(attributeIds1, 5000); + + // Test equals + assertEquals("CMAB with same data should be equal", cmab1, cmab2); + assertNotEquals("CMAB with different attributeIds should not be equal", cmab1, cmab3); + assertNotEquals("CMAB with different trafficAllocation should not be equal", cmab1, cmab4); + + // Test reflexivity + assertEquals("CMAB should equal itself", cmab1, cmab1); + + // Test null comparison + assertNotEquals("CMAB should not equal null", cmab1, null); + + // Test hashCode consistency + assertEquals("Equal objects should have same hashCode", cmab1.hashCode(), cmab2.hashCode()); + } + + @Test + public void testCmabToString() { + List attributeIds = Arrays.asList("attr1", "attr2"); + int trafficAllocation = 4000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + String result = cmab.toString(); + + assertNotNull("toString should not return null", result); + assertTrue("toString should contain attributeIds", result.contains("attributeIds")); + assertTrue("toString should contain trafficAllocation", result.contains("trafficAllocation")); + assertTrue("toString should contain attr1", result.contains("attr1")); + assertTrue("toString should contain attr2", result.contains("attr2")); + assertTrue("toString should contain 4000", result.contains("4000")); + } + + @Test + public void testCmabToStringWithEmptyAttributeIds() { + List attributeIds = Collections.emptyList(); + int trafficAllocation = 2000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + String result = cmab.toString(); + + assertNotNull("toString should not return null", result); + assertTrue("toString should contain attributeIds", result.contains("attributeIds")); + assertTrue("toString should contain trafficAllocation", result.contains("trafficAllocation")); + assertTrue("toString should contain 2000", result.contains("2000")); + } + + @Test + public void testCmabWithDuplicateAttributeIds() { + List attributeIds = Arrays.asList("attr1", "attr2", "attr1", "attr3"); + int trafficAllocation = 4000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should match exactly (including duplicates)", + attributeIds, cmab.getAttributeIds()); + assertEquals("Should have 4 elements (including duplicate)", 4, cmab.getAttributeIds().size()); + } + + @Test + public void testCmabWithRealWorldAttributeIds() { + // Test with realistic attribute IDs from Optimizely + List attributeIds = Arrays.asList("808797688", "808797689", "10401066117"); + int trafficAllocation = 4000; + + Cmab cmab = new Cmab(attributeIds, trafficAllocation); + + assertEquals("AttributeIds should match", attributeIds, cmab.getAttributeIds()); + assertEquals("TrafficAllocation should match", trafficAllocation, cmab.getTrafficAllocation()); + assertTrue("Should contain first attribute ID", cmab.getAttributeIds().contains("808797688")); + assertTrue("Should contain second attribute ID", cmab.getAttributeIds().contains("808797689")); + assertTrue("Should contain third attribute ID", cmab.getAttributeIds().contains("10401066117")); + } +} \ No newline at end of file diff --git a/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java b/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java new file mode 100644 index 000000000..bcc8beafe --- /dev/null +++ b/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java @@ -0,0 +1,233 @@ +package com.optimizely.ab.cmab.parser; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import com.optimizely.ab.config.Cmab; +import com.optimizely.ab.config.Experiment; +import com.optimizely.ab.config.Group; +import com.optimizely.ab.config.ProjectConfig; +import com.optimizely.ab.config.parser.ConfigParseException; +import com.optimizely.ab.config.parser.ConfigParser; +import com.optimizely.ab.config.parser.GsonConfigParser; +import com.optimizely.ab.config.parser.JacksonConfigParser; +import com.optimizely.ab.config.parser.JsonConfigParser; +import com.optimizely.ab.config.parser.JsonSimpleConfigParser; + +/** + * Tests CMAB parsing across all config parsers using real datafile + */ +@RunWith(Parameterized.class) +public class CmabParsingTest { + + @Parameterized.Parameters(name = "{index}: {0}") + public static Collection data() { + return Arrays.asList(new Object[][]{ + {"JsonSimpleConfigParser", new JsonSimpleConfigParser()}, + {"GsonConfigParser", new GsonConfigParser()}, + {"JacksonConfigParser", new JacksonConfigParser()}, + {"JsonConfigParser", new JsonConfigParser()} + }); + } + + private final String parserName; + private final ConfigParser parser; + + public CmabParsingTest(String parserName, ConfigParser parser) { + this.parserName = parserName; + this.parser = parser; + } + + private String loadCmabDatafile() throws IOException { + return Resources.toString(Resources.getResource("config/cmab-config.json"), Charsets.UTF_8); + } + + @Test + public void testParseExperimentWithValidCmab() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + Experiment experiment = config.getExperimentKeyMapping().get("exp_with_cmab"); + assertNotNull("Experiment 'exp_with_cmab' should exist in " + parserName, experiment); + + Cmab cmab = experiment.getCmab(); + assertNotNull("CMAB should not be null for experiment with CMAB in " + parserName, cmab); + + assertEquals("Should have 2 attribute IDs in " + parserName, 2, cmab.getAttributeIds().size()); + assertTrue("Should contain attribute '10401066117' in " + parserName, + cmab.getAttributeIds().contains("10401066117")); + assertTrue("Should contain attribute '10401066170' in " + parserName, + cmab.getAttributeIds().contains("10401066170")); + assertEquals("Traffic allocation should be 4000 in " + parserName, 4000, cmab.getTrafficAllocation()); + } + + @Test + public void testParseExperimentWithoutCmab() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + Experiment experiment = config.getExperimentKeyMapping().get("exp_without_cmab"); + assertNotNull("Experiment 'exp_without_cmab' should exist in " + parserName, experiment); + assertNull("CMAB should be null when not specified in " + parserName, experiment.getCmab()); + } + + @Test + public void testParseExperimentWithEmptyAttributeIds() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + Experiment experiment = config.getExperimentKeyMapping().get("exp_with_empty_cmab"); + assertNotNull("Experiment 'exp_with_empty_cmab' should exist in " + parserName, experiment); + + Cmab cmab = experiment.getCmab(); + assertNotNull("CMAB should not be null even with empty attributeIds in " + parserName, cmab); + assertTrue("AttributeIds should be empty in " + parserName, cmab.getAttributeIds().isEmpty()); + assertEquals("Traffic allocation should be 2000 in " + parserName, 2000, cmab.getTrafficAllocation()); + } + + @Test + public void testParseExperimentWithNullCmab() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + Experiment experiment = config.getExperimentKeyMapping().get("exp_with_null_cmab"); + assertNotNull("Experiment 'exp_with_null_cmab' should exist in " + parserName, experiment); + assertNull("CMAB should be null when explicitly set to null in " + parserName, experiment.getCmab()); + } + + @Test + public void testParseGroupExperimentWithCmab() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + // Find the group experiment + Experiment groupExperiment = null; + for (Group group : config.getGroups()) { + for (Experiment exp : group.getExperiments()) { + if ("group_exp_with_cmab".equals(exp.getKey())) { + groupExperiment = exp; + break; + } + } + } + + assertNotNull("Group experiment 'group_exp_with_cmab' should exist in " + parserName, groupExperiment); + + Cmab cmab = groupExperiment.getCmab(); + assertNotNull("Group experiment CMAB should not be null in " + parserName, cmab); + assertEquals("Should have 1 attribute ID in " + parserName, 1, cmab.getAttributeIds().size()); + assertEquals("Should contain correct attribute in " + parserName, + "10401066117", cmab.getAttributeIds().get(0)); + assertEquals("Traffic allocation should be 6000 in " + parserName, 6000, cmab.getTrafficAllocation()); + } + + @Test + public void testParseAllExperimentsFromDatafile() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + // Check all expected experiments exist + assertTrue("Should have 'exp_with_cmab' in " + parserName, + config.getExperimentKeyMapping().containsKey("exp_with_cmab")); + assertTrue("Should have 'exp_without_cmab' in " + parserName, + config.getExperimentKeyMapping().containsKey("exp_without_cmab")); + assertTrue("Should have 'exp_with_empty_cmab' in " + parserName, + config.getExperimentKeyMapping().containsKey("exp_with_empty_cmab")); + assertTrue("Should have 'exp_with_null_cmab' in " + parserName, + config.getExperimentKeyMapping().containsKey("exp_with_null_cmab")); + } + + @Test + public void testParseProjectConfigStructure() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + // Verify basic project config data + assertEquals("Project ID should match in " + parserName, "10431130345", config.getProjectId()); + assertEquals("Account ID should match in " + parserName, "10367498574", config.getAccountId()); + assertEquals("Version should match in " + parserName, "4", config.getVersion()); + assertEquals("Revision should match in " + parserName, "241", config.getRevision()); + + // Verify component counts based on your cmab-config.json + assertEquals("Should have 5 experiments in " + parserName, 5, config.getExperiments().size()); + assertEquals("Should have 2 audiences in " + parserName, 2, config.getAudiences().size()); + assertEquals("Should have 2 attributes in " + parserName, 2, config.getAttributes().size()); + assertEquals("Should have 1 event in " + parserName, 1, config.getEventTypes().size()); + assertEquals("Should have 1 group in " + parserName, 1, config.getGroups().size()); + assertEquals("Should have 1 feature flag in " + parserName, 1, config.getFeatureFlags().size()); + } + + @Test + public void testCmabFieldsAreCorrectlyParsed() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + // Test experiment with full CMAB + Experiment expWithCmab = config.getExperimentKeyMapping().get("exp_with_cmab"); + Cmab cmab = expWithCmab.getCmab(); + + assertNotNull("CMAB object should exist in " + parserName, cmab); + assertEquals("CMAB should have exactly 2 attributes in " + parserName, + Arrays.asList("10401066117", "10401066170"), cmab.getAttributeIds()); + assertEquals("CMAB traffic allocation should be 4000 in " + parserName, 4000, cmab.getTrafficAllocation()); + + // Test experiment with empty CMAB + Experiment expWithEmptyCmab = config.getExperimentKeyMapping().get("exp_with_empty_cmab"); + Cmab emptyCmab = expWithEmptyCmab.getCmab(); + + assertNotNull("Empty CMAB object should exist in " + parserName, emptyCmab); + assertTrue("CMAB attributeIds should be empty in " + parserName, emptyCmab.getAttributeIds().isEmpty()); + assertEquals("Empty CMAB traffic allocation should be 2000 in " + parserName, + 2000, emptyCmab.getTrafficAllocation()); + } + + @Test + public void testExperimentIdsAndKeysMatch() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + // Verify experiment IDs and keys from your datafile + Experiment expWithCmab = config.getExperimentKeyMapping().get("exp_with_cmab"); + assertEquals("exp_with_cmab ID should match in " + parserName, "10390977673", expWithCmab.getId()); + + Experiment expWithoutCmab = config.getExperimentKeyMapping().get("exp_without_cmab"); + assertEquals("exp_without_cmab ID should match in " + parserName, "10420810910", expWithoutCmab.getId()); + + Experiment expWithEmptyCmab = config.getExperimentKeyMapping().get("exp_with_empty_cmab"); + assertEquals("exp_with_empty_cmab ID should match in " + parserName, "10420810911", expWithEmptyCmab.getId()); + + Experiment expWithNullCmab = config.getExperimentKeyMapping().get("exp_with_null_cmab"); + assertEquals("exp_with_null_cmab ID should match in " + parserName, "10420810912", expWithNullCmab.getId()); + } + + @Test + public void testCmabDoesNotAffectOtherExperimentFields() throws IOException, ConfigParseException { + String datafile = loadCmabDatafile(); + ProjectConfig config = parser.parseProjectConfig(datafile); + + Experiment expWithCmab = config.getExperimentKeyMapping().get("exp_with_cmab"); + + // Verify other fields are still parsed correctly + assertEquals("Experiment status should be parsed correctly in " + parserName, + "Running", expWithCmab.getStatus()); + assertEquals("Experiment should have correct layer ID in " + parserName, + "10420273888", expWithCmab.getLayerId()); + assertEquals("Experiment should have 2 variations in " + parserName, + 2, expWithCmab.getVariations().size()); + assertEquals("Experiment should have 1 audience in " + parserName, + 1, expWithCmab.getAudienceIds().size()); + assertEquals("Experiment should have correct audience ID in " + parserName, + "13389141123", expWithCmab.getAudienceIds().get(0)); + } +} \ No newline at end of file diff --git a/core-api/src/test/resources/config/cmab-config.json b/core-api/src/test/resources/config/cmab-config.json new file mode 100644 index 000000000..505308cda --- /dev/null +++ b/core-api/src/test/resources/config/cmab-config.json @@ -0,0 +1,226 @@ +{ + "version": "4", + "sendFlagDecisions": true, + "rollouts": [ + { + "experiments": [ + { + "audienceIds": ["13389130056"], + "forcedVariations": {}, + "id": "3332020515", + "key": "3332020515", + "layerId": "3319450668", + "status": "Running", + "trafficAllocation": [ + { + "endOfRange": 10000, + "entityId": "3324490633" + } + ], + "variations": [ + { + "featureEnabled": true, + "id": "3324490633", + "key": "3324490633", + "variables": [] + } + ] + } + ], + "id": "3319450668" + } + ], + "anonymizeIP": true, + "botFiltering": true, + "projectId": "10431130345", + "variables": [], + "featureFlags": [ + { + "experimentIds": ["10390977673"], + "id": "4482920077", + "key": "feature_1", + "rolloutId": "3319450668", + "variables": [ + { + "defaultValue": "42", + "id": "2687470095", + "key": "i_42", + "type": "integer" + } + ] + } + ], + "experiments": [ + { + "status": "Running", + "key": "exp_with_cmab", + "layerId": "10420273888", + "trafficAllocation": [ + { + "entityId": "10389729780", + "endOfRange": 10000 + } + ], + "audienceIds": ["13389141123"], + "variations": [ + { + "variables": [], + "featureEnabled": true, + "id": "10389729780", + "key": "variation_a" + }, + { + "variables": [], + "id": "10416523121", + "key": "variation_b" + } + ], + "forcedVariations": {}, + "id": "10390977673", + "cmab": { + "attributeIds": ["10401066117", "10401066170"], + "trafficAllocation": 4000 + } + }, + { + "status": "Running", + "key": "exp_without_cmab", + "layerId": "10417730432", + "trafficAllocation": [ + { + "entityId": "10418551353", + "endOfRange": 10000 + } + ], + "audienceIds": [], + "variations": [ + { + "variables": [], + "featureEnabled": true, + "id": "10418551353", + "key": "variation_with_traffic" + }, + { + "variables": [], + "featureEnabled": false, + "id": "10418510624", + "key": "variation_no_traffic" + } + ], + "forcedVariations": {}, + "id": "10420810910" + }, + { + "status": "Running", + "key": "exp_with_empty_cmab", + "layerId": "10417730433", + "trafficAllocation": [], + "audienceIds": [], + "variations": [ + { + "variables": [], + "featureEnabled": true, + "id": "10418551354", + "key": "variation_empty_cmab" + } + ], + "forcedVariations": {}, + "id": "10420810911", + "cmab": { + "attributeIds": [], + "trafficAllocation": 2000 + } + }, + { + "status": "Running", + "key": "exp_with_null_cmab", + "layerId": "10417730434", + "trafficAllocation": [ + { + "entityId": "10418551355", + "endOfRange": 7500 + } + ], + "audienceIds": [], + "variations": [ + { + "variables": [], + "featureEnabled": true, + "id": "10418551355", + "key": "variation_null_cmab" + } + ], + "forcedVariations": {}, + "id": "10420810912", + "cmab": null + } + ], + "audiences": [ + { + "id": "13389141123", + "conditions": "[\"and\", [\"or\", [\"or\", {\"match\": \"exact\", \"name\": \"gender\", \"type\": \"custom_attribute\", \"value\": \"f\"}]]]", + "name": "gender" + }, + { + "id": "13389130056", + "conditions": "[\"and\", [\"or\", [\"or\", {\"match\": \"exact\", \"name\": \"country\", \"type\": \"custom_attribute\", \"value\": \"US\"}]]]", + "name": "US" + } + ], + "groups": [ + { + "policy": "random", + "trafficAllocation": [ + { + "entityId": "10390965532", + "endOfRange": 10000 + } + ], + "experiments": [ + { + "status": "Running", + "key": "group_exp_with_cmab", + "layerId": "10420222423", + "trafficAllocation": [], + "audienceIds": [], + "variations": [ + { + "variables": [], + "featureEnabled": false, + "id": "10389752311", + "key": "group_variation_a" + } + ], + "forcedVariations": {}, + "id": "10390965532", + "cmab": { + "attributeIds": ["10401066117"], + "trafficAllocation": 6000 + } + } + ], + "id": "13142870430" + } + ], + "attributes": [ + { + "id": "10401066117", + "key": "gender" + }, + { + "id": "10401066170", + "key": "age" + } + ], + "accountId": "10367498574", + "events": [ + { + "experimentIds": [ + "10420810910" + ], + "id": "10404198134", + "key": "event1" + } + ], + "revision": "241" +} \ No newline at end of file From 0d3a88df5314654a5e30d613d07bc71e195b6d96 Mon Sep 17 00:00:00 2001 From: FarhanAnjum-opti Date: Tue, 12 Aug 2025 22:24:36 +0600 Subject: [PATCH 3/4] Add copyright notice to CmabTest and CmabParsingTest files --- .../java/com/optimizely/ab/cmab/CmabTest.java | 16 ++++++++++++++++ .../ab/cmab/parser/CmabParsingTest.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java b/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java index 665f015a1..40f1340b7 100644 --- a/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java +++ b/core-api/src/test/java/com/optimizely/ab/cmab/CmabTest.java @@ -1,3 +1,19 @@ +/* + * + * Copyright 2025 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.optimizely.ab.cmab; import java.util.Arrays; diff --git a/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java b/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java index bcc8beafe..4a6ed8f20 100644 --- a/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java +++ b/core-api/src/test/java/com/optimizely/ab/cmab/parser/CmabParsingTest.java @@ -1,3 +1,19 @@ +/** + * + * Copyright 2025 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.optimizely.ab.cmab.parser; import java.io.IOException; From 50334f11aa2ca327d0f9a6e215acb2b0f66ae68d Mon Sep 17 00:00:00 2001 From: FarhanAnjum-opti Date: Wed, 13 Aug 2025 03:59:44 +0600 Subject: [PATCH 4/4] Refactor cmab parsing logic to simplify null check in JsonConfigParser --- .../com/optimizely/ab/config/parser/JsonConfigParser.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java b/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java index 06d3ef4d2..10ca9685f 100644 --- a/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java +++ b/core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java @@ -176,9 +176,7 @@ private List parseExperiments(JSONArray experimentJson, String group Cmab cmab = null; if (experimentObject.has("cmab")) { JSONObject cmabObject = experimentObject.optJSONObject("cmab"); - if (cmabObject != null) { - cmab = parseCmab(cmabObject); - } + cmab = parseCmab(cmabObject); } experiments.add(new Experiment(id, key, status, layerId, audienceIds, conditions, variations, userIdToVariationKeyMap,