Skip to content

Commit 1907a69

Browse files
committed
added more tests
1 parent 80ff695 commit 1907a69

File tree

2 files changed

+198
-33
lines changed

2 files changed

+198
-33
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.RegularExpressions;
23

34
namespace OpenFeature.Contrib.Providers.AwsAppConfig
45
{
@@ -31,7 +32,7 @@ public class AppConfigKey
3132
/// <summary>
3233
/// Gets whether this key has an attribute component
3334
/// </summary>
34-
public bool HasAttribute => !string.IsNullOrEmpty(AttributeKey);
35+
public bool HasAttribute => !string.IsNullOrWhiteSpace(AttributeKey);
3536

3637
/// <summary>
3738
/// Initializes a new instance of the <see cref="AppConfigKey"/> class that represents a structured key
@@ -73,6 +74,10 @@ public class AppConfigKey
7374
/// </example>
7475
public AppConfigKey(string key)
7576
{
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+
7681
if(string.IsNullOrWhiteSpace(key))
7782
{
7883
throw new ArgumentException("Key cannot be null or empty");
@@ -110,12 +115,12 @@ public AppConfigKey(string configurationProfileId, string flagKey, string attrib
110115
{
111116
if (string.IsNullOrWhiteSpace(configurationProfileId))
112117
{
113-
throw new ArgumentException("Configuration Profile ID cannot be null or empty");
118+
throw new ArgumentNullException("Configuration Profile ID cannot be null or empty");
114119
}
115120

116121
if (string.IsNullOrWhiteSpace(flagKey))
117122
{
118-
throw new ArgumentException("Flag key cannot be null or empty");
123+
throw new ArgumentNullException("Flag key cannot be null or empty");
119124
}
120125

121126
ConfigurationProfileId = configurationProfileId;

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

Lines changed: 190 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class AppConfigKeyTests
66
{
77
[Fact]
8-
public void Constructor_WithValidParameters_ShouldSetProperties()
8+
public void Constructor_3input_WithValidParameters_ShouldSetProperties()
99
{
1010
// Arrange
1111
var configProfileID = "TestConfigProfile";
@@ -23,17 +23,15 @@ public void Constructor_WithValidParameters_ShouldSetProperties()
2323

2424
[Theory]
2525
[InlineData("", "env", "config")]
26-
[InlineData("app", "", "config")]
27-
[InlineData("app", "env", "")]
26+
[InlineData("app", "", "config")]
2827
[InlineData(null, "env", "config")]
29-
[InlineData("app", null, "config")]
30-
[InlineData("app", "env", null)]
31-
public void Constructor_WithInvalidParameters_ShouldThrowArgumentException(
32-
string application, string environment, string configuration)
28+
[InlineData("app", null, "config")]
29+
public void Constructor_3input_WithInvalidParameters_ShouldThrowArgumentException(
30+
string confiProfileId, string flagKey, string attributeKey)
3331
{
3432
// Act & Assert
3533
Assert.Throws<ArgumentNullException>(() =>
36-
new AppConfigKey(application, environment, configuration));
34+
new AppConfigKey(confiProfileId, flagKey, attributeKey));
3735
}
3836

3937
[Theory]
@@ -59,7 +57,7 @@ public void ToString_ShouldReturnFormattedString(
5957
[InlineData("app-123", "env-123", "config-123")]
6058
[InlineData("app_123", "env_123", "config_123")]
6159
[InlineData("app.123", "env.123", "config.123")]
62-
public void Constructor_WithSpecialCharacters_ShouldAcceptValidPatterns(
60+
public void Constructor_3input_WithSpecialCharacters_ShouldAcceptValidPatterns(
6361
string configProfileId, string flagKey, string attributeKey)
6462
{
6563
// Arrange & Act
@@ -69,42 +67,204 @@ public void Constructor_WithSpecialCharacters_ShouldAcceptValidPatterns(
6967
Assert.Equal(configProfileId, key.ConfigurationProfileId);
7068
Assert.Equal(flagKey, key.FlagKey);
7169
Assert.Equal(attributeKey, key.AttributeKey);
72-
}
70+
}
7371

74-
[Theory]
75-
[InlineData("app$123", "env", "config")]
76-
[InlineData("app", "env#123", "config")]
77-
[InlineData("app", "env", "config@123")]
78-
public void Constructor_WithInvalidCharacters_ShouldThrowArgumentException(
79-
string application, string environment, string configuration)
72+
[Fact]
73+
public void Constructor_3input_WithWhitespaceValues_ShouldThrowArgumentException()
8074
{
75+
// Arrange
76+
var application = " ";
77+
var environment = "env";
78+
var configuration = "config";
79+
8180
// Act & Assert
82-
Assert.Throws<ArgumentException>(() =>
81+
Assert.Throws<ArgumentNullException>(() =>
8382
new AppConfigKey(application, environment, configuration));
8483
}
8584

8685
[Fact]
87-
public void Constructor_WithWhitespaceValues_ShouldThrowArgumentException()
86+
public void Constructor_WithNullKey_ThrowsArgumentException()
87+
{
88+
// Act & Assert
89+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey(null));
90+
Assert.Equal("Key cannot be null or empty", exception.Message);
91+
}
92+
93+
[Fact]
94+
public void Constructor_WithEmptyKey_ThrowsArgumentException()
95+
{
96+
// Act & Assert
97+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey(string.Empty));
98+
Assert.Equal("Key cannot be null or empty", exception.Message);
99+
}
100+
101+
[Fact]
102+
public void Constructor_WithWhitespaceKey_ThrowsArgumentException()
103+
{
104+
// Act & Assert
105+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey(" "));
106+
Assert.Equal("Key cannot be null or empty", exception.Message);
107+
}
108+
109+
[Fact]
110+
public void Constructor_WithSinglePart_ThrowsArgumentException()
111+
{
112+
// Act & Assert
113+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey("singlepart"));
114+
Assert.Equal("Invalid key format. Flag key is expected in configurationProfileId:flagKey[:attributeKey] format", exception.Message);
115+
}
116+
117+
[Fact]
118+
public void Constructor_WithTwoParts_SetsPropertiesCorrectly()
88119
{
89120
// Arrange
90-
var application = " ";
91-
var environment = "env";
92-
var configuration = "config";
121+
var key = "profile123:flag456";
122+
123+
// Act
124+
var appConfigKey = new AppConfigKey(key);
125+
126+
// Assert
127+
Assert.Equal("profile123", appConfigKey.ConfigurationProfileId);
128+
Assert.Equal("flag456", appConfigKey.FlagKey);
129+
Assert.Null(appConfigKey.AttributeKey);
130+
Assert.False(appConfigKey.HasAttribute);
131+
}
93132

133+
[Fact]
134+
public void Constructor_WithThreeParts_SetsPropertiesCorrectly()
135+
{
136+
// Arrange
137+
var key = "profile123:flag456:attr789";
138+
139+
// Act
140+
var appConfigKey = new AppConfigKey(key);
141+
142+
// Assert
143+
Assert.Equal("profile123", appConfigKey.ConfigurationProfileId);
144+
Assert.Equal("flag456", appConfigKey.FlagKey);
145+
Assert.Equal("attr789", appConfigKey.AttributeKey);
146+
Assert.True(appConfigKey.HasAttribute);
147+
}
148+
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()
167+
{
94168
// Act & Assert
95-
Assert.Throws<ArgumentException>(() =>
96-
new AppConfigKey(application, environment, configuration));
169+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey("::"));
170+
Assert.Equal("Invalid key format. Flag key is expected in configurationProfileId:flagKey[:attributeKey] format", exception.Message);
97171
}
98172

99173
[Theory]
100-
[InlineData("a", "env", "config")] // too short
101-
[InlineData("app", "e", "config")] // too short
102-
[InlineData("app", "env", "c")] // too short
103-
public void Constructor_WithTooShortValues_ShouldThrowArgumentException(
104-
string application, string environment, string configuration)
174+
[InlineData("profile123::attr789")]
175+
[InlineData(":flag456:attr789")]
176+
[InlineData("::attr789")]
177+
public void Constructor_WithEmptyMiddleParts_PreservesNonEmptyParts(string key)
105178
{
106179
// Act & Assert
107-
Assert.Throws<ArgumentException>(() =>
108-
new AppConfigKey(application, environment, configuration));
180+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey(key));
181+
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+
}
199+
200+
[Fact]
201+
public void Constructor_WithLeadingSeparator_ThrowsArgumentException()
202+
{
203+
// Arrange
204+
var key = ":profile123:flag456";
205+
206+
// Act & Assert
207+
var exception = Assert.Throws<ArgumentException>(() => new AppConfigKey(key));
208+
Assert.Equal("Invalid key format. Flag key is expected in configurationProfileId:flagKey[:attributeKey] format", exception.Message);
209+
}
210+
211+
[Fact]
212+
public void HasAttribute_WhenAttributeKeyIsNull_ReturnsFalse()
213+
{
214+
// Arrange
215+
var appConfigKey = new AppConfigKey("profileId", "flagKey");
216+
217+
// Act & Assert
218+
Assert.False(appConfigKey.HasAttribute);
219+
}
220+
221+
[Fact]
222+
public void HasAttribute_WhenAttributeKeyIsEmpty_ReturnsFalse()
223+
{
224+
// Arrange
225+
var appConfigKey = new AppConfigKey("profileId", "flagKey", "");
226+
227+
// Act & Assert
228+
Assert.False(appConfigKey.HasAttribute);
229+
}
230+
231+
[Fact]
232+
public void HasAttribute_WhenAttributeKeyIsWhitespace_ReturnsFalse()
233+
{
234+
// Arrange
235+
var appConfigKey = new AppConfigKey("profileId", "flagKey", " ");
236+
237+
// Act & Assert
238+
Assert.False(appConfigKey.HasAttribute);
239+
}
240+
241+
[Fact]
242+
public void HasAttribute_WhenAttributeKeyIsProvided_ReturnsTrue()
243+
{
244+
// Arrange
245+
var appConfigKey = new AppConfigKey("profileId", "flagKey", "attributeKey");
246+
247+
// Act & Assert
248+
Assert.True(appConfigKey.HasAttribute);
249+
}
250+
251+
[Fact]
252+
public void HasAttribute_WhenConstructedWithStringWithAttribute_ReturnsTrue()
253+
{
254+
// Arrange
255+
var appConfigKey = new AppConfigKey("profileId:flagKey:attributeKey");
256+
257+
// Act & Assert
258+
Assert.True(appConfigKey.HasAttribute);
259+
}
260+
261+
[Fact]
262+
public void HasAttribute_WhenConstructedWithStringWithoutAttribute_ReturnsFalse()
263+
{
264+
// Arrange
265+
var appConfigKey = new AppConfigKey("profileId:flagKey");
266+
267+
// Act & Assert
268+
Assert.False(appConfigKey.HasAttribute);
109269
}
110270
}

0 commit comments

Comments
 (0)