Skip to content

Commit

Permalink
feat(connector): connector controller change url added (#1094)
Browse files Browse the repository at this point in the history
Reviewed-by: Phil Schneider <[email protected]>
Refs: #549
  • Loading branch information
AnuragNagpure authored Nov 5, 2024
1 parent c12e016 commit 6103bde
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,16 +384,18 @@ public async Task ProcessClearinghouseSelfDescription(SelfDescriptionResponseDat
}

/// <inheritdoc />
public Task UpdateConnectorUrl(Guid connectorId, ConnectorUpdateRequest data)
public Task UpdateConnectorUrl(Guid connectorId, ConnectorUpdateRequest data, CancellationToken cancellationToken)
{
data.ConnectorUrl.EnsureValidHttpUrl(() => nameof(data.ConnectorUrl));
return UpdateConnectorUrlInternal(connectorId, data);
return UpdateConnectorUrlInternal(connectorId, data, cancellationToken);
}

private async Task UpdateConnectorUrlInternal(Guid connectorId, ConnectorUpdateRequest data)
private async Task UpdateConnectorUrlInternal(Guid connectorId, ConnectorUpdateRequest data, CancellationToken cancellationToken)
{
var connectorsRepository = portalRepositories
.GetInstance<IConnectorsRepository>();
var documentRepository = portalRepositories
.GetInstance<IDocumentRepository>();
var connector = await connectorsRepository
.GetConnectorUpdateInformation(connectorId, _identityData.CompanyId)
.ConfigureAwait(ConfigureAwaitOptions.None);
Expand Down Expand Up @@ -433,6 +435,24 @@ private async Task UpdateConnectorUrlInternal(Guid connectorId, ConnectorUpdateR
con.ConnectorUrl = data.ConnectorUrl;
});

if (connector.SelfDescriptionDocumentId != null)
{
documentRepository.AttachAndModifyDocument(connector.SelfDescriptionDocumentId.Value, null, doc =>
{
doc.DocumentStatusId = DocumentStatusId.INACTIVE;
});
}

if (connector.SelfDescriptionCompanyDocumentId is null)
{
throw ConflictException.Create(AdministrationConnectorErrors.CONNECTOR_CONFLICT_NO_DESCRIPTION, [new("connectorId", connectorId.ToString())]);
}

var selfDescriptionDocumentUrl = $"{_settings.SelfDescriptionDocumentUrl}/{connector.SelfDescriptionCompanyDocumentId}";
await sdFactoryBusinessLogic
.RegisterConnectorAsync(connectorId, selfDescriptionDocumentUrl, bpn, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);

await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public interface IConnectorsBusinessLogic
/// </summary>
/// <param name="connectorId">Id of the connector</param>
/// <param name="data">Update data for the connector</param>
Task UpdateConnectorUrl(Guid connectorId, ConnectorUpdateRequest data);
/// <param name="cancellationToken">CancellationToken</param>
Task UpdateConnectorUrl(Guid connectorId, ConnectorUpdateRequest data, CancellationToken cancellationToken);

/// <summary>
/// Gets the offer subscription data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public async Task<NoContentResult> ProcessClearinghouseSelfDescription([FromBody
/// </summary>
/// <param name="connectorId" example="5636F9B9-C3DE-4BA5-8027-00D17A2FECFB">Id of the connector to trigger the daps call.</param>
/// <param name="data">The update data</param>
/// <param name="cancellationToken">CancellationToken</param>
/// <returns>NoContent Result.</returns>
/// <remarks>Example: PUT: /api/administration/connectors/{connectorId}/connectorUrl</remarks>
/// <response code="204">Url was successfully updated.</response>
Expand All @@ -224,9 +225,9 @@ public async Task<NoContentResult> ProcessClearinghouseSelfDescription([FromBody
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status503ServiceUnavailable)]
public async Task<NoContentResult> UpdateConnectorUrl([FromRoute] Guid connectorId, [FromBody] ConnectorUpdateRequest data)
public async Task<NoContentResult> UpdateConnectorUrl([FromRoute] Guid connectorId, [FromBody] ConnectorUpdateRequest data, CancellationToken cancellationToken)
{
await logic.UpdateConnectorUrl(connectorId, data)
await logic.UpdateConnectorUrl(connectorId, data, cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public record ConnectorUpdateInformation(
ConnectorTypeId Type,
bool IsHostCompany,
string ConnectorUrl,
string? Bpn);
string? Bpn,
Guid? SelfDescriptionDocumentId,
Guid? SelfDescriptionCompanyDocumentId);
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ public Connector AttachAndModifyConnector(Guid connectorId, Action<Connector>? i
c.TypeId,
c.HostId == companyId,
c.ConnectorUrl,
c.Provider!.BusinessPartnerNumber
))
c.Provider!.BusinessPartnerNumber,
c.SelfDescriptionDocumentId,
c.Host!.SelfDescriptionDocumentId))
.SingleOrDefaultAsync();

public void DeleteConnector(Guid connectorId) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ public async Task UpdateConnectorUrl_WithoutConnector_ThrowsNotFoundException()
.Returns<ConnectorUpdateInformation?>(null);

// Act
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://test.de"));
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://test.de"), CancellationToken.None);

// Assert
var ex = await Assert.ThrowsAsync<NotFoundException>(Act);
Expand All @@ -994,7 +994,7 @@ public async Task UpdateConnectorUrl_WithSameUrlAsStored_ReturnsWithoutDoing()
.Returns(data);

// Act
await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://test.de"));
await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://test.de"), CancellationToken.None);

// Assert
A.CallTo(() => _portalRepositories.SaveAsync()).MustNotHaveHappened();
Expand All @@ -1013,7 +1013,7 @@ public async Task UpdateConnectorUrl_WithUserNotOfHostCompany_ThrowsForbiddenExc
.Returns(data);

