Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 7b48129

Browse files
committed
Updated tests to check for empty list instead of null
1 parent e5e18b7 commit 7b48129

21 files changed

Lines changed: 628 additions & 375 deletions

.vscode/mcp.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"servers": {
3+
"azure-mcp-server": {
4+
"type": "stdio",
5+
"command": "D:/dev/github.com/BrianCollet/azure-mcp/src/bin/Debug/net9.0/azmcp.exe",
6+
"args": ["server", "start", "--service", "cosmos,redis"]
7+
}
8+
}
9+
}

tests/Areas/Authorization/UnitTests/RoleAssignmentListCommandTests.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,67 +24,71 @@ public class RoleAssignmentListCommandTests
2424
private readonly IServiceProvider _serviceProvider;
2525
private readonly IAuthorizationService _authorizationService;
2626
private readonly ILogger<RoleAssignmentListCommand> _logger;
27+
private readonly RoleAssignmentListCommand _command;
28+
private readonly CommandContext _context;
29+
private readonly Parser _parser;
30+
private readonly string _knownSubscriptionId = "00000000-0000-0000-0000-000000000001";
31+
private readonly string _knownScope = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1";
2732

2833
public RoleAssignmentListCommandTests()
2934
{
3035
_authorizationService = Substitute.For<IAuthorizationService>();
3136
_logger = Substitute.For<ILogger<RoleAssignmentListCommand>>();
3237

33-
var collection = new ServiceCollection();
34-
collection.AddSingleton(_authorizationService);
38+
var collection = new ServiceCollection().AddSingleton(_authorizationService);
3539

3640
_serviceProvider = collection.BuildServiceProvider();
41+
_command = new(_logger);
42+
_context = new(_serviceProvider);
43+
_parser = new(_command.GetCommand());
3744
}
3845

