Skip to content

Commit

Permalink
fix(custodian): log did response
Browse files Browse the repository at this point in the history
Refs: CPLP-3110
  • Loading branch information
Phil91 committed Aug 4, 2023
1 parent d73c9f2 commit 694b0c0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\framework\Framework.DateTimeProvider\Framework.DateTimeProvider.csproj" />
<ProjectReference Include="..\..\framework\Framework.HttpClient\Framework.HttpClient.csproj" />
<ProjectReference Include="..\..\framework\Framework.Logging\Framework.Logging.csproj" />
<ProjectReference Include="..\..\framework\Framework.Token\Framework.Token.csproj" />
Expand Down
7 changes: 5 additions & 2 deletions src/externalsystems/Custodian.Library/CustodianService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DateTimeProvider;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.HttpClientExtensions;
using Org.Eclipse.TractusX.Portal.Backend.Framework.IO;
Expand All @@ -36,11 +37,13 @@ public class CustodianService : ICustodianService
{
private static readonly JsonSerializerOptions Options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
private readonly ITokenService _tokenService;
private readonly IDateTimeProvider _dateTimeProvider;
private readonly CustodianSettings _settings;

public CustodianService(ITokenService tokenService, IOptions<CustodianSettings> settings)
public CustodianService(ITokenService tokenService, IDateTimeProvider dateTimeProvider, IOptions<CustodianSettings> settings)
{
_tokenService = tokenService;
_dateTimeProvider = dateTimeProvider;
_settings = settings.Value;
}

Expand Down Expand Up @@ -89,7 +92,7 @@ public async Task<string> CreateWalletAsync(string bpn, string name, Cancellatio
return "Service Response for custodian-post is null";
}

return JsonSerializer.Serialize(walletResponse, Options);
return JsonSerializer.Serialize(new WalletCreationLogData(walletResponse.Did, _dateTimeProvider.OffsetNow), Options);
}
catch (JsonException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public record WalletErrorResponse([property: JsonPropertyName("message")] string

public record MembershipErrorResponse([property: JsonPropertyName("title")] string Title);

public record WalletCreationResponse([property: JsonPropertyName("DID")] string Did, [property: JsonPropertyName("CreatedAt")] DateTimeOffset CreatedAt);
public record WalletCreationResponse([property: JsonPropertyName("did")] string Did);

public record WalletCreationLogData([property: JsonPropertyName("did")] string Did, [property: JsonPropertyName("createdAt")] DateTimeOffset CreatedAt);
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DateTimeProvider;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Token;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
Expand All @@ -41,6 +42,7 @@ public class CustodianServiceTests
private readonly ITokenService _tokenService;
private readonly IOptions<CustodianSettings> _options;
private readonly IFixture _fixture;
private readonly IDateTimeProvider _dateTimeProvider;

public CustodianServiceTests()
{
Expand All @@ -62,22 +64,24 @@ public CustodianServiceTests()
KeycloakTokenAddress = "https://key.cloak.com"
});
_tokenService = A.Fake<ITokenService>();
_dateTimeProvider = A.Fake<IDateTimeProvider>();
}

#endregion

#region Create Wallet

[Theory]
[InlineData("did:sov:GamAMqXnXr1chS4viYXoxB", true)]
[InlineData(null, false)]
public async Task CreateWallet_WithValidData_DoesNotThrowException(string DID, bool IsCreatedAt)
[Fact]
public async Task CreateWallet_WithValidData_DoesNotThrowException()
{
// Arrange
const string bpn = "123";
const string name = "test";
var data = JsonSerializer.Serialize(
new WalletCreationResponse(DID, IsCreatedAt ? DateTimeOffset.UtcNow : default), JsonOptions);
const string did = "did:sov:GamAMqXnXr1chS4viYXoxB";
var now = DateTimeOffset.UtcNow;
A.CallTo(() => _dateTimeProvider.OffsetNow)
.Returns(now);
var data = JsonSerializer.Serialize(new WalletCreationResponse(did), JsonOptions);
var httpMessageHandlerMock =
new HttpMessageHandlerMock(HttpStatusCode.OK, data.ToFormContent("application/json"));
var httpClient = new HttpClient(httpMessageHandlerMock)
Expand All @@ -86,14 +90,14 @@ public async Task CreateWallet_WithValidData_DoesNotThrowException(string DID, b
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
var result = await sut.CreateWalletAsync(bpn, name, CancellationToken.None).ConfigureAwait(false);

// Assert
result.Should().NotBeNull();
result.Should().Be(data);
result.Should().Be($"{{\"did\":\"{did}\",\"createdAt\":\"{now:O}\"}}");
}

[Theory]
Expand All @@ -116,7 +120,7 @@ public async Task CreateWallet_WithConflict_ThrowsServiceExceptionWithErrorConte
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._)).Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
async Task Act() => await sut.CreateWalletAsync(bpn, name, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -142,7 +146,7 @@ public async Task CreateWallet_withSuccessStatusCode_JsonException()
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
var result = await sut.CreateWalletAsync(bpn, name, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -174,7 +178,7 @@ public async Task GetWalletByBpnAsync_WithValidData_ReturnsWallets()
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
var result = await sut.GetWalletByBpnAsync(validBpn, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -203,7 +207,7 @@ public async Task GetWalletByBpnAsync_WithWalletDataNull_ThrowsServiceException(
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
async Task Act() => await sut.GetWalletByBpnAsync(validBpn, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -223,7 +227,7 @@ public async Task GetWalletByBpnAsync_WithInvalidData_ThrowsServiceException()
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._)).Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
async Task Act() => await sut.GetWalletByBpnAsync("invalidBpn", CancellationToken.None).ConfigureAwait(false);
Expand All @@ -249,7 +253,7 @@ public async Task SetMembership_WithValidData_DoesNotThrowException()
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
var result = await sut.SetMembership(bpn, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -270,7 +274,7 @@ public async Task SetMembership_WithConflict_DoesNotThrowException()
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
var result = await sut.SetMembership(bpn, CancellationToken.None).ConfigureAwait(false);
Expand All @@ -295,7 +299,7 @@ public async Task SetMembership_WithConflict_ThrowsServiceExceptionWithErrorCont
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._)).Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
async Task Act() => await sut.SetMembership(bpn, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -324,7 +328,7 @@ public async Task TriggerFramework_WithValidData_DoesNotThrowException()
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
await sut.TriggerFrameworkAsync(bpn, data, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -358,7 +362,7 @@ public async Task TriggerFramework_WithConflict_ThrowsServiceExceptionWithErrorC
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._)).Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
async Task Act() => await sut.TriggerFrameworkAsync(bpn, data, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -386,7 +390,7 @@ public async Task TriggerDismantler_WithValidData_DoesNotThrowException()
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._))
.Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
await sut.TriggerDismantlerAsync(bpn, VerifiedCredentialTypeId.DISMANTLER_CERTIFICATE, CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -417,7 +421,7 @@ public async Task TriggerDismantler_WithConflict_ThrowsServiceExceptionWithError
BaseAddress = new Uri("https://base.address.com")
};
A.CallTo(() => _tokenService.GetAuthorizedClient<CustodianService>(_options.Value, A<CancellationToken>._)).Returns(httpClient);
var sut = new CustodianService(_tokenService, _options);
var sut = new CustodianService(_tokenService, _dateTimeProvider, _options);

// Act
async Task Act() => await sut.TriggerDismantlerAsync(bpn, VerifiedCredentialTypeId.DISMANTLER_CERTIFICATE, CancellationToken.None).ConfigureAwait(false);
Expand Down

0 comments on commit 694b0c0

Please sign in to comment.