Skip to content

Commit

Permalink
Small refactoring + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larsolavk committed Nov 21, 2024
1 parent 90ecdaf commit 0578f9b
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 34 deletions.
51 changes: 17 additions & 34 deletions src/oed-authz/Services/PipService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,39 @@
using oed_authz.Models;

namespace oed_authz.Services;
public class PipService : IPolicyInformationPointService
public class PipService(IRoleAssignmentsRepository repository)
: IPolicyInformationPointService
{
private readonly IRoleAssignmentsRepository _oedRoleRepositoryService;

public PipService(IRoleAssignmentsRepository oedRoleRepositoryService)
{
_oedRoleRepositoryService = oedRoleRepositoryService;
}

public async Task<PipResponse> HandlePipRequest(PipRequest pipRequest)
{
if (pipRequest.RecipientSsn is not null && !Utils.IsValidSsn(pipRequest.RecipientSsn))
// EstateSsn is required for now
if (string.IsNullOrWhiteSpace(pipRequest.EstateSsn) || !Utils.IsValidSsn(pipRequest.EstateSsn))
{
throw new ArgumentException(nameof(pipRequest.RecipientSsn));
throw new ArgumentException($"Invalid {nameof(pipRequest.EstateSsn)}", nameof(pipRequest));
}

if (pipRequest.EstateSsn is not null && !Utils.IsValidSsn(pipRequest.EstateSsn))
if (pipRequest.RecipientSsn is not null && !Utils.IsValidSsn(pipRequest.RecipientSsn))
{
throw new ArgumentException(nameof(pipRequest.EstateSsn));
throw new ArgumentException($"Invalid {nameof(pipRequest.RecipientSsn)}", nameof(pipRequest));
}

List<RoleAssignment> roleAssignments;
if (pipRequest.RecipientSsn is not null && pipRequest.EstateSsn is not null)
{
roleAssignments = await _oedRoleRepositoryService.GetRoleAssignmentsForPerson(
pipRequest.EstateSsn, pipRequest.RecipientSsn);
}
else if (pipRequest.EstateSsn is not null)
{
roleAssignments = await _oedRoleRepositoryService.GetRoleAssignmentsForEstate(pipRequest.EstateSsn);
}
else
{
throw new ArgumentNullException(nameof(pipRequest), "Both recipientSsn and estateSsn cannot be null");
}
var roleAssignments = pipRequest.RecipientSsn is not null
? await repository.GetRoleAssignmentsForPerson(pipRequest.EstateSsn, pipRequest.RecipientSsn)
: await repository.GetRoleAssignmentsForEstate(pipRequest.EstateSsn);

var pipRoleAssignments = new List<PipRoleAssignment>();
foreach (var result in roleAssignments)
{
pipRoleAssignments.Add(new PipRoleAssignment
return new PipResponse {
EstateSsn = pipRequest.EstateSsn,
RoleAssignments = roleAssignments
.Select(result => new PipRoleAssignment
{
Id = result.Id,
EstateSsn = result.EstateSsn,
RoleCode = result.RoleCode,
Created = result.Created,
HeirSsn = result.HeirSsn,
RecipientSsn = result.RecipientSsn
});
}

return new PipResponse { RoleAssignments = pipRoleAssignments };
})
.ToList()
};
}
}
142 changes: 142 additions & 0 deletions test/oed-authz.UnitTests/Services/PipServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using FakeItEasy;
using FluentAssertions;
using oed_authz.Interfaces;
using oed_authz.Models;
using oed_authz.Services;
using oed_authz.Settings;
using oed_authz.UnitTests.TestUtils;

namespace oed_authz.UnitTests.Services;

public class PipServiceTests
{
private readonly IRoleAssignmentsRepository _fakeRepository = A.Fake<IRoleAssignmentsRepository>();

public PipServiceTests()
{
A.CallTo(() => _fakeRepository.GetRoleAssignmentsForEstate(A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole("12345678901"),
factory.FormuesfulmaktRole("12345678902"),
});
});

A.CallTo(() => _fakeRepository.GetRoleAssignmentsForPerson(A<string>._, A<string>._))
.ReturnsLazily((call) =>
{
var estateSsn = call.Arguments.Get<string>("estateSsn")!;
var recipientSsn = call.Arguments.Get<string>("recipientSsn")!;
var factory = new RoleAssignmentFactory(estateSsn);
return Task.FromResult(new List<RoleAssignment>
{
factory.ProbateRole(recipientSsn),
});
});
}

[Fact]
public async Task HandlePipRequest_When_OnlyEstateSsnIsSpecified_Should_ReturnAllRolesFromEstate()
{
// Arrange
var pipRequest = new PipRequest
{
EstateSsn = "11111111111",
RecipientSsn = null
};

var sut = new PipService(_fakeRepository);

// Act
var result = await sut.HandlePipRequest(pipRequest);

// Assert
//result.EstateSsn.Should().Be("11111111111");
result.RoleAssignments.Should().HaveCount(2);
result.RoleAssignments.Should().AllSatisfy(ra => { ra.EstateSsn.Should().Be("11111111111"); });

result.RoleAssignments.Should().ContainSingle(ra =>
ra.RecipientSsn == "12345678901" &&
ra.RoleCode == Constants.ProbateRoleCode);

result.RoleAssignments.Should().ContainSingle(ra =>
ra.RecipientSsn == "12345678902" &&
ra.RoleCode == Constants.FormuesfullmaktRoleCode);
}

[Fact]
public async Task HandlePipRequest_When_BothEstateSsnAndRecipientSsnAreSpecified_Should_ReturnOnlyRolesForTheGivenRecipienAndEstate()
{
// Arrange
var pipRequest = new PipRequest
{
EstateSsn = "11111111111",
RecipientSsn = "12345678901"
};

var sut = new PipService(_fakeRepository);

// Act
var result = await sut.HandlePipRequest(pipRequest);

// Assert
//result.EstateSsn.Should().Be("11111111111");
result.RoleAssignments.Should().HaveCount(1);
result.RoleAssignments.Should().ContainSingle(ra =>
ra.RecipientSsn == "12345678901" &&
ra.RoleCode == Constants.ProbateRoleCode);
}

[Fact]
public async Task HandlePipRequest_When_OnlyRecipientSsnIsSpecified_Should_Throw()
{
// Arrange
var pipRequest = new PipRequest
{
EstateSsn = null!,
RecipientSsn = "12345678901"
};

var sut = new PipService(_fakeRepository);

// Act
var act = async () => { await sut.HandlePipRequest(pipRequest); };

// Assert
await act.Should()
.ThrowAsync<ArgumentException>()
.Where(e =>
e.Message.Contains($"Invalid {nameof(PipRequest.EstateSsn)}") &&
e.ParamName == "pipRequest");
}

[Fact]
public async Task HandlePipRequest_When_InvalidRecipientSsnIsSpecified_Should_Throw()
{
// Arrange
var pipRequest = new PipRequest
{
EstateSsn = "11111111111"!,
RecipientSsn = "abc123"
};

var sut = new PipService(_fakeRepository);

// Act
var act = async () => { await sut.HandlePipRequest(pipRequest); };

// Assert
await act.Should()
.ThrowAsync<ArgumentException>()
.Where(e =>
e.Message.Contains($"Invalid {nameof(PipRequest.RecipientSsn)}") &&
e.ParamName == "pipRequest");
}

}

0 comments on commit 0578f9b

Please sign in to comment.