Skip to content

Commit

Permalink
Merge branch 'main' into feature/dp-931-list-informal-consortium
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmverma authored Dec 13, 2024
2 parents 1b8b840 + 2b37b70 commit f7314ba
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ namespace CO.CDP.OrganisationApp.Tests.Pages;

public class OrganisationOverviewTest
{
private readonly Mock<IOrganisationClient> _organisationClientMock;
private readonly Mock<IOrganisationClient> _organisationClientMock;
private readonly Mock<EntityVerificationClient.IPponClient> _pponClient = new();
private readonly OrganisationOverviewModel _model;

public OrganisationOverviewTest()
{
_organisationClientMock = new Mock<IOrganisationClient>();
_model = new OrganisationOverviewModel(_organisationClientMock.Object);
_model = new OrganisationOverviewModel(_organisationClientMock.Object, _pponClient.Object);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class OrganisationDetailsSummaryModelTest
{
private readonly Mock<ISession> sessionMock;
private readonly Mock<IOrganisationClient> organisationClientMock;
private readonly Mock<EntityVerificationClient.IPponClient> pponClient;
private static readonly Guid _organisationId = Guid.NewGuid();

public OrganisationDetailsSummaryModelTest()
Expand All @@ -25,14 +26,15 @@ public OrganisationDetailsSummaryModelTest()
sessionMock.Setup(session => session.Get<UserDetails>(Session.UserDetailsKey))
.Returns(new UserDetails { UserUrn = "urn:test" });
organisationClientMock = new Mock<IOrganisationClient>();
pponClient = new Mock<EntityVerificationClient.IPponClient>();
}

[Fact]
public void OnGet_ValidSession_ReturnsRegistrationDetails()
public async Task OnGet_ValidSession_ReturnsRegistrationDetails()
{
var model = GivenOrganisationDetailModel();

model.OnGet();
await model.OnGet();

model.RegistrationDetails.As<RegistrationDetails>().Should().NotBeNull();
}
Expand Down Expand Up @@ -232,6 +234,6 @@ private OrganisationDetailsSummaryModel GivenOrganisationDetailModel()
sessionMock.Setup(s => s.Get<RegistrationDetails>(Session.RegistrationDetailsKey))
.Returns(registrationDetails);

return new OrganisationDetailsSummaryModel(sessionMock.Object, organisationClientMock.Object);
return new OrganisationDetailsSummaryModel(sessionMock.Object, organisationClientMock.Object, pponClient.Object);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CO.CDP.EntityVerificationClient;
using System.Collections.Generic;

namespace CO.CDP.OrganisationApp.Constants;
Expand Down Expand Up @@ -35,18 +36,36 @@ public static class OrganisationSchemeType
{ Ppon, "Ppon" }
};

public static string? SchemeDescription(this string? scheme)
public static string? SchemeDescription(this string? scheme, ICollection<IdentifierRegistries>? registriesDetails = null)
{
if (string.IsNullOrWhiteSpace(scheme))
return null;

OrganisationScheme.TryGetValue(scheme, out var value);

if (string.IsNullOrEmpty(value))
if (OrganisationScheme.TryGetValue(scheme, out var value) && !string.IsNullOrEmpty(value))
{
return value;
}

if (registriesDetails != null)
{
value = "Other";
var registryMatch = registriesDetails.FirstOrDefault(r => r.Scheme?.Equals(scheme, StringComparison.OrdinalIgnoreCase) == true);
if (registryMatch != null)
{

if (!string.IsNullOrWhiteSpace(registryMatch.Countrycode) && Constants.Country.NonUKCountries.TryGetValue(registryMatch.Countrycode, out var countryName))
{
return $"{countryName} - {registryMatch.RegisterName}";
}

return $"{registryMatch.RegisterName}";
}
var schemeCountryCode = scheme.Contains("-") ? scheme.Split('-')[0] : scheme;
if (!string.IsNullOrWhiteSpace(schemeCountryCode) && Constants.Country.NonUKCountries.TryGetValue(schemeCountryCode, out var countryNameforOthers))
{
return $"{countryNameforOthers} - Other";
}
}

return value;
return "Other";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@
}
else
{
@organisationDetails.Identifier.Scheme.SchemeDescription()
@organisationDetails.Identifier.Scheme.SchemeDescription(Model.IdentifierRegistriesDetails)
<br />
@organisationDetails.Identifier.Id
}
</li>
@foreach (var aidentifier in organisationDetails.AdditionalIdentifiersToShow())
{
<li>
@aidentifier.Scheme.SchemeDescription()
@aidentifier.Scheme.SchemeDescription(Model.IdentifierRegistriesDetails)
<br />
@aidentifier.Id
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using CO.CDP.EntityVerificationClient;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Constants;
using CO.CDP.OrganisationApp.WebApiClients;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OrganisationApiException = CO.CDP.Organisation.WebApiClient.ApiException;
using DevolvedRegulation = CO.CDP.OrganisationApp.Constants.DevolvedRegulation;
using OrganisationWebApiClient = CO.CDP.Organisation.WebApiClient;
using EntityVerificationApiException = CO.CDP.EntityVerificationClient.ApiException;

namespace CO.CDP.OrganisationApp.Pages.Organisation;

[Authorize(Policy = OrgScopeRequirement.Viewer)]
public class OrganisationOverviewModel(IOrganisationClient organisationClient) : PageModel
public class OrganisationOverviewModel(IOrganisationClient organisationClient, IPponClient pponClient) : PageModel
{
public OrganisationWebApiClient.Organisation? OrganisationDetails { get; set; }

public ICollection<IdentifierRegistries>? IdentifierRegistriesDetails { get; set; }

public BuyerInformation? BuyerInformation { get; set; }

public List<DevolvedRegulation>? Regulations { get; set; }
Expand All @@ -28,6 +33,8 @@ public async Task<IActionResult> OnGet()
{
OrganisationDetails = await organisationClient.GetOrganisationAsync(Id);

IdentifierRegistriesDetails = await GetIdentifierDetails(OrganisationDetails);

if (OrganisationDetails.IsBuyer() || OrganisationDetails.IsPendingBuyer())
{
BuyerInformation = await organisationClient.GetOrganisationBuyerInformationAsync(OrganisationDetails.Id);
Expand All @@ -43,9 +50,33 @@ public async Task<IActionResult> OnGet()
}
return Page();
}
catch (ApiException ex) when (ex.StatusCode == 404)
catch (OrganisationApiException ex) when (ex.StatusCode == 404)
{
return Redirect("/page-not-found");
}
}

private async Task<ICollection<IdentifierRegistries>> GetIdentifierDetails(OrganisationWebApiClient.Organisation organisationDetails)
{
var identifiers = organisationDetails.AdditionalIdentifiers?.ToList()
?? new List<OrganisationWebApiClient.Identifier>();

if (organisationDetails.Identifier != null)
{
identifiers.Add(organisationDetails.Identifier);
}

var schemes = identifiers
.Where(x => x != null)
.Select(x => x.Scheme)
.ToArray();
try
{
return await pponClient.GetIdentifierRegistriesDetailAsync(schemes);
}
catch
{
return new List<IdentifierRegistries>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Organisation identifier
</dt>
<dd class="govuk-summary-list__value">
@details.OrganisationScheme.SchemeDescription()
@details.OrganisationScheme.SchemeDescription(Model.IdentifierRegistriesDetails)
@if (details.OrganisationScheme != "Other")
{
<br />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CO.CDP.EntityVerificationClient;
using CO.CDP.OrganisationApp.Constants;
using CO.CDP.OrganisationApp.Models;
using CO.CDP.OrganisationApp.WebApiClients;
Expand All @@ -9,14 +10,18 @@ namespace CO.CDP.OrganisationApp.Pages.Registration;
[ValidateRegistrationStep]
public class OrganisationDetailsSummaryModel(
ISession session,
OrganisationWebApiClient.IOrganisationClient organisationClient) : RegistrationStepModel(session)
OrganisationWebApiClient.IOrganisationClient organisationClient,
IPponClient pponClient) : RegistrationStepModel(session)
{
public override string CurrentPage => OrganisationSummaryPage;

public string? Error { get; set; }

public void OnGet()
public ICollection<IdentifierRegistries>? IdentifierRegistriesDetails { get; set; }

public async Task OnGet()
{
IdentifierRegistriesDetails = await GetIdentifierDetails();
}

public async Task<IActionResult> OnPost()
Expand Down Expand Up @@ -98,4 +103,17 @@ public async Task<IActionResult> OnPost()
roles: [details.OrganisationType!.Value.AsPartyRole()]
);
}
private async Task<ICollection<IdentifierRegistries>> GetIdentifierDetails()
{
var scheme = RegistrationDetails.OrganisationScheme;

try
{
return await pponClient.GetIdentifierRegistriesDetailAsync(new[] { scheme });
}
catch (EntityVerificationClient.ApiException ex) when (ex.StatusCode == 404)
{
return new List<IdentifierRegistries>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,53 @@ public async Task ItFindsregistriesByCountryCode()
found.Select(r => r.Scheme).Should().BeEquivalentTo(new[] { "Scheme1", "Scheme2" });
}

[Fact]
public async Task ItFindsregistriesBySchemeCodes()
{
await using var context = postgreSql.EntityVerificationContext();
using var repository = PponRepository(context);

var schemeCodes = new[] { "SCHEME4", "SCHEME5" };

var identifierRegistries = new List<IdentifierRegistries>
{
new IdentifierRegistries
{
CountryCode = "FR",
Scheme = "Scheme4",
RegisterName = "FR Registry1",
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
},
new IdentifierRegistries
{
CountryCode = "FR",
Scheme = "Scheme5",
RegisterName = "FR Registry2",
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
},
new IdentifierRegistries
{
CountryCode = "CA",
Scheme = "Scheme6",
RegisterName = "CA Registry3",
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
}
}.AsQueryable();

await context.AddRangeAsync(identifierRegistries);
await context.SaveChangesAsync();

var found = await repository.GetIdentifierRegistriesNameAsync(schemeCodes);

found.Should().NotBeNull();
found.Should().HaveCount(2);
found.Should().OnlyContain(r => r.CountryCode == "FR");
found.Select(r => r.Scheme).Should().BeEquivalentTo(new[] { "Scheme4", "Scheme5" });
}

[Fact]
public void ItRejectsAnAlreadyKnownPponId()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using CO.CDP.EntityVerification.Persistence;
using CO.CDP.EntityVerification.Tests.Persistence;
using CO.CDP.EntityVerification.UseCase;
using CO.CDP.OrganisationInformation.Persistence;
using CO.CDP.Testcontainers.PostgreSql;
using FluentAssertions;
using Moq;
using static CO.CDP.EntityVerification.UseCase.GetIdentifierRegistriesUseCase;
using static CO.CDP.EntityVerification.UseCase.GetIdentifierRegistriesUseCase.GetIdentifierRegistriesException;

namespace CO.CDP.EntityVerification.Tests.UseCase;

using Moq;
using Xunit;

public class GetIdentifierRegistriesDetailsUseCaseTest
{
private readonly Mock<IPponRepository> _repository;
private readonly GetIdentifierRegistriesDetailsUseCase _useCase;

public GetIdentifierRegistriesDetailsUseCaseTest()
{
_repository = new Mock<IPponRepository>();

_useCase = new GetIdentifierRegistriesDetailsUseCase(_repository.Object);
}

[Fact]
public async Task Execute_ShouldReturnIdentifiers_WhenRepositoryReturnsData()
{
var schemecodes = new string[] { "scheme1", "scheme2" };
var rawIdentifiers = new[]
{
new IdentifierRegistries { Id = 1, CountryCode = "US", Scheme = "scheme1", RegisterName = "Register1", CreatedOn = DateTimeOffset.Now, UpdatedOn = DateTimeOffset.Now },
new IdentifierRegistries { Id = 2, CountryCode = "FR", Scheme = "scheme2", RegisterName = "Register2", CreatedOn = DateTimeOffset.Now, UpdatedOn = DateTimeOffset.Now }
};

_repository
.Setup(repo => repo.GetIdentifierRegistriesNameAsync(schemecodes))
.ReturnsAsync(rawIdentifiers);

var result = await _useCase.Execute(schemecodes);

result.Should().NotBeNull();
result.Should().HaveCount(2);

result.ElementAt(0).Countrycode.Should().Be("US");
result.ElementAt(0).Scheme.Should().Be("scheme1");
result.ElementAt(0).RegisterName.Should().Be("Register1");

result.ElementAt(1).Countrycode.Should().Be("FR");
result.ElementAt(1).Scheme.Should().Be("scheme2");
result.ElementAt(1).RegisterName.Should().Be("Register2");
}

[Fact]
public async Task Execute_ShouldReturnEmpty_WhenRepositoryReturnsNoData()
{
var schemecodes = new string[] { "scheme1", "scheme2" };
_repository
.Setup(repo => repo.GetIdentifierRegistriesNameAsync(schemecodes))
.ReturnsAsync(Enumerable.Empty<IdentifierRegistries>());

var result = await _useCase.Execute(schemecodes);

result.Should().BeEmpty();
}

[Fact]
public async Task Execute_ShouldThrowException_WhenRepositoryThrowsException()
{
var schemecodes = new string[] { "scheme1", "scheme2" };
_repository
.Setup(repo => repo.GetIdentifierRegistriesNameAsync(schemecodes))
.ThrowsAsync(new System.Exception("Database error"));

await Assert.ThrowsAsync<System.Exception>(() => _useCase.Execute(schemecodes));
}

}
Loading

0 comments on commit f7314ba

Please sign in to comment.