Skip to content

Commit

Permalink
feat(get): get company certificate types (#490)
Browse files Browse the repository at this point in the history
* Business logic for getting company certificate types added
* test cases added for GetCertificateTypes endpoint
* tests: adjust inivitation test
Refs: #469
---------
Co-authored-by: Phil Schneider <[email protected]>
Co-authored-by: Norbert Truchsess <[email protected]>
  • Loading branch information
AnuragNagpure authored Feb 9, 2024
1 parent 569a535 commit cd77d86
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ public interface IStaticDataBusinessLogic
/// </summary>
/// <returns>A list of bpns</returns>
IAsyncEnumerable<OperatorBpnData> GetOperatorBpns();

/// <summary>
/// Get all company certificates
/// </summary>
/// <returns>A list of company certificates</returns>
IAsyncEnumerable<CompanyCertificateTypeData> GetCertificateTypes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public IAsyncEnumerable<LicenseTypeData> GetAllLicenseType() =>
/// <inheritdoc />
public IAsyncEnumerable<OperatorBpnData> GetOperatorBpns() =>
_portalRepositories.GetInstance<ICompanyRepository>().GetOperatorBpns();

/// <inheritdoc />
public IAsyncEnumerable<CompanyCertificateTypeData> GetCertificateTypes() =>
_portalRepositories.GetInstance<IStaticDataRepository>().GetCertificateTypes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,19 @@ public IAsyncEnumerable<LicenseTypeData> GetLicenseTypes() =>
[ProducesResponseType(typeof(IAsyncEnumerable<OperatorBpnData>), StatusCodes.Status200OK)]
public IAsyncEnumerable<OperatorBpnData> GetOperatorBpns() =>
_logic.GetOperatorBpns();

/// <summary>
/// Retrieve all certificate types
/// </summary>
/// <returns>AsyncEnumerable of certificate types</returns>
/// <remarks>
/// Example: GET: /api/administration/staticdata/certificateTypes
/// </remarks>
/// <response code="200">Returns a list of all of the Language i.e german and english</response>
[HttpGet]
[Authorize(Roles = "view_certificates")]
[Route("certificateTypes")]
[ProducesResponseType(typeof(IEnumerable<CompanyCertificateTypeData>), StatusCodes.Status200OK)]
public IAsyncEnumerable<CompanyCertificateTypeData> GetCertificateTypes() =>
_logic.GetCertificateTypes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using System.Text.Json.Serialization;

namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;

/// <summary>
/// Model for CompanyCertificateTypeData
/// </summary>]
/// <param name="CompanyCertificateTypeId">CompanyCertificate Type Id</param>
/// <param name="Description">description</param>
/// <returns></returns>
public record CompanyCertificateTypeData(
[property: JsonPropertyName("certificateType")] CompanyCertificateTypeId CompanyCertificateTypeId,
IEnumerable<CertificateTypeDescription> Description);

/// <summary>
/// Model for CertificateTypeDescription
/// </summary>
/// <param name="LanguageShortName">language</param>
/// <param name="Description">long Description</param>
/// <returns></returns>
public record CertificateTypeDescription(string LanguageShortName, string Description);
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public interface IStaticDataRepository
/// <returns>AsyncEnumerable of the result Counties with long names</returns>
IAsyncEnumerable<CountryLongNameData> GetAllCountries();

/// <summary>
/// Get all Certificate Types
/// </summary>
/// <returns>AsyncEnumerable of the result Company Certificates with Long Description</returns>
IAsyncEnumerable<CompanyCertificateTypeData> GetCertificateTypes();

/// <summary>
/// Retrieve Unique Identifier Data for Country Alpha2Code
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ public IAsyncEnumerable<LicenseTypeData> GetLicenseTypeData() =>
_dbContext.LicenseTypes.AsNoTracking()
.Select(licenseType => new LicenseTypeData((int)licenseType.Id, licenseType.Label))
.AsAsyncEnumerable();

///<inheritdoc />
public IAsyncEnumerable<CompanyCertificateTypeData> GetCertificateTypes() =>
_dbContext.CompanyCertificateTypes.AsNoTracking()
.Where(type => type.CompanyCertificateTypeAssignedStatus!.CompanyCertificateTypeStatusId == CompanyCertificateTypeStatusId.ACTIVE)
.Select(type => new CompanyCertificateTypeData
(
type.Id,
type.CompanyCertificateTypeDescriptions
.Select(x => new CertificateTypeDescription(x.LanguageShortName, x.Description))
))
.AsAsyncEnumerable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CompanyCertificateStatus
private CompanyCertificateStatus()
{
Label = null!;
CompanyCertificates = new HashSet<CompanyCertificate>();
}

public CompanyCertificateStatus(CompanyCertificateStatusId certificateStatusId) : this()
Expand All @@ -39,4 +40,8 @@ public CompanyCertificateStatus(CompanyCertificateStatusId certificateStatusId)

[MaxLength(255)]
public string Label { get; private set; }

// Navigation Properties

public virtual ICollection<CompanyCertificate> CompanyCertificates { get; private set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private CompanyCertificateType()
{
Label = null!;
CompanyCertificates = new HashSet<CompanyCertificate>();
CompanyCertificateTypeDescriptions = new HashSet<CompanyCertificateTypeDescription>();
}

public CompanyCertificateType(CompanyCertificateTypeId companyCertificateTypeId) : this()
Expand All @@ -45,4 +46,5 @@ public CompanyCertificateType(CompanyCertificateTypeId companyCertificateTypeId)

public virtual CompanyCertificateTypeAssignedStatus? CompanyCertificateTypeAssignedStatus { get; set; }
public virtual ICollection<CompanyCertificate> CompanyCertificates { get; private set; }
public virtual ICollection<CompanyCertificateTypeDescription> CompanyCertificateTypeDescriptions { get; private set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CompanyCertificateTypeStatus
private CompanyCertificateTypeStatus()
{
Label = null!;
CompanyCertificateTypeAssignedStatuses = new HashSet<CompanyCertificateTypeAssignedStatus>();
}

public CompanyCertificateTypeStatus(CompanyCertificateTypeStatusId companyCertificateTypeStatusId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public InvitationBusinessLogicTests()
_mailingService = A.Fake<IMailingService>();
_options = A.Fake<IOptions<InvitationSettings>>();

_companyName = _fixture.Create<string>();
_companyName = "testCompany";
_idpName = _fixture.Create<string>();
_companyId = _fixture.Create<Guid>();
_identityProviderId = _fixture.Create<Guid>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ public async Task GetAllLanguages_ReturnsExpectedResult()
result.Should().HaveCount(3).And.ContainInOrder(data);
}

[Fact]
public async Task GetCertificateTypes_ReturnsExpectedResult()
{
// Arrange
var data = _fixture.CreateMany<CompanyCertificateTypeData>(12).ToImmutableArray();

A.CallTo(() => _staticDataRepository.GetCertificateTypes())
.Returns(data.ToAsyncEnumerable());

var sut = new StaticDataBusinessLogic(_portalRepositories);

// Act
var result = await sut.GetCertificateTypes().ToListAsync().ConfigureAwait(false);

// Assert
A.CallTo(() => _staticDataRepository.GetCertificateTypes())
.MustHaveHappenedOnceExactly();
result.Should().HaveCount(12).And.ContainInOrder(data);
}

[Fact]
public async Task GetOperatorBpns_ReturnsExpectedResult()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,20 @@ public async Task GetOperatorBpns_ReturnsExpectedResult()
A.CallTo(() => _logic.GetOperatorBpns()).MustHaveHappenedOnceExactly();
result.Should().HaveCount(5).And.AllBeOfType<OperatorBpnData>();
}

[Fact]
public async Task GetCertificateTypes_ReturnsExpectedResult()
{
//Arrange
var data = _fixture.CreateMany<CompanyCertificateTypeData>(5).ToAsyncEnumerable();
A.CallTo(() => _logic.GetCertificateTypes())
.Returns(data);

//Act
var result = await _controller.GetCertificateTypes().ToListAsync().ConfigureAwait(false);

// Assert
A.CallTo(() => _logic.GetCertificateTypes()).MustHaveHappenedOnceExactly();
result.Should().HaveCount(5).And.AllBeOfType<CompanyCertificateTypeData>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
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.Enums;
using Xunit.Extensions.AssemblyFixture;

Expand Down Expand Up @@ -156,6 +157,50 @@ public async Task GetAllLanguages_ReturnsExpected()

#endregion

#region GetCertificateTypes

[Fact]
public async Task GetCertificateTypes_ReturnsExpectedResult()
{
// Arrange
var sut = await CreateSut().ConfigureAwait(false);

// Act
var results = await sut.GetCertificateTypes().ToListAsync().ConfigureAwait(false);

// Assert
results.Should().HaveCount(12);
}

[Fact]
public async Task GetCertificateTypes_WithInactiveCertificateType_ReturnsExpectedResult()
{
// Arrange
var (context, sut) = await CreateSutWithContext().ConfigureAwait(false);
var active = new CompanyCertificateTypeAssignedStatus(CompanyCertificateTypeId.ISO_15504_SPICE, CompanyCertificateTypeStatusId.ACTIVE);
var inactive = new CompanyCertificateTypeAssignedStatus(CompanyCertificateTypeId.ISO_15504_SPICE, CompanyCertificateTypeStatusId.INACTVIE);
context.Remove(active);
context.Add(inactive);
await context.SaveChangesAsync().ConfigureAwait(false);

try
{
// Act
var results = await sut.GetCertificateTypes().ToListAsync().ConfigureAwait(false);

// Assert
results.Should().HaveCount(11);
}
finally
{
context.Remove(inactive);
context.Add(active);
await context.SaveChangesAsync().ConfigureAwait(false);
}
}

#endregion

#region setup

private async Task<StaticDataRepository> CreateSut()
Expand All @@ -165,5 +210,12 @@ private async Task<StaticDataRepository> CreateSut()
return sut;
}

private async Task<(PortalDbContext, StaticDataRepository)> CreateSutWithContext()
{
var context = await _dbTestDbFixture.GetPortalDbContext().ConfigureAwait(false);
var sut = new StaticDataRepository(context);
return (context, sut);
}

#endregion
}

0 comments on commit cd77d86

Please sign in to comment.