Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions SAMA.Data/SAMA.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions SAMA.Shared/SAMA.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ public override async Task InitializeTestAsync()
_mockScheduler = Substitute.For<CheckSchedulerService>(null, null, null);
_mockEventService = Substitute.For<EventSubscriptionService>(null, null, null);

var alertChangeDetectionService = new AlertChangeDetectionService();

_service = new AlertCommandService(
DbContext,
_mockScheduler,
_mockEventService,
alertChangeDetectionService,
new AlertChangeDetectionService(),
new DashboardCacheService(ServiceProvider),
Substitute.For<ILogger<AlertCommandService>>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ public override async Task InitializeTestAsync()
_mockScheduler = Substitute.For<CheckSchedulerService>(null, null, null);
_mockEventService = Substitute.For<EventSubscriptionService>(null, null, null);

var changeDetectionService = new CheckChangeDetectionService();

_service = new CheckCommandService(
DbContext,
_mockScheduler,
_mockEventService,
changeDetectionService,
new CheckChangeDetectionService(),
new DashboardCacheService(ServiceProvider),
Substitute.For<ILogger<CheckCommandService>>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using NSubstitute;
using SAMA.Data.Entities;
using SAMA.Web.Services;
using SAMA.Web.Services.Commands;

namespace SAMA.Tests.Integration.Web.Services.Commands;
Expand All @@ -16,7 +17,7 @@ public override async Task InitializeTestAsync()
{
await base.InitializeTestAsync();

_service = new WorkspaceCommandService(DbContext, Substitute.For<ILogger<WorkspaceCommandService>>());
_service = new WorkspaceCommandService(DbContext, new DashboardCacheService(ServiceProvider), Substitute.For<ILogger<WorkspaceCommandService>>());
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Alerts/CreateModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Setup()
{
_mockCheckQuery = Substitute.For<CheckQueryService>(null!, null!, null!, null!);
_mockChannelQuery = Substitute.For<ChannelQueryService>(null!, null!);
_mockAlertCommand = Substitute.For<AlertCommandService>(null!, null!, null!, null!, null!);
_mockAlertCommand = Substitute.For<AlertCommandService>(null!, null!, null!, null!, null!, null!);
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);

_pageModel = new CreateModel(_mockWorkspaceQuery, _mockChannelQuery, _mockCheckQuery, _mockAlertCommand);
Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Alerts/DeleteModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DeleteModelTests
public void Setup()
{
_mockAlertQuery = Substitute.For<AlertQueryService>((SamaDbContext)null!);
_mockAlertCommand = Substitute.For<AlertCommandService>(null!, null!, null!, null!, null!);
_mockAlertCommand = Substitute.For<AlertCommandService>(null!, null!, null!, null!, null!, null!);
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);

_pageModel = new DeleteModel(_mockWorkspaceQuery, _mockAlertQuery, _mockAlertCommand);
Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Alerts/EditModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Setup()
_mockCheckQuery = Substitute.For<CheckQueryService>(null!, null!, null!, null!);
_mockChannelQuery = Substitute.For<ChannelQueryService>(null!, null!);
_mockAlertQuery = Substitute.For<AlertQueryService>((SamaDbContext)null!);
_mockAlertCommand = Substitute.For<AlertCommandService>(null!, null!, null!, null!, null!);
_mockAlertCommand = Substitute.For<AlertCommandService>(null!, null!, null!, null!, null!, null!);
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);

_pageModel = new EditModel(_mockWorkspaceQuery, _mockChannelQuery, _mockCheckQuery, _mockAlertQuery, _mockAlertCommand);
Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Checks/CreateModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Setup()
{
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);
_mockCheckConfigService = Substitute.For<CheckConfigurationService>();
_mockCheckCommand = Substitute.For<CheckCommandService>(null!, null!, null!, null!, null!);
_mockCheckCommand = Substitute.For<CheckCommandService>(null!, null!, null!, null!, null!, null!);
_mockGlobalSettings = Substitute.For<GlobalSettingsService>(null!, null!, null!, null!);

_pageModel = new CreateModel(_mockWorkspaceQuery, _mockCheckConfigService, _mockCheckCommand, _mockGlobalSettings);
Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Checks/DeleteModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DeleteModelTests
public void Setup()
{
_mockCheckQuery = Substitute.For<CheckQueryService>(null!, null!, null!, null!);
_mockCheckCommand = Substitute.For<CheckCommandService>(null!, null!, null!, null!, null!);
_mockCheckCommand = Substitute.For<CheckCommandService>(null!, null!, null!, null!, null!, null!);
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);

_pageModel = new DeleteModel(_mockWorkspaceQuery, _mockCheckQuery, _mockCheckCommand);
Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Checks/EditModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Setup()
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);
_mockConfigService = Substitute.For<CheckConfigurationService>();
_mockCheckQuery = Substitute.For<CheckQueryService>(null!, null!, null!, null!);
_mockCheckCommand = Substitute.For<CheckCommandService>(null!, null!, null!, null!, null!);
_mockCheckCommand = Substitute.For<CheckCommandService>(null!, null!, null!, null!, null!, null!);

_pageModel = new EditModel(_mockWorkspaceQuery, _mockCheckQuery, _mockConfigService, _mockCheckCommand);
PageModelTestHelpers.ConfigurePageModel(_pageModel);
Expand Down
130 changes: 24 additions & 106 deletions SAMA.Tests.Unit/Web/Pages/Dashboard/IndexModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using SAMA.Data;
using SAMA.Data.Entities;
using SAMA.Tests.Unit.TestUtilities;
using SAMA.Web.Models;
using SAMA.Web.Pages.Dashboard;
using SAMA.Web.Services;
using SAMA.Web.Services.Queries;
using static SAMA.Web.Services.DashboardCacheService;

namespace SAMA.Tests.Unit.Web.Pages.Dashboard;

[TestClass]
public class IndexModelTests
{
private WorkspaceQueryService _mockWorkspaceQuery = null!;
private CheckQueryService _mockCheckQuery = null!;
private AlertQueryService _mockAlertQuery = null!;
private GlobalSettingsService _mockGlobalSettings = null!;
private MarkdownService _markdownService = null!;
private DashboardCacheService _cacheService = null!;
private IndexModel _pageModel = null!;

[TestInitialize]
public void Setup()
{
_mockWorkspaceQuery = Substitute.For<WorkspaceQueryService>(null!, null!);
_mockCheckQuery = Substitute.For<CheckQueryService>(null!, null!, null!, null!);
_mockAlertQuery = Substitute.For<AlertQueryService>((SamaDbContext)null!);
_mockGlobalSettings = Substitute.For<GlobalSettingsService>(null, null, null, null);
_markdownService = new MarkdownService();
_cacheService = new DashboardCacheService(new ServiceCollection().BuildServiceProvider());

_pageModel = new IndexModel(_mockWorkspaceQuery, _mockCheckQuery, _mockAlertQuery, _mockGlobalSettings, _markdownService);
_pageModel = new IndexModel(_mockWorkspaceQuery, _mockGlobalSettings, _markdownService, _cacheService);
PageModelTestHelpers.ConfigurePageModel(_pageModel);

_mockWorkspaceQuery.GetWorkspaceDetailsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(new WorkspaceDetailsViewModel());
}

private void PrePopulateCache(Guid workspaceId, List<CheckListItemViewModel>? checks = null, List<RecentAlertViewModel>? alerts = null)
{
_cacheService.SetWorkspaceData(workspaceId, new WorkspaceDashboardData(
checks ?? [],
alerts ?? []
));
_cacheService.SetTimeline(workspaceId, 24, new WorkspaceIncidentTimelineViewModel());
_cacheService.SetTrends(workspaceId, 24, new WorkspaceResponseTimeTrendsViewModel());
}

[TestMethod]
Expand All @@ -50,16 +62,9 @@ public async Task OnGetAsyncShouldReturnPageWhenWorkspaceExists()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "Test Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);
PrePopulateCache(workspaceId);

var result = await _pageModel.OnGetAsync(workspaceId, null, null);

Expand All @@ -71,16 +76,9 @@ public async Task OnGetAsyncShouldPopulateRefreshIntervalFromGlobalSettings()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "Test Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(10);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);
PrePopulateCache(workspaceId);

await _pageModel.OnGetAsync(workspaceId, null, null);

Expand Down Expand Up @@ -121,15 +119,9 @@ public async Task OnGetAsyncShouldPopulateChecksList()
AlertCount = 0
}
};
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);
PrePopulateCache(workspaceId, checks: checks);

