Skip to content

Commit

Permalink
Unit test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuragNagpure authored and Phil91 committed Feb 17, 2024
1 parent f2b2874 commit 69c65be
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,16 @@ public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificates
{
if (string.IsNullOrWhiteSpace(businessPartnerNumber))
{
throw new ControllerArgumentException("{businessPartnerNumber} must not be empty", "businessPartnerNumber");
throw new ControllerArgumentException("businessPartnerNumber must not be empty");
}

var companyCertificateRepository = _portalRepositories.GetInstance<ICompanyCertificateRepository>();

var companyId = await companyCertificateRepository.GetCompanyId(businessPartnerNumber).ConfigureAwait(false);
if (companyId == null)
{
throw new ControllerArgumentException($"company does not exist for {businessPartnerNumber}");
}

return await companyCertificateRepository.GetCompanyCertificateData(companyId.Id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,23 @@ public async Task<NoContentResult> CreateCompanyCertificate([FromForm] CompanyCe
return NoContent();
}

/// <summary>
/// Gets the companyCertificates Details
/// </summary>
/// <returns>the companyCertificates details</returns>
/// <remarks>Example: GET: api/administration/companydata/businessPartnerNumber}/companyCertificates</remarks>
/// <response code="200">Returns the companyCertificates details.</response>
[HttpGet]
[Authorize(Roles = "view_certificates")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Route("company/{businessPartnerNumber}/companyCertificates")]
[ProducesResponseType(typeof(CompanyRoleConsentViewData), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status409Conflict)]
public Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificatesBpn(string businessPartnerNumber) =>
_logic.GetCompanyCertificatesBpnOthers(businessPartnerNumber);

/// <summary>
/// Retrieves all company certificates with respect userId.
/// </summary>
Expand Down Expand Up @@ -386,21 +403,4 @@ public async Task<NoContentResult> RejectCredential([FromRoute] Guid credentialI
await _logic.RejectCredential(credentialId).ConfigureAwait(false);
return NoContent();
}

/// <summary>
/// Gets the companyCertificates Details
/// </summary>
/// <returns>the companyCertificates details</returns>
/// <remarks>Example: GET: api/administration/companydata/businessPartnerNumber}/companyCertificates</remarks>
/// <response code="200">Returns the companyCertificates details.</response>
[HttpGet]
[Authorize(Roles = "view_certificates")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Route("{businessPartnerNumber}/companyCertificates")]
[ProducesResponseType(typeof(CompanyRoleConsentViewData), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status409Conflict)]
public async IEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesBpn([FromQuery] string businessPartnerNumber) =>
await _logic.GetCompanyCertificatesBpnOthers(businessPartnerNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertif
/// <inheritdoc />
public async Task<IEnumerable<CompanyCertificateBpnData>> GetCompanyCertificateData(Guid companyId) =>
_context.CompanyCertificates
.Where(x => x.CompanyId == companyId)
.Where(x => x.CompanyId == companyId && x.CompanyCertificateStatusId == CompanyCertificateStatusId.ACTIVE)
.Select(ccb => new CompanyCertificateBpnData(
ccb.CompanyCertificateTypeId,
ccb.CompanyCertificateStatusId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,82 @@ public async Task CheckCompanyCertificateType_WithInvalidCall_ThrowsControllerAr
}
#endregion

#region GetCompanyCertificateWithBpnNumber

[Fact]
public async Task GetCompanyCertificateWithNullOrEmptyBpn_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();

// Act
async Task Act() => await _sut.GetCompanyCertificatesBpnOthers(string.Empty).ConfigureAwait(false);

// Assert
var error = await Assert.ThrowsAsync<ControllerArgumentException>(Act).ConfigureAwait(false);
error.Message.Should().StartWith("businessPartnerNumber must not be empty");
}

[Fact]
public async Task GetCompanyCertificateWithNoCompanyId_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();
var businessPartnerNumber = "BPNL07800HZ01644";
Company company = null;

A.CallTo(() => _companyCertificateRepository.GetCompanyId(businessPartnerNumber))
.Returns(company);

// Act
async Task Act() => await _sut.GetCompanyCertificatesBpnOthers(businessPartnerNumber).ConfigureAwait(false);

// Assert
var error = await Assert.ThrowsAsync<ControllerArgumentException>(Act).ConfigureAwait(false);
error.Message.Should().StartWith($"company does not exist for {businessPartnerNumber}");
}

[Fact]
public async Task GetCompanyCertificateWithBpnNumber_WithValidRequest_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();
var data = _fixture.Build<CompanyCertificateBpnData>()
.With(x => x.companyCertificateStatus, CompanyCertificateStatusId.ACTIVE)
.With(x => x.companyCertificateType, CompanyCertificateTypeId.ISO_9001)
.With(x => x.documentId, Guid.NewGuid())
.With(x => x.validFrom, DateTime.UtcNow)
.With(x => x.validTill, DateTime.UtcNow)
.CreateMany(5).AsEnumerable();
A.CallTo(() => _companyCertificateRepository.GetCompanyId("BPNL07800HZ01643"))
.Returns(new Company(companyId, "test", CompanyStatusId.ACTIVE, DateTime.UtcNow));
A.CallTo(() => _companyCertificateRepository.GetCompanyCertificateData(companyId))
.Returns(data);

// Act
var result = await _sut.GetCompanyCertificatesBpnOthers("BPNL07800HZ01643").ConfigureAwait(false);

// Assert
result.Should().HaveCount(5);
}

[Fact]
public async Task GetCompanyCertificateWithBpnNumber_WithEmptyResult_ReturnsExpected()
{
// Arrange
var companyId = Guid.NewGuid();
A.CallTo(() => _companyCertificateRepository.GetCompanyId("BPNL07800HZ01643"))
.Returns(new Company(companyId, "test", CompanyStatusId.ACTIVE, DateTime.UtcNow));

// Act
var result = await _sut.GetCompanyCertificatesBpnOthers("BPNL07800HZ01643").ConfigureAwait(false);

// Assert
result.Should().BeEmpty();
}

#endregion

#region GetCredentials

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Tests.Setup;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Tests;
Expand Down Expand Up @@ -80,6 +81,7 @@ public async Task CreateCompanyCertificateData_WithValidData_ReturnsExpected()
.Which.Entity.Should().BeOfType<CompanyCertificate>()
.Which.CompanyCertificateStatusId.Should().Be(CompanyCertificateStatusId.ACTIVE);
}

