Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit a9e143d

Browse files
authored
Added Unit Testcases for Group List Command (#694)
* Added Testcases for Group List Command * Addressed review comments
1 parent 1b4e1d4 commit a9e143d

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.CommandLine;
5+
using System.CommandLine.Parsing;
6+
using System.Text.Json;
7+
using AzureMcp.Areas.Group.Commands;
8+
using AzureMcp.Models.Command;
9+
using AzureMcp.Models.ResourceGroup;
10+
using AzureMcp.Options;
11+
using AzureMcp.Services.Azure.ResourceGroup;
12+
using AzureMcp.Tests.Helpers;
13+
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.Logging;
15+
using ModelContextProtocol.Server;
16+
using NSubstitute;
17+
using Xunit;
18+
19+
namespace AzureMcp.Tests.Areas.Group.UnitTests;
20+
21+
[Trait("Area", "Group")]
22+
public class GroupListCommandTests
23+
{
24+
private readonly IServiceProvider _serviceProvider;
25+
private readonly IMcpServer _mcpServer;
26+
private readonly ILogger<GroupListCommand> _logger;
27+
private readonly IResourceGroupService _resourceGroupService;
28+
private readonly GroupListCommand _command;
29+
private readonly CommandContext _context;
30+
private readonly Parser _parser;
31+
32+
public GroupListCommandTests()
33+
{
34+
_mcpServer = Substitute.For<IMcpServer>();
35+
_resourceGroupService = Substitute.For<IResourceGroupService>();
36+
_logger = Substitute.For<ILogger<GroupListCommand>>();
37+
var collection = new ServiceCollection()
38+
.AddSingleton(_mcpServer)
39+
.AddSingleton(_resourceGroupService);
40+
41+
_serviceProvider = collection.BuildServiceProvider();
42+
_command = new(_logger);
43+
_context = new(_serviceProvider);
44+
_parser = new(_command.GetCommand());
45+
}
46+
47+
[Fact]
48+
public async Task ExecuteAsync_WithValidSubscription_ReturnsResourceGroups()
49+
{
50+
// Arrange
51+
var subscriptionId = "test-subscription-id";
52+
var expectedGroups = new List<ResourceGroupInfo>
53+
{
54+
ResourceGroupTestHelpers.CreateResourceGroupInfo("rg1", subscriptionId, "East US"),
55+
ResourceGroupTestHelpers.CreateResourceGroupInfo("rg2", subscriptionId, "West US")
56+
};
57+
58+
_resourceGroupService
59+
.GetResourceGroups(Arg.Is<string>(x => x == subscriptionId), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
60+
.Returns(expectedGroups);
61+
62+
var args = _parser.Parse($"--subscription {subscriptionId}");
63+
64+
// Act
65+
var result = await _command.ExecuteAsync(_context, args);
66+
67+
// Assert
68+
Assert.NotNull(result);
69+
Assert.Equal(200, result.Status);
70+
Assert.NotNull(result.Results);
71+
72+
var jsonDoc = JsonDocument.Parse(JsonSerializer.Serialize(result.Results));
73+
var groupsArray = jsonDoc.RootElement.GetProperty("groups");
74+
75+
Assert.Equal(2, groupsArray.GetArrayLength());
76+
77+
var first = groupsArray[0];
78+
var second = groupsArray[1];
79+
80+
Assert.Equal("rg1", first.GetProperty("name").GetString());
81+
Assert.Equal("/subscriptions/test-subscription-id/resourceGroups/rg1", first.GetProperty("id").GetString());
82+
Assert.Equal("East US", first.GetProperty("location").GetString());
83+
84+
Assert.Equal("rg2", second.GetProperty("name").GetString());
85+
Assert.Equal("/subscriptions/test-subscription-id/resourceGroups/rg2", second.GetProperty("id").GetString());
86+
Assert.Equal("West US", second.GetProperty("location").GetString());
87+
88+
await _resourceGroupService.Received(1).GetResourceGroups(
89+
Arg.Is<string>(x => x == subscriptionId),
90+
Arg.Any<string>(),
91+
Arg.Any<RetryPolicyOptions>());
92+
}
93+
94+
[Fact]
95+
public async Task ExecuteAsync_WithTenant_PassesTenantToService()
96+
{
97+
// Arrange
98+
var subscriptionId = "test-subscription-id";
99+
var tenantId = "test-tenant-id";
100+
var expectedGroups = new List<ResourceGroupInfo>
101+
{
102+
ResourceGroupTestHelpers.CreateResourceGroupInfo("rg1", subscriptionId, "East US")
103+
};
104+
105+
_resourceGroupService
106+
.GetResourceGroups(
107+
Arg.Is<string>(x => x == subscriptionId),
108+
Arg.Is<string>(x => x == tenantId),
109+
Arg.Any<RetryPolicyOptions>())
110+
.Returns(expectedGroups);
111+
112+
var args = _parser.Parse($"--subscription {subscriptionId} --tenant {tenantId}");
113+
114+
// Act
115+
var result = await _command.ExecuteAsync(_context, args);
116+
117+
// Assert
118+
Assert.NotNull(result);
119+
Assert.Equal(200, result.Status);
120+
await _resourceGroupService.Received(1).GetResourceGroups(
121+
Arg.Is<string>(x => x == subscriptionId),
122+
Arg.Is<string>(x => x == tenantId),
123+
Arg.Any<RetryPolicyOptions>());
124+
}
125+
126+
[Fact]
127+
public async Task ExecuteAsync_EmptyResourceGroupList_ReturnsNullResults()
128+
{
129+
// Arrange
130+
var subscriptionId = "test-subscription-id";
131+
_resourceGroupService
132+
.GetResourceGroups(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
133+
.Returns([]);
134+
135+
var args = _parser.Parse($"--subscription {subscriptionId}");
136+
137+
// Act
138+
var result = await _command.ExecuteAsync(_context, args);
139+
140+
// Assert
141+
Assert.NotNull(result);
142+
Assert.Equal(200, result.Status);
143+
Assert.Null(result.Results);
144+
}
145+
146+
[Fact]
147+
public async Task ExecuteAsync_ServiceThrowsException_ReturnsErrorInResponse()
148+
{
149+
// Arrange
150+
var subscriptionId = "test-subscription-id";
151+
var expectedError = "Test error message";
152+
_resourceGroupService
153+
.GetResourceGroups(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
154+
.Returns(Task.FromException<List<ResourceGroupInfo>>(new Exception(expectedError)));
155+
156+
var args = _parser.Parse($"--subscription {subscriptionId}");
157+
158+
// Act
159+
var result = await _command.ExecuteAsync(_context, args);
160+
161+
// Assert
162+
Assert.NotNull(result);
163+
Assert.Equal(500, result.Status);
164+
Assert.Contains(expectedError, result.Message);
165+
}
166+
167+
[Fact]
168+
public async Task ExecuteAsync_MissingSubscription_FailsValidation()
169+
{
170+
// Arrange
171+
var args = _parser.Parse(""); // No subscription provided
172+
173+
// Act
174+
var result = await _command.ExecuteAsync(_context, args);
175+
176+
// Assert
177+
Assert.NotNull(result);
178+
Assert.Equal(400, result.Status);
179+
Assert.Contains("required", result.Message.ToLower());
180+
// Verify service was not called when validation fails
181+
await _resourceGroupService.DidNotReceive().GetResourceGroups(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>());
182+
}
183+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using AzureMcp.Models.ResourceGroup;
5+
6+
namespace AzureMcp.Tests.Helpers;
7+
8+
/// <summary>
9+
/// Helper methods for creating test resource group data.
10+
/// </summary>
11+
public static class ResourceGroupTestHelpers
12+
{
13+
/// <summary>
14+
/// Creates a ResourceGroupInfo with the specified properties for testing.
15+
/// </summary>
16+
/// <param name="name">The name of the resource group</param>
17+
/// <param name="subscriptionId">The subscription ID (used to construct the full resource ID)</param>
18+
/// <param name="location">The Azure region/location</param>
19+
/// <returns>A ResourceGroupInfo instance for testing</returns>
20+
public static ResourceGroupInfo CreateResourceGroupInfo(string name, string subscriptionId, string location)
21+
{
22+
var id = $"/subscriptions/{subscriptionId}/resourceGroups/{name}";
23+
return new ResourceGroupInfo(name, id, location);
24+
}
25+
26+
27+
}

0 commit comments

Comments
 (0)