Skip to content

Commit

Permalink
test: added unit test for abstract base classes implementing IAsyncZe…
Browse files Browse the repository at this point in the history
…ebeWorker
  • Loading branch information
VonDerBeck committed Mar 25, 2024
1 parent 4a06db6 commit c3065b6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,41 @@ public async Task VariablesAreDeserializedWhenJobImplementGenericAbstractJob()
Assert.Equal(expected, job.getVariables());
}

[Fact]
public async Task ImplementationOfAbstractJobHandlerBaseIsCalledCorrectly()
{
var random = new Random();

var jobVariables = new ExtendedJobHState()
{
Guid = Guid.NewGuid(),
ExtraAttr = "Hello Impl"
};
var expectedSerializedVariables = new ZeebeVariablesSerializer().Serialize(jobVariables);
deserializerMock.Setup(m => m.Deserialize<ExtendedJobHState>(expectedSerializedVariables))
.Returns(jobVariables);

PrepareAsyncJobHandlersFor<JobHandlerI, ExtendedJobHState>();
var expectedHandler = jobHandlerInfoCollection.First();
var expectedKey = random.Next();

var jobMock = new Mock<IJob>();
jobMock.SetupGet(m => m.Type).Returns(expectedHandler.JobType);
jobMock.SetupGet(m => m.Key).Returns(expectedKey);
jobMock.SetupGet(m => m.Variables).Returns(expectedSerializedVariables);

JobHandlerI.guids.Clear();

var handler = Create();
await handler.HandleJob(jobClientMock.Object, jobMock.Object, cancellationToken);

this.jobClientMock.Verify(c => c.NewCompleteJobCommand(expectedKey), Times.Once);
this.jobClientMock.Verify(c => c.NewThrowErrorCommand(expectedKey), Times.Never);
this.jobClientMock.Verify(c => c.NewFailCommand(expectedKey), Times.Never);

Assert.Single(JobHandlerI.guids);
Assert.Equal(jobVariables.Guid, JobHandlerI.guids.First());
}

[Fact]
public async Task RetryTimeOutIsUsedWhenJobIsCompleted()
Expand Down Expand Up @@ -382,6 +417,7 @@ private static Mock<IServiceProvider> CreateIServiceProviderMock(Mock<HandleJobD
mock.Setup(m => m.GetService(typeof(JobHandlerF))).Returns(new JobHandlerF(handleJobDelegateMock.Object));
mock.Setup(m => m.GetService(typeof(JobHandlerG))).Returns(new JobHandlerG(handleJobDelegateMock.Object));
mock.Setup(m => m.GetService(typeof(JobHandlerH))).Returns(new JobHandlerH(handleJobDelegateMock.Object));
mock.Setup(m => m.GetService(typeof(JobHandlerI))).Returns(new JobHandlerI());

return mock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void ThrowsArgumentNullExceptionWhenAssemblyProviderIsNull()
[Fact]
public void AllJobHandlersAreFoundWhenCreated() {
var actual = Handlers();
var expected = 8;
var expected = 9;

Assert.Equal(expected, actual.Count());
}
Expand Down
25 changes: 25 additions & 0 deletions test/Zeebe.Client.Accelerator.Unit.Tests/Stubs/JobHandlerI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Zeebe.Client.Accelerator.Unit.Tests.Stubs
{
public class JobHandlerI : TypedInputHandlerBase<ExtendedJobHState>
{
public static IList<Guid> guids = new List<Guid>();

protected override Task HandleJob(ExtendedJobHState variables, CancellationToken cancellationToken)
{
guids.Add(variables.Guid);
return Task.CompletedTask;
}
}

public class ExtendedJobHState : JobHState
{
public string ExtraAttr { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Zeebe.Client.Accelerator.Abstractions;
using Zeebe.Client.Accelerator.Attributes;

namespace Zeebe.Client.Accelerator.Unit.Tests.Stubs
{
public abstract class TypedInputHandlerBase<TInput> : IAsyncZeebeWorker<TInput> where TInput : JobHState, new ()
{
protected abstract Task HandleJob(TInput variables, CancellationToken cancellationToken);

public async Task HandleJob(ZeebeJob<TInput> job, CancellationToken cancellationToken)
{
await HandleJob(job.getVariables(), cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ public ZeebeHostedServiceTests()
.Where(m => m.Name.Equals(nameof(JobHandlerH.HandleJob)))
.First()
),
CreateJobHandlerInfo(
typeof(JobHandlerI)
.GetMethods()
.Where(m => m.Name.Equals(nameof(JobHandlerI.HandleJob)))
.First()
),
};

this.handleJobDelegateMock = new Mock<HandleJobDelegate>();
Expand Down

0 comments on commit c3065b6

Please sign in to comment.