// Act
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"));
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"), CancellationToken.None);

// Assert
var ex = await Assert.ThrowsAsync<ForbiddenException>(Act);
Expand All @@ -1034,7 +1034,7 @@ public async Task UpdateConnectorUrl_WithInactiveConnector_ThrowsConflictExcepti
.Returns(data);

// Act
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"));
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"), CancellationToken.None);

// Assert
var ex = await Assert.ThrowsAsync<ConflictException>(Act);
Expand All @@ -1057,7 +1057,7 @@ public async Task UpdateConnectorUrl_WithBpnNotSet_ThrowsConflictException()
.Returns(data);

// Act
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"));
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"), CancellationToken.None);

// Assert
var ex = await Assert.ThrowsAsync<ConflictException>(Act);
Expand All @@ -1082,7 +1082,7 @@ public async Task UpdateConnectorUrl_WithCompanyBpnNotSet_ThrowsConflictExceptio
.Returns<string?>(null);

// Act
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"));
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"), CancellationToken.None);

// Assert
var ex = await Assert.ThrowsAsync<ConflictException>(Act);
Expand All @@ -1093,7 +1093,9 @@ public async Task UpdateConnectorUrl_WithCompanyBpnNotSet_ThrowsConflictExceptio
public async Task UpdateConnectorUrl_WithValidData_CallsExpected()
{
// Arrange
var documentId = Guid.NewGuid();
var connectorId = Guid.NewGuid();
var document = new Document(documentId, null!, null!, null!, default, default, DocumentStatusId.PENDING, default);
var connector = _fixture.Build<Connector>()
.With(x => x.ConnectorUrl, "https://old.de")
.Create();
Expand All @@ -1103,7 +1105,10 @@ public async Task UpdateConnectorUrl_WithValidData_CallsExpected()
.With(x => x.Status, ConnectorStatusId.ACTIVE)
.With(x => x.Type, ConnectorTypeId.CONNECTOR_AS_A_SERVICE)
.With(x => x.Bpn, "BPNL123456789")
.With(x => x.SelfDescriptionDocumentId, documentId)
.With(x => x.SelfDescriptionCompanyDocumentId, Guid.NewGuid())
.Create();

A.CallTo(() => _connectorsRepository.GetConnectorUpdateInformation(connectorId, _identity.CompanyId))
.Returns(data);
A.CallTo(() => _connectorsRepository.AttachAndModifyConnector(connectorId, null, A<Action<Connector>>._))
Expand All @@ -1112,14 +1117,48 @@ public async Task UpdateConnectorUrl_WithValidData_CallsExpected()
initialize?.Invoke(connector);
setOptionalProperties.Invoke(connector);
});
A.CallTo(() => _documentRepository.AttachAndModifyDocument(A<Guid>._, A<Action<Document>>._, A<Action<Document>>._))
.Invokes((Guid documentId, Action<Document>? initialize, Action<Document> modify)
=>
{
initialize?.Invoke(document);
modify(document);
});

// Act
await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"));
await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"), CancellationToken.None);

// Assert
A.CallTo(() => _portalRepositories.SaveAsync()).MustHaveHappenedOnceExactly();
A.CallTo(() => _connectorsRepository.AttachAndModifyConnector(connectorId, null, A<Action<Connector>>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => _documentRepository.AttachAndModifyDocument((Guid)data.SelfDescriptionDocumentId!, null, A<Action<Document>>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => _sdFactoryBusinessLogic.RegisterConnectorAsync(A<Guid>._, A<string>._, A<string>._, A<CancellationToken>._)).MustHaveHappenedOnceExactly();
connector.ConnectorUrl.Should().Be("https://new.de");
document.DocumentStatusId.Should().Be(DocumentStatusId.INACTIVE);
}

[Fact]
public async Task UpdateConnectorUrl_WithSelfDescriptionCompanyIdNotSet_ThrowsConflictException()
{
// Arrange
var connectorId = Guid.NewGuid();
var data = _fixture.Build<ConnectorUpdateInformation>()
.With(x => x.ConnectorUrl, "https://old.de")
.With(x => x.IsHostCompany, true)
.With(x => x.Status, ConnectorStatusId.ACTIVE)
.With(x => x.Type, ConnectorTypeId.CONNECTOR_AS_A_SERVICE)
.With(x => x.Bpn, "BPNL123456789")
.With(x => x.SelfDescriptionCompanyDocumentId, default(Guid?))
.Create();
A.CallTo(() => _connectorsRepository.GetConnectorUpdateInformation(connectorId, _identity.CompanyId))
.Returns(data);

// Act
async Task Act() => await _logic.UpdateConnectorUrl(connectorId, new ConnectorUpdateRequest("https://new.de"), CancellationToken.None);

// Assert
var ex = await Assert.ThrowsAsync<ConflictException>(Act);
ex.Message.Should().Be(AdministrationConnectorErrors.CONNECTOR_CONFLICT_NO_DESCRIPTION.ToString());
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ public async Task UpdateConnectorUrl_ReturnsExpectedResult()
var data = new ConnectorUpdateRequest("https://test.com");

// Act
var result = await _controller.UpdateConnectorUrl(connectorId, data);
var result = await _controller.UpdateConnectorUrl(connectorId, data, CancellationToken.None);

// Assert
A.CallTo(() => _logic.UpdateConnectorUrl(connectorId, data)).MustHaveHappenedOnceExactly();
A.CallTo(() => _logic.UpdateConnectorUrl(connectorId, data, CancellationToken.None)).MustHaveHappenedOnceExactly();
result.Should().BeOfType<NoContentResult>();
}

Expand Down

0 comments on commit 6103bde

Please sign in to comment.