diff --git a/src/Altinn.Notifications.Core/Services/ApplicationOwnerConfigService.cs b/src/Altinn.Notifications.Core/Services/ApplicationOwnerConfigService.cs new file mode 100644 index 00000000..97879275 --- /dev/null +++ b/src/Altinn.Notifications.Core/Services/ApplicationOwnerConfigService.cs @@ -0,0 +1,36 @@ +using Altinn.Notifications.Core.Models; +using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Services.Interfaces; + +namespace Altinn.Notifications.Core.Services; + +/// +/// An implementation of with the necessary business logic +/// to handle application owner specific configuration in the Notifications application. +/// +public class ApplicationOwnerConfigService : IApplicationOwnerConfigService +{ + private readonly IApplicationOwnerConfigRepository _applicationOwnerConfigRepository; + + /// + /// Initializes a new instance of the class. + /// + /// A repository implementation + public ApplicationOwnerConfigService(IApplicationOwnerConfigRepository applicationOwnerConfigRepository) + { + _applicationOwnerConfigRepository = applicationOwnerConfigRepository; + } + + /// + public async Task GetApplicationOwnerConfig(string orgId) + { + return await _applicationOwnerConfigRepository.GetApplicationOwnerConfig(orgId) + ?? new ApplicationOwnerConfig(orgId); + } + + /// + public async Task WriteApplicationOwnerConfig(ApplicationOwnerConfig applicationOwnerConfig) + { + await _applicationOwnerConfigRepository.WriteApplicationOwnerConfig(applicationOwnerConfig); + } +} diff --git a/src/Altinn.Notifications.Core/Services/Interfaces/IApplicationOwnerConfigService.cs b/src/Altinn.Notifications.Core/Services/Interfaces/IApplicationOwnerConfigService.cs new file mode 100644 index 00000000..9fc07fc5 --- /dev/null +++ b/src/Altinn.Notifications.Core/Services/Interfaces/IApplicationOwnerConfigService.cs @@ -0,0 +1,25 @@ +using Altinn.Notifications.Core.Models; + +namespace Altinn.Notifications.Core.Services.Interfaces; + +/// +/// This interface is a description of an application owner configuration service implementation. +/// Such a service will be required to have methods needed to register, change and retrieve application +/// owner configuration. +/// +public interface IApplicationOwnerConfigService +{ + /// + /// Get the full set of configuration options for a specified application owner. + /// + /// The unique id of an application owner. + /// A representing the result of the asynchronous operation. + Task GetApplicationOwnerConfig(string orgId); + + /// + /// Write a full object to storage. + /// + /// The configuration object to write to storage. + /// A representing the result of the asynchronous operation. + Task WriteApplicationOwnerConfig(ApplicationOwnerConfig applicationOwnerConfig); +} diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/ApplicationOwnerConfigServiceTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/ApplicationOwnerConfigServiceTests.cs new file mode 100644 index 00000000..178952e0 --- /dev/null +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/ApplicationOwnerConfigServiceTests.cs @@ -0,0 +1,74 @@ +using System.Threading.Tasks; + +using Altinn.Notifications.Core.Models; +using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Services; + +using Moq; + +using Xunit; + +namespace Altinn.Notifications.Tests.Notifications.Core.TestingServices; + +public class ApplicationOwnerConfigServiceTests +{ + [Fact] + public async Task GetApplicationOwnerConfigTest_Repository_returns_found_config_Service_returns_found_config() + { + // Arrange + const string OrgId = "exists"; + + Mock repositoryMock = new(); + repositoryMock.Setup(a => a.GetApplicationOwnerConfig(It.Is(o => o == OrgId))) + .ReturnsAsync(new ApplicationOwnerConfig(OrgId)); + + ApplicationOwnerConfigService target = new ApplicationOwnerConfigService(repositoryMock.Object); + + // Act + var actual = await target.GetApplicationOwnerConfig(OrgId); + + // Assert + repositoryMock.VerifyAll(); + + Assert.NotNull(actual); + } + + [Fact] + public async Task GetApplicationOwnerConfigTest_Repository_returns_null_Service_returns_empty_config() + { + // Arrange + const string OrgId = "wrong"; + + Mock repositoryMock = new(); + repositoryMock.Setup(a => a.GetApplicationOwnerConfig(It.Is(o => o == OrgId))) + .ReturnsAsync((ApplicationOwnerConfig?)null); + + ApplicationOwnerConfigService target = new ApplicationOwnerConfigService(repositoryMock.Object); + + // Act + var actual = await target.GetApplicationOwnerConfig(OrgId); + + // Assert + repositoryMock.VerifyAll(); + + Assert.NotNull(actual); + } + + [Fact] + public async Task WriteApplicationOwnerConfigTest_Repository_returns_null_Service_returns_empty_config() + { + // Arrange + const string OrgId = "exists"; + + Mock repositoryMock = new(); + repositoryMock.Setup(a => a.WriteApplicationOwnerConfig(It.Is(c => c.OrgId == OrgId))); + + ApplicationOwnerConfigService target = new ApplicationOwnerConfigService(repositoryMock.Object); + + // Act + await target.WriteApplicationOwnerConfig(new ApplicationOwnerConfig(OrgId)); + + // Assert + repositoryMock.VerifyAll(); + } +}