From 4e422509118d88c6dfe588ba2ec2c97c62874d2a Mon Sep 17 00:00:00 2001 From: Phil Schneider Date: Mon, 26 Aug 2024 10:35:47 +0200 Subject: [PATCH] tests: adjust unit tests Refs: #209 --- .../Models/CreateCredentialResponse.cs | 9 ---- .../CredentialExpiryProcessTypeExecutor.cs | 31 +++++-------- .../ProcessStepRepositoryTests.cs | 35 +++++++++++++++ .../Services/WalletServiceTests.cs | 4 +- .../BusinessLogic/IssuerBusinessLogicTests.cs | 44 +++++++++++-------- ...redentialExpiryProcessTypeExecutorTests.cs | 28 +++++++++--- 6 files changed, 96 insertions(+), 55 deletions(-) diff --git a/src/externalservices/Wallet.Service/Models/CreateCredentialResponse.cs b/src/externalservices/Wallet.Service/Models/CreateCredentialResponse.cs index 167feebb..d090f508 100644 --- a/src/externalservices/Wallet.Service/Models/CreateCredentialResponse.cs +++ b/src/externalservices/Wallet.Service/Models/CreateCredentialResponse.cs @@ -38,15 +38,6 @@ public record SignData( [property: JsonPropertyName("keyName")] string? KeyName ); -public record CreateCredentialRequest( - [property: JsonPropertyName("application")] string Application, - [property: JsonPropertyName("payload")] CredentialPayload Payload -); - -public record CredentialPayload( - [property: JsonPropertyName("issue")] JsonDocument Issue -); - public record CreateSignedCredentialResponse( [property: JsonPropertyName("id")] Guid Id, [property: JsonPropertyName("jwt")] string Jwt diff --git a/src/processes/CredentialProcess.Worker/Expiry/CredentialExpiryProcessTypeExecutor.cs b/src/processes/CredentialProcess.Worker/Expiry/CredentialExpiryProcessTypeExecutor.cs index e8b010c8..5342e1ca 100644 --- a/src/processes/CredentialProcess.Worker/Expiry/CredentialExpiryProcessTypeExecutor.cs +++ b/src/processes/CredentialProcess.Worker/Expiry/CredentialExpiryProcessTypeExecutor.cs @@ -22,16 +22,17 @@ using Org.Eclipse.TractusX.SsiCredentialIssuer.DBAccess; using Org.Eclipse.TractusX.SsiCredentialIssuer.DBAccess.Repositories; using Org.Eclipse.TractusX.SsiCredentialIssuer.Entities.Enums; +using Org.Eclipse.TractusX.SsiCredentialIssuer.Entities.Extensions; using Org.Eclipse.TractusX.SsiCredentialIssuer.Processes.Worker.Library; using System.Collections.Immutable; namespace Org.Eclipse.TractusX.SsiCredentialIssuer.CredentialProcess.Worker.Expiry; -public class CredentialExpiryProcessTypeExecutor : IProcessTypeExecutor +public class CredentialExpiryProcessTypeExecutor( + IIssuerRepositories issuerRepositories, + ICredentialExpiryProcessHandler credentialExpiryProcessHandler) + : IProcessTypeExecutor { - private readonly IIssuerRepositories _issuerRepositories; - private readonly ICredentialExpiryProcessHandler _credentialExpiryProcessHandler; - private readonly IEnumerable _executableProcessSteps = ImmutableArray.Create( ProcessStepTypeId.REVOKE_CREDENTIAL, ProcessStepTypeId.TRIGGER_NOTIFICATION, @@ -39,14 +40,6 @@ public class CredentialExpiryProcessTypeExecutor : IProcessTypeExecutor private Guid _credentialId; - public CredentialExpiryProcessTypeExecutor( - IIssuerRepositories issuerRepositories, - ICredentialExpiryProcessHandler credentialExpiryProcessHandler) - { - _issuerRepositories = issuerRepositories; - _credentialExpiryProcessHandler = credentialExpiryProcessHandler; - } - public ProcessTypeId GetProcessTypeId() => ProcessTypeId.DECLINE_CREDENTIAL; public bool IsExecutableStepTypeId(ProcessStepTypeId processStepTypeId) => _executableProcessSteps.Contains(processStepTypeId); public IEnumerable GetExecutableStepTypeIds() => _executableProcessSteps; @@ -54,7 +47,7 @@ public CredentialExpiryProcessTypeExecutor( public async ValueTask InitializeProcess(Guid processId, IEnumerable processStepTypeIds) { - var (exists, credentialId) = await _issuerRepositories.GetInstance().GetDataForProcessId(processId).ConfigureAwait(ConfigureAwaitOptions.None); + var (exists, credentialId) = await issuerRepositories.GetInstance().GetDataForProcessId(processId).ConfigureAwait(ConfigureAwaitOptions.None); if (!exists) { throw new NotFoundException($"process {processId} does not exist or is not associated with an credential"); @@ -80,30 +73,30 @@ public CredentialExpiryProcessTypeExecutor( { (nextStepTypeIds, stepStatusId, modified, processMessage) = processStepTypeId switch { - ProcessStepTypeId.REVOKE_CREDENTIAL => await _credentialExpiryProcessHandler.RevokeCredential(_credentialId, cancellationToken) + ProcessStepTypeId.REVOKE_CREDENTIAL => await credentialExpiryProcessHandler.RevokeCredential(_credentialId, cancellationToken) .ConfigureAwait(ConfigureAwaitOptions.None), - ProcessStepTypeId.TRIGGER_NOTIFICATION => await _credentialExpiryProcessHandler.TriggerNotification(_credentialId, cancellationToken) + ProcessStepTypeId.TRIGGER_NOTIFICATION => await credentialExpiryProcessHandler.TriggerNotification(_credentialId, cancellationToken) .ConfigureAwait(ConfigureAwaitOptions.None), - ProcessStepTypeId.TRIGGER_MAIL => await _credentialExpiryProcessHandler.TriggerMail(_credentialId, cancellationToken) + ProcessStepTypeId.TRIGGER_MAIL => await credentialExpiryProcessHandler.TriggerMail(_credentialId, cancellationToken) .ConfigureAwait(ConfigureAwaitOptions.None), _ => (null, ProcessStepStatusId.TODO, false, null) }; } catch (Exception ex) when (ex is not SystemException) { - (stepStatusId, processMessage, nextStepTypeIds) = ProcessError(ex); + (stepStatusId, processMessage, nextStepTypeIds) = ProcessError(ex, processStepTypeId); modified = true; } return new IProcessTypeExecutor.StepExecutionResult(modified, stepStatusId, nextStepTypeIds, null, processMessage); } - private static (ProcessStepStatusId StatusId, string? ProcessMessage, IEnumerable? nextSteps) ProcessError(Exception ex) + private static (ProcessStepStatusId StatusId, string? ProcessMessage, IEnumerable? nextSteps) ProcessError(Exception ex, ProcessStepTypeId processStepTypeId) { return ex switch { ServiceException { IsRecoverable: true } => (ProcessStepStatusId.TODO, ex.Message, null), - _ => (ProcessStepStatusId.FAILED, ex.Message, null) + _ => (ProcessStepStatusId.FAILED, ex.Message, Enumerable.Repeat(processStepTypeId.GetRetriggerStep(), 1)) }; } } diff --git a/tests/database/SsiCredentialIssuer.DbAccess.Tests/ProcessStepRepositoryTests.cs b/tests/database/SsiCredentialIssuer.DbAccess.Tests/ProcessStepRepositoryTests.cs index fc2fe9e7..31cc12f2 100644 --- a/tests/database/SsiCredentialIssuer.DbAccess.Tests/ProcessStepRepositoryTests.cs +++ b/tests/database/SsiCredentialIssuer.DbAccess.Tests/ProcessStepRepositoryTests.cs @@ -341,6 +341,41 @@ public async Task GetProcessStepData_ReturnsExpected() #endregion + #region IsValidProcess + + [Fact] + public async Task IsValidProcess_ReturnsExpected() + { + // Arrange + var processId = new Guid("dd371565-9489-4907-a2e4-b8cbfe7a8cd2"); + var sut = await CreateSut(); + + // Act + var result = await sut.IsValidProcess(processId, ProcessTypeId.CREATE_CREDENTIAL, Enumerable.Repeat(ProcessStepTypeId.SAVE_CREDENTIAL_DOCUMENT, 1)); + + // Assert + result.ProcessExists.Should().BeTrue(); + result.ProcessData.ProcessSteps.Should().ContainSingle() + .And.Satisfy(x => x.ProcessStepStatusId == ProcessStepStatusId.TODO); + } + + [Fact] + public async Task IsValidProcess_WithNonExisting_ReturnsExpected() + { + // Arrange + var processId = Guid.NewGuid(); + var sut = await CreateSut(); + + // Act + var result = await sut.IsValidProcess(processId, ProcessTypeId.CREATE_CREDENTIAL, Enumerable.Repeat(ProcessStepTypeId.SAVE_CREDENTIAL_DOCUMENT, 1)); + + // Assert + result.ProcessExists.Should().BeFalse(); + result.ProcessData.Should().BeNull(); + } + + #endregion + private async Task<(ProcessStepRepository sut, IssuerDbContext dbContext)> CreateSutWithContext() { var context = await _dbTestDbFixture.GetDbContext(); diff --git a/tests/externalservices/Wallet.Service.Tests/Services/WalletServiceTests.cs b/tests/externalservices/Wallet.Service.Tests/Services/WalletServiceTests.cs index e967fde4..c05402d5 100644 --- a/tests/externalservices/Wallet.Service.Tests/Services/WalletServiceTests.cs +++ b/tests/externalservices/Wallet.Service.Tests/Services/WalletServiceTests.cs @@ -74,7 +74,9 @@ x.Content is JsonContent && (x.Content as JsonContent)!.ObjectType == typeof(CreateSignedCredentialRequest) && ((x.Content as JsonContent)!.Value as CreateSignedCredentialRequest)!.Application == "catena-x-portal" && ((x.Content as JsonContent)!.Value as CreateSignedCredentialRequest)!.Payload.Signature.ProofMechanism == "external" && - ((x.Content as JsonContent)!.Value as CreateSignedCredentialRequest)!.Payload.Signature.ProofType == "jwt" + ((x.Content as JsonContent)!.Value as CreateSignedCredentialRequest)!.Payload.Signature.ProofType == "jwt" && + ((x.Content as JsonContent)!.Value as CreateSignedCredentialRequest)!.Payload.Signature.KeyName == null && + ((x.Content as JsonContent)!.Value as CreateSignedCredentialRequest)!.Payload.Issue == payload ); result.Should().BeOfType().Which.Id.Should().Be(id); } diff --git a/tests/issuer/SsiCredentialIssuer.Service.Tests/BusinessLogic/IssuerBusinessLogicTests.cs b/tests/issuer/SsiCredentialIssuer.Service.Tests/BusinessLogic/IssuerBusinessLogicTests.cs index 654d6297..fba4b791 100644 --- a/tests/issuer/SsiCredentialIssuer.Service.Tests/BusinessLogic/IssuerBusinessLogicTests.cs +++ b/tests/issuer/SsiCredentialIssuer.Service.Tests/BusinessLogic/IssuerBusinessLogicTests.cs @@ -953,12 +953,18 @@ public async Task CreateFrameworkCredential_ReturnsExpected() #region RetriggerProcessStep - [Fact] - public async Task RetriggerProcessStep_WithInvalidProcess_ThrowsNotFoundException() + [Theory] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_CREATE_SIGNED_CREDENTIAL)] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_SAVE_CREDENTIAL_DOCUMENT)] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_CREATE_CREDENTIAL_FOR_HOLDER)] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_TRIGGER_CALLBACK)] + [InlineData(ProcessTypeId.DECLINE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_REVOKE_CREDENTIAL)] + [InlineData(ProcessTypeId.DECLINE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_TRIGGER_NOTIFICATION)] + [InlineData(ProcessTypeId.DECLINE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_TRIGGER_MAIL)] + public async Task RetriggerProcessStep_WithInvalidProcess_ThrowsNotFoundException(ProcessTypeId processId, ProcessStepTypeId retriggerStep) { // Arrange - var retriggerStep = ProcessStepTypeId.RETRIGGER_CREATE_SIGNED_CREDENTIAL; - var process = new Process(Guid.NewGuid(), ProcessTypeId.CREATE_CREDENTIAL, Guid.NewGuid()); + var process = new Process(Guid.NewGuid(), processId, Guid.NewGuid()); var processStep = new ProcessStep(Guid.NewGuid(), retriggerStep, ProcessStepStatusId.TODO, process.Id, DateTimeOffset.UtcNow); var processSteps = new List(); A.CallTo(() => _processStepRepository.IsValidProcess(A._, A._, A>._)) @@ -976,13 +982,10 @@ public async Task RetriggerProcessStep_WithInvalidProcess_ThrowsNotFoundExceptio A? Initialize, Action Modify)>>._)) .Invokes((IEnumerable<(Guid ProcessStepId, Action? Initialize, Action Modify)> processStepStatus) => { - foreach (var ps in processStepStatus) + foreach (var ps in processStepStatus.Where(p => p.ProcessStepId == processStep.Id)) { - if (ps.ProcessStepId == processStep.Id) - { - ps.Initialize?.Invoke(processStep); - ps.Modify(processStep); - } + ps.Initialize?.Invoke(processStep); + ps.Modify(processStep); } }); Task Act() => _sut.RetriggerProcessStep(process.Id, retriggerStep, CancellationToken.None); @@ -998,11 +1001,17 @@ public async Task RetriggerProcessStep_WithInvalidProcess_ThrowsNotFoundExceptio } [Theory] - [InlineData(ProcessStepTypeId.RETRIGGER_CREATE_SIGNED_CREDENTIAL, ProcessStepTypeId.CREATE_SIGNED_CREDENTIAL)] - public async Task RetriggerProcessStep_ReturnsExpected(ProcessStepTypeId retriggerStep, ProcessStepTypeId expectedStepTypeId) + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_CREATE_SIGNED_CREDENTIAL, ProcessStepTypeId.CREATE_SIGNED_CREDENTIAL)] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_SAVE_CREDENTIAL_DOCUMENT, ProcessStepTypeId.SAVE_CREDENTIAL_DOCUMENT)] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_CREATE_CREDENTIAL_FOR_HOLDER, ProcessStepTypeId.CREATE_CREDENTIAL_FOR_HOLDER)] + [InlineData(ProcessTypeId.CREATE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_TRIGGER_CALLBACK, ProcessStepTypeId.TRIGGER_CALLBACK)] + [InlineData(ProcessTypeId.DECLINE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_REVOKE_CREDENTIAL, ProcessStepTypeId.REVOKE_CREDENTIAL)] + [InlineData(ProcessTypeId.DECLINE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_TRIGGER_NOTIFICATION, ProcessStepTypeId.TRIGGER_NOTIFICATION)] + [InlineData(ProcessTypeId.DECLINE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_TRIGGER_MAIL, ProcessStepTypeId.TRIGGER_MAIL)] + public async Task RetriggerProcessStep_ReturnsExpected(ProcessTypeId processTypeId, ProcessStepTypeId retriggerStep, ProcessStepTypeId expectedStepTypeId) { // Arrange - var process = new Process(Guid.NewGuid(), ProcessTypeId.CREATE_CREDENTIAL, Guid.NewGuid()); + var process = new Process(Guid.NewGuid(), processTypeId, Guid.NewGuid()); var processStep = new ProcessStep(Guid.NewGuid(), retriggerStep, ProcessStepStatusId.TODO, process.Id, DateTimeOffset.UtcNow); var processSteps = new List(); A.CallTo(() => _processStepRepository.IsValidProcess(A._, A._, A>._)) @@ -1020,13 +1029,10 @@ public async Task RetriggerProcessStep_ReturnsExpected(ProcessStepTypeId retrigg A? Initialize, Action Modify)>>._)) .Invokes((IEnumerable<(Guid ProcessStepId, Action? Initialize, Action Modify)> processStepStatus) => { - foreach (var ps in processStepStatus) + foreach (var ps in processStepStatus.Where(p => p.ProcessStepId == processStep.Id)) { - if (ps.ProcessStepId == processStep.Id) - { - ps.Initialize?.Invoke(processStep); - ps.Modify(processStep); - } + ps.Initialize?.Invoke(processStep); + ps.Modify(processStep); } }); diff --git a/tests/processes/CredentialProcess.Worker.Tests/CredentialExpiryProcessTypeExecutorTests.cs b/tests/processes/CredentialProcess.Worker.Tests/CredentialExpiryProcessTypeExecutorTests.cs index 3115ccac..afe908f7 100644 --- a/tests/processes/CredentialProcess.Worker.Tests/CredentialExpiryProcessTypeExecutorTests.cs +++ b/tests/processes/CredentialProcess.Worker.Tests/CredentialExpiryProcessTypeExecutorTests.cs @@ -168,8 +168,11 @@ public async Task ExecuteProcessStep_WithValidData_CallsExpected() result.SkipStepTypeIds.Should().BeNull(); } - [Fact] - public async Task ExecuteProcessStep_WithRecoverableServiceException_ReturnsToDo() + [Theory] + [InlineData(ProcessStepTypeId.REVOKE_CREDENTIAL)] + [InlineData(ProcessStepTypeId.TRIGGER_NOTIFICATION)] + [InlineData(ProcessStepTypeId.TRIGGER_MAIL)] + public async Task ExecuteProcessStep_WithRecoverableServiceException_ReturnsToDo(ProcessStepTypeId processStepTypeId) { // Arrange InitializeProcess var validProcessId = Guid.NewGuid(); @@ -187,9 +190,13 @@ public async Task ExecuteProcessStep_WithRecoverableServiceException_ReturnsToDo // Arrange A.CallTo(() => _credentialExpiryProcessHandler.RevokeCredential(credentialId, A._)) .Throws(new ServiceException("this is a test", true)); + A.CallTo(() => _credentialExpiryProcessHandler.TriggerMail(credentialId, A._)) + .Throws(new ServiceException("this is a test", true)); + A.CallTo(() => _credentialExpiryProcessHandler.TriggerNotification(credentialId, A._)) + .Throws(new ServiceException("this is a test", true)); // Act - var result = await _sut.ExecuteProcessStep(ProcessStepTypeId.REVOKE_CREDENTIAL, Enumerable.Empty(), CancellationToken.None); + var result = await _sut.ExecuteProcessStep(processStepTypeId, Enumerable.Empty(), CancellationToken.None); // Assert result.Modified.Should().BeTrue(); @@ -199,8 +206,11 @@ public async Task ExecuteProcessStep_WithRecoverableServiceException_ReturnsToDo result.SkipStepTypeIds.Should().BeNull(); } - [Fact] - public async Task ExecuteProcessStep_WithServiceException_ReturnsFailedAndRetriggerStep() + [Theory] + [InlineData(ProcessStepTypeId.REVOKE_CREDENTIAL, ProcessStepTypeId.RETRIGGER_REVOKE_CREDENTIAL)] + [InlineData(ProcessStepTypeId.TRIGGER_NOTIFICATION, ProcessStepTypeId.RETRIGGER_TRIGGER_NOTIFICATION)] + [InlineData(ProcessStepTypeId.TRIGGER_MAIL, ProcessStepTypeId.RETRIGGER_TRIGGER_MAIL)] + public async Task ExecuteProcessStep_WithServiceException_ReturnsFailedAndRetriggerStep(ProcessStepTypeId processStepTypeId, ProcessStepTypeId expectedRetriggerStep) { // Arrange InitializeProcess var validProcessId = Guid.NewGuid(); @@ -218,13 +228,17 @@ public async Task ExecuteProcessStep_WithServiceException_ReturnsFailedAndRetrig // Arrange A.CallTo(() => _credentialExpiryProcessHandler.RevokeCredential(credentialId, A._)) .Throws(new ServiceException("this is a test")); + A.CallTo(() => _credentialExpiryProcessHandler.TriggerMail(credentialId, A._)) + .Throws(new ServiceException("this is a test")); + A.CallTo(() => _credentialExpiryProcessHandler.TriggerNotification(credentialId, A._)) + .Throws(new ServiceException("this is a test")); // Act - var result = await _sut.ExecuteProcessStep(ProcessStepTypeId.REVOKE_CREDENTIAL, Enumerable.Empty(), CancellationToken.None); + var result = await _sut.ExecuteProcessStep(processStepTypeId, Enumerable.Empty(), CancellationToken.None); // Assert result.Modified.Should().BeTrue(); - result.ScheduleStepTypeIds.Should().BeNull(); + result.ScheduleStepTypeIds.Should().ContainSingle().Which.Should().Be(expectedRetriggerStep); result.ProcessStepStatusId.Should().Be(ProcessStepStatusId.FAILED); result.ProcessMessage.Should().Be("this is a test"); result.SkipStepTypeIds.Should().BeNull();