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 CacheLookupValueTests
12+ {
13+ class SimpleCacheLookupValuePreferExternal : IDocument
14+ {
15+ public void Inbound ( IInboundContext context )
16+ {
17+ context . CacheLookupValue ( new CacheLookupValueConfig ( )
18+ {
19+ Key = "key" , VariableName = "variable" , CachingType = "prefer-external"
20+ } ) ;
21+ }
22+ }
23+
24+ class SimpleCacheLookupValueFromInternal : IDocument
25+ {
26+ public void Inbound ( IInboundContext context )
27+ {
28+ context . CacheLookupValue ( new CacheLookupValueConfig ( )
29+ {
30+ Key = "key" , VariableName = "variable" , CachingType = "internal"
31+ } ) ;
32+ }
33+ }
34+
35+ class SimpleCacheLookupValueFromExternal : IDocument
36+ {
37+ public void Inbound ( IInboundContext context )
38+ {
39+ context . CacheLookupValue ( new CacheLookupValueConfig ( )
40+ {
41+ Key = "key" , VariableName = "variable" , CachingType = "external"
42+ } ) ;
43+ }
44+ }
45+
46+ class SimpleCacheLookupValueWithDefaultValue : IDocument
47+ {
48+ public void Inbound ( IInboundContext context )
49+ {
50+ context . CacheLookupValue ( new CacheLookupValueConfig ( )
51+ {
52+ Key = "key" , VariableName = "variable" , DefaultValue = "test-value"
53+ } ) ;
54+ }
55+ }
56+
57+ [ TestMethod ]
58+ public void CacheLookupValue_Callback ( )
59+ {
60+ var test = new SimpleCacheLookupValuePreferExternal ( ) . AsTestDocument ( ) ;
61+ var executedCallback = false ;
62+ test . SetupInbound ( ) . CacheLookupValue ( ) . WithCallback ( ( _ , _ ) =>
63+ {
64+ executedCallback = true ;
65+ } ) ;
66+
67+ test . RunInbound ( ) ;
68+
69+ executedCallback . Should ( ) . BeTrue ( ) ;
70+ }
71+
72+ [ TestMethod ]
73+ public void CacheLookupValue_WithValueCallback ( )
74+ {
75+ var test = new SimpleCacheLookupValuePreferExternal ( ) . AsTestDocument ( ) ;
76+ test . SetupInbound ( ) . CacheLookupValue ( ) . WithValue ( "test" ) ;
77+
78+ test . RunInbound ( ) ;
79+
80+ test . Context . Variables . Should ( ) . ContainKey ( "variable" ) . WhoseValue . Should ( ) . Be ( "test" ) ;
81+ }
82+
83+ [ TestMethod ]
84+ public void CacheLookupValue_PreferExternal_SetupCacheStore_WithInternalValue ( )
85+ {
86+ var test = new SimpleCacheLookupValuePreferExternal ( ) . AsTestDocument ( ) ;
87+ test . SetupCacheStore ( ) . WithInternalCacheValue ( "key" , "test" ) ;
88+
89+ test . RunInbound ( ) ;
90+
91+ test . Context . Variables . Should ( ) . ContainKey ( "variable" ) . WhoseValue . Should ( ) . Be ( "test" ) ;
92+ }
93+
94+ [ TestMethod ]
95+ public void CacheLookupValue_PreferExternal_SetupCacheStore_WithExternalCacheSetup ( )
96+ {
97+ var test = new SimpleCacheLookupValuePreferExternal ( ) . AsTestDocument ( ) ;
98+ test . SetupCacheStore ( ) . WithExternalCacheSetup ( ) . WithExternalCacheValue ( "key" , "test" ) ;
99+
100+ test . RunInbound ( ) ;
101+
102+ test . Context . Variables . Should ( ) . ContainKey ( "variable" ) . WhoseValue . Should ( ) . Be ( "test" ) ;
103+ }
104+
105+ [ TestMethod ]
106+ public void CacheLookupValue_WithDefaultValue ( )
107+ {
108+ var test = new SimpleCacheLookupValueWithDefaultValue ( ) . AsTestDocument ( ) ;
109+
110+ test . RunInbound ( ) ;
111+
112+ test . Context . Variables . Should ( ) . ContainKey ( "variable" ) . WhoseValue . Should ( ) . Be ( "test-value" ) ;
113+ }
114+
115+ [ TestMethod ]
116+ public void CacheLookupValue_InternalCache_WillNotFindValueFromExternalCache ( )
117+ {
118+ var test = new SimpleCacheLookupValueFromInternal ( ) . AsTestDocument ( ) ;
119+ test . SetupCacheStore ( ) . WithExternalCacheSetup ( ) . WithExternalCacheValue ( "key" , "test" ) ;
120+
121+ test . RunInbound ( ) ;
122+
123+ test . Context . Variables . Should ( ) . NotContainKey ( "variable" ) ;
124+ }
125+
126+ [ TestMethod ]
127+ public void CacheLookupValue_Internal_WillNotFindValueInInternalCache ( )
128+ {
129+ var test = new SimpleCacheLookupValueFromInternal ( ) . AsTestDocument ( ) ;
130+
131+ test . RunInbound ( ) ;
132+
133+ test . Context . Variables . Should ( ) . NotContainKey ( "variable" ) ;
134+ }
135+
136+ [ TestMethod ]
137+ public void CacheLookupValue_ExternalCache_WillNotFindValueInInternalCache ( )
138+ {
139+ var test = new SimpleCacheLookupValueFromExternal ( ) . AsTestDocument ( ) ;
140+ test . SetupCacheStore ( ) . WithInternalCacheValue ( "key" , "test" ) ;
141+
142+ test . RunInbound ( ) ;
143+
144+ test . Context . Variables . Should ( ) . NotContainKey ( "variable" ) ;
145+ }
146+
147+ [ TestMethod ]
148+ public void CacheLookupValue_ExternalCache_WillNotFindValue_WhenExternalCacheNotSetup ( )
149+ {
150+ var test = new SimpleCacheLookupValueFromExternal ( ) . AsTestDocument ( ) ;
151+ test . SetupCacheStore ( ) . WithExternalCacheValue ( "key" , "test" ) ;
152+
153+ test . RunInbound ( ) ;
154+
155+ test . Context . Variables . Should ( ) . NotContainKey ( "variable" ) ;
156+ }
157+ }
0 commit comments