await _pageModel.OnGetAsync(workspaceId, null, null);

Expand All @@ -143,7 +135,6 @@ public async Task OnGetAsyncShouldPopulateRecentAlertsList()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "Test Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>
{
new()
Expand All @@ -165,14 +156,9 @@ public async Task OnGetAsyncShouldPopulateRecentAlertsList()
SentAt = DateTimeOffset.UtcNow.AddMinutes(-10)
}
};

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);
PrePopulateCache(workspaceId, alerts: alerts);

await _pageModel.OnGetAsync(workspaceId, null, null);

Expand All @@ -186,92 +172,24 @@ public async Task OnGetAsyncShouldPopulateEmptyListsWhenNoData()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "Test Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);
PrePopulateCache(workspaceId);

await _pageModel.OnGetAsync(workspaceId, null, null);

Assert.IsEmpty(_pageModel.Checks);
Assert.IsEmpty(_pageModel.RecentAlerts);
}

[TestMethod]
public async Task OnGetAsyncShouldCallGetChecksForWorkspaceAsyncWithCorrectId()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "Test Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);

await _pageModel.OnGetAsync(workspaceId, null, null);

await _mockCheckQuery.Received(1).GetChecksForWorkspaceAsync(workspaceId);
}

[TestMethod]
public async Task OnGetAsyncShouldCallGetRecentAlertsForWorkspaceAsyncWithCorrectParameters()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "Test Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, 50).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(50);

