-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to deserialize the config contents
- Loading branch information
asilverman
committed
Jan 22, 2024
1 parent
d09a014
commit 50f0a8d
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/Bicep.Core.UnitTests/Configuration/ProviderConfigurationTests.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,63 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
using Bicep.Core.Configuration; | ||
using Bicep.Core.Json; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Bicep.Core.UnitTests.Configuration; | ||
|
||
[TestClass] | ||
public class ProvidersConfigurationTests | ||
{ | ||
private static readonly JsonSerializerOptions DefaultDeserializeOptions = new() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
ReadCommentHandling = JsonCommentHandling.Skip, | ||
Converters = { new JsonStringEnumConverter() }, | ||
}; | ||
|
||
[TestMethod] | ||
public void MyExploratoryTest() | ||
{ | ||
var data = JsonElementFactory.CreateElement(""" | ||
{ | ||
"providers": { | ||
"az": { | ||
"source": { | ||
"registry": "mcr.microsoft.com/bicep/providers", | ||
"version": "0.2.3" | ||
}, | ||
"isDefaultImport": true | ||
}, | ||
"kubernetes": { | ||
"source": { | ||
"builtin": true | ||
} | ||
} | ||
} | ||
} | ||
"""); | ||
|
||
var res = JsonSerializer.Deserialize<ProvidersConfigurationSection>(data, DefaultDeserializeOptions); | ||
res.Should().NotBeNull(); | ||
res!.Providers.Should().NotBeNull(); | ||
res.Providers.Count.Should().Be(2); | ||
res.Providers["az"].Source.Should().NotBeNull(); | ||
// verifies that 'registry' and 'version' are valid properties for 'source' | ||
res.Providers["az"].Source!.Registry.Should().Be("mcr.microsoft.com/bicep/providers"); | ||
res.Providers["az"].Source!.Version.Should().Be("0.2.3"); | ||
// verifies the default value for 'IsDefaultImport' is true when specified | ||
res.Providers["az"].IsDefaultImport.Should().BeTrue(); | ||
res.Providers["kubernetes"].Source.Should().NotBeNull(); | ||
// verifies that 'builtin' is a valid property for 'source' | ||
res.Providers["kubernetes"].Source!.Builtin.Should().BeTrue(); | ||
// verifies the default value for 'IsDefaultImport' is false when unspecified | ||
res.Providers["kubernetes"].IsDefaultImport.Should().BeFalse(); | ||
} | ||
} |
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,30 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Immutable; | ||
using System.Text.Json; | ||
using Bicep.Core.Extensions; | ||
|
||
namespace Bicep.Core.Configuration; | ||
|
||
public record ProvidersConfigurationSection(ImmutableSortedDictionary<string, ProviderDescriptor> Providers) { } | ||
|
||
public record ProviderDescriptor(bool IsDefaultImport, ProviderSource? Source) { } | ||
|
||
public record ProviderSource(bool Builtin, string? Registry, string? Version) { } | ||
|
||
public partial class ProvidersConfiguration : ConfigurationSection<ProvidersConfigurationSection> | ||
{ | ||
private readonly string? configurationPath; | ||
|
||
private ProvidersConfiguration(ProvidersConfigurationSection data, string? configurationPath) | ||
: base(data) | ||
{ | ||
this.configurationPath = configurationPath; | ||
} | ||
|
||
public static ProvidersConfiguration Bind(JsonElement element, string? configurationPath) => new(element.ToNonNullObject<ProvidersConfigurationSection>(), configurationPath); | ||
|
||
} | ||
|
||
|