|
| 1 | +/* |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.IO; |
| 19 | +using Newtonsoft.Json; |
| 20 | +using Newtonsoft.Json.Linq; |
| 21 | +using NUnit.Framework; |
| 22 | +using OptimizelySDK.Entity; |
| 23 | + |
| 24 | +namespace OptimizelySDK.Tests |
| 25 | +{ |
| 26 | + [TestFixture] |
| 27 | + public class HoldoutTests |
| 28 | + { |
| 29 | + private JObject testData; |
| 30 | + |
| 31 | + [SetUp] |
| 32 | + public void Setup() |
| 33 | + { |
| 34 | + // Load test data |
| 35 | + var testDataPath = Path.Combine(TestContext.CurrentContext.TestDirectory, |
| 36 | + "TestData", "HoldoutTestData.json"); |
| 37 | + var jsonContent = File.ReadAllText(testDataPath); |
| 38 | + testData = JObject.Parse(jsonContent); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void TestHoldoutDeserialization() |
| 43 | + { |
| 44 | + // Test global holdout deserialization |
| 45 | + var globalHoldoutJson = testData["globalHoldout"].ToString(); |
| 46 | + var globalHoldout = JsonConvert.DeserializeObject<Holdout>(globalHoldoutJson); |
| 47 | + |
| 48 | + Assert.IsNotNull(globalHoldout); |
| 49 | + Assert.AreEqual("holdout_global_1", globalHoldout.Id); |
| 50 | + Assert.AreEqual("global_holdout", globalHoldout.Key); |
| 51 | + Assert.AreEqual("Running", globalHoldout.Status); |
| 52 | + Assert.AreEqual("layer_1", globalHoldout.LayerId); |
| 53 | + Assert.IsNotNull(globalHoldout.Variations); |
| 54 | + Assert.AreEqual(1, globalHoldout.Variations.Length); |
| 55 | + Assert.IsNotNull(globalHoldout.TrafficAllocation); |
| 56 | + Assert.AreEqual(1, globalHoldout.TrafficAllocation.Length); |
| 57 | + Assert.IsNotNull(globalHoldout.IncludedFlags); |
| 58 | + Assert.AreEqual(0, globalHoldout.IncludedFlags.Length); |
| 59 | + Assert.IsNotNull(globalHoldout.ExcludedFlags); |
| 60 | + Assert.AreEqual(0, globalHoldout.ExcludedFlags.Length); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void TestHoldoutWithIncludedFlags() |
| 65 | + { |
| 66 | + var includedHoldoutJson = testData["includedFlagsHoldout"].ToString(); |
| 67 | + var includedHoldout = JsonConvert.DeserializeObject<Holdout>(includedHoldoutJson); |
| 68 | + |
| 69 | + Assert.IsNotNull(includedHoldout); |
| 70 | + Assert.AreEqual("holdout_included_1", includedHoldout.Id); |
| 71 | + Assert.AreEqual("included_holdout", includedHoldout.Key); |
| 72 | + Assert.IsNotNull(includedHoldout.IncludedFlags); |
| 73 | + Assert.AreEqual(2, includedHoldout.IncludedFlags.Length); |
| 74 | + Assert.Contains("flag_1", includedHoldout.IncludedFlags); |
| 75 | + Assert.Contains("flag_2", includedHoldout.IncludedFlags); |
| 76 | + Assert.IsNotNull(includedHoldout.ExcludedFlags); |
| 77 | + Assert.AreEqual(0, includedHoldout.ExcludedFlags.Length); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void TestHoldoutWithExcludedFlags() |
| 82 | + { |
| 83 | + var excludedHoldoutJson = testData["excludedFlagsHoldout"].ToString(); |
| 84 | + var excludedHoldout = JsonConvert.DeserializeObject<Holdout>(excludedHoldoutJson); |
| 85 | + |
| 86 | + Assert.IsNotNull(excludedHoldout); |
| 87 | + Assert.AreEqual("holdout_excluded_1", excludedHoldout.Id); |
| 88 | + Assert.AreEqual("excluded_holdout", excludedHoldout.Key); |
| 89 | + Assert.IsNotNull(excludedHoldout.IncludedFlags); |
| 90 | + Assert.AreEqual(0, excludedHoldout.IncludedFlags.Length); |
| 91 | + Assert.IsNotNull(excludedHoldout.ExcludedFlags); |
| 92 | + Assert.AreEqual(2, excludedHoldout.ExcludedFlags.Length); |
| 93 | + Assert.Contains("flag_3", excludedHoldout.ExcludedFlags); |
| 94 | + Assert.Contains("flag_4", excludedHoldout.ExcludedFlags); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void TestHoldoutWithEmptyFlags() |
| 99 | + { |
| 100 | + var globalHoldoutJson = testData["globalHoldout"].ToString(); |
| 101 | + var globalHoldout = JsonConvert.DeserializeObject<Holdout>(globalHoldoutJson); |
| 102 | + |
| 103 | + Assert.IsNotNull(globalHoldout); |
| 104 | + Assert.IsNotNull(globalHoldout.IncludedFlags); |
| 105 | + Assert.AreEqual(0, globalHoldout.IncludedFlags.Length); |
| 106 | + Assert.IsNotNull(globalHoldout.ExcludedFlags); |
| 107 | + Assert.AreEqual(0, globalHoldout.ExcludedFlags.Length); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void TestHoldoutEquality() |
| 112 | + { |
| 113 | + var holdoutJson = testData["globalHoldout"].ToString(); |
| 114 | + var holdout1 = JsonConvert.DeserializeObject<Holdout>(holdoutJson); |
| 115 | + var holdout2 = JsonConvert.DeserializeObject<Holdout>(holdoutJson); |
| 116 | + |
| 117 | + Assert.IsNotNull(holdout1); |
| 118 | + Assert.IsNotNull(holdout2); |
| 119 | + // Note: This test depends on how Holdout implements equality |
| 120 | + // If Holdout doesn't override Equals, this will test reference equality |
| 121 | + // You may need to implement custom equality logic for Holdout |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public void TestHoldoutStatusParsing() |
| 126 | + { |
| 127 | + var globalHoldoutJson = testData["globalHoldout"].ToString(); |
| 128 | + var globalHoldout = JsonConvert.DeserializeObject<Holdout>(globalHoldoutJson); |
| 129 | + |
| 130 | + Assert.IsNotNull(globalHoldout); |
| 131 | + Assert.AreEqual("Running", globalHoldout.Status); |
| 132 | + |
| 133 | + // Test that the holdout is considered activated when status is "Running" |
| 134 | + // This assumes there's an IsActivated property or similar logic |
| 135 | + // Adjust based on actual Holdout implementation |
| 136 | + } |
| 137 | + |
| 138 | + [Test] |
| 139 | + public void TestHoldoutVariationsDeserialization() |
| 140 | + { |
| 141 | + var holdoutJson = testData["includedFlagsHoldout"].ToString(); |
| 142 | + var holdout = JsonConvert.DeserializeObject<Holdout>(holdoutJson); |
| 143 | + |
| 144 | + Assert.IsNotNull(holdout); |
| 145 | + Assert.IsNotNull(holdout.Variations); |
| 146 | + Assert.AreEqual(1, holdout.Variations.Length); |
| 147 | + |
| 148 | + var variation = holdout.Variations[0]; |
| 149 | + Assert.AreEqual("var_2", variation.Id); |
| 150 | + Assert.AreEqual("treatment", variation.Key); |
| 151 | + Assert.AreEqual(true, variation.FeatureEnabled); |
| 152 | + } |
| 153 | + |
| 154 | + [Test] |
| 155 | + public void TestHoldoutTrafficAllocationDeserialization() |
| 156 | + { |
| 157 | + var holdoutJson = testData["excludedFlagsHoldout"].ToString(); |
| 158 | + var holdout = JsonConvert.DeserializeObject<Holdout>(holdoutJson); |
| 159 | + |
| 160 | + Assert.IsNotNull(holdout); |
| 161 | + Assert.IsNotNull(holdout.TrafficAllocation); |
| 162 | + Assert.AreEqual(1, holdout.TrafficAllocation.Length); |
| 163 | + |
| 164 | + var trafficAllocation = holdout.TrafficAllocation[0]; |
| 165 | + Assert.AreEqual("var_3", trafficAllocation.EntityId); |
| 166 | + Assert.AreEqual(10000, trafficAllocation.EndOfRange); |
| 167 | + } |
| 168 | + |
| 169 | + [Test] |
| 170 | + public void TestHoldoutNullSafety() |
| 171 | + { |
| 172 | + // Test that holdout can handle null/missing includedFlags and excludedFlags |
| 173 | + var minimalHoldoutJson = @"{ |
| 174 | + ""id"": ""test_holdout"", |
| 175 | + ""key"": ""test_key"", |
| 176 | + ""status"": ""Running"", |
| 177 | + ""layerId"": ""test_layer"", |
| 178 | + ""variations"": [], |
| 179 | + ""trafficAllocation"": [], |
| 180 | + ""audienceIds"": [], |
| 181 | + ""audienceConditions"": [] |
| 182 | + }"; |
| 183 | + |
| 184 | + var holdout = JsonConvert.DeserializeObject<Holdout>(minimalHoldoutJson); |
| 185 | + |
| 186 | + Assert.IsNotNull(holdout); |
| 187 | + Assert.AreEqual("test_holdout", holdout.Id); |
| 188 | + Assert.AreEqual("test_key", holdout.Key); |
| 189 | + |
| 190 | + // Verify that missing includedFlags and excludedFlags are handled properly |
| 191 | + // This depends on how the Holdout entity handles missing properties |
| 192 | + Assert.IsNotNull(holdout.IncludedFlags); |
| 193 | + Assert.IsNotNull(holdout.ExcludedFlags); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments