Skip to content

Commit

Permalink
tests: adjust unit tests
Browse files Browse the repository at this point in the history
Refs: #209
  • Loading branch information
Phil91 committed Aug 26, 2024
1 parent a33acfc commit 4e42250
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,32 @@
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<ProcessStepTypeId> _executableProcessSteps = ImmutableArray.Create(
ProcessStepTypeId.REVOKE_CREDENTIAL,
ProcessStepTypeId.TRIGGER_NOTIFICATION,
ProcessStepTypeId.TRIGGER_MAIL);

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<ProcessStepTypeId> GetExecutableStepTypeIds() => _executableProcessSteps;
public ValueTask<bool> IsLockRequested(ProcessStepTypeId processStepTypeId) => ValueTask.FromResult(false);

public async ValueTask<IProcessTypeExecutor.InitializationResult> InitializeProcess(Guid processId, IEnumerable<ProcessStepTypeId> processStepTypeIds)
{
var (exists, credentialId) = await _issuerRepositories.GetInstance<ICredentialRepository>().GetDataForProcessId(processId).ConfigureAwait(ConfigureAwaitOptions.None);
var (exists, credentialId) = await issuerRepositories.GetInstance<ICredentialRepository>().GetDataForProcessId(processId).ConfigureAwait(ConfigureAwaitOptions.None);
if (!exists)
{
throw new NotFoundException($"process {processId} does not exist or is not associated with an credential");
Expand All @@ -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<ProcessStepTypeId>? nextSteps) ProcessError(Exception ex)
private static (ProcessStepStatusId StatusId, string? ProcessMessage, IEnumerable<ProcessStepTypeId>? 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))
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateSignedCredentialResponse>().Which.Id.Should().Be(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessStep>();
A.CallTo(() => _processStepRepository.IsValidProcess(A<Guid>._, A<ProcessTypeId>._, A<IEnumerable<ProcessStepTypeId>>._))
Expand All @@ -976,13 +982,10 @@ public async Task RetriggerProcessStep_WithInvalidProcess_ThrowsNotFoundExceptio
A<IEnumerable<(Guid ProcessStepId, Action<ProcessStep>? Initialize, Action<ProcessStep> Modify)>>._))
.Invokes((IEnumerable<(Guid ProcessStepId, Action<ProcessStep>? Initialize, Action<ProcessStep> 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);
Expand All @@ -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<ProcessStep>();
A.CallTo(() => _processStepRepository.IsValidProcess(A<Guid>._, A<ProcessTypeId>._, A<IEnumerable<ProcessStepTypeId>>._))
Expand All @@ -1020,13 +1029,10 @@ public async Task RetriggerProcessStep_ReturnsExpected(ProcessStepTypeId retrigg
A<IEnumerable<(Guid ProcessStepId, Action<ProcessStep>? Initialize, Action<ProcessStep> Modify)>>._))
.Invokes((IEnumerable<(Guid ProcessStepId, Action<ProcessStep>? Initialize, Action<ProcessStep> 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);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -187,9 +190,13 @@ public async Task ExecuteProcessStep_WithRecoverableServiceException_ReturnsToDo
// Arrange
A.CallTo(() => _credentialExpiryProcessHandler.RevokeCredential(credentialId, A<CancellationToken>._))
.Throws(new ServiceException("this is a test", true));
A.CallTo(() => _credentialExpiryProcessHandler.TriggerMail(credentialId, A<CancellationToken>._))
.Throws(new ServiceException("this is a test", true));
A.CallTo(() => _credentialExpiryProcessHandler.TriggerNotification(credentialId, A<CancellationToken>._))
.Throws(new ServiceException("this is a test", true));

// Act
var result = await _sut.ExecuteProcessStep(ProcessStepTypeId.REVOKE_CREDENTIAL, Enumerable.Empty<ProcessStepTypeId>(), CancellationToken.None);
var result = await _sut.ExecuteProcessStep(processStepTypeId, Enumerable.Empty<ProcessStepTypeId>(), CancellationToken.None);

// Assert
result.Modified.Should().BeTrue();
Expand All @@ -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();
Expand All @@ -218,13 +228,17 @@ public async Task ExecuteProcessStep_WithServiceException_ReturnsFailedAndRetrig
// Arrange
A.CallTo(() => _credentialExpiryProcessHandler.RevokeCredential(credentialId, A<CancellationToken>._))
.Throws(new ServiceException("this is a test"));
A.CallTo(() => _credentialExpiryProcessHandler.TriggerMail(credentialId, A<CancellationToken>._))
.Throws(new ServiceException("this is a test"));
A.CallTo(() => _credentialExpiryProcessHandler.TriggerNotification(credentialId, A<CancellationToken>._))
.Throws(new ServiceException("this is a test"));

// Act
var result = await _sut.ExecuteProcessStep(ProcessStepTypeId.REVOKE_CREDENTIAL, Enumerable.Empty<ProcessStepTypeId>(), CancellationToken.None);
var result = await _sut.ExecuteProcessStep(processStepTypeId, Enumerable.Empty<ProcessStepTypeId>(), 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();
Expand Down

0 comments on commit 4e42250

Please sign in to comment.