await _pageModel.OnGetAsync(workspaceId, null, null);

await _mockAlertQuery.Received(1).GetRecentAlertsForWorkspaceAsync(workspaceId, 50);
}

[TestMethod]
public async Task OnGetAsyncShouldNotCallQueryServicesWhenWorkspaceDoesNotExist()
{
var workspaceId = Guid.NewGuid();
_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(null));

await _pageModel.OnGetAsync(workspaceId, null, null);

await _mockCheckQuery.DidNotReceive().GetChecksForWorkspaceAsync(Arg.Any<Guid>());
await _mockAlertQuery.DidNotReceive().GetRecentAlertsForWorkspaceAsync(Arg.Any<Guid>(), Arg.Any<int>());
}

[TestMethod]
public async Task OnGetAsyncShouldLoadWorkspaceContextWithCorrectParameters()
{
var workspaceId = Guid.NewGuid();
var workspace = new Workspace { Id = workspaceId, Name = "My Workspace" };
var checks = new List<CheckListItemViewModel>();
var alerts = new List<RecentAlertViewModel>();

_mockWorkspaceQuery.GetWorkspaceByIdAsync(workspaceId).Returns(Task.FromResult<Workspace?>(workspace));
_mockCheckQuery.GetChecksForWorkspaceAsync(workspaceId).Returns(Task.FromResult(checks));
_mockAlertQuery.GetRecentAlertsForWorkspaceAsync(workspaceId, Arg.Any<int>()).Returns(Task.FromResult(alerts));
_mockCheckQuery.GetWorkspaceIncidentTimelineAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceIncidentTimelineViewModel()));
_mockCheckQuery.GetWorkspaceResponseTimeTrendsAsync(workspaceId, Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(new WorkspaceResponseTimeTrendsViewModel()));
_mockGlobalSettings.DashboardRefreshIntervalSeconds.Returns(5);
_mockGlobalSettings.MaxRecentAlerts.Returns(20);
PrePopulateCache(workspaceId);

await _pageModel.OnGetAsync(workspaceId, null, null);

Expand Down
2 changes: 1 addition & 1 deletion SAMA.Tests.Unit/Web/Pages/Workspaces/CreateModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CreateModelTests
[TestInitialize]
public void Setup()
{
_mockWorkspaceCommand = Substitute.For<WorkspaceCommandService>(null!, null!);
_mockWorkspaceCommand = Substitute.For<WorkspaceCommandService>(null!, null!, null!);
_mockMarkdownService = Substitute.For<MarkdownService>();

_pageModel = new CreateModel(_mockWorkspaceCommand, _mockMarkdownService);
Expand Down
Loading
Loading