|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using OpenFeature.Model; |
| 4 | +using OpenFeature.Tests.Internal; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +#nullable enable |
| 8 | +namespace OpenFeature.Tests; |
| 9 | + |
| 10 | +public class FlagMetadataTest |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + [Specification("1.4.14", |
| 14 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 15 | + public void GetBool_Should_Return_Null_If_Key_Not_Found() |
| 16 | + { |
| 17 | + // Arrange |
| 18 | + var flagMetadata = new FlagMetadata(); |
| 19 | + |
| 20 | + // Act |
| 21 | + var result = flagMetadata.GetBool("nonexistentKey"); |
| 22 | + |
| 23 | + // Assert |
| 24 | + Assert.Null(result); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + [Specification("1.4.14", |
| 29 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 30 | + [Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")] |
| 31 | + public void GetBool_Should_Return_Value_If_Key_Found() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + var metadata = new Dictionary<string, object> |
| 35 | + { |
| 36 | + { |
| 37 | + "boolKey", true |
| 38 | + } |
| 39 | + }; |
| 40 | + var flagMetadata = new FlagMetadata(metadata); |
| 41 | + |
| 42 | + // Act |
| 43 | + var result = flagMetadata.GetBool("boolKey"); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.True(result); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + [Specification("1.4.14", |
| 51 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 52 | + public void GetBool_Should_Throw_Value_Is_Invalid() |
| 53 | + { |
| 54 | + // Arrange |
| 55 | + var metadata = new Dictionary<string, object> |
| 56 | + { |
| 57 | + { |
| 58 | + "wrongKey", "11a" |
| 59 | + } |
| 60 | + }; |
| 61 | + var flagMetadata = new FlagMetadata(metadata); |
| 62 | + |
| 63 | + // Act |
| 64 | + var result = flagMetadata.GetBool("wrongKey"); |
| 65 | + |
| 66 | + // Assert |
| 67 | + Assert.Null(result); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + [Specification("1.4.14", |
| 72 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 73 | + public void GetInt_Should_Return_Null_If_Key_Not_Found() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + var flagMetadata = new FlagMetadata(); |
| 77 | + |
| 78 | + // Act |
| 79 | + var result = flagMetadata.GetInt("nonexistentKey"); |
| 80 | + |
| 81 | + // Assert |
| 82 | + Assert.Null(result); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + [Specification("1.4.14", |
| 87 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 88 | + [Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")] |
| 89 | + public void GetInt_Should_Return_Value_If_Key_Found() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + var metadata = new Dictionary<string, object> |
| 93 | + { |
| 94 | + { |
| 95 | + "intKey", 1 |
| 96 | + } |
| 97 | + }; |
| 98 | + var flagMetadata = new FlagMetadata(metadata); |
| 99 | + |
| 100 | + // Act |
| 101 | + var result = flagMetadata.GetInt("intKey"); |
| 102 | + |
| 103 | + // Assert |
| 104 | + Assert.NotNull(result); |
| 105 | + Assert.Equal(1, result); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + [Specification("1.4.14", |
| 110 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 111 | + public void GetInt_Should_Throw_Value_Is_Invalid() |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + var metadata = new Dictionary<string, object> |
| 115 | + { |
| 116 | + { |
| 117 | + "wrongKey", "11a" |
| 118 | + } |
| 119 | + }; |
| 120 | + var flagMetadata = new FlagMetadata(metadata); |
| 121 | + |
| 122 | + // Act |
| 123 | + var result = flagMetadata.GetInt("wrongKey"); |
| 124 | + |
| 125 | + // Assert |
| 126 | + Assert.Null(result); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + [Specification("1.4.14", |
| 131 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 132 | + public void GetDouble_Should_Return_Null_If_Key_Not_Found() |
| 133 | + { |
| 134 | + // Arrange |
| 135 | + var flagMetadata = new FlagMetadata(); |
| 136 | + |
| 137 | + // Act |
| 138 | + var result = flagMetadata.GetDouble("nonexistentKey"); |
| 139 | + |
| 140 | + // Assert |
| 141 | + Assert.Null(result); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + [Specification("1.4.14", |
| 146 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 147 | + [Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")] |
| 148 | + public void GetDouble_Should_Return_Value_If_Key_Found() |
| 149 | + { |
| 150 | + // Arrange |
| 151 | + var metadata = new Dictionary<string, object> |
| 152 | + { |
| 153 | + { |
| 154 | + "doubleKey", 1.2 |
| 155 | + } |
| 156 | + }; |
| 157 | + var flagMetadata = new FlagMetadata(metadata); |
| 158 | + |
| 159 | + // Act |
| 160 | + var result = flagMetadata.GetDouble("doubleKey"); |
| 161 | + |
| 162 | + // Assert |
| 163 | + Assert.NotNull(result); |
| 164 | + Assert.Equal(1.2, result); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + [Specification("1.4.14", |
| 169 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 170 | + public void GetDouble_Should_Throw_Value_Is_Invalid() |
| 171 | + { |
| 172 | + // Arrange |
| 173 | + var metadata = new Dictionary<string, object> |
| 174 | + { |
| 175 | + { |
| 176 | + "wrongKey", "11a" |
| 177 | + } |
| 178 | + }; |
| 179 | + var flagMetadata = new FlagMetadata(metadata); |
| 180 | + |
| 181 | + // Act |
| 182 | + var result = flagMetadata.GetDouble("wrongKey"); |
| 183 | + |
| 184 | + // Assert |
| 185 | + Assert.Null(result); |
| 186 | + } |
| 187 | + |
| 188 | + [Fact] |
| 189 | + [Specification("1.4.14", |
| 190 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 191 | + public void GetString_Should_Return_Null_If_Key_Not_Found() |
| 192 | + { |
| 193 | + // Arrange |
| 194 | + var flagMetadata = new FlagMetadata(); |
| 195 | + |
| 196 | + // Act |
| 197 | + var result = flagMetadata.GetString("nonexistentKey"); |
| 198 | + |
| 199 | + // Assert |
| 200 | + Assert.Null(result); |
| 201 | + } |
| 202 | + |
| 203 | + [Fact] |
| 204 | + [Specification("1.4.14", |
| 205 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 206 | + [Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")] |
| 207 | + public void GetString_Should_Return_Value_If_Key_Found() |
| 208 | + { |
| 209 | + // Arrange |
| 210 | + var metadata = new Dictionary<string, object> |
| 211 | + { |
| 212 | + { |
| 213 | + "stringKey", "11" |
| 214 | + } |
| 215 | + }; |
| 216 | + var flagMetadata = new FlagMetadata(metadata); |
| 217 | + |
| 218 | + // Act |
| 219 | + var result = flagMetadata.GetString("stringKey"); |
| 220 | + |
| 221 | + // Assert |
| 222 | + Assert.NotNull(result); |
| 223 | + Assert.Equal("11", result); |
| 224 | + } |
| 225 | + |
| 226 | + [Fact] |
| 227 | + [Specification("1.4.14", |
| 228 | + "If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")] |
| 229 | + public void GetString_Should_Throw_Value_Is_Invalid() |
| 230 | + { |
| 231 | + // Arrange |
| 232 | + var metadata = new Dictionary<string, object> |
| 233 | + { |
| 234 | + { |
| 235 | + "wrongKey", new object() |
| 236 | + } |
| 237 | + }; |
| 238 | + var flagMetadata = new FlagMetadata(metadata); |
| 239 | + |
| 240 | + // Act |
| 241 | + var result = flagMetadata.GetString("wrongKey"); |
| 242 | + |
| 243 | + // Assert |
| 244 | + Assert.Null(result); |
| 245 | + } |
| 246 | +} |
0 commit comments