Skip to content

Commit

Permalink
refactored summary service and added tests #GCPActive
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-sbuad committed Nov 13, 2023
1 parent ef75ef6 commit e8340d0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ namespace Altinn.Notifications.Core.Services
public class NotificationSummaryService : INotificationSummaryService
{
private readonly INotificationSummaryRepository _summaryRepository;
private readonly Dictionary<EmailNotificationResultType, string> _emailResultDescriptions;
private static Dictionary<EmailNotificationResultType, string> _emailResultDescriptions = new()
{
{ EmailNotificationResultType.New, "The email has been created, but has not been picked up for processing yet." },
{ EmailNotificationResultType.Sending, "The email is being processed and will be attempted sent shortly." },
{ EmailNotificationResultType.Succeeded, "The email has been accepted by the third party email service and will be sent shortly." },
{ EmailNotificationResultType.Delivered, "The email was delivered to the recipient. No errors reported, making it likely it was received by the recipient." },
{ EmailNotificationResultType.Failed_RecipientNotIdentified, "Email was not sent because the recipient's email address was not found." }
};

private readonly List<EmailNotificationResultType> _successResults = new()
private static List<EmailNotificationResultType> _successResults = new()
{
EmailNotificationResultType.Succeeded,
EmailNotificationResultType.Delivered
Expand All @@ -26,15 +33,6 @@ public class NotificationSummaryService : INotificationSummaryService
public NotificationSummaryService(INotificationSummaryRepository summaryRepository)
{
_summaryRepository = summaryRepository;

_emailResultDescriptions = new()
{
{ EmailNotificationResultType.New, "The email has been created, but has not been picked up for processing yet." },
{ EmailNotificationResultType.Sending, "The email is being processed and will be attempted sent shortly." },
{ EmailNotificationResultType.Succeeded, "The email has been accepted by the third party email service and will be sent shortly." },
{ EmailNotificationResultType.Delivered, "The email was delivered to the recipient. No errors reported, making it likely it was received by the recipient." },
{ EmailNotificationResultType.Failed_RecipientNotIdentified, "Email was not sent because the recipient's email address was not found." }
};
}

/// <inheritdoc/>
Expand Down Expand Up @@ -62,14 +60,30 @@ private void ProcessNotificationResults(EmailNotificationSummary summary)
foreach (EmailNotificationWithResult notification in summary.Notifications)
{
NotificationResult<EmailNotificationResultType> resultStatus = notification.ResultStatus;
if (_successResults.Contains(resultStatus.Result))
if (IsSuccessResult(resultStatus.Result))
{
notification.Succeeded = true;
++summary.Succeeded;
}

resultStatus.SetResultDescription(_emailResultDescriptions[resultStatus.Result]);
resultStatus.SetResultDescription(GetResultDescription(resultStatus.Result));
}
}

/// <summary>
/// Checks if the <see cref="EmailNotificationResultType"/> is a success result
/// </summary>
internal static bool IsSuccessResult(EmailNotificationResultType result)
{
return _successResults.Contains(result);
}

/// <summary>
/// Gets the English description of the <see cref="EmailNotificationResultType"/>"
/// </summary>
internal static string GetResultDescription(EmailNotificationResultType result)
{
return _emailResultDescriptions[result];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ public async Task Get_InvalidScopeInToken_Forbidden()
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
}

[Fact]
public async Task Get_UserClaimsPrincipal_Forbidden()
{
// Arrange
HttpClient client = GetTestClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", PrincipalUtil.GetUserToken(1337));

HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, _basePath);

// Act
HttpResponseMessage response = await client.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
}

[Fact]
public async Task Get_InvalidGuid_BadRequest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public async Task Get_ValidOrderId_Ok()
Assert.True(summary?.Notifications.Count > 0);
Assert.Equal(_orderId, summary?.OrderId);
Assert.Equal(_notificationId, summary?.Notifications[0].Id);
Assert.Equal(1, summary?.Generated);
Assert.Equal(0, summary?.Succeeded);
}

private HttpClient GetTestClient()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;

using Altinn.Notifications.Core.Enums;
using Altinn.Notifications.Core.Services;

using Xunit;

namespace Altinn.Notifications.Tests.Notifications.Core.TestingServices
{
public class NotificationSummaryServicTests
{
[Theory]
[InlineData(EmailNotificationResultType.New, false)]
[InlineData(EmailNotificationResultType.Sending, false)]
[InlineData(EmailNotificationResultType.Succeeded, true)]
[InlineData(EmailNotificationResultType.Delivered, true)]
[InlineData(EmailNotificationResultType.Failed_RecipientNotIdentified, false)]
public void IsSuccessResult_CheckResultForAllEnums(EmailNotificationResultType result, bool expectedIsSuccess)
{
bool actualIsSuccess = NotificationSummaryService.IsSuccessResult(result);
Assert.Equal(expectedIsSuccess, actualIsSuccess);
}

[Theory]
[InlineData(EmailNotificationResultType.New, "The email has been created, but has not been picked up for processing yet.")]
[InlineData(EmailNotificationResultType.Sending, "The email is being processed and will be attempted sent shortly.")]
[InlineData(EmailNotificationResultType.Succeeded, "The email has been accepted by the third party email service and will be sent shortly.")]
[InlineData(EmailNotificationResultType.Delivered, "The email was delivered to the recipient. No errors reported, making it likely it was received by the recipient.")]
[InlineData(EmailNotificationResultType.Failed_RecipientNotIdentified, "Email was not sent because the recipient's email address was not found.")]
public void GetResultDescription_ExpectedDescription(EmailNotificationResultType result, string expected)
{
string actual = NotificationSummaryService.GetResultDescription(result);
Assert.Equal(expected, actual);
}

[Fact]
public void GetResultDescription_AllResultTypesHaveDescriptions()
{
foreach (EmailNotificationResultType resultType in Enum.GetValues(typeof(EmailNotificationResultType)))
{
string resultDescrption = NotificationSummaryService.GetResultDescription(resultType);
Assert.NotEmpty(resultDescrption);
}
}
}
}

0 comments on commit e8340d0

Please sign in to comment.