Skip to content

Commit

Permalink
fix(bpdm): fix bpdm push
Browse files Browse the repository at this point in the history
* set sharing state to ready
* ignore nullable values for bpdm push

Refs: eclipse-tractusx#450
  • Loading branch information
Phil91 committed Jan 29, 2024
1 parent 4cfb46f commit b7bc0f2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 24 deletions.
20 changes: 16 additions & 4 deletions src/externalsystems/Bpdm.Library/BpdmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
Expand All @@ -39,7 +40,8 @@ public class BpdmService : IBpdmService
Converters =
{
new JsonStringEnumConverter(allowIntegerValues: false),
}
},
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

public BpdmService(ITokenService tokenService, IOptions<BpdmServiceSettings> options)
Expand Down Expand Up @@ -111,17 +113,27 @@ public async Task<bool> PutInputLegalEntity(BpdmTransferData data, CancellationT
)
};

await httpClient.PutAsJsonAsync("/companies/test-company/api/catena/input/legal-entities", requestData, Options, cancellationToken)
await httpClient.PutAsJsonAsync("/companies/test-company/api/catena/input/business-partners", requestData, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-put-legal-entities", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
return true;
}

public async Task<bool> SetSharingStateToReady(string externalId, CancellationToken cancellationToken)
{
var httpClient = await _tokenService.GetAuthorizedClient<BpdmService>(_settings, cancellationToken).ConfigureAwait(false);

var content = new { externalIds = Enumerable.Repeat(externalId, 1) };
await httpClient.PutAsJsonAsync("/companies/test-company/api/catena/sharing-state/ready", content, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-put-sharing-state-ready", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
return true;
}

public async Task<BpdmLegalEntityOutputData> FetchInputLegalEntity(string externalId, CancellationToken cancellationToken)
{
var httpClient = await _tokenService.GetAuthorizedClient<BpdmService>(_settings, cancellationToken).ConfigureAwait(false);

var data = Enumerable.Repeat(externalId, 1);
var result = await httpClient.PostAsJsonAsync("/companies/test-company/api/catena/output/legal-entities/search", data, Options, cancellationToken)
var result = await httpClient.PostAsJsonAsync("/companies/test-company/api/catena/output/business-partners/search", data, Options, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-search-legal-entities", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
try
{
Expand All @@ -145,7 +157,7 @@ public async Task<BpdmSharingState> GetSharingState(Guid applicationId, Cancella
{
var httpClient = await _tokenService.GetAuthorizedClient<BpdmService>(_settings, cancellationToken).ConfigureAwait(false);

var url = $"/companies/test-company/api/catena/sharing-state?externalIds={applicationId}&businessPartnerType={BpdmSharingStateBusinessPartnerType.LEGAL_ENTITY}";
var url = $"/companies/test-company/api/catena/sharing-state?externalIds={applicationId}";
var result = await httpClient.GetAsync(url, cancellationToken)
.CatchingIntoServiceExceptionFor("bpdm-sharing-state", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE).ConfigureAwait(false);
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public BpdmBusinessLogic(IPortalRepositories portalRepositories, IBpdmService bp
data.Identifiers);

await _bpdmService.PutInputLegalEntity(bpdmTransferData, cancellationToken).ConfigureAwait(false);
await _bpdmService.SetSharingStateToReady(context.ApplicationId.ToString(), cancellationToken).ConfigureAwait(false);

return new IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult(
ProcessStepStatusId.DONE,
Expand Down Expand Up @@ -132,7 +133,7 @@ await HandlePullLegalEntityInternal(context, result.CompanyId, result.BpdmData,
var legalEntity = await _bpdmService.FetchInputLegalEntity(context.ApplicationId.ToString(), cancellationToken)
.ConfigureAwait(false);

if (string.IsNullOrEmpty(legalEntity.Bpn))
if (string.IsNullOrEmpty(legalEntity.LegalEntity?.Bpnl))
{
return new IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult(ProcessStepStatusId.TODO, null, null, null, false, null);
}
Expand All @@ -148,7 +149,7 @@ await HandlePullLegalEntityInternal(context, result.CompanyId, result.BpdmData,
},
company =>
{
company.BusinessPartnerNumber = legalEntity.Bpn;
company.BusinessPartnerNumber = legalEntity.LegalEntity?.Bpnl;
});

var registrationValidationFailed = context.Checklist[ApplicationChecklistEntryTypeId.REGISTRATION_VERIFICATION] == ApplicationChecklistEntryStatusId.FAILED;
Expand Down
8 changes: 8 additions & 0 deletions src/externalsystems/Bpdm.Library/IBpdmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public interface IBpdmService
/// <param name="cancellationToken">Cancellation Token</param>
/// <returns>Returns <c>true</c> if the service call was successful, otherwise <c>false</c></returns>
Task<bool> PutInputLegalEntity(BpdmTransferData data, CancellationToken cancellationToken);

/// <summary>
/// Sets the sharing state for the external id to ready
/// </summary>
/// <param name="externalId">The external id</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <returns>bool if successful</returns>
Task<bool> SetSharingStateToReady(string externalId, CancellationToken cancellationToken);
Task<BpdmLegalEntityOutputData> FetchInputLegalEntity(string externalId, CancellationToken cancellationToken);
Task<BpdmSharingState> GetSharingState(Guid applicationId, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,30 @@ public record PageOutputResponseBpdmLegalEntityData(

public record BpdmLegalEntityOutputData(
[property: JsonPropertyName("externalId")] string? ExternalId,
[property: JsonPropertyName("bpnl")] string? Bpn,
[property: JsonPropertyName("legalShortName")] string? LegalShortName,
[property: JsonPropertyName("legalForm")] string? LegalForm,
[property: JsonPropertyName("nameParts")] IEnumerable<string> NameParts,
[property: JsonPropertyName("identifiers")] IEnumerable<BpdmIdentifier> Identifiers,
[property: JsonPropertyName("states")] IEnumerable<BpdmStatus> States,
[property: JsonPropertyName("classifications")] IEnumerable<BpdmProfileClassification> Classifications,
[property: JsonPropertyName("legalNameParts")] IEnumerable<string> LegalNameParts,
[property: JsonPropertyName("roles")] IEnumerable<string> Roles,
[property: JsonPropertyName("legalAddress")] BpdmLegalAddressResponse LegalAddress
[property: JsonPropertyName("isOwnCompanyData")] bool IsOwnCompanyData,
[property: JsonPropertyName("legalEntity")] BpdmLegelEntityData? LegalEntity,
[property: JsonPropertyName("site")] BpdmSite? Site,
[property: JsonPropertyName("address")] BpdmLegalAddressResponse Address
);

public record BpdmLegelEntityData(
[property: JsonPropertyName("legalEntityBpn")] string? Bpnl,
[property: JsonPropertyName("legalName")] string? LegalName,
[property: JsonPropertyName("shortName")] string? ShortName,
[property: JsonPropertyName("legalForm")] string? LegalForm,
[property: JsonPropertyName("classifications")] IEnumerable<BpdmProfileClassification> Classifications
);

public record BpdmLegalAddressResponse(
[property: JsonPropertyName("externalId")] string ExternalId,
[property: JsonPropertyName("legalEntityExternalId")] string LegalEntityExternalId,
[property: JsonPropertyName("siteExternalId")] string SiteExternalId,
[property: JsonPropertyName("bpna")] string Bpn,
[property: JsonPropertyName("nameParts")] IEnumerable<string> NameParts,
[property: JsonPropertyName("states")] IEnumerable<BpdmAddressState> States,
[property: JsonPropertyName("identifiers")] IEnumerable<BpdmAddressIdentifier> Identifiers,
[property: JsonPropertyName("addressBpn")] string? Bpna,
[property: JsonPropertyName("name")] string? Name,
[property: JsonPropertyName("addressType")] string? AddressType,
[property: JsonPropertyName("physicalPostalAddress")] BpdmAddressPhysicalPostalAddress? PhysicalPostalAddress,
[property: JsonPropertyName("alternativePostalAddress")] BpdmAddressAlternativePostalAddress? AlternativePostalAddress,
[property: JsonPropertyName("roles")] IEnumerable<string> Roles
[property: JsonPropertyName("alternativePostalAddress")] BpdmAddressAlternativePostalAddress? AlternativePostalAddress
);

public record BpdmStatus(
Expand Down
4 changes: 2 additions & 2 deletions tests/externalsystems/Bpdm.Library/BpdmBusinessLogicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,9 @@ private void SetupForHandlePullLegalEntity(Company? company = null)
A.CallTo(() => _bpdmService.FetchInputLegalEntity(A<string>.That.Matches(x => x == IdWithStateCreated.ToString()), A<CancellationToken>._))
.ThrowsAsync(new ServiceException("not found", System.Net.HttpStatusCode.NotFound));
A.CallTo(() => _bpdmService.FetchInputLegalEntity(A<string>.That.Matches(x => x == IdWithoutZipCode.ToString()), A<CancellationToken>._))
.Returns(_fixture.Build<BpdmLegalEntityOutputData>().With(x => x.Bpn, (string?)null).Create());
.Returns(_fixture.Build<BpdmLegalEntityOutputData>().With(x => x.LegalEntity, (BpdmLegelEntityData?)null).Create());
A.CallTo(() => _bpdmService.FetchInputLegalEntity(A<string>.That.Matches(x => x == IdWithBpn.ToString()), A<CancellationToken>._))
.Returns(_fixture.Build<BpdmLegalEntityOutputData>().With(x => x.Bpn, "CAXSDUMMYCATENAZZ").Create());
.Returns(_fixture.Build<BpdmLegalEntityOutputData>().With(x => x.LegalEntity, new BpdmLegelEntityData("CAXSDUMMYCATENAZZ", null, null, null, Enumerable.Empty<BpdmProfileClassification>())).Create());
A.CallTo(() => _bpdmService.GetSharingState(A<Guid>.That.Matches(x => x == IdWithBpn || x == IdWithStateCreated || x == IdWithoutZipCode || x == IdWithoutSharingProcessStarted), A<CancellationToken>._))
.Returns(_fixture.Build<BpdmSharingState>()
.With(x => x.SharingStateType, BpdmSharingStateType.Success)
Expand Down
48 changes: 47 additions & 1 deletion tests/externalsystems/Bpdm.Library/BpdmServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,52 @@ public async Task PutInputLegalEntity_WithInvalidData_ThrowsServiceException()

#endregion

#region Trigger SetSharingStateToReady

[Fact]
public async Task SetSharingStateToReady_WithValidData_DoesNotThrowException()
{
// Arrange
var externalId = Guid.NewGuid().ToString();
var httpMessageHandlerMock = new HttpMessageHandlerMock(HttpStatusCode.OK);
var httpClient = new HttpClient(httpMessageHandlerMock)
{
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<BpdmService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new BpdmService(_tokenService, _options);

// Act
var result = await sut.SetSharingStateToReady(externalId, CancellationToken.None).ConfigureAwait(false);

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

[Fact]
public async Task SetSharingStateToReady_WithInvalidData_ThrowsServiceException()
{
// Arrange
var externalId = Guid.NewGuid().ToString();
var httpMessageHandlerMock = new HttpMessageHandlerMock(HttpStatusCode.BadRequest);
var httpClient = new HttpClient(httpMessageHandlerMock)
{
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<BpdmService>(_options.Value, A<CancellationToken>._)).Returns(httpClient);
var sut = new BpdmService(_tokenService, _options);

// Act
async Task Act() => await sut.SetSharingStateToReady(externalId, CancellationToken.None).ConfigureAwait(false);

// Assert
var ex = await Assert.ThrowsAsync<ServiceException>(Act);
ex.Message.Should().Be("call to external system bpdm-put-sharing-state-ready failed with statuscode 400");
}

#endregion

#region FetchInputLegalEntity

[Fact]
Expand Down Expand Up @@ -192,7 +238,7 @@ public async Task FetchInputLegalEntity_WithValidResult_ReturnsExpected()
// Assert
result.Should().NotBeNull();
result.ExternalId.Should().Be(externalId);
result.Bpn.Should().Be("BPNL00000007QGTF");
result.LegalEntity?.Bpnl.Should().Be("BPNL00000007QGTF");
}

[Fact]
Expand Down

0 comments on commit b7bc0f2

Please sign in to comment.