Skip to content

Commit 5c4f092

Browse files
committed
added more tests
1 parent 1907a69 commit 5c4f092

File tree

3 files changed

+62
-64
lines changed

3 files changed

+62
-64
lines changed

src/OpenFeature.Contrib.Providers.AwsAppConfig/AppConfigKey.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ public class AppConfigKey
7474
/// </example>
7575
public AppConfigKey(string key)
7676
{
77-
// Regular expression for validating the format with last part as 0 or 1 or empty
78-
string pattern = @"^[^:]+:[^:]+:(0|1)?$";
79-
var match = Regex.IsMatch(key, pattern);
80-
8177
if(string.IsNullOrWhiteSpace(key))
8278
{
8379
throw new ArgumentException("Key cannot be null or empty");
8480
}
8581

82+
// Regular expression for validating the format with last part as 0 or 1 or empty
83+
string pattern = @"^[^:]+:[^:]+(:[^:]+)?$";
84+
var match = Regex.IsMatch(key, pattern);
85+
86+
if(!match) throw new ArgumentException("Invalid key format. Flag key is expected in configurationProfileId:flagKey[:attributeKey] format");
87+
88+
8689
var parts = key.Split(Separator, StringSplitOptions.RemoveEmptyEntries);
8790

8891
if(parts.Length < 2 )
@@ -135,7 +138,7 @@ public AppConfigKey(string configurationProfileId, string flagKey, string attrib
135138
/// A string in the format "configurationProfileId:flagKey[:attributeKey]".
136139
/// The attributeKey part is only included if it exists.
137140
/// </returns>
138-
public string ToKeyString()
141+
public override string ToString()
139142
{
140143
return $"{ConfigurationProfileId}{Separator}{FlagKey}{(HasAttribute ? Separator + AttributeKey : "")}";
141144
}

src/OpenFeature.Contrib.Providers.AwsAppConfig/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ app.MapGet("/flagKey", async (IFeatureClient featureClient) =>
109109
{
110110
// NOTE: Refere AppConfig Key section above to understand how AppConfig configuration is strucutred.
111111
var key = new AppConfigKey(configurationProfileId, flagKey, "enabled");
112-
var isEnabled = await featureClient.GetBooleanValue(key.ToKeyString(), false);
112+
var isEnabled = await featureClient.GetBooleanValue(key.ToString(), false);
113113
return Results.Ok(new { FeatureEnabled = isEnabled });
114114
})
115115
.WithName("GetFeatureStatus")
@@ -119,7 +119,7 @@ app.MapGet("/flagKey/attributeKey", async (IFeatureClient featureClient) =>
119119
{
120120
// NOTE: Refere AppConfig Key section above to understand how AppConfig configuration is strucutred.
121121
var key = new AppConfigKey(configurationProfileId, flagKey, attributeKey);
122-
var config = await featureClient.GetStringValue(key.ToKeyString(), "default");
122+
var config = await featureClient.GetStringValue(key.ToString(), "default");
123123
return Results.Ok(new { Configuration = config });
124124
})
125125
.WithName("GetFeatureConfig")
@@ -133,7 +133,7 @@ app.MapGet("/flagKey/attributeKey", async (IFeatureClient featureClient) =>
133133
app.MapGet("/protected-feature", async (IFeatureClient featureClient) =>
134134
{
135135
var key = new AppConfigKey(configurationProfileId, "protected-feature", "enabled");
136-
var isFeatureEnabled = await featureClient.GetBooleanValue(key.ToKeyString(), false);
136+
var isFeatureEnabled = await featureClient.GetBooleanValue(key.ToString(), false);
137137

138138
if (!isFeatureEnabled)
139139
{

test/OpenFeature.Contrib.Providers.AwsAppConfig.Test/AppConfigKeyTests.cs

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,7 @@ public void Constructor_3input_WithInvalidParameters_ShouldThrowArgumentExceptio
3232
// Act & Assert
3333
Assert.Throws<ArgumentNullException>(() =>
3434
new AppConfigKey(confiProfileId, flagKey, attributeKey));
35-
}
36-
37-
[Theory]
38-
[InlineData("app1", "env1", "config1")]
39-
[InlineData("my-app", "my-env", "my-config")]
40-
[InlineData("APP", "ENV", "CONFIG")]
41-
public void ToString_ShouldReturnFormattedString(
42-
string configProfileId, string flagKey, string attributeKey)
43-
{
44-
// Arrange
45-
var key = new AppConfigKey(configProfileId, flagKey, attributeKey);
46-
47-
// Act
48-
var result = key.ToString();
49-
50-
// Assert
51-
Assert.Contains(configProfileId, result);
52-
Assert.Contains(flagKey, result);
53-
Assert.Contains(attributeKey, result);
54-
}
35+
}
5536

5637
[Theory]
5738
[InlineData("app-123", "env-123", "config-123")]
@@ -146,29 +127,23 @@ public void Constructor_WithThreeParts_SetsPropertiesCorrectly()
146127
Assert.True(appConfigKey.HasAttribute);
147128
}
148129

149-
[Fact]
150-
public void Constructor_WithMoreThanThreeParts_IgnoresExtraParts()
151-
{
152-
// Arrange
153-
var key = "profile123:flag456:attr789:extra:parts";
154-
155-
// Act
156-
var appConfigKey = new AppConfigKey(key);
157-
158-
// Assert
159-
Assert.Equal("profile123", appConfigKey.ConfigurationProfileId);
160-
Assert.Equal("flag456", appConfigKey.FlagKey);
161-
Assert.Equal("attr789", appConfigKey.AttributeKey);
162-
Assert.True(appConfigKey.HasAttribute);
163-
}
164-
165-
[Fact]
166-
public void Constructor_WithEmptyParts_ThrowsArgumentException()
130+
[Theory]
131+
[InlineData("profile123:flag456:attr789:extra:parts")]
132+
[InlineData("profile123::attr789:extra:parts")]
133+
[InlineData("profile123:flagkey456:")]
134+
[InlineData("profile123:")]
135+
[InlineData(":flagkey456")]
136+
[InlineData(":flagkey456:")]
137+
[InlineData("::attribute789")]
138+
[InlineData("::")]
139+
[InlineData(":::")]
140+
[InlineData("RandomSgring)()@*Q()*#Q$@#$")]
141+
public void Constructor_WithInvalidPattern_ShouldThrowArgumentException(string key)
167142
{
168143
// Act & Assert
169144
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey("::"));
170145
Assert.Equal("Invalid key format. Flag key is expected in configurationProfileId:flagKey[:attributeKey] format", exception.Message);
171-
}
146+
}
172147

173148
[Theory]
174149
[InlineData("profile123::attr789")]
@@ -179,23 +154,7 @@ public void Constructor_WithEmptyMiddleParts_PreservesNonEmptyParts(string key)
179154
// Act & Assert
180155
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey(key));
181156
Assert.Equal("Invalid key format. Flag key is expected in configurationProfileId:flagKey[:attributeKey] format", exception.Message);
182-
}
183-
184-
[Fact]
185-
public void Constructor_WithTrailingSeparator_HandlesProperly()
186-
{
187-
// Arrange
188-
var key = "profile123:flag456:";
189-
190-
// Act
191-
var appConfigKey = new AppConfigKey(key);
192-
193-
// Assert
194-
Assert.Equal("profile123", appConfigKey.ConfigurationProfileId);
195-
Assert.Equal("flag456", appConfigKey.FlagKey);
196-
Assert.Null(appConfigKey.AttributeKey);
197-
Assert.False(appConfigKey.HasAttribute);
198-
}
157+
}
199158

