diff --git a/src/Altinn.Notifications.Integrations/Profile/Mappers/UserContactPointsDtoMapperExtension.cs b/src/Altinn.Notifications.Integrations/Profile/Mappers/UserContactPointsDtoMapperExtension.cs new file mode 100644 index 00000000..fe1cd3cc --- /dev/null +++ b/src/Altinn.Notifications.Integrations/Profile/Mappers/UserContactPointsDtoMapperExtension.cs @@ -0,0 +1,27 @@ +using Altinn.Notifications.Core.Models.ContactPoints; +using Altinn.Notifications.Integrations.Profile.Models; + +namespace Altinn.Notifications.Integrations.Profile.Mappers; + +/// +/// Extension class to map user contact points from DTO to domain model +/// +public static class UserContactPointsDtoMapperExtension +{ + /// + /// Maps the DTO model to domain model + /// + /// This DTO object + /// The UserContactPoints object mapped from this DTO object + public static UserContactPoints ToUserContactPoint(this UserContactPointsDto userContactPointDto) + { + return new UserContactPoints + { + UserId = userContactPointDto.UserId ?? 0, + NationalIdentityNumber = userContactPointDto.NationalIdentityNumber ?? string.Empty, + IsReserved = userContactPointDto.IsReserved, + MobileNumber = userContactPointDto.MobileNumber ?? string.Empty, + Email = userContactPointDto.Email ?? string.Empty + }; + } +} diff --git a/src/Altinn.Notifications.Integrations/Profile/Models/UserContactPointsDto.cs b/src/Altinn.Notifications.Integrations/Profile/Models/UserContactPointsDto.cs new file mode 100644 index 00000000..8b389c3c --- /dev/null +++ b/src/Altinn.Notifications.Integrations/Profile/Models/UserContactPointsDto.cs @@ -0,0 +1,32 @@ +namespace Altinn.Notifications.Integrations.Profile.Models; + +/// +/// DTO for user contact points received from the ProfileClient, describing the availability of contact points for a user +/// +public class UserContactPointsDto +{ + /// + /// Gets or sets the ID of the user + /// + public int? UserId { get; set; } + + /// + /// Gets or sets the national identityt number of the user + /// + public string? NationalIdentityNumber { get; set; } + + /// + /// Gets or sets a boolean indicating whether the user has reserved themselves from electronic communication + /// + public bool IsReserved { get; set; } + + /// + /// Gets or sets the mobile number + /// + public string? MobileNumber { get; set; } + + /// + /// Gets or sets the email address + /// + public string? Email { get; set; } +} diff --git a/src/Altinn.Notifications.Integrations/Profile/ProfileClient.cs b/src/Altinn.Notifications.Integrations/Profile/ProfileClient.cs index b5bcdf4b..85a6e275 100644 --- a/src/Altinn.Notifications.Integrations/Profile/ProfileClient.cs +++ b/src/Altinn.Notifications.Integrations/Profile/ProfileClient.cs @@ -7,6 +7,8 @@ using Altinn.Notifications.Core.Shared; using Altinn.Notifications.Integrations.Configuration; using Altinn.Notifications.Integrations.Profile; +using Altinn.Notifications.Integrations.Profile.Mappers; +using Altinn.Notifications.Integrations.Profile.Models; using Altinn.Notifications.Integrations.Register; using Microsoft.Extensions.Options; @@ -47,8 +49,9 @@ public async Task> GetUserContactPoints(List nat } string responseContent = await response.Content.ReadAsStringAsync(); - List contactPoints = JsonSerializer.Deserialize(responseContent, JsonSerializerOptionsProvider.Options)!.ContactPointsList; - return contactPoints!; + List contactPoints = JsonSerializer.Deserialize(responseContent, JsonSerializerOptionsProvider.Options)!.ContactPointsList; + + return contactPoints.Select(contactPointDto => contactPointDto.ToUserContactPoint()).ToList(); } /// diff --git a/src/Altinn.Notifications.Integrations/Profile/UserContactPointsList.cs b/src/Altinn.Notifications.Integrations/Profile/UserContactPointsList.cs index 1467d5bd..0b8afb36 100644 --- a/src/Altinn.Notifications.Integrations/Profile/UserContactPointsList.cs +++ b/src/Altinn.Notifications.Integrations/Profile/UserContactPointsList.cs @@ -1,4 +1,5 @@ using Altinn.Notifications.Core.Models.ContactPoints; +using Altinn.Notifications.Integrations.Profile.Models; namespace Altinn.Notifications.Integrations.Profile; @@ -10,5 +11,5 @@ public class UserContactPointsList /// /// A list containing contact points for users /// - public List ContactPointsList { get; set; } = []; + public List ContactPointsList { get; set; } = []; } diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/EmailNotificationRepositoryTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/EmailNotificationRepositoryTests.cs index b148c8e2..849d2cf4 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/EmailNotificationRepositoryTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/EmailNotificationRepositoryTests.cs @@ -84,7 +84,7 @@ public async Task GetNewNotifications() List emailToBeSent = await repo.GetNewNotifications(); // Assert - Assert.NotEmpty(emailToBeSent.Where(s => s.NotificationId == emailNotification.Id)); + Assert.Contains(emailToBeSent, s => s.NotificationId == emailNotification.Id); } [Fact] diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs index e580ecda..a975c656 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/SmsNotificationRepositoryTests.cs @@ -83,7 +83,7 @@ public async Task GetNewNotifications() List smsToBeSent = await repo.GetNewNotifications(); // Assert - Assert.NotEmpty(smsToBeSent.Where(s => s.NotificationId == smsNotification.Id)); + Assert.Contains(smsToBeSent, s => s.NotificationId == smsNotification.Id); } [Fact] diff --git a/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/Mappers/UserContactPointsDtoMapperExtensionTests.cs b/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/Mappers/UserContactPointsDtoMapperExtensionTests.cs new file mode 100644 index 00000000..9c1242f9 --- /dev/null +++ b/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/Mappers/UserContactPointsDtoMapperExtensionTests.cs @@ -0,0 +1,64 @@ +using Altinn.Notifications.Integrations.Profile.Mappers; +using Altinn.Notifications.Integrations.Profile.Models; +using Xunit; + +namespace Altinn.Notifications.Tests.Notifications.Integrations.Profile.Mappers; + +public class UserContactPointsDtoMapperExtensionTests +{ + [Fact] + public void ToUserContactPoint_NullValues_MapsCorrectly() + { + // Arrange + var userContactPointsDTO = new UserContactPointsDto + { + UserId = null, + NationalIdentityNumber = null, + IsReserved = false, + Email = null, + MobileNumber = null + }; + + // Act + var mappedResult = userContactPointsDTO.ToUserContactPoint(); + + // Assert + Assert.Equal(0, mappedResult.UserId); + Assert.Equal(string.Empty, mappedResult.NationalIdentityNumber); + Assert.False(mappedResult.IsReserved); + Assert.Equal(string.Empty, mappedResult.Email); + Assert.Equal(string.Empty, mappedResult.MobileNumber); + } + + [Fact] + public void ToUserContactPoint_WithValues_MapsCorrectly() + { + // Arrange + var testData = new + { + userId = 123, + nationalIdentityNumber = "12345678910", + email = "test@machinery.no", + isReserved = false, + mobileNumber = "+4712345678" + }; + var userContactPointsDTO = new UserContactPointsDto + { + UserId = testData.userId, + NationalIdentityNumber = testData.nationalIdentityNumber, + Email = testData.email, + IsReserved = testData.isReserved, + MobileNumber = testData.mobileNumber + }; + + // Act + var mappedResult = userContactPointsDTO.ToUserContactPoint(); + + // Assert + Assert.Equal(testData.userId, mappedResult.UserId); + Assert.Equal(testData.nationalIdentityNumber, mappedResult.NationalIdentityNumber); + Assert.Equal(testData.email, mappedResult.Email); + Assert.Equal(testData.mobileNumber, mappedResult.MobileNumber); + Assert.Equal(testData.isReserved, mappedResult.IsReserved); + } +} diff --git a/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/ProfileClientTests.cs b/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/ProfileClientTests.cs index 3141c832..6e777a3a 100644 --- a/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/ProfileClientTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications.Integrations/Profile/ProfileClientTests.cs @@ -12,6 +12,7 @@ using Altinn.Notifications.Integrations.Clients; using Altinn.Notifications.Integrations.Configuration; using Altinn.Notifications.Integrations.Profile; +using Altinn.Notifications.Integrations.Profile.Models; using Altinn.Notifications.Integrations.Register; using Microsoft.Extensions.Options; @@ -128,15 +129,15 @@ private Task GetUserProfileResponse(UserContactPointLookup switch (lookup.NationalIdentityNumbers[0]) { case "empty-list": - contentData = new UserContactPointsList() { ContactPointsList = new List() }; + contentData = new UserContactPointsList() { ContactPointsList = new List() }; break; case "populated-list": contentData = new UserContactPointsList() { ContactPointsList = [ - new UserContactPoints() { NationalIdentityNumber = "01025101038", Email = string.Empty }, - new UserContactPoints() { NationalIdentityNumber = "01025101037", Email = string.Empty } + new UserContactPointsDto() { NationalIdentityNumber = "01025101038", Email = string.Empty }, + new UserContactPointsDto() { NationalIdentityNumber = "01025101037", Email = string.Empty } ] }; break;