Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for nullability of UserId in Profile API - user contact point lookups #644

Merged
merged 8 commits into from
Nov 14, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Altinn.Notifications.Core.Models.ContactPoints;
using Altinn.Notifications.Integrations.Profile.Models;

namespace Altinn.Notifications.Integrations.Profile.Mappers;

/// <summary>
/// Extension class to map from DTO / Profile API model to domain model for UserContactPoints
/// </summary>
public static class UserContactPointsDtoMapperExtension
{
/// <summary>
/// Maps the DTO model to domain model
/// </summary>
/// <param name="userContactPointDto">This DTO object</param>
/// <returns>The UserContactPoints object mapped from this DTO object</returns>
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
};
}
}
hggutvik marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Altinn.Notifications.Integrations.Profile.Models;

/// <summary>
/// Data transfer object for the API model describing the availability of contact points for a user, received from the ProfileClient
/// </summary>
public class UserContactPointsDto
{
/// <summary>
/// Gets or sets the ID of the user
/// </summary>
public int? UserId { get; set; }

/// <summary>
/// Gets or sets the national identityt number of the user
/// </summary>
public string? NationalIdentityNumber { get; set; }

/// <summary>
/// Gets or sets a boolean indicating whether the user has reserved themselves from electronic communication
/// </summary>
public bool IsReserved { get; set; }

/// <summary>
/// Gets or sets the mobile number
/// </summary>
public string? MobileNumber { get; set; }

/// <summary>
/// Gets or sets the email address
/// </summary>
public string? Email { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,8 +49,9 @@ public async Task<List<UserContactPoints>> GetUserContactPoints(List<string> nat
}

string responseContent = await response.Content.ReadAsStringAsync();
List<UserContactPoints> contactPoints = JsonSerializer.Deserialize<UserContactPointsList>(responseContent, JsonSerializerOptionsProvider.Options)!.ContactPointsList;
return contactPoints!;
List<UserContactPointsDto> contactPoints = JsonSerializer.Deserialize<UserContactPointsList>(responseContent, JsonSerializerOptionsProvider.Options)!.ContactPointsList;

return contactPoints.Select(contactPointDto => contactPointDto.ToUserContactPoint()).ToList();
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Altinn.Notifications.Core.Models.ContactPoints;
using Altinn.Notifications.Integrations.Profile.Models;

namespace Altinn.Notifications.Integrations.Profile;

Expand All @@ -10,5 +11,5 @@ public class UserContactPointsList
/// <summary>
/// A list containing contact points for users
/// </summary>
public List<UserContactPoints> ContactPointsList { get; set; } = [];
public List<UserContactPointsDto> ContactPointsList { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task GetNewNotifications()
List<Email> emailToBeSent = await repo.GetNewNotifications();

// Assert
Assert.NotEmpty(emailToBeSent.Where(s => s.NotificationId == emailNotification.Id));
Assert.Contains(emailToBeSent, s => s.NotificationId == emailNotification.Id);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task GetNewNotifications()
List<Sms> smsToBeSent = await repo.GetNewNotifications();

// Assert
Assert.NotEmpty(smsToBeSent.Where(s => s.NotificationId == smsNotification.Id));
Assert.Contains(smsToBeSent, s => s.NotificationId == smsNotification.Id);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Email = null,
IsReserved = false,
MobileNumber = null
};

// Act
var mappedResult = userContactPointsDTO.ToUserContactPoint();

// Assert
Assert.Equal(0, mappedResult.UserId);
Assert.Equal(string.Empty, mappedResult.NationalIdentityNumber);
Assert.Equal(string.Empty, mappedResult.Email);
Assert.Equal(string.Empty, mappedResult.MobileNumber);
Assert.False(mappedResult.IsReserved);
}

[Fact]
public void ToUserContactPoint_WithValues_MapsCorrectly()
{
// Arrange
var testData = new
{
userId = 123,
nationalIdentityNumber = "12345678910",
email = "[email protected]",
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,15 +129,15 @@ private Task<HttpResponseMessage> GetUserProfileResponse(UserContactPointLookup
switch (lookup.NationalIdentityNumbers[0])
{
case "empty-list":
contentData = new UserContactPointsList() { ContactPointsList = new List<UserContactPoints>() };
contentData = new UserContactPointsList() { ContactPointsList = new List<UserContactPointsDto>() };
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;
Expand Down
Loading