200159
[Fact]
201160
public void Constructor_WithLeadingSeparator_ThrowsArgumentException()
@@ -267,4 +226,40 @@ public void HasAttribute_WhenConstructedWithStringWithoutAttribute_ReturnsFalse(
267226
// Act & Assert
268227
Assert.False(appConfigKey.HasAttribute);
269228
}
229+
230+
[Theory]
231+
[InlineData("app1", "env1", "config1")]
232+
[InlineData("my-app", "my-env", "my-config")]
233+
[InlineData("APP", "ENV", "CONFIG")]
234+
public void ToString_ShouldReturnFormattedString(
235+
string configProfileId, string flagKey, string attributeKey)
236+
{
237+
// Arrange
238+
var key = new AppConfigKey(configProfileId, flagKey, attributeKey);
239+
240+
// Act
241+
var result = key.ToString();
242+
243+
// Assert
244+
Assert.Contains(configProfileId, result);
245+
Assert.Contains(flagKey, result);
246+
Assert.Contains(attributeKey, result);
247+
}
248+
249+
[Theory]
250+
[InlineData("app1:env1:config1")]
251+
[InlineData("my-app:my-env:my-config")]
252+
[InlineData("APP:ENV")]
253+
public void ToString_WithSingleInput_ShouldReturnFormattedString(string input)
254+
{
255+
// Arrange
256+
var key = new AppConfigKey(input);
257+
258+
// Act
259+
var result = key.ToString();
260+
261+
// Assert
262+
Assert.Contains(key.ConfigurationProfileId, result);
263+
Assert.Contains(key.FlagKey, result);
264+
}
270265
}

0 commit comments

Comments
 (0)