Skip to content

Commit

Permalink
Further separate the data access details from the Core layer (#213)
Browse files Browse the repository at this point in the history
* Rebranding IUserProfileClient -> IUserProfileRepository

* Rebrand IUnitProfileClient -> IUnitProfileRepository

* Structuring Unit and User profile repositories under Integrations.SblBridge

* Extract UserRegisteredContactPoint to separate class

* Improve readability of running instruction

* Add newline to better separate Altinn refs from other references

* Remove trace of the Client abstraction in private class field
  • Loading branch information
hggutvik authored Oct 7, 2024
1 parent fa92fc5 commit 0dc9edc
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cd altinn-profile

### Running the application in a docker container

- Start Altinn Profile docker container run the command
- Start Altinn Profile docker container by running the command

```cmd
podman compose up -d --build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Altinn.Profile.Core.Unit.ContactPoints;
using Altinn.Profile.Core.Unit.ContactPoints;

namespace Altinn.Profile.Core.Integrations
{
/// <summary>
/// Interface describing a client for the user profile service
/// </summary>
public interface IUnitProfileClient
public interface IUnitProfileRepository
{
/// <summary>
/// Provides a list of user registered contact points based on the lookup criteria
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Altinn.Profile.Core.Integrations;
/// <summary>
/// Interface describing a client for the user profile service
/// </summary>
public interface IUserProfileClient
public interface IUserProfileRepository
{
/// <summary>
/// Method that fetches a user based on a user id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
namespace Altinn.Profile.Core.Unit.ContactPoints
{
/// <summary>
/// Implementation of the <see cref="IUnitContactPointsService"/> interface using a REST client to retrieve profile data "/>
/// Implementation of the <see cref="IUnitContactPointsService"/> interface using an <see cref="IUnitProfileRepository"/> retrieve profile data "/>
/// </summary>
public class UnitContactPointService : IUnitContactPointsService
{
private readonly IUnitProfileClient _unitClient;
private readonly IUnitProfileRepository _unitRepository;

/// <summary>
/// Initializes a new instance of the <see cref="UnitContactPointService"/> class.
/// </summary>
public UnitContactPointService(IUnitProfileClient unitClient)
public UnitContactPointService(IUnitProfileRepository unitRepository)
{
_unitClient = unitClient;
_unitRepository = unitRepository;
}

/// <inheritdoc/>
public async Task<Result<UnitContactPointsList, bool>> GetUserRegisteredContactPoints(UnitContactPointLookup lookup) => await _unitClient.GetUserRegisteredContactPoints(lookup);
public async Task<Result<UnitContactPointsList, bool>> GetUserRegisteredContactPoints(UnitContactPointLookup lookup) => await _unitRepository.GetUserRegisteredContactPoints(lookup);
}
}
20 changes: 10 additions & 10 deletions src/Altinn.Profile.Core/User/UserProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
namespace Altinn.Profile.Core.User;

/// <summary>
/// Implementation of <see cref="IUserProfileService"/> that uses <see cref="IUserProfileClient"/> to fetch user profiles."/>
/// Implementation of <see cref="IUserProfileService"/> that uses <see cref="IUserProfileRepository"/> to fetch user profiles."/>
/// </summary>
public class UserProfileService : IUserProfileService
{
private readonly IUserProfileClient _userProfileClient;
private readonly IUserProfileRepository _userProfileRepo;

/// <summary>
/// Initializes a new instance of the <see cref="UserProfileService"/> class.
/// </summary>
/// <param name="userProfileClient">The user profile client available through DI</param>
public UserProfileService(IUserProfileClient userProfileClient)
/// <param name="userProfileRepo">The user profile client available through DI</param>
public UserProfileService(IUserProfileRepository userProfileRepo)
{
_userProfileClient = userProfileClient;
_userProfileRepo = userProfileRepo;
}

/// <inheritdoc/>
public async Task<Result<UserProfile, bool>> GetUser(int userId)
{
return await _userProfileClient.GetUser(userId);
return await _userProfileRepo.GetUser(userId);
}

/// <inheritdoc/>
public async Task<Result<UserProfile, bool>> GetUser(string ssn)
{
return await _userProfileClient.GetUser(ssn);
return await _userProfileRepo.GetUser(ssn);
}

/// <inheritdoc/>
public async Task<Result<UserProfile, bool>> GetUserByUsername(string username)
{
return await _userProfileClient.GetUserByUsername(username);
return await _userProfileRepo.GetUserByUsername(username);
}

/// <inheritdoc/>
public async Task<Result<UserProfile, bool>> GetUserByUuid(Guid userUuid)
{
return await _userProfileClient.GetUserByUuid(userUuid);
return await _userProfileRepo.GetUserByUuid(userUuid);
}

/// <inheritdoc/>
public async Task<Result<List<UserProfile>, bool>> GetUserListByUuid(List<Guid> userUuidList)
{
return await _userProfileClient.GetUserListByUuid(userUuidList);
return await _userProfileRepo.GetUserListByUuid(userUuidList);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Altinn.Profile.Core.Unit.ContactPoints;
using Altinn.Profile.Core.User.ContactPoints;

namespace Altinn.Profile.Integrations.SblBridge
namespace Altinn.Profile.Integrations.SblBridge.Unit.Profile
{
/// <summary>
/// Model describing a container for a list of contact points.
Expand All @@ -26,7 +26,7 @@ public class PartyNotificationContactPoints
/// <summary>
/// Gets or sets a list of multiple contanct points associated with the organisation.
/// </summary>
public List<UserRegisteredContactPoint> ContactPoints { get; set; } = new List<UserRegisteredContactPoint>();
public List<UserRegisteredContactPoint> ContactPoints { get; set; } = [];

/// <summary>
/// Maps a list of <see cref="PartyNotificationContactPoints"/> to a list of <see cref="UnitContactPoints"/>.
Expand All @@ -48,28 +48,4 @@ public static UnitContactPointsList MapToUnitContactPoints(List<PartyNotificatio
return new UnitContactPointsList() { ContactPointsList = contactPoints };
}
}

/// <summary>
/// Model describing the contact information that a user has associated with a party they can represent.
/// </summary>
public class UserRegisteredContactPoint
{
/// <summary>
/// Gets or sets the legacy user id for the owner of this party notification endpoint.
/// </summary>
/// <remarks>
/// This was named as legacy for consistency. Property for UUID will probably never be added.
/// </remarks>
public int LegacyUserId { get; set; }

/// <summary>
/// Gets or sets the email address for this contact point.
/// </summary>
public string Email { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the mobile number for this contact point.
/// </summary>
public string MobileNumber { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using System.Text;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

using Altinn.Profile.Core;
using Altinn.Profile.Core.Integrations;
using Altinn.Profile.Core.Unit.ContactPoints;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Altinn.Profile.Integrations.SblBridge;
namespace Altinn.Profile.Integrations.SblBridge.Unit.Profile;

/// <summary>
/// Represents an implementation of <see cref="IUnitProfileClient"/> using SBLBridge to obtain unit profile information.
/// Represents an implementation of <see cref="IUnitProfileRepository"/> using SBLBridge to obtain unit profile information.
/// </summary>
public class UnitProfileClient : IUnitProfileClient
public class UnitProfileClient : IUnitProfileRepository
{
private readonly ILogger<UnitProfileClient> _logger;
private readonly HttpClient _client;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Altinn.Profile.Integrations.SblBridge.Unit.Profile
{
/// <summary>
/// Model describing the contact information that a user has associated with a party they can represent.
/// </summary>
public class UserRegisteredContactPoint
{
/// <summary>
/// Gets or sets the legacy user id for the owner of this party notification endpoint.
/// </summary>
/// <remarks>
/// This was named as legacy for consistency. Property for UUID will probably never be added.
/// </remarks>
public int LegacyUserId { get; set; }

/// <summary>
/// Gets or sets the email address for this contact point.
/// </summary>
public string Email { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the mobile number for this contact point.
/// </summary>
public string MobileNumber { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Web;

using Altinn.Platform.Profile.Models;
using Altinn.Profile.Core;
using Altinn.Profile.Core.Integrations;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Altinn.Profile.Integrations.SblBridge;
namespace Altinn.Profile.Integrations.SblBridge.User.Profile;

/// <summary>
/// Represents an implementation of <see cref="IUserProfileClient"/> using SBLBridge to obtain profile information.
/// Represents an implementation of <see cref="IUserProfileRepository"/> using SBLBridge to obtain profile information.
/// </summary>
public class UserProfileClient : IUserProfileClient
public class UserProfileClient : IUserProfileRepository
{
private readonly ILogger<UserProfileClient> _logger;
private readonly HttpClient _client;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Altinn.Profile.Core.Integrations;
using Altinn.Profile.Integrations.SblBridge;

using Altinn.Profile.Integrations.SblBridge.Unit.Profile;
using Altinn.Profile.Integrations.SblBridge.User.Profile;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -23,7 +24,7 @@ public static void AddSblBridgeClients(this IServiceCollection services, IConfig
?? throw new ArgumentNullException(nameof(config), "Required SblBridgeSettings is missing from application configuration");

services.Configure<SblBridgeSettings>(config.GetSection(nameof(SblBridgeSettings)));
services.AddHttpClient<IUserProfileClient, UserProfileClient>();
services.AddHttpClient<IUnitProfileClient, UnitProfileClient>();
services.AddHttpClient<IUserProfileRepository, UserProfileClient>();
services.AddHttpClient<IUnitProfileRepository, UnitProfileClient>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Altinn.Profile.Controllers;
using Altinn.Profile.Core.Unit.ContactPoints;
using Altinn.Profile.Integrations.SblBridge;
using Altinn.Profile.Integrations.SblBridge.Unit.Profile;
using Altinn.Profile.Tests.IntegrationTests.Mocks;
using Altinn.Profile.Tests.IntegrationTests.Utils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Altinn.Common.AccessToken.Services;
using Altinn.Profile.Core.Integrations;
using Altinn.Profile.Integrations.SblBridge;
using Altinn.Profile.Integrations.SblBridge.Unit.Profile;
using Altinn.Profile.Integrations.SblBridge.User.Profile;
using Altinn.Profile.Tests.IntegrationTests.Mocks;
using Altinn.Profile.Tests.IntegrationTests.Mocks.Authentication;
using AltinnCore.Authentication.JwtCookie;
Expand Down Expand Up @@ -56,13 +58,13 @@ public HttpClient GetTestServerClient()
// Using the real/actual implementation of IUserProfileService, but with a mocked message handler.
// Haven't found any other ways of injecting a mocked message handler to simulate SBL Bridge.
services.AddSingleton<IUserProfileClient>(
services.AddSingleton<IUserProfileRepository>(
new UserProfileClient(
new HttpClient(SblBridgeHttpMessageHandler),
UserProfileClientLogger.Object,
SblBridgeSettingsOptions.Object));
services.AddSingleton<IUnitProfileClient>(
services.AddSingleton<IUnitProfileRepository>(
new UnitProfileClient(
new HttpClient(SblBridgeHttpMessageHandler),
UnitProfileClientLogger.Object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Altinn.Profile.Core.Unit.ContactPoints;
using Altinn.Profile.Core.User.ContactPoints;
using Altinn.Profile.Integrations.SblBridge;
using Altinn.Profile.Integrations.SblBridge.Unit.Profile;

using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
Expand All @@ -8,6 +8,7 @@
using Altinn.Profile.Core;
using Altinn.Profile.Core.Unit.ContactPoints;
using Altinn.Profile.Integrations.SblBridge;
using Altinn.Profile.Integrations.SblBridge.Unit.Profile;
using Altinn.Profile.Tests.IntegrationTests.Mocks;

using Microsoft.Extensions.Logging;
Expand All @@ -19,7 +20,7 @@

namespace Altinn.Profile.Tests.Profile.Integrations.SblBridge
{
public class UnitProfileClientTest
public class UnitProfileRepositoryTest
{
[Fact]
public async Task GetUserRegisteredContactPoints_BridgeRespondsWithOk_UnitContactPointsListReturned()
Expand Down

0 comments on commit 0dc9edc

Please sign in to comment.