-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement cache lookup value in emulator (#64)
- Loading branch information
Showing
6 changed files
with
236 additions
and
1 deletion.
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
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,42 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Data; | ||
|
||
public class CacheStore | ||
{ | ||
private readonly Dictionary<string, object> _internalCache = new(); | ||
private readonly Dictionary<string, object> _externalCache = new(); | ||
|
||
private bool _isExternalCacheSetup = false; | ||
|
||
internal Dictionary<string, object>? GetCache(string type) => | ||
type switch | ||
{ | ||
"internal" => _internalCache, | ||
"external" => _isExternalCacheSetup ? _externalCache : null, | ||
"prefer-external" => _isExternalCacheSetup ? _externalCache : _internalCache, | ||
_ => throw new ArgumentException($"Unrecognized type {type}", nameof(type)), | ||
}; | ||
|
||
public IReadOnlyDictionary<string, object> InternalCache => _internalCache; | ||
public IReadOnlyDictionary<string, object> ExternalCache => _externalCache; | ||
|
||
public CacheStore WithExternalCacheSetup(bool isSetup = true) | ||
{ | ||
_isExternalCacheSetup = isSetup; | ||
return this; | ||
} | ||
|
||
public CacheStore WithExternalCacheValue(string key, object value) | ||
{ | ||
_externalCache.Add(key, value); | ||
return this; | ||
} | ||
|
||
public CacheStore WithInternalCacheValue(string key, object value) | ||
{ | ||
_internalCache.Add(key, value); | ||
return this; | ||
} | ||
} |
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
157 changes: 157 additions & 0 deletions
157
test/Test.Testing/Emulator/Policies/CacheLookupValueTests.cs
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,157 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.ApiManagement.PolicyToolkit.Authoring; | ||
using Azure.ApiManagement.PolicyToolkit.Testing; | ||
using Azure.ApiManagement.PolicyToolkit.Testing.Document; | ||
|
||
namespace Test.Emulator.Emulator.Policies; | ||
|
||
[TestClass] | ||
public class CacheLookupValueTests | ||
{ | ||
class SimpleCacheLookupValuePreferExternal : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.CacheLookupValue(new CacheLookupValueConfig() | ||
{ | ||
Key = "key", VariableName = "variable", CachingType = "prefer-external" | ||
}); | ||
} | ||
} | ||
|
||
class SimpleCacheLookupValueFromInternal : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.CacheLookupValue(new CacheLookupValueConfig() | ||
{ | ||
Key = "key", VariableName = "variable", CachingType = "internal" | ||
}); | ||
} | ||
} | ||
|
||
class SimpleCacheLookupValueFromExternal : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.CacheLookupValue(new CacheLookupValueConfig() | ||
{ | ||
Key = "key", VariableName = "variable", CachingType = "external" | ||
}); | ||
} | ||
} | ||
|
||
class SimpleCacheLookupValueWithDefaultValue : IDocument | ||
{ | ||
public void Inbound(IInboundContext context) | ||
{ | ||
context.CacheLookupValue(new CacheLookupValueConfig() | ||
{ | ||
Key = "key", VariableName = "variable", DefaultValue = "test-value" | ||
}); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_Callback() | ||
{ | ||
var test = new SimpleCacheLookupValuePreferExternal().AsTestDocument(); | ||
var executedCallback = false; | ||
test.SetupInbound().CacheLookupValue().WithCallback((_, _) => | ||
{ | ||
executedCallback = true; | ||
}); | ||
|
||
test.RunInbound(); | ||
|
||
executedCallback.Should().BeTrue(); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_WithValueCallback() | ||
{ | ||
var test = new SimpleCacheLookupValuePreferExternal().AsTestDocument(); | ||
test.SetupInbound().CacheLookupValue().WithValue("test"); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().ContainKey("variable").WhoseValue.Should().Be("test"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_PreferExternal_SetupCacheStore_WithInternalValue() | ||
{ | ||
var test = new SimpleCacheLookupValuePreferExternal().AsTestDocument(); | ||
test.SetupCacheStore().WithInternalCacheValue("key", "test"); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().ContainKey("variable").WhoseValue.Should().Be("test"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_PreferExternal_SetupCacheStore_WithExternalCacheSetup() | ||
{ | ||
var test = new SimpleCacheLookupValuePreferExternal().AsTestDocument(); | ||
test.SetupCacheStore().WithExternalCacheSetup().WithExternalCacheValue("key", "test"); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().ContainKey("variable").WhoseValue.Should().Be("test"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_WithDefaultValue() | ||
{ | ||
var test = new SimpleCacheLookupValueWithDefaultValue().AsTestDocument(); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().ContainKey("variable").WhoseValue.Should().Be("test-value"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_InternalCache_WillNotFindValueFromExternalCache() | ||
{ | ||
var test = new SimpleCacheLookupValueFromInternal().AsTestDocument(); | ||
test.SetupCacheStore().WithExternalCacheSetup().WithExternalCacheValue("key", "test"); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().NotContainKey("variable"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_Internal_WillNotFindValueInInternalCache() | ||
{ | ||
var test = new SimpleCacheLookupValueFromInternal().AsTestDocument(); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().NotContainKey("variable"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_ExternalCache_WillNotFindValueInInternalCache() | ||
{ | ||
var test = new SimpleCacheLookupValueFromExternal().AsTestDocument(); | ||
test.SetupCacheStore().WithInternalCacheValue("key", "test"); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().NotContainKey("variable"); | ||
} | ||
|
||
[TestMethod] | ||
public void CacheLookupValue_ExternalCache_WillNotFindValue_WhenExternalCacheNotSetup() | ||
{ | ||
var test = new SimpleCacheLookupValueFromExternal().AsTestDocument(); | ||
test.SetupCacheStore().WithExternalCacheValue("key", "test"); | ||
|
||
test.RunInbound(); | ||
|
||
test.Context.Variables.Should().NotContainKey("variable"); | ||
} | ||
} |