Skip to content

Commit

Permalink
tests: add unit tests for sd creation process
Browse files Browse the repository at this point in the history
Refs: #813
  • Loading branch information
Phil91 authored and ntruchsess committed Aug 6, 2024
1 parent 59d5d59 commit 25c536e
Show file tree
Hide file tree
Showing 33 changed files with 1,036 additions and 69 deletions.
15 changes: 15 additions & 0 deletions src/Portal.Backend.sln
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserProvisioning.Executor.T
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SelfDescriptionCreation.Executor", "processes\SelfDescriptionCreation.Executor\SelfDescriptionCreation.Executor.csproj", "{08535520-F6E6-4DAF-AB36-12C48437368A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SelfDescriptionCreation.Executor.Tests", "..\tests\processes\SelfDescriptionCreation.Executor.Tests\SelfDescriptionCreation.Executor.Tests.csproj", "{E87898BA-59A4-4F02-9594-50756BBF4E22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1808,6 +1810,18 @@ Global
{08535520-F6E6-4DAF-AB36-12C48437368A}.Release|x64.Build.0 = Release|Any CPU
{08535520-F6E6-4DAF-AB36-12C48437368A}.Release|x86.ActiveCfg = Release|Any CPU
{08535520-F6E6-4DAF-AB36-12C48437368A}.Release|x86.Build.0 = Release|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Debug|x64.ActiveCfg = Debug|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Debug|x64.Build.0 = Debug|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Debug|x86.ActiveCfg = Debug|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Debug|x86.Build.0 = Debug|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Release|Any CPU.Build.0 = Release|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Release|x64.ActiveCfg = Release|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Release|x64.Build.0 = Release|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Release|x86.ActiveCfg = Release|Any CPU
{E87898BA-59A4-4F02-9594-50756BBF4E22}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1943,5 +1957,6 @@ Global
{9636BEC8-6929-4852-8DC8-8B41609630A3} = {282CEF03-292F-4A49-83C6-997567D0FF5F}
{E5494227-BDFE-41F2-A12F-54292D76C29F} = {282CEF03-292F-4A49-83C6-997567D0FF5F}
{08535520-F6E6-4DAF-AB36-12C48437368A} = {282CEF03-292F-4A49-83C6-997567D0FF5F}
{E87898BA-59A4-4F02-9594-50756BBF4E22} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,18 @@ public async Task<int> DeleteCompanyCertificateAsync(Guid documentId)

return (documentDetails.FileName, documentDetails.Content, documentDetails.MediaTypeId.MapToMediaType());
}

public async Task TriggerSelfDescriptionCreation()
{
var hasMissingSdDocumentCompanies = await portalRepositories.GetInstance<ICompanyRepository>().HasAnyCompaniesWithMissingSelfDescription().ConfigureAwait(ConfigureAwaitOptions.None);
if (hasMissingSdDocumentCompanies)
{
var processStepRepository = portalRepositories.GetInstance<IProcessStepRepository>();
var processId = processStepRepository.CreateProcess(ProcessTypeId.SELF_DESCRIPTION_CREATION).Id;

processStepRepository.CreateProcessStep(ProcessStepTypeId.SELF_DESCRIPTION_COMPANY_CREATION, ProcessStepStatusId.TODO, processId);

await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ private async Task<Guid> CreateConnectorInternalAsync(ConnectorInputModel connec
throw UnexpectedConditionException.Create(AdministrationConnectorErrors.CONNECTOR_UNEXPECTED_NO_BPN_ASSIGNED, new ErrorParameter[] { new("companyId", companyId.ToString()) });
}

if (result.SelfDescriptionDocumentId is null)
{
throw UnexpectedConditionException.Create(AdministrationConnectorErrors.CONNECTOR_UNEXPECTED_NO_DESCRIPTION, new ErrorParameter[] { new("companyId", companyId.ToString()) });
}

await ValidateTechnicalUser(technicalUserId, companyId).ConfigureAwait(ConfigureAwaitOptions.None);

var connectorRequestModel = new ConnectorRequestModel(name, connectorUrl, ConnectorTypeId.COMPANY_CONNECTOR, location, companyId, companyId, technicalUserId);
Expand Down Expand Up @@ -435,4 +430,17 @@ public IAsyncEnumerable<OfferSubscriptionConnectorData> GetConnectorOfferSubscri
size,
_settings.MaxPageSize,
portalRepositories.GetInstance<IConnectorsRepository>().GetConnectorsWithMissingSdDocument());

public async Task TriggerSelfDescriptionCreation()
{
var hasMissingSdDocumentConnectors = await portalRepositories.GetInstance<IConnectorsRepository>().HasAnyConnectorsWithMissingSelfDescription().ConfigureAwait(ConfigureAwaitOptions.None);
if (hasMissingSdDocumentConnectors)
{
var processStepRepository = portalRepositories.GetInstance<IProcessStepRepository>();
var processId = processStepRepository.CreateProcess(ProcessTypeId.SELF_DESCRIPTION_CREATION).Id;
processStepRepository.CreateProcessStep(ProcessStepTypeId.SELF_DESCRIPTION_CONNECTOR_CREATION, ProcessStepStatusId.TODO, processId);

await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public interface ICompanyDataBusinessLogic
Task<Pagination.Response<CompanyCertificateData>> GetAllCompanyCertificatesAsync(int page, int size, CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType);
Task<DimUrlsResponse> GetDimServiceUrls();
Task<Pagination.Response<CompanyMissingSdDocumentData>> GetCompaniesWithMissingSdDocument(int page, int size);
Task TriggerSelfDescriptionCreation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ public interface IConnectorsBusinessLogic
IAsyncEnumerable<OfferSubscriptionConnectorData> GetConnectorOfferSubscriptionData(bool? connectorIdSet);

Task<Pagination.Response<ConnectorMissingSdDocumentData>> GetConnectorsWithMissingSdDocument(int page, int size);

Task TriggerSelfDescriptionCreation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,4 @@ public interface IRegistrationBusinessLogic
/// </summary>
/// <param name="processId">Id of the Process</param>
Task RetriggerDeleteCentralUser(Guid processId);

Task TriggerSelfDescriptionCreation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private async Task UpdateCompanyBpnInternal(Guid applicationId, string bpn)
/// <inheritdoc />
public async Task ProcessClearinghouseResponseAsync(ClearinghouseResponseData data, CancellationToken cancellationToken)
{
logger.LogInformation("Process SelfDescription called with the following data {Data}", data);
logger.LogInformation("Process SelfDescription called with the following data {Data}", data.ToString().Replace(Environment.NewLine, string.Empty));
var result = await portalRepositories.GetInstance<IApplicationRepository>().GetSubmittedApplicationIdsByBpn(data.BusinessPartnerNumber.ToUpper()).ToListAsync(cancellationToken).ConfigureAwait(false);
if (!result.Any())
{
Expand All @@ -358,7 +358,7 @@ public async Task ProcessClearinghouseResponseAsync(ClearinghouseResponseData da
/// <inheritdoc />
public async Task ProcessDimResponseAsync(string bpn, DimWalletData data, CancellationToken cancellationToken)
{
logger.LogInformation("Process Dim called with the following data {Data}", data);
logger.LogInformation("Process Dim called with the following data {Data}", data.ToString().Replace(Environment.NewLine, string.Empty));

await dimBusinessLogic.ProcessDimResponse(bpn, data, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
Expand Down Expand Up @@ -435,7 +435,7 @@ private async Task TriggerChecklistInternal(Guid applicationId, ApplicationCheck
/// <inheritdoc />
public async Task ProcessClearinghouseSelfDescription(SelfDescriptionResponseData data, CancellationToken cancellationToken)
{
logger.LogInformation("Process SelfDescription called with the following data {Data}", data);
logger.LogInformation("Process SelfDescription called with the following data {Data}", data.ToString().Replace(Environment.NewLine, string.Empty));
var isExistingCompany = await portalRepositories.GetInstance<ICompanyRepository>().IsExistingCompany(data.ExternalId);

if (isExistingCompany)
Expand Down Expand Up @@ -701,27 +701,4 @@ private async Task RetriggerProcessStepInternal(Guid processId, ProcessStepTypeI
context.FinalizeProcessStep();
await portalRepositories.SaveAsync().ConfigureAwait(false);
}

public async Task TriggerSelfDescriptionCreation()
{
var hasMissingSdDocumentCompanies = await portalRepositories.GetInstance<ICompanyRepository>().HasAnyCompaniesWithMissingSelfDescription().ConfigureAwait(ConfigureAwaitOptions.None);
var hasMissingSdDocumentConnectors = await portalRepositories.GetInstance<IConnectorsRepository>().HasAnyConnectorsWithMissingSelfDescription().ConfigureAwait(ConfigureAwaitOptions.None);
if (hasMissingSdDocumentCompanies || hasMissingSdDocumentConnectors)
{
var processStepRepository = portalRepositories.GetInstance<IProcessStepRepository>();
var processId = processStepRepository.CreateProcess(ProcessTypeId.SELF_DESCRIPTION_CREATION).Id;

if (hasMissingSdDocumentCompanies)
{
processStepRepository.CreateProcessStep(ProcessStepTypeId.SELF_DESCRIPTION_COMPANY_CREATION, ProcessStepStatusId.TODO, processId);
}

if (hasMissingSdDocumentConnectors)
{
processStepRepository.CreateProcessStep(ProcessStepTypeId.SELF_DESCRIPTION_CONNECTOR_CREATION, ProcessStepStatusId.TODO, processId);
}

await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,24 @@ public Task<DimUrlsResponse> GetDimServiceUrls() =>
[ProducesResponseType(typeof(Pagination.Response<CompanyMissingSdDocumentData>), StatusCodes.Status200OK)]
public Task<Pagination.Response<CompanyMissingSdDocumentData>> GetCompaniesWithMissingSdDocument([FromQuery] int page = 0, [FromQuery] int size = 15) =>
logic.GetCompaniesWithMissingSdDocument(page, size);

/// <summary>
/// Triggers the process to create the missing self description documents
/// </summary>
/// <returns>NoContent</returns>
/// Example: POST: /api/administration/companyData/trigger-self-description
/// <response code="204">Empty response on success.</response>
/// <response code="404">No Process found for the processId</response>
[HttpPost]
[Authorize(Roles = "approve_new_partner")]
[Authorize(Policy = PolicyTypes.CompanyUser)]
[Route("trigger-self-description")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
public async Task<NoContentResult> TriggerSelfDescriptionProcess()
{
await logic.TriggerSelfDescriptionCreation().ConfigureAwait(false);
return NoContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,24 @@ public IAsyncEnumerable<OfferSubscriptionConnectorData> GetConnectorOfferSubscri
[ProducesResponseType(typeof(Pagination.Response<ConnectorMissingSdDocumentData>), StatusCodes.Status200OK)]
public Task<Pagination.Response<ConnectorMissingSdDocumentData>> GetConnectorsWithMissingSdDocument([FromQuery] int page = 0, [FromQuery] int size = 15) =>
_businessLogic.GetConnectorsWithMissingSdDocument(page, size);

/// <summary>
/// Triggers the process to create the missing self description documents
/// </summary>
/// <returns>NoContent</returns>
/// Example: POST: /api/administration/connectors/trigger-self-description
/// <response code="204">Empty response on success.</response>
/// <response code="404">No Process found for the processId</response>
[HttpPost]
[Authorize(Roles = "approve_new_partner")]
[Authorize(Policy = PolicyTypes.CompanyUser)]
[Route("trigger-self-description")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
public async Task<NoContentResult> TriggerSelfDescriptionProcess()
{
await _businessLogic.TriggerSelfDescriptionCreation().ConfigureAwait(false);
return NoContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -581,24 +581,4 @@ public async Task<NoContentResult> RetriggerDeleteCentralUser([FromRoute] Guid p
[ProducesResponseType(typeof(Pagination.Response<CompanyDetailsOspOnboarding>), StatusCodes.Status200OK)]
public Task<Pagination.Response<CompanyDetailsOspOnboarding>> GetOspCompanyDetailsAsync([FromQuery] int page, [FromQuery] int size, [FromQuery] CompanyApplicationStatusFilter? companyApplicationStatusFilter = null, [FromQuery] string? companyName = null) =>
_logic.GetOspCompanyDetailsAsync(page, size, companyApplicationStatusFilter, companyName);

/// <summary>
/// Triggers the process to create the missing self description documents
/// </summary>
/// <returns>NoContent</returns>
/// Example: POST: api/administration/registration/trigger-self-description
/// <response code="204">Empty response on success.</response>
/// <response code="404">No Process found for the processId</response>
[HttpPost]
[Authorize(Roles = "approve_new_partner")]
[Authorize(Policy = PolicyTypes.CompanyUser)]
[Route("trigger-self-description")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
public async Task<NoContentResult> TriggerSelfDescriptionProcess()
{
await _logic.TriggerSelfDescriptionCreation().ConfigureAwait(false);
return NoContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using Org.Eclipse.TractusX.Portal.Backend.Processes.ApplicationChecklist.Library;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.BusinessLogic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

using Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Processes.ApplicationChecklist.Library;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.BusinessLogic;

Expand Down
3 changes: 3 additions & 0 deletions src/externalsystems/Custodian.Library/CustodianService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Org.Eclipse.TractusX.Portal.Backend.Custodian.Library;

Expand Down
2 changes: 2 additions & 0 deletions src/externalsystems/Custodian.Library/ICustodianService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
using System.Threading;
using System.Threading.Tasks;

namespace Org.Eclipse.TractusX.Portal.Backend.Custodian.Library;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using System;
using System.Text.Json.Serialization;

namespace Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using System;
using System.Text.Json.Serialization;

namespace Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;

public record CompanyMissingSdDocumentData(
Guid Id,
Guid CompanyId,
string Name
);
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ public record ConnectorOfferSubscription(Guid AssignedOfferSubscriptionIds, Offe

public record TechnicalUserData(Guid Id, string Name, string? ClientId, string Description);

public record ConnectorMissingSdDocumentData(Guid Id, string Name, Guid CompanyId, string CompanyName);
public record ConnectorMissingSdDocumentData(Guid ConnectorId, string Name, Guid CompanyId, string CompanyName);
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,15 @@ public void RemoveProviderCompanyDetails(Guid providerCompanyDetailId) =>
skip,
take,
context.Companies.AsNoTracking()
.Where(x => x.CompanyStatusId == CompanyStatusId.ACTIVE && x.SelfDescriptionDocumentId == null)
.GroupBy(c => c.AddressId),
.Where(c =>
c.CompanyStatusId == CompanyStatusId.ACTIVE &&
c.SelfDescriptionDocumentId == null &&
c.CompanyApplications.Any(ca =>
ca.ApplicationChecklistEntries.Any(a =>
a.ApplicationChecklistEntryTypeId == ApplicationChecklistEntryTypeId.SELF_DESCRIPTION_LP &&
a.ApplicationChecklistEntryStatusId != ApplicationChecklistEntryStatusId.TO_DO &&
a.ApplicationChecklistEntryStatusId != ApplicationChecklistEntryStatusId.IN_PROGRESS)))
.GroupBy(c => c.CompanyStatusId),
c => c.OrderByDescending(company => company.Name),
c => new CompanyMissingSdDocumentData(
c.Id,
Expand All @@ -427,6 +434,7 @@ public void RemoveProviderCompanyDetails(Guid providerCompanyDetailId) =>

public Task<bool> HasAnyCompaniesWithMissingSelfDescription() =>
context.Companies.AnyAsync(c =>
c.CompanyStatusId == CompanyStatusId.ACTIVE &&
c.SelfDescriptionDocumentId == null &&
c.CompanyApplications.Any(ca =>
ca.ApplicationChecklistEntries.Any(a =>
Expand All @@ -436,6 +444,7 @@ public Task<bool> HasAnyCompaniesWithMissingSelfDescription() =>

public IAsyncEnumerable<(Guid Id, IEnumerable<(UniqueIdentifierId Id, string Value)> UniqueIdentifiers, string? BusinessPartnerNumber, string CountryCode)> GetCompaniesWithMissingSelfDescription() =>
context.Companies.Where(c =>
c.CompanyStatusId == CompanyStatusId.ACTIVE &&
c.SelfDescriptionDocumentId == null &&
c.CompanyApplications.Any(ca =>
ca.ApplicationChecklistEntries.Any(a =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void DeleteConnectorAssignedSubscriptions(Guid connectorId, IEnumerable<G
take,
_context.Connectors.AsNoTracking()
.Where(x => x.StatusId == ConnectorStatusId.ACTIVE && x.SelfDescriptionDocumentId == null)
.GroupBy(c => c.ProviderId),
.GroupBy(c => c.StatusId),
connector => connector.OrderByDescending(c => c.Name),
con => new ConnectorMissingSdDocumentData(
con.Id,
Expand All @@ -225,7 +225,7 @@ public Task<bool> HasAnyConnectorsWithMissingSelfDescription() =>

public IAsyncEnumerable<(Guid Id, string? BusinessPartnerNumber, Guid? SelfDescriptionDocumentId)> GetConnectorsWithMissingSelfDescription() =>
_context.Connectors
.Where(c => c.SelfDescriptionDocumentId == null)
.Where(c => c.StatusId == ConnectorStatusId.ACTIVE && c.SelfDescriptionDocumentId == null)
.Select(c => new ValueTuple<Guid, string?, Guid?>(c.Id, c.Provider!.BusinessPartnerNumber, c.Provider.SelfDescriptionDocumentId))
.ToAsyncEnumerable();
}
Loading

0 comments on commit 25c536e

Please sign in to comment.