Skip to content

Commit

Permalink
Merge pull request #30 from AMalininHere/remove-internal-extensions
Browse files Browse the repository at this point in the history
Remove internal extensions
  • Loading branch information
TechnoBerry committed Apr 19, 2021
2 parents aed811b + 42640dd commit cd250a7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 80 deletions.
7 changes: 4 additions & 3 deletions src/Camunda.Worker/BpmnErrorResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Net;
using System.Threading.Tasks;
using Camunda.Worker.Client;
using Camunda.Worker.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Camunda.Worker
{
Expand All @@ -27,8 +28,8 @@ public async Task ExecuteResultAsync(IExternalTaskContext context)
}
catch (ClientException e) when (e.StatusCode == HttpStatusCode.InternalServerError)
{
context.LogWarning<CompleteResult>(
"Failed completion of task {TaskId}. Reason: {Reason}",
var logger = context.ServiceProvider.GetService<ILogger<BpmnErrorResult>>();
logger?.LogWarning(e, "Failed completion of task {TaskId}. Reason: {Reason}",
context.Task.Id, e.Message
);
await context.ReportFailureAsync(e.ErrorType, e.ErrorMessage);
Expand Down
7 changes: 4 additions & 3 deletions src/Camunda.Worker/CompleteResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Net;
using System.Threading.Tasks;
using Camunda.Worker.Client;
using Camunda.Worker.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Camunda.Worker
{
Expand All @@ -24,8 +25,8 @@ public async Task ExecuteResultAsync(IExternalTaskContext context)
}
catch (ClientException e) when (e.StatusCode == HttpStatusCode.InternalServerError)
{
context.LogWarning<CompleteResult>(
"Failed completion of task {TaskId}. Reason: {Reason}",
var logger = context.ServiceProvider.GetService<ILogger<CompleteResult>>();
logger?.LogWarning(e, "Failed completion of task {TaskId}. Reason: {Reason}",
context.Task.Id, e.Message
);
await context.ReportFailureAsync(e.ErrorType, e.ErrorMessage);
Expand Down
14 changes: 0 additions & 14 deletions src/Camunda.Worker/Extensions/ExternalTaskContextExtensions.cs

This file was deleted.

47 changes: 20 additions & 27 deletions test/Camunda.Worker.Tests/BpmnErrorResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
using Camunda.Worker.Client;
Expand All @@ -23,28 +21,28 @@ public BpmnErrorResultTest()
[Fact]
public async Task TestExecuteResultAsync()
{
// Arrange
_contextMock
.Setup(context => context.ReportBpmnErrorAsync(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IDictionary<string, Variable>>()
))
.Returns(Task.CompletedTask);
.Returns(Task.CompletedTask)
.Verifiable();

var result = new BpmnErrorResult("TEST_CODE", "Test message");

// Act
await result.ExecuteResultAsync(_contextMock.Object);

_contextMock.Verify(
context => context.ReportBpmnErrorAsync(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IDictionary<string, Variable>>()
),
Times.Once()
);
// Assert
_contextMock.Verify();
_contextMock.VerifyNoOtherCalls();
}

[Fact]
public async Task TestExecuteResultWithFailedCompletion()
{
// Arrange
_contextMock
.Setup(context => context.ReportBpmnErrorAsync(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IDictionary<string, Variable>>()
Expand All @@ -53,31 +51,26 @@ public async Task TestExecuteResultWithFailedCompletion()
{
Type = "an error type",
Message = "an error message"
}, HttpStatusCode.InternalServerError));

Expression<Func<IExternalTaskContext, Task>> failureExpression = context => context.ReportFailureAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<int?>()
);
}, HttpStatusCode.InternalServerError))
.Verifiable();

_contextMock
.Setup(failureExpression)
.Returns(Task.CompletedTask);
.Setup(context => context.ReportFailureAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<int?>()
))
.Returns(Task.CompletedTask)
.Verifiable();

