-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* risk filter support * release 1.5.9 * remaining tests * 1.6.0 version
- Loading branch information
1 parent
d8627fe
commit d010392
Showing
13 changed files
with
395 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Castle.Config; | ||
using Castle.Infrastructure; | ||
using Castle.Infrastructure.Exceptions; | ||
using Castle.Messages; | ||
using Castle.Messages.Responses; | ||
|
||
namespace Castle.Actions | ||
{ | ||
internal static class Filter | ||
{ | ||
public static async Task<RiskResponse> Execute( | ||
Func<Task<RiskResponse>> send, | ||
CastleConfiguration configuration, | ||
IInternalLogger logger) | ||
{ | ||
try | ||
{ | ||
return await send(); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Warn(() => "Failover, " + e); | ||
return CreateFailoverResponse(configuration.FailOverStrategy, e); | ||
} | ||
} | ||
|
||
private static RiskResponse CreateFailoverResponse(ActionType strategy, string reason) | ||
{ | ||
if (strategy == ActionType.None) | ||
{ | ||
throw new CastleExternalException("Attempted failover, but no strategy was set."); | ||
} | ||
|
||
return new RiskResponse() | ||
{ | ||
Action = strategy, | ||
Failover = true, | ||
FailoverReason = reason | ||
}; | ||
} | ||
|
||
private static RiskResponse CreateFailoverResponse(ActionType strategy, Exception exception) | ||
{ | ||
return CreateFailoverResponse(strategy, exception is CastleTimeoutException ? "timeout" : "server error"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Castle.Config; | ||
using Castle.Infrastructure; | ||
using Castle.Infrastructure.Exceptions; | ||
using Castle.Messages; | ||
using Castle.Messages.Responses; | ||
|
||
namespace Castle.Actions | ||
{ | ||
internal static class Risk | ||
{ | ||
public static async Task<RiskResponse> Execute( | ||
Func<Task<RiskResponse>> send, | ||
CastleConfiguration configuration, | ||
IInternalLogger logger) | ||
{ | ||
try | ||
{ | ||
return await send(); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Warn(() => "Failover, " + e); | ||
return CreateFailoverResponse(configuration.FailOverStrategy, e); | ||
} | ||
} | ||
|
||
private static RiskResponse CreateFailoverResponse(ActionType strategy, string reason) | ||
{ | ||
if (strategy == ActionType.None) | ||
{ | ||
throw new CastleExternalException("Attempted failover, but no strategy was set."); | ||
} | ||
|
||
return new RiskResponse() | ||
{ | ||
Action = strategy, | ||
Failover = true, | ||
FailoverReason = reason | ||
}; | ||
} | ||
|
||
private static RiskResponse CreateFailoverResponse(ActionType strategy, Exception exception) | ||
{ | ||
return CreateFailoverResponse(strategy, exception is CastleTimeoutException ? "timeout" : "server error"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
namespace Castle.Messages.Responses | ||
{ | ||
public class Policy | ||
{ | ||
public string Id { get; set; } | ||
public string RevisionId { get; set; } | ||
public string Name { get; set; } | ||
public ActionType Action { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Castle.Messages.Responses | ||
{ | ||
|
||
public class RiskResponse | ||
{ | ||
public DeviceItem Device { get; set; } | ||
public float Risk { get; set; } | ||
|
||
public Policy Policy { get; set; } | ||
public JObject Signals { get; set; } | ||
|
||
// FailOver | ||
public ActionType Action { get; set; } | ||
|
||
public bool Failover { get; set; } | ||
|
||
public string FailoverReason { get; set; } | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Castle.Actions; | ||
using Castle.Config; | ||
using Castle.Infrastructure; | ||
using Castle.Infrastructure.Exceptions; | ||
using Castle.Messages; | ||
using Castle.Messages.Responses; | ||
using FluentAssertions; | ||
using NSubstitute; | ||
using Tests.SetUp; | ||
using Xunit; | ||
|
||
namespace Tests | ||
{ | ||
public class When_sending_filter | ||
{ | ||
[Theory, AutoFakeData] | ||
public async Task Should_return_response_if_successful( | ||
CastleConfiguration configuration, RiskResponse response) | ||
{ | ||
Task<RiskResponse> Send() => Task.FromResult(response); | ||
|
||
var logger = Substitute.For<IInternalLogger>(); | ||
|
||
var result = await Filter.Execute(Send, configuration, logger); | ||
|
||
result.Should().Be(response); | ||
} | ||
|
||
|
||
[Theory, AutoFakeData] | ||
public async Task Should_return_failover_response_if_timeout( | ||
string requestUri, | ||
CastleConfiguration configuration) | ||
{ | ||
configuration.FailOverStrategy = ActionType.Allow; | ||
var logger = Substitute.For<IInternalLogger>(); | ||
|
||
Task<RiskResponse> Send() => throw new CastleTimeoutException(requestUri, configuration.Timeout); | ||
|
||
var result = await Filter.Execute(Send, configuration, logger); | ||
|
||
result.Failover.Should().Be(true); | ||
result.FailoverReason.Should().Be("timeout"); | ||
} | ||
|
||
[Theory, AutoFakeData] | ||
public async Task Should_log_failover_exception_as_warning( | ||
Exception exception, | ||
CastleConfiguration configuration) | ||
{ | ||
configuration.FailOverStrategy = ActionType.Allow; | ||
var logger = Substitute.For<IInternalLogger>(); | ||
|
||
Task<RiskResponse> Send() => throw exception; | ||
|
||
await Filter.Execute(Send, configuration, logger); | ||
|
||
logger.Received().Warn(Arg.Is<Func<string>>(x => x() == "Failover, " + exception)); | ||
} | ||
|
||
[Theory, AutoFakeData] | ||
public async Task Should_throw_exception_if_failing_over_with_no_strategy( | ||
Exception exception, | ||
CastleConfiguration configuration) | ||
{ | ||
configuration.FailOverStrategy = ActionType.None; | ||
var logger = Substitute.For<IInternalLogger>(); | ||
|
||
Task<RiskResponse> Send() => throw exception; | ||
|
||
Func<Task> act = async () => await Filter.Execute(Send, configuration, logger); | ||
|
||
await act.Should().ThrowAsync<CastleExternalException>(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.