-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AppConfiguration] Implemented SettingFieldsExtensions
- Loading branch information
Showing
5 changed files
with
124 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
sdk/appconfiguration/Azure.Data.AppConfiguration/src/Models/SettingFieldsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Azure.Data.AppConfiguration | ||
{ | ||
internal static class SettingFieldsExtensions | ||
{ | ||
/// <summary> | ||
/// Maps a SettingFields member to its corresponding service name in accordance with the REST API specification. | ||
/// </summary> | ||
private static readonly IReadOnlyDictionary<SettingFields, string> s_serviceNameMap = new Dictionary<SettingFields, string>() | ||
{ | ||
{ SettingFields.Key , "key" }, | ||
{ SettingFields.Label , "label" }, | ||
{ SettingFields.Value , "value" }, | ||
{ SettingFields.ContentType , "content_type" }, | ||
{ SettingFields.ETag , "etag" }, | ||
{ SettingFields.LastModified, "last_modified" }, | ||
{ SettingFields.IsReadOnly , "locked" }, | ||
{ SettingFields.Tags , "tags" } | ||
}; | ||
|
||
public static IEnumerable<string> Split(this SettingFields fields) | ||
{ | ||
if (fields == SettingFields.All) | ||
{ | ||
return null; | ||
} | ||
|
||
var splitFields = new List<string>(); | ||
|
||
foreach (SettingFields currentField in s_serviceNameMap.Keys) | ||
{ | ||
if ((fields & currentField) == currentField) | ||
{ | ||
splitFields.Add(s_serviceNameMap[currentField]); | ||
} | ||
} | ||
|
||
return splitFields; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sdk/appconfiguration/Azure.Data.AppConfiguration/tests/SettingFieldsExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Data.AppConfiguration.Tests | ||
{ | ||
public class SettingFieldsExtensionsTests | ||
{ | ||
[Test] | ||
public void SplitReturnsNullForAll() | ||
{ | ||
Assert.IsNull(SettingFields.All.Split()); | ||
} | ||
|
||
[Test] | ||
[TestCase(SettingFields.Key, "key")] | ||
[TestCase(SettingFields.Label, "label")] | ||
[TestCase(SettingFields.Value, "value")] | ||
[TestCase(SettingFields.ContentType, "content_type")] | ||
[TestCase(SettingFields.ETag, "etag")] | ||
[TestCase(SettingFields.LastModified, "last_modified")] | ||
[TestCase(SettingFields.IsReadOnly, "locked")] | ||
[TestCase(SettingFields.Tags, "tags")] | ||
public void SplitWithSingleField(SettingFields fields, string expectedFieldString) | ||
{ | ||
IEnumerable<string> splitFields = fields.Split(); | ||
string fieldString = splitFields.Single(); | ||
|
||
Assert.AreEqual(fieldString, expectedFieldString); | ||
} | ||
|
||
[Test] | ||
public void SplitWithMultipleFields() | ||
{ | ||
SettingFields fields = SettingFields.Key | SettingFields.ContentType | SettingFields.LastModified | SettingFields.IsReadOnly; | ||
IEnumerable<string> splitFields = fields.Split(); | ||
|
||
Assert.AreEqual(splitFields.Count(), 4); | ||
CollectionAssert.Contains(splitFields, "key"); | ||
CollectionAssert.Contains(splitFields, "content_type"); | ||
CollectionAssert.Contains(splitFields, "last_modified"); | ||
CollectionAssert.Contains(splitFields, "locked"); | ||
} | ||
|
||
[Test] | ||
public void SplitSupportsAllPossibleSettingFields() | ||
{ | ||
foreach (SettingFields fields in Enum.GetValues(typeof(SettingFields))) | ||
{ | ||
if (fields == SettingFields.All) | ||
{ | ||
continue; | ||
} | ||
|
||
IEnumerable<string> splitFields = fields.Split(); | ||
|
||
// If this assertion fails, it's likely that a new enum value has been added to SettingFields | ||
// but a corresponding entry has not been added to s_serviceNameMap in SettingFieldsExtensions. | ||
Assert.AreEqual(splitFields.Count(), 1, $"{nameof(SettingFields)} enum value {fields} could not be mapped to a string."); | ||
} | ||
} | ||
} | ||
} |