Skip to content

Commit

Permalink
Prepare for nullability of UserId in Profile API - user contact point…
Browse files Browse the repository at this point in the history
… lookups (#644)

* Declare nullability on properties after changes in Profile API

* Revert "Declare nullability on properties after changes in Profile API"

This reverts commit d4fadb0.

* Introduce a DTO class for UserContactPoints as received from Profile API

* Add test for mapping of UserContactPoints DTO -> domain model

* Fix build warning: replace NotEmpty -> Contains

* Fix warning on pascal-casing by renaming DTO class

* Rename files, modify comments

* Change file names for real
  • Loading branch information
hggutvik authored Nov 14, 2024
1 parent fae46ea commit 45aff84
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 8 deletions.
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 user contact points from DTO to domain model
/// </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
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Altinn.Notifications.Integrations.Profile.Models;

/// <summary>
/// DTO for user contact points received from the ProfileClient, describing the availability of contact points for a user
/// </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,
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 = "[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

0 comments on commit 45aff84

Please sign in to comment.