3946
[Fact]
4047
public async Task ExecuteAsync_ReturnsRoleAssignments_WhenRoleAssignmentsExist()
4148
{
4249
// Arrange
43-
var subscriptionId = "00000000-0000-0000-0000-000000000001";
44-
var scope = $"/subscriptions/{subscriptionId}/resourceGroups/rg1";
4550
var id1 = "00000000-0000-0000-0000-000000000001";
4651
var id2 = "00000000-0000-0000-0000-000000000002";
4752
var expectedRoleAssignments = new List<RoleAssignment>
4853
{
4954
new RoleAssignment
5055
{
51-
Id = $"/subscriptions/{subscriptionId}/resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{id1}",
56+
Id = $"/subscriptions/{_knownSubscriptionId}/resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{id1}",
5257
Name = "Test role definition 1",
5358
PrincipalId = new Guid(id1),
5459
PrincipalType = "User",
55-
RoleDefinitionId = $"/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/{id1}",
56-
Scope = scope,
60+
RoleDefinitionId = $"/subscriptions/{_knownSubscriptionId}/providers/Microsoft.Authorization/roleDefinitions/{id1}",
61+
Scope = _knownScope,
5762
Description = "Role assignment for azmcp test 1",
5863
DelegatedManagedIdentityResourceId = string.Empty,
5964
Condition = string.Empty
6065
},
6166
new RoleAssignment
6267
{
63-
Id = $"/subscriptions/{subscriptionId}/resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{id2}",
68+
Id = $"/subscriptions/{_knownSubscriptionId}/resourcegroups/azure-mcp/providers/Microsoft.Authorization/roleAssignments/{id2}",
6469
Name = "Test role definition 2",
6570
PrincipalId = new Guid(id2),
6671
PrincipalType = "User",
67-
RoleDefinitionId = $"/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/{id2}",
68-
Scope = scope,
72+
RoleDefinitionId = $"/subscriptions/{_knownSubscriptionId}/providers/Microsoft.Authorization/roleDefinitions/{id2}",
73+
Scope = _knownScope,
6974
Description = "Role assignment for azmcp test 2",
7075
DelegatedManagedIdentityResourceId = string.Empty,
7176
Condition = "ActionMatches{'Microsoft.Authorization/roleAssignments/write'}"
7277
}
7378
};
7479
_authorizationService.ListRoleAssignments(
75-
Arg.Is(scope),
80+
Arg.Is(_knownScope),
7681
Arg.Any<string>(),
7782
Arg.Any<RetryPolicyOptions>())
7883
.Returns(expectedRoleAssignments);
79-
var command = new RoleAssignmentListCommand(_logger);
80-
var args = command.GetCommand().Parse([
81-
"--subscription", subscriptionId,
82-
"--scope", scope,
84+
85+
var args = _parser.Parse([
86+
"--subscription", _knownSubscriptionId,
87+
"--scope", _knownScope,
8388
]);
84-
var context = new CommandContext(_serviceProvider);
8589

8690
// Act
87-
var response = await command.ExecuteAsync(context, args);
91+
var response = await _command.ExecuteAsync(_context, args);
8892

8993
// Assert
9094
Assert.NotNull(response);
@@ -98,49 +102,48 @@ public async Task ExecuteAsync_ReturnsRoleAssignments_WhenRoleAssignmentsExist()
98102
}
99103

100104
[Fact]
101-
public async Task ExecuteAsync_ReturnsNull_WhenNoRoleAssignments()
105+
public async Task ExecuteAsync_ReturnsEmptyList_WhenNoRoleAssignments()
102106
{
103107
// Arrange
104-
var subscriptionId = "00000000-0000-0000-0000-000000000001";
105-
var scope = $"/subscriptions/{subscriptionId}/resourceGroups/rg1";
106-
_authorizationService.ListRoleAssignments(scope, null, null)
108+
_authorizationService.ListRoleAssignments(Arg.Is(_knownScope), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
107109
.Returns([]);
108110

109-
var command = new RoleAssignmentListCommand(_logger);
110-
var args = command.GetCommand().Parse([
111-
"--subscription", subscriptionId,
112-
"--scope", scope
111+
var args = _parser.Parse([
112+
"--subscription", _knownSubscriptionId,
113+
"--scope", _knownScope
113114
]);
114-
var context = new CommandContext(_serviceProvider);
115115

116116
// Act
117-
var response = await command.ExecuteAsync(context, args);
117+
var response = await _command.ExecuteAsync(_context, args);
118118

119119
// Assert
120120
Assert.NotNull(response);
121-
Assert.Null(response.Results);
121+
Assert.NotNull(response.Results);
122+
123+
var json = JsonSerializer.Serialize(response.Results);
124+
var result = JsonSerializer.Deserialize<RoleAssignmentListResult>(json);
125+
126+
Assert.NotNull(result);
127+
Assert.NotNull(result.Assignments);
128+
Assert.Empty(result.Assignments);
122129
}
123130

124131
[Fact]
125132
public async Task ExecuteAsync_HandlesException()
126133
{
127134
// Arrange
128135
var expectedError = "Test error";
129-
var subscriptionId = "00000000-0000-0000-0000-000000000001";
130-
var scope = $"/subscriptions/{subscriptionId}/resourceGroups/rg1";
131136

132-
_authorizationService.ListRoleAssignments(scope, null, Arg.Any<RetryPolicyOptions>())
137+
_authorizationService.ListRoleAssignments(Arg.Is(_knownScope), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
133138
.ThrowsAsync(new Exception(expectedError));
134139

135-
var command = new RoleAssignmentListCommand(_logger);
136-
var args = command.GetCommand().Parse([
137-
"--subscription", subscriptionId,
138-
"--scope", scope
140+
var args = _parser.Parse([
141+
"--subscription", _knownSubscriptionId,
142+
"--scope", _knownScope
139143
]);
140-
var context = new CommandContext(_serviceProvider);
141144

142145
// Act
143-
var response = await command.ExecuteAsync(context, args);
146+
var response = await _command.ExecuteAsync(_context, args);
144147

145148
// Assert
146149
Assert.NotNull(response);

tests/Areas/Cosmos/UnitTests/AccountListCommandTests.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,36 @@ public class AccountListCommandTests
2323
private readonly IServiceProvider _serviceProvider;
2424
private readonly ICosmosService _cosmosService;
2525
private readonly ILogger<AccountListCommand> _logger;
26+
private readonly AccountListCommand _command;
27+
private readonly CommandContext _context;
28+
private readonly Parser _parser;
29+
private readonly string _knownSubscriptionId = "00000000-0000-0000-0000-000000000001";
2630

2731
public AccountListCommandTests()
2832
{
2933
_cosmosService = Substitute.For<ICosmosService>();
3034
_logger = Substitute.For<ILogger<AccountListCommand>>();
3135

32-
var collection = new ServiceCollection();
33-
collection.AddSingleton(_cosmosService);
36+
var collection = new ServiceCollection().AddSingleton(_cosmosService);
3437

3538
_serviceProvider = collection.BuildServiceProvider();
39+
_command = new(_logger);
40+
_context = new(_serviceProvider);
41+
_parser = new(_command.GetCommand());
3642
}
3743

3844
[Fact]
3945
public async Task ExecuteAsync_ReturnsAccounts_WhenAccountsExist()
4046
{
4147
// Arrange
4248
var expectedAccounts = new List<string> { "account1", "account2" };
43-
_cosmosService.GetCosmosAccounts(Arg.Is("sub123"), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
49+
_cosmosService.GetCosmosAccounts(Arg.Is(_knownSubscriptionId), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
4450
.Returns(expectedAccounts);
4551

46-
var command = new AccountListCommand(_logger);
47-
var args = command.GetCommand().Parse(["--subscription", "sub123"]);
48-
var context = new CommandContext(_serviceProvider);
52+
var args = _parser.Parse(["--subscription", _knownSubscriptionId]);
4953

5054
// Act
51-
var response = await command.ExecuteAsync(context, args);
55+
var response = await _command.ExecuteAsync(_context, args);
5256

5357
// Assert
5458
Assert.NotNull(response);
@@ -62,40 +66,42 @@ public async Task ExecuteAsync_ReturnsAccounts_WhenAccountsExist()
6266
}
6367

6468
[Fact]
65-
public async Task ExecuteAsync_ReturnsNull_WhenNoAccounts()
69+
public async Task ExecuteAsync_ReturnsEmptyList_WhenNoAccounts()
6670
{
6771
// Arrange
68-
_cosmosService.GetCosmosAccounts("sub123", null, null)
72+
_cosmosService.GetCosmosAccounts(Arg.Is(_knownSubscriptionId), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
6973
.Returns([]);
7074

71-
var command = new AccountListCommand(_logger);
72-
var args = command.GetCommand().Parse(["--subscription", "sub123"]);
73-
var context = new CommandContext(_serviceProvider);
75+
var args = _parser.Parse(["--subscription", _knownSubscriptionId]);
7476

7577
// Act
76-
var response = await command.ExecuteAsync(context, args);
78+
var response = await _command.ExecuteAsync(_context, args);
7779

7880
// Assert
7981
Assert.NotNull(response);
80-
Assert.Null(response.Results);
82+
Assert.NotNull(response.Results);
83+
84+
var json = JsonSerializer.Serialize(response.Results);
85+
var result = JsonSerializer.Deserialize<AccountListResult>(json);
86+
87+
Assert.NotNull(result);
88+
Assert.NotNull(result.Accounts);
89+
Assert.Empty(result.Accounts);
8190
}
8291

8392
[Fact]
8493
public async Task ExecuteAsync_HandlesException()
8594
{
8695
// Arrange
8796
var expectedError = "Test error";
88-
var subscriptionId = "sub123";
8997

90-
_cosmosService.GetCosmosAccounts(subscriptionId, null, Arg.Any<RetryPolicyOptions>())
98+
_cosmosService.GetCosmosAccounts(Arg.Is(_knownSubscriptionId), Arg.Any<string>(), Arg.Any<RetryPolicyOptions>())
9199
.ThrowsAsync(new Exception(expectedError));
92100

93-
var command = new AccountListCommand(_logger);
94-
var args = command.GetCommand().Parse(["--subscription", subscriptionId]);
95-
var context = new CommandContext(_serviceProvider);
101+
var args = _parser.Parse(["--subscription", _knownSubscriptionId]);
96102

97103
// Act
98-
var response = await command.ExecuteAsync(context, args);
104+
var response = await _command.ExecuteAsync(_context, args);
99105

100106
// Assert
101107
Assert.NotNull(response);
@@ -108,4 +114,4 @@ private class AccountListResult
108114
[JsonPropertyName("accounts")]
109115
public List<string> Accounts { get; set; } = [];
110116
}
111-
}
117+
}

tests/Areas/KeyVault/UnitTests/Key/KeyListCommandTests.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class KeyListCommandTests
2626
private readonly KeyListCommand _command;
2727
private readonly CommandContext _context;
2828
private readonly Parser _parser;
29+
private readonly string _knownSubscriptionId = "00000000-0000-0000-0000-000000000001";
30+
private readonly string _knownVaultName = "vault123";
2931

3032
public KeyListCommandTests()
3133
{
@@ -45,16 +47,14 @@ public KeyListCommandTests()
4547
public async Task ExecuteAsync_ReturnsKeys_WhenKeysExist()
4648
{
4749
// Arrange
48-
var subscriptionId = "sub123";
49-
var vaultName = "vault123";
5050
var expectedKeys = new List<string> { "key1", "key2" };
5151

52-
_keyVaultService.ListKeys(Arg.Is(vaultName), Arg.Any<bool>(), Arg.Is(subscriptionId), Arg.Any<string>(),
52+
_keyVaultService.ListKeys(Arg.Is(_knownVaultName), Arg.Any<bool>(), Arg.Is(_knownSubscriptionId), Arg.Any<string>(),
5353
Arg.Any<RetryPolicyOptions>()).Returns(expectedKeys);
5454

5555
var args = _parser.Parse([
56-
"--vault", vaultName,
57-
"--subscription", subscriptionId
56+
"--vault", _knownVaultName,
57+
"--subscription", _knownSubscriptionId
5858
]);
5959

6060
// Act
@@ -72,42 +72,44 @@ public async Task ExecuteAsync_ReturnsKeys_WhenKeysExist()
7272
}
7373

7474
[Fact]
75-
public async Task ExecuteAsync_ReturnsNull_WhenNoKeys()
75+
public async Task ExecuteAsync_ReturnsEmptyList_WhenNoKeys()
7676
{
7777
// Arrange
78-
var subscriptionId = "sub123";
79-
var vaultName = "vault123";
80-
81-
_keyVaultService.ListKeys(vaultName, Arg.Any<bool>(), Arg.Is(subscriptionId), Arg.Any<string>(),
78+
_keyVaultService.ListKeys(_knownVaultName, Arg.Any<bool>(), Arg.Is(_knownSubscriptionId), Arg.Any<string>(),
8279
Arg.Any<RetryPolicyOptions>()).Returns([]);
8380

8481
var args = _parser.Parse([
85-
"--vault", vaultName,
86-
"--subscription", subscriptionId
82+
"--vault", _knownVaultName,
83+
"--subscription", _knownSubscriptionId
8784
]);
8885

8986
// Act
9087
var response = await _command.ExecuteAsync(_context, args);
9188

9289
// Assert
9390
Assert.NotNull(response);
94-
Assert.Null(response.Results);
91+
Assert.NotNull(response.Results);
92+
93+
var json = JsonSerializer.Serialize(response.Results);
94+
var result = JsonSerializer.Deserialize<KeyListResult>(json);
95+
96+
Assert.NotNull(result);
97+
Assert.NotNull(result.Keys);
98+
Assert.Empty(result.Keys);
9599
}
96100

97101
[Fact]
98102
public async Task ExecuteAsync_HandlesException()
99103
{
100104
// Arrange
101105
var expectedError = "Test error";
102-
var subscriptionId = "sub123";
103-
var vaultName = "vault123";
104106

105-
_keyVaultService.ListKeys(vaultName, Arg.Any<bool>(), Arg.Is(subscriptionId), Arg.Any<string>(),
107+
_keyVaultService.ListKeys(_knownVaultName, Arg.Any<bool>(), Arg.Is(_knownSubscriptionId), Arg.Any<string>(),
106108
Arg.Any<RetryPolicyOptions>()).ThrowsAsync(new Exception(expectedError));
107109

108110
var args = _parser.Parse([
109-
"--vault", vaultName,
110-
"--subscription", subscriptionId
111+
"--vault", _knownVaultName,
112+
"--subscription", _knownSubscriptionId
111113
]);
112114

113115
// Act

0 commit comments

Comments
 (0)