From e8340d0eb81a33b80d7e68945bcec279b3480d81 Mon Sep 17 00:00:00 2001 From: acn-sbuad Date: Mon, 13 Nov 2023 14:29:27 +0100 Subject: [PATCH] refactored summary service and added tests #GCPActive --- .../Services/NotificationSummaryService.cs | 40 ++++++++++------ .../EmailNotificationsControllerTests.cs | 16 ------- .../EmailNotificationsController/GetTests.cs | 2 + .../NotificationSummaryServicTests.cs | 46 +++++++++++++++++++ 4 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/NotificationSummaryServicTests.cs diff --git a/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs b/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs index f82bd3fd..43154ce1 100644 --- a/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs +++ b/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs @@ -12,9 +12,16 @@ namespace Altinn.Notifications.Core.Services public class NotificationSummaryService : INotificationSummaryService { private readonly INotificationSummaryRepository _summaryRepository; - private readonly Dictionary _emailResultDescriptions; + private static Dictionary _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 _successResults = new() + private static List _successResults = new() { EmailNotificationResultType.Succeeded, EmailNotificationResultType.Delivered @@ -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." } - }; } /// @@ -62,14 +60,30 @@ private void ProcessNotificationResults(EmailNotificationSummary summary) foreach (EmailNotificationWithResult notification in summary.Notifications) { NotificationResult 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)); } } + + /// + /// Checks if the is a success result + /// + internal static bool IsSuccessResult(EmailNotificationResultType result) + { + return _successResults.Contains(result); + } + + /// + /// Gets the English description of the " + /// + internal static string GetResultDescription(EmailNotificationResultType result) + { + return _emailResultDescriptions[result]; + } } } diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/EmailNotificationsControllerTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/EmailNotificationsControllerTests.cs index b5fa7c3f..26a9dc32 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/EmailNotificationsControllerTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/EmailNotificationsControllerTests.cs @@ -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() { diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/GetTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/GetTests.cs index 0e74f49b..f942c09d 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/GetTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications/EmailNotificationsController/GetTests.cs @@ -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() diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/NotificationSummaryServicTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/NotificationSummaryServicTests.cs new file mode 100644 index 00000000..b798f4f5 --- /dev/null +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/NotificationSummaryServicTests.cs @@ -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); + } + } + } +}