Skip to content

Commit

Permalink
Merge pull request #51 from ionut-openminds/master
Browse files Browse the repository at this point in the history
Added AllVariables flag to HandlerVariablesAttribute
  • Loading branch information
TechnoBerry committed Mar 18, 2022
2 parents 1a333c5 + 08017b3 commit 2838b75
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Camunda.Worker/CamundaWorkerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static HandlerMetadata CollectMetadataFromAttributes(Type handlerType)
return new HandlerMetadata(topicsAttribute.TopicNames, topicsAttribute.LockDuration)
{
LocalVariables = variablesAttribute?.LocalVariables ?? false,
Variables = variablesAttribute?.Variables,
Variables = variablesAttribute?.AllVariables ?? false ? null : variablesAttribute?.Variables,
IncludeExtensionProperties = topicsAttribute.IncludeExtensionProperties
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Camunda.Worker/Execution/HandlerMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public HandlerMetadata(IReadOnlyList<string> topicNames, int lockDuration = Cons

/// <summary>Determines whether custom extension properties defined in the BPMN activity of the external task (e.g. via the Extensions tab in the Camunda modeler) should be included in the response. Default: false.</summary>
public bool IncludeExtensionProperties { get; set; }

public IReadOnlyList<string>? Variables { get; set; }

public IReadOnlyList<string>? ProcessDefinitionIds { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/Camunda.Worker/HandlerVariablesAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public HandlerVariablesAttribute(params string[] variables)
public IReadOnlyList<string> Variables { get; }

public bool LocalVariables { get; set; }

/// <summary>
///Setting this to true will retrieve all the process variables from Camunda without the need of knowing their names
/// </summary>
public bool AllVariables { get; set; }
}
}
39 changes: 39 additions & 0 deletions test/Camunda.Worker.Tests/CamundaWorkerBuilderExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,45 @@ public void TestAddHandlerWithoutTopic()
Assert.Throws<Exception>(() => _builderMock.Object.AddHandler<HandlerWithoutTopics>());
}

[Fact]
public void TestAddHandlerWithAllVariables()
{
// Arrange
var savedMetadata = new List<HandlerMetadata>();

_builderMock
.Setup(builder => builder.AddHandler(It.IsAny<ExternalTaskDelegate>(), It.IsAny<HandlerMetadata>()))
.Callback((ExternalTaskDelegate _, HandlerMetadata metadata) => savedMetadata.Add(metadata))
.Returns(_builderMock.Object);

// Act
_builderMock.Object.AddHandler<HandlerWithAllVariables>();

// Assert
_builderMock.Verify(
builder => builder.AddHandler(It.IsAny<ExternalTaskDelegate>(), It.IsAny<HandlerMetadata>()),
Times.Once());
Assert.Contains(_services, d => d.Lifetime == ServiceLifetime.Transient &&
d.ServiceType == typeof(HandlerWithAllVariables));

var metadata = Assert.Single(savedMetadata);
Assert.NotNull(metadata);
Assert.Null(metadata.Variables);
}

[HandlerTopics("testTopic")]
[HandlerVariables(AllVariables = true)]
private class HandlerWithAllVariables : IExternalTaskHandler
{
public Task<IExecutionResult> HandleAsync(ExternalTask externalTask, CancellationToken cancellationToken)
{
return Task.FromResult<IExecutionResult>(new CompleteResult
{
Variables = externalTask.Variables
});
}
}

[HandlerTopics("testTopic_1", "testTopic_1")]
[HandlerVariables("testVariable", LocalVariables = true)]
private class HandlerWithTopics : IExternalTaskHandler
Expand Down

0 comments on commit 2838b75

Please sign in to comment.