Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add notification condition tests #1385

Merged
merged 8 commits into from
Nov 4, 2024
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
5 changes: 4 additions & 1 deletion tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[*]
# CA1707: Identifiers should not contain underscores
dotnet_diagnostic.CA1707.severity = none
dotnet_diagnostic.CA1707.severity = none

# IDE0010: Add missing cases to switch statements
dotnet_diagnostic.IDE0010.severity = none
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.DialogActivities.Queries.NotificationCondition;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create;
using Digdir.Domain.Dialogporten.Application.Integration.Tests.Common;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Activities;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Transmissions;
using Digdir.Tool.Dialogporten.GenerateFakeData;
using FluentAssertions;

namespace Digdir.Domain.Dialogporten.Application.Integration.Tests.Features.V1.ServiceOwner.NotificationCondition;

[Collection(nameof(DialogCqrsCollectionFixture))]
public class NotificationConditionTests(DialogApplication application) : ApplicationCollectionFixture(application)
{
private static readonly bool[] ExpectedSendNotificationsValues = [true, false];

public static IEnumerable<object[]> NotificationConditionTestData() =>
from bool expectedSendNotificationValue in ExpectedSendNotificationsValues
from DialogActivityType.Values activityType in Enum.GetValues(typeof(DialogActivityType.Values))
from NotificationConditionType conditionType in Enum.GetValues(typeof(NotificationConditionType))
select new object[] { activityType, conditionType, expectedSendNotificationValue };
oskogstad marked this conversation as resolved.
Show resolved Hide resolved


[Theory, MemberData(nameof(NotificationConditionTestData))]
public async Task SendNotification_Should_Be_True_When_Conditions_Are_Met(
DialogActivityType.Values activityType,
NotificationConditionType conditionType,
bool expectedSendNotificationValue)
{
// Arrange
var createDialogCommand = DialogGenerator.GenerateSimpleFakeDialog();
switch (conditionType)
{
case NotificationConditionType.Exists when expectedSendNotificationValue:
case NotificationConditionType.NotExists when !expectedSendNotificationValue:
AddActivityRequirements(createDialogCommand, activityType);
break;
}

var response = await Application.Send(createDialogCommand);
response.TryPickT0(out var dialogId, out _);

var notificationConditionQuery = new NotificationConditionQuery
{
DialogId = dialogId.Value,
ActivityType = activityType,
ConditionType = conditionType,
};

if (activityType is DialogActivityType.Values.TransmissionOpened)
{
var transmissionId = createDialogCommand.Transmissions.FirstOrDefault()?.Id ?? Guid.NewGuid();
notificationConditionQuery.TransmissionId = transmissionId;
}
oskogstad marked this conversation as resolved.
Show resolved Hide resolved

// Act
var queryResult = await Application.Send(notificationConditionQuery);
oskogstad marked this conversation as resolved.
Show resolved Hide resolved

// Assert
queryResult.TryPickT0(out var notificationConditionResult, out _);
queryResult.IsT0.Should().BeTrue();
notificationConditionResult.SendNotification.Should().Be(expectedSendNotificationValue);
}

private static void AddActivityRequirements(
CreateDialogCommand createDialogCommand,
DialogActivityType.Values activityType)
{
var activity = DialogGenerator.GenerateFakeDialogActivity(type: activityType);
createDialogCommand.Activities.Add(activity);

if (activityType is not DialogActivityType.Values.TransmissionOpened) return;

var transmission = DialogGenerator.GenerateFakeDialogTransmissions(type: DialogTransmissionType.Values.Information)[0];
createDialogCommand.Transmissions.Add(transmission);
createDialogCommand.Activities[0].TransmissionId = createDialogCommand.Transmissions[0].Id;
}

[Fact]
public async Task NotFound_Should_Be_Returned_When_Dialog_Does_Not_Exist()
{
// Arrange
var notificationConditionQuery = new NotificationConditionQuery
{
DialogId = Guid.NewGuid(),
ActivityType = DialogActivityType.Values.Information,
ConditionType = NotificationConditionType.Exists,
};

// Act
var queryResult = await Application.Send(notificationConditionQuery);

// Assert
queryResult.TryPickT2(out var notFound, out _);
queryResult.IsT2.Should().BeTrue();
notFound.Should().NotBeNull();
}
}