diff --git a/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Helpers/LiveTestSettingsTests.cs b/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Helpers/LiveTestSettingsTests.cs new file mode 100644 index 0000000000..7ef3202ab7 --- /dev/null +++ b/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Helpers/LiveTestSettingsTests.cs @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Text.Json; +using System.Text.Json.Serialization; +using Azure.Mcp.Tests.Helpers; +using Xunit; + +namespace Azure.Mcp.Core.UnitTests.Helpers; + +public class LiveTestSettingsTests +{ + [Fact] + public void JsonDeserialization_WithInvalidTestMode_ThrowsJsonException() + { + // Arrange - Create JSON with an invalid TestMode value + var json = """{"TestMode": "InvalidMode", "TenantId": "test-tenant"}"""; + + // Act & Assert - Verify JsonException is thrown + var exception = Assert.Throws(() => + JsonSerializer.Deserialize(json, new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true, + Converters = { new JsonStringEnumConverter() } + })); + + // Verify the error message mentions TestMode + Assert.Contains("TestMode", exception.Message); + } + + [Theory] + [InlineData("Live", TestMode.Live)] + [InlineData("Record", TestMode.Record)] + [InlineData("Playback", TestMode.Playback)] + [InlineData("live", TestMode.Live)] // Test case-insensitive + [InlineData("record", TestMode.Record)] + [InlineData("playback", TestMode.Playback)] + public void JsonDeserialization_WithValidTestMode_LoadsSuccessfully(string testModeValue, TestMode expectedMode) + { + // Arrange - Create JSON with valid TestMode value + var json = $$$"""{"TestMode": "{{{testModeValue}}}", "TenantId": "test-tenant", "SubscriptionId": "test-subscription"}"""; + + // Act - Deserialize the JSON + var settings = JsonSerializer.Deserialize(json, new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true, + Converters = { new JsonStringEnumConverter() } + }); + + // Assert - Verify the settings were loaded correctly + Assert.NotNull(settings); + Assert.Equal(expectedMode, settings.TestMode); + Assert.Equal("test-tenant", settings.TenantId); + Assert.Equal("test-subscription", settings.SubscriptionId); + } + + /// + /// Proxy class for testing LiveTestSettings JSON deserialization without file I/O + /// + private class LiveTestSettingsProxy + { + public TestMode TestMode { get; set; } = TestMode.Live; + public string TenantId { get; set; } = string.Empty; + public string SubscriptionId { get; set; } = string.Empty; + } +} diff --git a/core/Azure.Mcp.Core/tests/Azure.Mcp.Tests/Client/Helpers/LiveTestSettings.cs b/core/Azure.Mcp.Core/tests/Azure.Mcp.Tests/Client/Helpers/LiveTestSettings.cs index ef350bae80..842f5bc20d 100644 --- a/core/Azure.Mcp.Core/tests/Azure.Mcp.Tests/Client/Helpers/LiveTestSettings.cs +++ b/core/Azure.Mcp.Core/tests/Azure.Mcp.Tests/Client/Helpers/LiveTestSettings.cs @@ -53,16 +53,26 @@ public static bool TryLoadTestSettings([NotNullWhen(true)] out LiveTestSettings? { var json = File.ReadAllText(path); - settings = JsonSerializer.Deserialize(json, new JsonSerializerOptions() + try { - PropertyNameCaseInsensitive = true, - Converters = { new JsonStringEnumConverter() } - }); + settings = JsonSerializer.Deserialize(json, new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true, + Converters = { new JsonStringEnumConverter() } + }); - if (settings != null) + if (settings != null) + { + settings.SettingsDirectory = Path.GetDirectoryName(path) ?? string.Empty; + return true; + } + } + catch (JsonException ex) when (ex.Message.Contains("TestMode")) { - settings.SettingsDirectory = Path.GetDirectoryName(path) ?? string.Empty; - return true; + var validValues = string.Join(", ", Enum.GetNames()); + throw new InvalidOperationException( + $"Invalid TestMode value in {TestSettingsFileName}. Valid values are: {validValues}. " + + $"Error details: {ex.Message}", ex); } }