Skip to content

Commit

Permalink
chore: Add notification condition tests (#1385)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced integration tests for notification conditions in the
Dialogporten application, ensuring accurate notification sending based
on various conditions.
  
- **Bug Fixes**
- Added a configuration setting to suppress specific IDE warnings,
enhancing the development experience without impacting functionality.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
oskogstad authored Nov 4, 2024
1 parent 2ca4e94 commit 4be1e53
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
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 };


[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;
}

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

// 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();
}
}

0 comments on commit 4be1e53

Please sign in to comment.