|
| 1 | +using Xunit; |
| 2 | +using Moq; |
| 3 | +using OpenFeature.Model; |
| 4 | +using Amazon.AppConfigData.Model; |
| 5 | +using System.Text; |
| 6 | +using System.IO; |
| 7 | +using OpenFeature.Contrib.Providers.AwsAppConfig; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Collections.Generic; |
| 10 | + |
| 11 | +public class AppConfigProviderTests |
| 12 | +{ |
| 13 | + private readonly Mock<IRetrievalApi> _mockAppConfigApi; |
| 14 | + private readonly AppConfigProvider _provider; |
| 15 | + private readonly string _jsonContent; |
| 16 | + private const string ApplicationName = "TestApp"; |
| 17 | + private const string EnvironmentName = "TestEnv"; |
| 18 | + |
| 19 | + public AppConfigProviderTests() |
| 20 | + { |
| 21 | + _mockAppConfigApi = new Mock<IRetrievalApi>(); |
| 22 | + _provider = new AppConfigProvider(_mockAppConfigApi.Object, ApplicationName, EnvironmentName); |
| 23 | + _jsonContent = System.IO.File.ReadAllText("test-data.json"); |
| 24 | + } |
| 25 | + |
| 26 | + #region ResolveBooleanValueAsync Tests |
| 27 | + [Fact] |
| 28 | + public async Task ResolveBooleanValueAsync_WhenFlagExists_ReturnsCorrectValue() |
| 29 | + { |
| 30 | + // Arrange |
| 31 | + const string flagKey = "configProfileId:test-enabled-flag:enabled"; |
| 32 | + const bool expectedValue = true; |
| 33 | + SetupMockResponse(_jsonContent); |
| 34 | + |
| 35 | + // Act |
| 36 | + var result = await _provider.ResolveBooleanValueAsync(flagKey, false); |
| 37 | + |
| 38 | + // Assert |
| 39 | + Assert.Equal(expectedValue, result.Value); |
| 40 | + Assert.Equal(flagKey, result.FlagKey); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task ResolveBooleanValueAsync_WhenFlagDoesNotExist_ReturnsDefaultValue() |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + const string flagKey = "configProfileId:test-enabled-flag:enabled"; |
| 48 | + const bool defaultValue = false; |
| 49 | + SetupMockResponse("{}"); |
| 50 | + |
| 51 | + // Act |
| 52 | + var result = await _provider.ResolveBooleanValueAsync(flagKey, defaultValue); |
| 53 | + |
| 54 | + // Assert |
| 55 | + Assert.Equal(defaultValue, result.Value); |
| 56 | + } |
| 57 | + #endregion |
| 58 | + |
| 59 | + #region ResolveDoubleValueAsync Tests |
| 60 | + [Fact] |
| 61 | + public async Task ResolveDoubleValueAsync_WhenFlagExists_ReturnsCorrectValue() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + const string flagKey = "configProfileId:test-enabled-flag:doubleAttribute"; |
| 65 | + const double expectedValue = 3.14; |
| 66 | + SetupMockResponse(_jsonContent); |
| 67 | + |
| 68 | + // Act |
| 69 | + var result = await _provider.ResolveDoubleValueAsync(flagKey, 0.0); |
| 70 | + |
| 71 | + // Assert |
| 72 | + Assert.Equal(expectedValue, result.Value); |
| 73 | + Assert.Equal(flagKey, result.FlagKey); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public async Task ResolveDoubleValueAsync_WhenFlagDoesNotExist_ReturnsDefaultValue() |
| 78 | + { |
| 79 | + // Arrange |
| 80 | + const string flagKey = "configProfileId:test-enabled-flag:doubleAttribute"; |
| 81 | + const double defaultValue = 1.0; |
| 82 | + SetupMockResponse("{}"); |
| 83 | + |
| 84 | + // Act |
| 85 | + var result = await _provider.ResolveDoubleValueAsync(flagKey, defaultValue); |
| 86 | + |
| 87 | + // Assert |
| 88 | + Assert.Equal(defaultValue, result.Value); |
| 89 | + } |
| 90 | + #endregion |
| 91 | + |
| 92 | + #region ResolveIntegerValueAsync Tests |
| 93 | + [Fact] |
| 94 | + public async Task ResolveIntegerValueAsync_WhenFlagExists_ReturnsCorrectValue() |
| 95 | + { |
| 96 | + // Arrange |
| 97 | + const string flagKey = "configProfileId:test-enabled-flag:intAttribute"; |
| 98 | + const int expectedValue = 42; |
| 99 | + SetupMockResponse(_jsonContent); |
| 100 | + |
| 101 | + // Act |
| 102 | + var result = await _provider.ResolveIntegerValueAsync(flagKey, 0); |
| 103 | + |
| 104 | + // Assert |
| 105 | + Assert.Equal(expectedValue, result.Value); |
| 106 | + Assert.Equal(flagKey, result.FlagKey); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public async Task ResolveIntegerValueAsync_WhenFlagDoesNotExist_ReturnsDefaultValue() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + const string flagKey = "configProfileId:test-enabled-flag:intAttribute"; |
| 114 | + const int defaultValue = 100; |
| 115 | + SetupMockResponse("{}"); |
| 116 | + |
| 117 | + // Act |
| 118 | + var result = await _provider.ResolveIntegerValueAsync(flagKey, defaultValue); |
| 119 | + |
| 120 | + // Assert |
| 121 | + Assert.Equal(defaultValue, result.Value); |
| 122 | + } |
| 123 | + #endregion |
| 124 | + |
| 125 | + #region ResolveStringValueAsync Tests |
| 126 | + [Fact] |
| 127 | + public async Task ResolveStringValueAsync_WhenFlagExists_ReturnsCorrectValue() |
| 128 | + { |
| 129 | + // Arrange |
| 130 | + const string flagKey = "configProfileId:test-enabled-flag:stringAttribute"; |
| 131 | + const string expectedValue = "testValue"; |
| 132 | + SetupMockResponse(_jsonContent); |
| 133 | + |
| 134 | + // Act |
| 135 | + var result = await _provider.ResolveStringValueAsync(flagKey, "default"); |
| 136 | + |
| 137 | + // Assert |
| 138 | + Assert.Equal(expectedValue, result.Value); |
| 139 | + Assert.Equal(flagKey, result.FlagKey); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public async Task ResolveStringValueAsync_WhenFlagDoesNotExist_ReturnsDefaultValue() |
| 144 | + { |
| 145 | + // Arrange |
| 146 | + const string flagKey = "configProfileId:test-enabled-flag:stringAttribute"; |
| 147 | + const string defaultValue = "default-value"; |
| 148 | + SetupMockResponse("{}"); |
| 149 | + |
| 150 | + // Act |
| 151 | + var result = await _provider.ResolveStringValueAsync(flagKey, defaultValue); |
| 152 | + |
| 153 | + // Assert |
| 154 | + Assert.Equal(defaultValue, result.Value); |
| 155 | + } |
| 156 | + #endregion |
| 157 | + |
| 158 | + #region ResolveStructureValueAsync Tests |
| 159 | + [Fact] |
| 160 | + public async Task ResolveStructureValueAsync_WhenFlagExists_ReturnsCorrectValue() |
| 161 | + { |
| 162 | + // Arrange |
| 163 | + const string flagKey = "configProfileId:test-enabled-flag"; |
| 164 | + const string jsonValue = "{\"key\": \"value\", \"number\": 42}"; |
| 165 | + SetupMockResponse($"{{\"{flagKey}\": {jsonValue}}}"); |
| 166 | + |
| 167 | + // Act |
| 168 | + var result = await _provider.ResolveStructureValueAsync(flagKey, new Value()); |
| 169 | + |
| 170 | + // Assert |
| 171 | + Assert.NotNull(result.Value.AsStructure); |
| 172 | + Assert.Equal("value", result.Value.AsStructure["key"].AsString); |
| 173 | + Assert.Equal(42, result.Value.AsStructure["number"].AsInteger); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public async Task ResolveStructureValueAsync_WhenFlagDoesNotExist_ReturnsDefaultValue() |
| 178 | + { |
| 179 | + // Arrange |
| 180 | + const string flagKey = "configProfileId:test-enabled-flag"; |
| 181 | + var defaultValue = new Value(new Dictionary<string, Value> |
| 182 | + { |
| 183 | + ["default"] = new Value("default") |
| 184 | + }); |
| 185 | + SetupMockResponse("{}"); |
| 186 | + |
| 187 | + // Act |
| 188 | + var result = await _provider.ResolveStructureValueAsync(flagKey, defaultValue); |
| 189 | + |
| 190 | + // Assert |
| 191 | + Assert.Equal(defaultValue.AsStructure["default"].AsString, |
| 192 | + result.Value.AsStructure["default"].AsString); |
| 193 | + } |
| 194 | + #endregion |
| 195 | + |
| 196 | + #region Attribute Resolution Tests |
| 197 | + [Fact] |
| 198 | + public async Task ResolveValue_WithAttributeKey_ReturnsAttributeValue() |
| 199 | + { |
| 200 | + // Arrange |
| 201 | + const string flagKey = "myFlag:color"; |
| 202 | + const string expectedValue = "blue"; |
| 203 | + SetupMockResponse($"{{\"myFlag\": {{\"color\": \"{expectedValue}\"}}}}"); |
| 204 | + |
| 205 | + // Act |
| 206 | + var result = await _provider.ResolveStringValueAsync(flagKey, "default"); |
| 207 | + |
| 208 | + // Assert |
| 209 | + Assert.Equal(expectedValue, result.Value); |
| 210 | + } |
| 211 | + |
| 212 | + [Fact] |
| 213 | + public async Task ResolveValue_WithInvalidAttributeKey_ReturnsDefaultValue() |
| 214 | + { |
| 215 | + // Arrange |
| 216 | + const string flagKey = "myFlag:invalidAttribute"; |
| 217 | + const string defaultValue = "default"; |
| 218 | + SetupMockResponse("{\"myFlag\": {\"color\": \"blue\"}}"); |
| 219 | + |
| 220 | + // Act |
| 221 | + var result = await _provider.ResolveStringValueAsync(flagKey, defaultValue); |
| 222 | + |
| 223 | + // Assert |
| 224 | + Assert.Equal(defaultValue, result.Value); |
| 225 | + } |
| 226 | + #endregion |
| 227 | + |
| 228 | + private void SetupMockResponse(string jsonContent) |
| 229 | + { |
| 230 | + var response = new GetLatestConfigurationResponse |
| 231 | + { |
| 232 | + Configuration = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)) |
| 233 | + }; |
| 234 | + |
| 235 | + _mockAppConfigApi |
| 236 | + .Setup(x => x.GetLatestConfigurationAsync(It.IsAny<FeatureFlagProfile>())) |
| 237 | + .ReturnsAsync(response); |
| 238 | + } |
| 239 | +} |
0 commit comments