Skip to content

Commit

Permalink
Add ApplicationOwnerConfigService
Browse files Browse the repository at this point in the history
  • Loading branch information
SandGrainOne committed Oct 6, 2023
1 parent 60fdf10 commit 4859b0f
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An implementation of <see cref="IApplicationOwnerConfigService"/> with the necessary business logic
/// to handle application owner specific configuration in the Notifications application.
/// </summary>
public class ApplicationOwnerConfigService : IApplicationOwnerConfigService
{
private readonly IApplicationOwnerConfigRepository _applicationOwnerConfigRepository;

/// <summary>
/// Initializes a new instance of the <see cref="ApplicationOwnerConfigService"/> class.
/// </summary>
/// <param name="applicationOwnerConfigRepository">A repository implementation</param>
public ApplicationOwnerConfigService(IApplicationOwnerConfigRepository applicationOwnerConfigRepository)
{
_applicationOwnerConfigRepository = applicationOwnerConfigRepository;
}

/// <inheritdoc/>
public async Task<ApplicationOwnerConfig> GetApplicationOwnerConfig(string orgId)
{
return await _applicationOwnerConfigRepository.GetApplicationOwnerConfig(orgId)
?? new ApplicationOwnerConfig(orgId);
}

/// <inheritdoc/>
public async Task WriteApplicationOwnerConfig(ApplicationOwnerConfig applicationOwnerConfig)
{
await _applicationOwnerConfigRepository.WriteApplicationOwnerConfig(applicationOwnerConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Altinn.Notifications.Core.Models;

namespace Altinn.Notifications.Core.Services.Interfaces;

/// <summary>
/// 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.
/// </summary>
public interface IApplicationOwnerConfigService
{
/// <summary>
/// Get the full set of configuration options for a specified application owner.
/// </summary>
/// <param name="orgId">The unique id of an application owner.</param>
/// <returns>A <see cref="Task{ApplicationOwnerConfig}"/> representing the result of the asynchronous operation.</returns>
Task<ApplicationOwnerConfig> GetApplicationOwnerConfig(string orgId);

/// <summary>
/// Write a full <see cref="ApplicationOwnerConfig"/> object to storage.
/// </summary>
/// <param name="applicationOwnerConfig">The configuration object to write to storage.</param>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
Task WriteApplicationOwnerConfig(ApplicationOwnerConfig applicationOwnerConfig);
}
Original file line number Diff line number Diff line change
@@ -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<IApplicationOwnerConfigRepository> repositoryMock = new();
repositoryMock.Setup(a => a.GetApplicationOwnerConfig(It.Is<string>(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<IApplicationOwnerConfigRepository> repositoryMock = new();
repositoryMock.Setup(a => a.GetApplicationOwnerConfig(It.Is<string>(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<IApplicationOwnerConfigRepository> repositoryMock = new();
repositoryMock.Setup(a => a.WriteApplicationOwnerConfig(It.Is<ApplicationOwnerConfig>(c => c.OrgId == OrgId)));

ApplicationOwnerConfigService target = new ApplicationOwnerConfigService(repositoryMock.Object);

// Act
await target.WriteApplicationOwnerConfig(new ApplicationOwnerConfig(OrgId));

// Assert
repositoryMock.VerifyAll();
}
}

0 comments on commit 4859b0f

Please sign in to comment.