Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using AutoFixture.NUnit4;
using FluentAssertions;
using Microsoft.Identity.Client;
using Moq;
using SFA.DAS.PR.Application.AccountProviders.Queries.GetAccountProviders;
using SFA.DAS.PR.Application.Mediatr.Responses;
using SFA.DAS.PR.Domain.Common;
using SFA.DAS.PR.Domain.Entities;
using SFA.DAS.PR.Domain.Interfaces;
using SFA.DAS.Testing.AutoFixture;
Expand All @@ -17,11 +17,42 @@ public class GetAccountProvidersQueryHandlerTests
public async Task Handle_ProvidersFound_ReturnsAccountProvidersResult(
[Frozen] Mock<IAccountLegalEntityReadRepository> accountLegalEntityReadRepository,
GetAccountProvidersQueryHandler sut,
List<AccountLegalEntity> legalEntities,
long accountId,
long ukprn,
CancellationToken cancellationToken
)
{
var legalEntities = new List<AccountLegalEntity>()
{
new AccountLegalEntity
{
Id = 1,
PublicHashedId = "ABC123",
AccountId = accountId,
Name = "Test Legal Entity",
Account = new Account
{
Id = accountId,
Name = "Test Account",
AccountProviders = new List<AccountProvider>
{
new AccountProvider
{
Id = 1,
AccountId = accountId,
ProviderUkprn = ukprn,
Provider = new Provider
{
Ukprn = ukprn,
Name = "Test Provider",
Status = null
},
}
}
},
}
};

accountLegalEntityReadRepository.Setup(a =>
a.GetAccountLegalEntities(accountId, cancellationToken)
).ReturnsAsync(legalEntities);
Expand Down Expand Up @@ -53,5 +84,56 @@ CancellationToken cancellationToken

result.Result.Should().BeEquivalentTo(expectedResult, c => c.ExcludingMissingMembers());
}