#endregion

#region GetAllCertificateData
Expand All @@ -103,6 +105,7 @@ public async Task GetAllCertificates_ReturnsExpectedResult(CertificateSorting so
{
companyCertificateDetail.Data.Select(data => data.companyCertificateType).Should().BeInAscendingOrder();
}

if (sorting == CertificateSorting.CertificateTypeDesc)
{
companyCertificateDetail.Data.Select(data => data.companyCertificateType).Should().BeInDescendingOrder();
Expand Down Expand Up @@ -136,7 +139,53 @@ public async Task GetAllCertificates_WithExistingCompanyCertificateAndCertificat

#endregion

#region GetCompanyCertificatesBpn

[Fact]
public async Task GetCompanyId_WithExistingData()
{
// Arrange
var sut = await CreateSut();

// Act
var result = await sut.GetCompanyId("BPNL07800HZ01643").ConfigureAwait(false);

// Assert
result.Should().NotBeNull();
result.Should().BeOfType<Company>();
result.Id.Should().Be("3390c2d7-75c1-4169-aa27-6ce00e1f3cdd");
}

[Fact]
public async Task GetCompanyId_WithNoExistingData()
{
// Arrange
var sut = await CreateSut();

// Act
var result = await sut.GetCompanyId("BPNL07800HZ01644").ConfigureAwait(false);

// Assert
result.Should().BeNull();
}

[Fact]
public async Task GetCompanyCertificateData_NoResults_ReturnsExpected()
{
// Arrange
var sut = await CreateSut();

// Act
var result = await sut.GetCompanyCertificateData(Guid.NewGuid()).ConfigureAwait(false);

// Assert
result.Should().BeEmpty();
}

#endregion

#region Setup

private async Task<CompanyCertificateRepository> CreateSut()
{
var context = await _dbTestDbFixture.GetPortalDbContext().ConfigureAwait(false);
Expand Down

0 comments on commit 69c65be

Please sign in to comment.