From 6633ef68ef65dd9587a6456651a6b1f9fccfd58e Mon Sep 17 00:00:00 2001 From: acn-sbuad Date: Mon, 19 Feb 2024 15:04:20 +0100 Subject: [PATCH 1/4] update sms operation based on gatewayref only --- .../Models/Notification/SmsSendOperationResult.cs | 4 ++-- .../Persistence/ISmsNotificationRepository.cs | 2 +- .../Repository/SmsNotificationRepository.cs | 12 +++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Altinn.Notifications.Core/Models/Notification/SmsSendOperationResult.cs b/src/Altinn.Notifications.Core/Models/Notification/SmsSendOperationResult.cs index c8ae2e2a..159b584e 100644 --- a/src/Altinn.Notifications.Core/Models/Notification/SmsSendOperationResult.cs +++ b/src/Altinn.Notifications.Core/Models/Notification/SmsSendOperationResult.cs @@ -12,7 +12,7 @@ public class SmsSendOperationResult /// /// The notification id /// - public Guid NotificationId { get; set; } + public Guid? NotificationId { get; set; } /// /// The reference to the delivery in sms gateway @@ -59,7 +59,7 @@ public static bool TryParse(string input, out SmsSendOperationResult value) parsedOutput = Deserialize(input!); value = parsedOutput!; - return value.NotificationId != Guid.Empty; + return value.NotificationId != Guid.Empty || value.GatewayReference != string.Empty; } catch { diff --git a/src/Altinn.Notifications.Core/Persistence/ISmsNotificationRepository.cs b/src/Altinn.Notifications.Core/Persistence/ISmsNotificationRepository.cs index 81b8f28f..5836f56a 100644 --- a/src/Altinn.Notifications.Core/Persistence/ISmsNotificationRepository.cs +++ b/src/Altinn.Notifications.Core/Persistence/ISmsNotificationRepository.cs @@ -30,5 +30,5 @@ public interface ISmsNotificationRepository /// /// Sets result status of an sms notification and update operation id /// - public Task UpdateSendStatus(Guid notificationId, SmsNotificationResultType result, string? gatewayReference = null); + public Task UpdateSendStatus(Guid? notificationId, SmsNotificationResultType result, string? gatewayReference = null); } diff --git a/src/Altinn.Notifications.Persistence/Repository/SmsNotificationRepository.cs b/src/Altinn.Notifications.Persistence/Repository/SmsNotificationRepository.cs index aa024838..1bcf2da2 100644 --- a/src/Altinn.Notifications.Persistence/Repository/SmsNotificationRepository.cs +++ b/src/Altinn.Notifications.Persistence/Repository/SmsNotificationRepository.cs @@ -25,10 +25,12 @@ public class SmsNotificationRepository : ISmsNotificationRepository private const string _getSmsNotificationsSql = "select * from notifications.getsms_statusnew_updatestatus()"; private const string _getSmsRecipients = "select * from notifications.getsmsrecipients($1)"; // (_orderid) - private const string _updateSmsNotificationStatus = + private const string _updateSmsNotificationStatus = @"UPDATE notifications.smsnotifications - SET result = $1::smsnotificationresulttype, resulttime = now(), gatewayreference = $2 - WHERE alternateid = $3;"; // (_result, _gatewayreference, _alternateid, ) + SET result = $1::smsnotificationresulttype, + resulttime = now(), + gatewayreference = $2 + WHERE alternateid = $3 OR gatewayreference = $2;"; // (_result, _gatewayreference, _alternateid) /// /// Initializes a new instance of the class. @@ -109,13 +111,13 @@ public async Task> GetNewNotifications() } /// - public async Task UpdateSendStatus(Guid notificationId, SmsNotificationResultType result, string? gatewayReference = null) + public async Task UpdateSendStatus(Guid? notificationId, SmsNotificationResultType result, string? gatewayReference = null) { await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_updateSmsNotificationStatus); using TelemetryTracker tracker = new(_telemetryClient, pgcom); pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, result.ToString()); pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, gatewayReference ?? (object)DBNull.Value); - pgcom.Parameters.AddWithValue(NpgsqlDbType.Uuid, notificationId); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Uuid, notificationId ?? (object)DBNull.Value); await pgcom.ExecuteNonQueryAsync(); tracker.Track(); From 840bd08946389c1124a8d7b5875314cdcf65c399 Mon Sep 17 00:00:00 2001 From: acn-sbuad Date: Mon, 19 Feb 2024 15:27:39 +0100 Subject: [PATCH 2/4] added unit test --- .../SmsNotificationRepositoryTests.cs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs index 8b3ff4dd..546a94f4 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs @@ -108,7 +108,7 @@ public async Task GetRecipients() } [Fact] - public async Task UpdateSendStatus_WithGatewayRef() + public async Task UpdateSendStatus_WithNotificationId_WithGatewayRef() { // Arrange (NotificationOrder order, SmsNotification smsNotification) = await PostgreUtil.PopulateDBWithOrderAndSmsNotification(); @@ -136,7 +136,41 @@ FROM notifications.smsnotifications sms } [Fact] - public async Task UpdateSendStatus_WithoutGatewayRef() + public async Task UpdateSendStatus_WithoutNotificationId_WithGatewayRef() + { + // Arrange + (NotificationOrder order, SmsNotification smsNotification) = await PostgreUtil.PopulateDBWithOrderAndSmsNotification(); + _orderIdsToDelete.Add(order.Id); + + SmsNotificationRepository repo = (SmsNotificationRepository)ServiceUtil + .GetServices(new List() { typeof(ISmsNotificationRepository) }) + .First(i => i.GetType() == typeof(SmsNotificationRepository)); + + string gatewayReference = Guid.NewGuid().ToString(); + + string setGateqwaySql = $@"Update notifications.smsnotifications + SET gatewayreference = '{gatewayReference}' + WHERE alternateid = '{smsNotification.Id}'"; + + await PostgreUtil.RunSql(setGateqwaySql); + + // Act + await repo.UpdateSendStatus(null, SmsNotificationResultType.Accepted, gatewayReference); + + // Assert + string sql = $@"SELECT count(1) + FROM notifications.smsnotifications sms + WHERE sms.alternateid = '{smsNotification.Id}' + AND sms.result = '{SmsNotificationResultType.Accepted}' + AND sms.gatewayreference = '{gatewayReference}'"; + + int actualCount = await PostgreUtil.RunSqlReturnOutput(sql); + + Assert.Equal(1, actualCount); + } + + [Fact] + public async Task UpdateSendStatus_WithNotificationId_WithoutGatewayRef() { // Arrange (NotificationOrder order, SmsNotification smsNotification) = await PostgreUtil.PopulateDBWithOrderAndSmsNotification(); From 6ef8587b356936b8aa3efc4631b71e5b3aec2231 Mon Sep 17 00:00:00 2001 From: acn-sbuad Date: Mon, 19 Feb 2024 16:19:23 +0100 Subject: [PATCH 3/4] added unit tests --- .../SmsSendOperationResultTests.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs new file mode 100644 index 00000000..d6fd5e44 --- /dev/null +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs @@ -0,0 +1,35 @@ +using Altinn.Notifications.Core.Models.Notification; + +using Xunit; + +namespace Altinn.Notifications.Tests.Notifications.Core.TestingModels +{ + public class SmsSendOperationResultTests + { + [Fact] + public void TryParse_ValidInput_ReturnsTrue() + { + // Arrange + string input = "{\"notificationId\":\"d3b3f3e3-3e3b-3b3b-3b3b-3b3b3b3b3b3b\",\"gatewayReference\":\"123456789\",\"sendResult\":1}"; + + // Act + bool result = SmsSendOperationResult.TryParse(input, out SmsSendOperationResult value); + + // Assert + Assert.True(result); + } + + [Fact] + public void TryParse_ValidInput_NoNotificationId_ReturnsTrue() + { + // Arrange + string input = "{\"gatewayReference\":\"123456789\",\"sendResult\":1}"; + + // Act + bool result = SmsSendOperationResult.TryParse(input, out SmsSendOperationResult value); + + // Assert + Assert.True(result); + } + } +} From 02311cfbff35488e89fe2ac3d04dda07df9a858b Mon Sep 17 00:00:00 2001 From: acn-sbuad Date: Mon, 19 Feb 2024 16:20:06 +0100 Subject: [PATCH 4/4] simplified test --- .../TestingModels/SmsSendOperationResultTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs index d6fd5e44..ab97efb1 100644 --- a/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingModels/SmsSendOperationResultTests.cs @@ -13,7 +13,7 @@ public void TryParse_ValidInput_ReturnsTrue() string input = "{\"notificationId\":\"d3b3f3e3-3e3b-3b3b-3b3b-3b3b3b3b3b3b\",\"gatewayReference\":\"123456789\",\"sendResult\":1}"; // Act - bool result = SmsSendOperationResult.TryParse(input, out SmsSendOperationResult value); + bool result = SmsSendOperationResult.TryParse(input, out _); // Assert Assert.True(result); @@ -26,7 +26,7 @@ public void TryParse_ValidInput_NoNotificationId_ReturnsTrue() string input = "{\"gatewayReference\":\"123456789\",\"sendResult\":1}"; // Act - bool result = SmsSendOperationResult.TryParse(input, out SmsSendOperationResult value); + bool result = SmsSendOperationResult.TryParse(input, out _); // Assert Assert.True(result);