From 4a3f5d529fdd3179f1647def3e9f9d5085a09ca8 Mon Sep 17 00:00:00 2001 From: AnuragNagpure Date: Wed, 14 Feb 2024 17:57:34 +0530 Subject: [PATCH] Unit test cases added --- .../BusinessLogic/CompanyDataBusinessLogic.cs | 6 +- .../Controllers/CompanyDataController.cs | 34 ++++----- .../CompanyCertificateRepository.cs | 2 +- .../CompanyDataBusinessLogicTests.cs | 76 +++++++++++++++++++ .../CompanyCertificateRepositoryTests.cs | 46 +++++++++++ 5 files changed, 145 insertions(+), 19 deletions(-) diff --git a/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs b/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs index 33bd0fe3df..490b286ea0 100644 --- a/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs +++ b/src/administration/Administration.Service/BusinessLogic/CompanyDataBusinessLogic.cs @@ -622,12 +622,16 @@ public async Task> 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(); 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); } diff --git a/src/administration/Administration.Service/Controllers/CompanyDataController.cs b/src/administration/Administration.Service/Controllers/CompanyDataController.cs index a918658824..7f2a8021c4 100644 --- a/src/administration/Administration.Service/Controllers/CompanyDataController.cs +++ b/src/administration/Administration.Service/Controllers/CompanyDataController.cs @@ -293,6 +293,23 @@ public async Task CreateCompanyCertificate([FromForm] CompanyCe return NoContent(); } + /// + /// Gets the companyCertificates Details + /// + /// the companyCertificates details + /// Example: GET: api/administration/companydata/businessPartnerNumber}/companyCertificates + /// Returns the companyCertificates details. + [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> GetCompanyCertificatesBpn(string businessPartnerNumber) => + _logic.GetCompanyCertificatesBpnOthers(businessPartnerNumber); + /// /// Gets all outstanding, existing and inactive credentials /// @@ -365,21 +382,4 @@ public async Task RejectCredential([FromRoute] Guid credentialI await _logic.RejectCredential(credentialId).ConfigureAwait(false); return NoContent(); } - - /// - /// Gets the companyCertificates Details - /// - /// the companyCertificates details - /// Example: GET: api/administration/companydata/businessPartnerNumber}/companyCertificates - /// Returns the companyCertificates details. - [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 GetCompanyCertificatesBpn([FromQuery] string businessPartnerNumber) => - await _logic.GetCompanyCertificatesBpnOthers(businessPartnerNumber); } diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs index 8ffbcd1f4b..95f55a8dff 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/CompanyCertificateRepository.cs @@ -59,7 +59,7 @@ public CompanyCertificate CreateCompanyCertificate(Guid companyId, CompanyCertif /// public async Task> 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, diff --git a/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs b/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs index 09bebe92d6..8ffcdfa0cf 100644 --- a/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs +++ b/tests/administration/Administration.Service.Tests/BusinessLogic/CompanyDataBusinessLogicTests.cs @@ -1070,6 +1070,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(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(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() + .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] diff --git a/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs b/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs index 27ef55f21e..9f18117c3c 100644 --- a/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs +++ b/tests/portalbackend/PortalBackend.DBAccess.Tests/CompanyCertificateRepositoryTests.cs @@ -83,6 +83,52 @@ public async Task CreateCompanyCertificateData_WithValidData_ReturnsExpected() } #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(); + 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 CreateSut() {