var result = new BpmnErrorResult("TEST_CODE", "Test message");

// Act
await result.ExecuteResultAsync(_contextMock.Object);

_contextMock.Verify(
context => context.ReportBpmnErrorAsync(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IDictionary<string, Variable>>()
),
Times.Once()
);
_contextMock.Verify(failureExpression, Times.Once());
_contextMock.VerifyGet(c => c.Task, Times.Once());
// Assert
_contextMock.Verify();
_contextMock.VerifyGet(c => c.ServiceProvider, Times.Once());
_contextMock.VerifyNoOtherCalls();
}
Expand Down
39 changes: 17 additions & 22 deletions test/Camunda.Worker.Tests/CompleteResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
using Camunda.Worker.Client;
Expand All @@ -23,27 +21,25 @@ public CompleteResultTest()
[Fact]
public async Task TestExecuteResultAsync()
{
// Arrange
_contextMock
.Setup(context => context.CompleteAsync(
It.IsAny<IDictionary<string, Variable>>(),
It.IsAny<IDictionary<string, Variable>>()
))
.Returns(Task.CompletedTask);
.Returns(Task.CompletedTask)
.Verifiable();

var result = new CompleteResult
{
Variables = new Dictionary<string, Variable>()
};

//Act
await result.ExecuteResultAsync(_contextMock.Object);

_contextMock.Verify(
context => context.CompleteAsync(
It.IsAny<IDictionary<string, Variable>>(),
It.IsAny<IDictionary<string, Variable>>()
),
Times.Once()
);
// Assert
_contextMock.Verify();
_contextMock.VerifyNoOtherCalls();
}

Expand All @@ -59,18 +55,18 @@ public async Task TestExecuteResultWithFailedCompletion()
{
Type = "an error type",
Message = "an error message"
}, HttpStatusCode.InternalServerError));

Expression<Func<IExternalTaskContext, Task>> failureExpression = context => context.ReportFailureAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<int?>()
);
}, HttpStatusCode.InternalServerError))
.Verifiable();

_contextMock
.Setup(failureExpression)
.Returns(Task.CompletedTask);
.Setup(context => context.ReportFailureAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<int?>()
))
.Returns(Task.CompletedTask)
.Verifiable();

var result = new CompleteResult
{
Expand All @@ -86,8 +82,7 @@ public async Task TestExecuteResultWithFailedCompletion()
),
Times.Once()
);
_contextMock.Verify(failureExpression, Times.Once());
_contextMock.VerifyGet(c => c.Task, Times.Once());
_contextMock.Verify();
_contextMock.VerifyGet(c => c.ServiceProvider, Times.Once());
_contextMock.VerifyNoOtherCalls();
}
Expand Down
23 changes: 12 additions & 11 deletions test/Camunda.Worker.Tests/FailureResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Moq;
using Xunit;
Expand All @@ -13,22 +12,24 @@ public class FailureResultTest
[Fact]
public async Task TestExecuteResultAsync()
{
Expression<Func<IExternalTaskContext, Task>> expression = context => context.ReportFailureAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<int?>()
);

// Arrange
_contextMock
.Setup(expression)
.Returns(Task.CompletedTask);
.Setup(context => context.ReportFailureAsync(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<int?>()
))
.Returns(Task.CompletedTask)
.Verifiable();

var result = new FailureResult(new Exception("Message"));

// Act
await result.ExecuteResultAsync(_contextMock.Object);

_contextMock.Verify(expression, Times.Once());
// Assert
_contextMock.Verify();
_contextMock.VerifyNoOtherCalls();
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/Camunda.Worker.Tests/NoneResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ public class NoneResultTest
[Fact]
public async Task TestExecuteResultAsync()
{
// Arrange
var result = new NoneResult();

// Act
await result.ExecuteResultAsync(_contextMock.Object);

// Assert
_contextMock.VerifyNoOtherCalls();
}
}
Expand Down

0 comments on commit cd250a7

Please sign in to comment.