Skip to content

Commit 5825671

Browse files
authored
feat: Implement cache remove value policy in emulator (#69)
1 parent 3bccbff commit 5825671

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockCacheRemoveValueProvider
10+
{
11+
public static Setup CacheRemoveValue<T>(this MockPoliciesProvider<T> mock) where T : class =>
12+
CacheRemoveValue(mock, (_, _) => true);
13+
14+
public static Setup CacheRemoveValue<T>(
15+
this MockPoliciesProvider<T> mock,
16+
Func<GatewayContext, CacheRemoveValueConfig, bool> predicate
17+
) where T : class
18+
{
19+
var handler = mock.SectionContextProxy.GetHandler<CacheRemoveValueHandler>();
20+
return new Setup(predicate, handler);
21+
}
22+
23+
public class Setup
24+
{
25+
private readonly Func<GatewayContext, CacheRemoveValueConfig, bool> _predicate;
26+
private readonly CacheRemoveValueHandler _handler;
27+
28+
internal Setup(
29+
Func<GatewayContext, CacheRemoveValueConfig, bool> predicate,
30+
CacheRemoveValueHandler handler)
31+
{
32+
_predicate = predicate;
33+
_handler = handler;
34+
}
35+
36+
public void WithCallback(Action<GatewayContext, CacheRemoveValueConfig> callback) =>
37+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
38+
}
39+
}

src/Testing/Emulator/Policies/CacheRemoveValueHandler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55

66
namespace Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
77

8-
[Section(nameof(IInboundContext))]
8+
[
9+
Section(nameof(IInboundContext)),
10+
Section(nameof(IBackendContext)),
11+
Section(nameof(IOutboundContext)),
12+
Section(nameof(IOnErrorContext))
13+
]
914
internal class CacheRemoveValueHandler : PolicyHandler<CacheRemoveValueConfig>
1015
{
1116
public override string PolicyName => nameof(IInboundContext.CacheRemoveValue);
1217

1318
protected override void Handle(GatewayContext context, CacheRemoveValueConfig config)
1419
{
15-
throw new NotImplementedException();
20+
var store = context.CacheStore.GetCache(config.CachingType ?? "prefer-external");
21+
store?.Remove(config.Key);
1622
}
1723
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing;
6+
using Azure.ApiManagement.PolicyToolkit.Testing.Document;
7+
8+
namespace Test.Emulator.Emulator.Policies;
9+
10+
[TestClass]
11+
public class CacheRemoveValueTests
12+
{
13+
class SimpleCacheRemoveValue : IDocument
14+
{
15+
public void Inbound(IInboundContext context)
16+
{
17+
context.CacheRemoveValue(new CacheRemoveValueConfig() { Key = "key" });
18+
}
19+
}
20+
21+
[TestMethod]
22+
public void CacheRemoveValue_Callback()
23+
{
24+
var test = new SimpleCacheRemoveValue().AsTestDocument();
25+
var executedCallback = false;
26+
test.SetupInbound().CacheRemoveValue().WithCallback((_, _) =>
27+
{
28+
executedCallback = true;
29+
});
30+
31+
test.RunInbound();
32+
33+
executedCallback.Should().BeTrue();
34+
}
35+
36+
[TestMethod]
37+
public void CacheRemoveValue_SetupCacheRemove()
38+
{
39+
var test = new SimpleCacheRemoveValue().AsTestDocument();
40+
var cacheStore = test.SetupCacheStore().WithInternalCacheValue("key", "value");
41+
42+
test.RunInbound();
43+
44+
cacheStore.InternalCache.Should().NotContainKey("key");
45+
}
46+
47+
[TestMethod]
48+
public void CacheRemoveValue_SetupCacheRemove_WithExternalCacheSetup()
49+
{
50+
var test = new SimpleCacheRemoveValue().AsTestDocument();
51+
var cacheStore = test.SetupCacheStore().WithExternalCacheSetup().WithExternalCacheValue("key", "value");
52+
53+
test.RunInbound();
54+
55+
cacheStore.ExternalCache.Should().NotContainKey("key");
56+
}
57+
}

0 commit comments

Comments
 (0)