Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

namespace Azure.Mcp.Tools.AppService.Commands.Database;

public sealed class DatabaseAddCommand(ILogger<DatabaseAddCommand> logger) : BaseAppServiceCommand<DatabaseAddOptions>()
public sealed class DatabaseAddCommand(ILogger<DatabaseAddCommand> logger, IAppServiceService appServiceService) : BaseAppServiceCommand<DatabaseAddOptions>()
{
private const string CommandTitle = "Add Database to App Service";
private readonly ILogger<DatabaseAddCommand> _logger = logger;
private readonly IAppServiceService _appServiceService = appServiceService;

public override string Id => "14be1264-82c8-4a4c-8271-7cfe1fbebbc8";

Expand Down Expand Up @@ -74,8 +75,7 @@ public override async Task<CommandResponse> ExecuteAsync(CommandContext context,
{
context.Activity?.AddTag("subscription", options.Subscription);

var appServiceService = context.GetService<IAppServiceService>();
var connectionInfo = await appServiceService.AddDatabaseAsync(
var connectionInfo = await _appServiceService.AddDatabaseAsync(
options.AppName!,
options.ResourceGroup!,
options.DatabaseType!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

namespace Azure.Mcp.Tools.AppService.Commands.Webapp.Settings;

public sealed class AppSettingsGetCommand(ILogger<AppSettingsGetCommand> logger)
public sealed class AppSettingsGetCommand(ILogger<AppSettingsGetCommand> logger, IAppServiceService appServiceService)
: BaseAppServiceCommand<BaseAppServiceOptions>(resourceGroupRequired: true)
{
private const string CommandTitle = "Gets Azure App Service Web App Application Settings";
private readonly ILogger<AppSettingsGetCommand> _logger = logger;
private readonly IAppServiceService _appServiceService = appServiceService;
public override string Id => "825ef21f-392f-4cd4-8272-7e7dce12e293";
public override string Name => "get-appsettings";

Expand Down Expand Up @@ -64,8 +65,7 @@ public override async Task<CommandResponse> ExecuteAsync(CommandContext context,
{
context.Activity?.AddTag("subscription", options.Subscription);

var appServiceService = context.GetService<IAppServiceService>();
var appSettings = await appServiceService.GetAppSettingsAsync(
var appSettings = await _appServiceService.GetAppSettingsAsync(
options.Subscription!,
options.ResourceGroup!,
options.AppName!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

namespace Azure.Mcp.Tools.AppService.Commands.Webapp.Settings;

public sealed class AppSettingsUpdateCommand(ILogger<AppSettingsUpdateCommand> logger)
public sealed class AppSettingsUpdateCommand(ILogger<AppSettingsUpdateCommand> logger, IAppServiceService appServiceService)
: BaseAppServiceCommand<AppSettingsUpdateCommandOptions>(resourceGroupRequired: true)
{
private const string CommandTitle = "Updates Azure App Service Web App Application Settings";
private readonly ILogger<AppSettingsUpdateCommand> _logger = logger;
private readonly IAppServiceService _appServiceService = appServiceService;
public override string Id => "08ca52a3-f766-4c62-9597-702f629efaf6";
public override string Name => "update-appsettings";

Expand Down Expand Up @@ -115,8 +116,7 @@ public override async Task<CommandResponse> ExecuteAsync(CommandContext context,
{
context.Activity?.AddTag("subscription", options.Subscription);

var appServiceService = context.GetService<IAppServiceService>();
var updateResult = await appServiceService.UpdateAppSettingsAsync(
var updateResult = await _appServiceService.UpdateAppSettingsAsync(
options.Subscription!,
options.ResourceGroup!,
options.AppName!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

namespace Azure.Mcp.Tools.AppService.Commands.Webapp;

public sealed class WebappGetCommand(ILogger<WebappGetCommand> logger) : BaseAppServiceCommand<BaseAppServiceOptions>()
public sealed class WebappGetCommand(ILogger<WebappGetCommand> logger, IAppServiceService appServiceService) : BaseAppServiceCommand<BaseAppServiceOptions>()
{
private const string CommandTitle = "Gets Azure App Service Web App Details";
private readonly ILogger<WebappGetCommand> _logger = logger;
private readonly IAppServiceService _appServiceService = appServiceService;
public override string Id => "4412f1af-16e7-46db-8305-33e3d7ae06de";
public override string Name => "get";

Expand Down Expand Up @@ -76,8 +77,7 @@ public override async Task<CommandResponse> ExecuteAsync(CommandContext context,
{
context.Activity?.AddTag("subscription", options.Subscription);

var appServiceService = context.GetService<IAppServiceService>();
var webapps = await appServiceService.GetWebAppsAsync(
var webapps = await _appServiceService.GetWebAppsAsync(
options.Subscription!,
options.ResourceGroup,
options.AppName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ namespace Azure.Mcp.Tools.AppService.UnitTests.Commands.Database;
public class DatabaseAddCommandTests
{
private readonly IAppServiceService _appServiceService;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<DatabaseAddCommand> _logger;
private readonly DatabaseAddCommand _command;
private readonly CommandContext _context;

public DatabaseAddCommandTests()
{
_appServiceService = Substitute.For<IAppServiceService>();
_logger = Substitute.For<ILogger<DatabaseAddCommand>>();
var collection = new ServiceCollection();
collection.AddSingleton(_appServiceService);
collection.AddSingleton(_logger);
_serviceProvider = collection.BuildServiceProvider();
_command = new(_logger, _appServiceService);
_context = new(new ServiceCollection().BuildServiceProvider());
}

[Theory]
Expand Down Expand Up @@ -117,12 +116,10 @@ await _appServiceService.Received(1).AddDatabaseAsync(
public async Task ExecuteAsync_MissingRequiredParameter_ReturnsErrorResponse(params string[] commandArgs)
{
// Arrange
var command = new DatabaseAddCommand(_logger);
var args = command.GetCommand().Parse(commandArgs);
var context = new CommandContext(_serviceProvider);
var args = _command.GetCommand().Parse(commandArgs);

// Act
var response = await command.ExecuteAsync(context, args, TestContext.Current.CancellationToken);
var response = await _command.ExecuteAsync(_context, args, TestContext.Current.CancellationToken);

// Assert
Assert.NotNull(response);
Expand Down Expand Up @@ -178,11 +175,9 @@ public async Task ExecuteAsync_WithVariousParameters_AcceptsParameters(
parameters.Add("retry-delay", retryDelay.Value);

// Execute the command directly in unit tests rather than via the tool runner helper
var command = new DatabaseAddCommand(_logger);
var argList = parameters.SelectMany(kvp => new[] { $"--{kvp.Key}", kvp.Value?.ToString() ?? string.Empty }).ToArray();
var parseResult = command.GetCommand().Parse(argList);
var context = new CommandContext(_serviceProvider);
var response = await command.ExecuteAsync(context, parseResult, TestContext.Current.CancellationToken);
var parseResult = _command.GetCommand().Parse(argList);
var response = await _command.ExecuteAsync(_context, parseResult, TestContext.Current.CancellationToken);

// Test actual command execution and proper error handling
Assert.NotNull(response);
Expand Down Expand Up @@ -240,19 +235,17 @@ public async Task ExecuteAsync_ServiceThrowsException_ReturnsErrorResponse()
Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("Service error"));

var command = new DatabaseAddCommand(_logger);
var args = command.GetCommand().Parse([
var args = _command.GetCommand().Parse([
"--subscription", subscription,
"--resource-group", resourceGroup,
"--app", appName,
"--database-type", databaseType,
"--database-server", databaseServer,
"--database", databaseName
]);
var context = new CommandContext(_serviceProvider);

// Act
var response = await command.ExecuteAsync(context, args, TestContext.Current.CancellationToken);
var response = await _command.ExecuteAsync(_context, args, TestContext.Current.CancellationToken);
// Assert
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.InternalServerError, response.Status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Azure.Mcp.Tools.AppService.UnitTests.Commands.Webapp.Settings;
public class AppSettingsGetCommandTests
{
private readonly IAppServiceService _appServiceService;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<AppSettingsGetCommand> _logger;
private readonly AppSettingsGetCommand _command;
private readonly CommandContext _context;
Expand All @@ -32,11 +31,8 @@ public AppSettingsGetCommandTests()
_appServiceService = Substitute.For<IAppServiceService>();
_logger = Substitute.For<ILogger<AppSettingsGetCommand>>();

var collection = new ServiceCollection().AddSingleton(_appServiceService);
_serviceProvider = collection.BuildServiceProvider();

_command = new(_logger);
_context = new(_serviceProvider);
_command = new(_logger, _appServiceService);
_context = new(new ServiceCollection().BuildServiceProvider());
_commandDefinition = _command.GetCommand();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Azure.Mcp.Tools.AppService.UnitTests.Commands.Webapp.Settings;
public class AppSettingsUpdateCommandTests
{
private readonly IAppServiceService _appServiceService;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<AppSettingsUpdateCommand> _logger;
private readonly AppSettingsUpdateCommand _command;
private readonly CommandContext _context;
Expand All @@ -32,11 +31,8 @@ public AppSettingsUpdateCommandTests()
_appServiceService = Substitute.For<IAppServiceService>();
_logger = Substitute.For<ILogger<AppSettingsUpdateCommand>>();

var collection = new ServiceCollection().AddSingleton(_appServiceService);
_serviceProvider = collection.BuildServiceProvider();

_command = new(_logger);
_context = new(_serviceProvider);
_command = new(_logger, _appServiceService);
_context = new(new ServiceCollection().BuildServiceProvider());
_commandDefinition = _command.GetCommand();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Azure.Mcp.Tools.AppService.UnitTests.Commands.Webapp;
public class WebappGetCommandTests
{
private readonly IAppServiceService _appServiceService;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<WebappGetCommand> _logger;
private readonly WebappGetCommand _command;
private readonly CommandContext _context;
Expand All @@ -33,11 +32,8 @@ public WebappGetCommandTests()
_appServiceService = Substitute.For<IAppServiceService>();
_logger = Substitute.For<ILogger<WebappGetCommand>>();

var collection = new ServiceCollection().AddSingleton(_appServiceService);
_serviceProvider = collection.BuildServiceProvider();

_command = new(_logger);
_context = new(_serviceProvider);
_command = new(_logger, _appServiceService);
_context = new(new ServiceCollection().BuildServiceProvider());
_commandDefinition = _command.GetCommand();
}

Expand Down
Loading