[Test]
[RecursiveMoqAutoData]
public async Task Handle_ProvidersIsRemoved_ReturnsEmptyResultt(
[Frozen] Mock<IAccountLegalEntityReadRepository> accountLegalEntityReadRepository,
GetAccountProvidersQueryHandler sut,
long accountId,
long ukprn,
CancellationToken cancellationToken
)
{
var legalEntities = new List<AccountLegalEntity>()
{
new AccountLegalEntity
{
Id = 1,
PublicHashedId = "ABC123",
AccountId = accountId,
Name = "Test Legal Entity",
Account = new Account
{
Id = accountId,
Name = "Test Account",
AccountProviders = new List<AccountProvider>
{
new AccountProvider
{
Id = 1,
AccountId = accountId,
ProviderUkprn = ukprn,
Provider = new Provider
{
Ukprn = ukprn,
Name = "Test Provider",
Status = ProviderStatus.Removed
},
}
}
},
}
};
accountLegalEntityReadRepository.Setup(a =>
a.GetAccountLegalEntities(accountId, cancellationToken)
).ReturnsAsync(legalEntities);

GetAccountProvidersQueryResult expectedResult = new(accountId, []);

var result = await sut.Handle(new GetAccountProvidersQuery(accountId), cancellationToken);

result.Result.Should().BeEquivalentTo(expectedResult, c => c.ExcludingMissingMembers());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace SFA.DAS.PR.Application.UnitTests.Common.Validators;

public class UkprnFormatValidatorTests
{
{
public class TestUkprnEntity : IUkprnEntity
{
public long? Ukprn { get; set; }
Expand All @@ -17,6 +17,7 @@ public async Task ValidateUkrpnFormat_ReturnsValid()
{
var providerReadRepositoryMock = new Mock<IProviderReadRepository>();
providerReadRepositoryMock.Setup(a => a.ProviderExists(It.IsAny<long>(), CancellationToken.None)).ReturnsAsync(true);
providerReadRepositoryMock.Setup(a => a.ProviderRemoved(It.IsAny<long>(), CancellationToken.None)).ReturnsAsync(false);

var entity = new TestUkprnEntity { Ukprn = 10000003 };

Expand Down Expand Up @@ -69,4 +70,26 @@ public async Task ValidateUkrpnExists_ReturnsInvalid()
Assert.That(validationResult.Errors[0].ErrorMessage, Is.EqualTo(UkprnValidator.ProviderEntityExistValidationMessage));
}
}

[Test]
public async Task ValidateProviderRemoved_ReturnsInvalid()
{
var providerReadRepositoryMock = new Mock<IProviderReadRepository>();
providerReadRepositoryMock.Setup(a => a.ProviderExists(It.IsAny<long>(), CancellationToken.None)).ReturnsAsync(true);
providerReadRepositoryMock.Setup(a => a.ProviderRemoved(It.IsAny<long>(), CancellationToken.None)).ReturnsAsync(true);

var entity = new TestUkprnEntity { Ukprn = 10000001 };

var validator = new InlineValidator<TestUkprnEntity>();
validator.RuleFor(x => x.Ukprn)
.IsValidUkprn(providerReadRepositoryMock.Object);

var validationResult = await validator.ValidateAsync(entity);

using (Assert.EnterMultipleScope())
{
Assert.That(validationResult.IsValid, Is.False);
Assert.That(validationResult.Errors[0].ErrorMessage, Is.EqualTo(UkprnValidator.ProviderRemovedValidationMessage));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.PR.Application.UnitTests.Permissions.Queries.GetEmployerRelationships;

public class GetEmployerRelationshipsQueryHandlerTests
{
[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SFA.DAS.PR.Domain.Entities;
using SFA.DAS.PR.Domain.Common;
using SFA.DAS.PR.Domain.Entities;

namespace SFA.DAS.PR.Application.AccountProviders.Queries.GetAccountProviders;

Expand All @@ -19,6 +20,7 @@ public static List<AccountProviderModel> BuildAccountProviderModels(List<Account
{
IEnumerable<AccountProvider> accountProviders = legalEntities.Select(a => a.Account)
.SelectMany(a => a.AccountProviders)
.Where(p => p.Provider.Status != ProviderStatus.Removed)
.Distinct();

List<AccountProviderModel> providerModels = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class UkprnValidator

public const string ProviderEntityExistValidationMessage = "Provider must exist.";

public const string ProviderRemovedValidationMessage = "Provider has been removed.";

private static IRuleBuilderOptions<T, long?> IsValidUkprnFormat<T>(this IRuleBuilder<T, long?> ruleBuilder) where T : IUkprnEntity
{
return ruleBuilder
Expand All @@ -28,6 +30,11 @@ public static class UkprnValidator
return await providerReadRepository.ProviderExists(ukprn!.Value, cancellationToken);
})
.WithMessage(ProviderEntityExistValidationMessage)
.MustAsync(async (ukprn, cancellationToken) =>
{
return !await providerReadRepository.ProviderRemoved(ukprn!.Value, cancellationToken);
})
.WithMessage(ProviderRemovedValidationMessage)
.When(model => model.Ukprn.HasValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SFA.DAS.PR.Application.EmployerRelationships.Queries.GetProviderRelationships;
using SFA.DAS.PR.Domain.Common;
using SFA.DAS.PR.Domain.Entities;

namespace SFA.DAS.PR.Application.Permissions.Queries.GetEmployerRelationships;
Expand All @@ -20,7 +21,7 @@ public class AccountLegalEntityPermissionsModel
PublicHashedId = source.PublicHashedId,
Name = source.Name,
AccountId = source.AccountId,
Permissions = source.AccountProviderLegalEntities.Select(a => (ProviderPermissionsModel)a).ToList(),
Permissions = source.AccountProviderLegalEntities.Where(a => a.AccountProvider.Provider.Status != ProviderStatus.Removed).Select(a => (ProviderPermissionsModel)a).ToList(),
Requests = GetRequests(source)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task<ValidatedResponse<GetEmployerRelationshipsQueryResult>> Handle
{
Account? account = await employerRelationshipsReadRepository.GetRelationships(query.AccountId, cancellationToken);

if(account is null)
if (account is null)
{
return new ValidatedResponse<GetEmployerRelationshipsQueryResult>(new GetEmployerRelationshipsQueryResult());
}
Expand Down
Comment thread
raviudari01 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SFA.DAS.PR.Data.Repositories;
using SFA.DAS.PR.Data.UnitTests.InMemoryDatabases;
using SFA.DAS.PR.Data.UnitTests.Setup;
using SFA.DAS.PR.Domain.Common;
using SFA.DAS.PR.Domain.Entities;

namespace SFA.DAS.PR.Data.UnitTests.Repositories;
Expand Down Expand Up @@ -48,4 +49,48 @@ public async Task ProviderExists_Returns_False()

Assert.That(result, Is.False, $"result should be false");
}

[Test]
public async Task WhenProviderIsRemoved_ThenReturnsTrue()
{
bool result = false;

Provider provider = ProviderTestData.Create(10000001);
provider.Status = ProviderStatus.Removed;

using (var context = InMemoryProviderRelationshipsDataContext.CreateInMemoryContext(
$"{nameof(InMemoryProviderRelationshipsDataContext)}_{nameof(WhenProviderIsRemoved_ThenReturnsTrue)}"))
{
await context.AddAsync(provider);
await context.SaveChangesAsync(cancellationToken);

ProviderReadRepository sut = new(context);

result = await sut.ProviderRemoved(10000001, cancellationToken);
}

Assert.That(result, Is.True, "result should be true");
}

[Test]
public async Task WhenProviderIsNotRemoved_ThenReturnsFalse()
{
bool result = true;

Provider provider = ProviderTestData.Create(10000001);
provider.Status = null;

using (var context = InMemoryProviderRelationshipsDataContext.CreateInMemoryContext(
$"{nameof(InMemoryProviderRelationshipsDataContext)}_{nameof(WhenProviderIsNotRemoved_ThenReturnsFalse)}"))
{
await context.AddAsync(provider);
await context.SaveChangesAsync(cancellationToken);

ProviderReadRepository sut = new(context);

result = await sut.ProviderRemoved(10000001, cancellationToken);
}

Assert.That(result, Is.False, "result should be false");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SFA.DAS.PR.Domain.Entities;
using System.Diagnostics.CodeAnalysis;

namespace SFA.DAS.PR.Data.EntityConfiguration;

Expand All @@ -11,5 +11,10 @@ public class ProviderConfiguration : IEntityTypeConfiguration<Provider>
public void Configure(EntityTypeBuilder<Provider> builder)
{
builder.HasKey(p => p.Ukprn);

builder.Property(x => x.Status)
.HasConversion<string>()
.HasMaxLength(50)
.IsRequired(false);
}
}
8 changes: 8 additions & 0 deletions src/SFA.DAS.PR.Data/Repositories/ProviderReadRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using SFA.DAS.PR.Domain.Common;
using SFA.DAS.PR.Domain.Interfaces;

namespace SFA.DAS.PR.Data.Repositories;
Expand All @@ -11,4 +12,11 @@ public async Task<bool> ProviderExists(long ukprn, CancellationToken cancellatio
.AsNoTracking()
.AnyAsync(a => a.Ukprn == ukprn, cancellationToken);
}

public async Task<bool> ProviderRemoved(long ukprn, CancellationToken cancellationToken)
{
return await _providerRelationshipsDataContext.Providers
.AsNoTracking()
.AnyAsync(a => a.Ukprn == ukprn && a.Status == ProviderStatus.Removed, cancellationToken);
}
}
6 changes: 6 additions & 0 deletions src/SFA.DAS.PR.Domain/Common/ProviderStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SFA.DAS.PR.Domain.Common;

public enum ProviderStatus
{
Removed
}
5 changes: 4 additions & 1 deletion src/SFA.DAS.PR.Domain/Entities/Provider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace SFA.DAS.PR.Domain.Entities;
using SFA.DAS.PR.Domain.Common;

namespace SFA.DAS.PR.Domain.Entities;

public class Provider
{
public long Ukprn { get; set; }
public string Name { get; set; } = null!;
public DateTime Created { get; set; }
public DateTime? Updated { get; set; }
public ProviderStatus? Status { get; set; }

public virtual List<AccountProvider> AccountProviders { get; set; } = new();
public virtual List<Request> Requests { get; set; } = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface IProviderReadRepository
{
Task<bool> ProviderExists(long ukprn, CancellationToken cancellationToken);
Task<bool> ProviderRemoved(long ukprn, CancellationToken cancellationToken);
}
Loading