1 package org.apache.commons.jcs3.auxiliary.remote.http.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 import java.io.IOException;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.apache.commons.jcs3.auxiliary.remote.value.RemoteCacheResponse;
31 import org.apache.commons.jcs3.auxiliary.remote.value.RemoteRequestType;
32 import org.apache.commons.jcs3.engine.CacheElement;
33 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
34
35
36 public class RemoteHttpCacheClientUnitTest
37 extends TestCase
38 {
39
40
41
42
43
44 public void testGet_nullFromDispatcher()
45 throws IOException
46 {
47
48 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
49 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
50
51 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
52 client.setRemoteDispatcher( mockDispatcher );
53
54 final String cacheName = "test";
55 final String key = "key";
56
57 mockDispatcher.setupRemoteCacheResponse = null;
58
59
60 final ICacheElement<String, String> result = client.get( cacheName, key );
61
62
63 assertNull( "Wrong result.", result );
64 assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
65 .getRequestType() );
66 }
67
68
69
70
71
72
73 public void testGet_normal()
74 throws IOException
75 {
76
77 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
78 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
79
80 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
81 client.setRemoteDispatcher( mockDispatcher );
82
83 final String cacheName = "test";
84 final String key = "key";
85
86 final ICacheElement<String, String> expected = new CacheElement<>( cacheName, key, "value" );
87 final RemoteCacheResponse<ICacheElement<String, String>> remoteHttpCacheResponse =
88 new RemoteCacheResponse<>();
89 remoteHttpCacheResponse.setPayload( expected );
90
91 mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
92
93
94 final ICacheElement<String, String> result = client.get( cacheName, key );
95
96
97 assertEquals( "Wrong result.", expected, result );
98 assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
99 .getRequestType() );
100 }
101
102
103
104
105
106
107 public void testGetMatching_normal()
108 throws IOException
109 {
110
111 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
112 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
113
114 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
115 client.setRemoteDispatcher( mockDispatcher );
116
117 final String cacheName = "test";
118 final String pattern = "key";
119
120 final ICacheElement<String, String> expected = new CacheElement<>( cacheName, "key", "value" );
121 final Map<String, ICacheElement<String, String>> expectedMap = new HashMap<>();
122 expectedMap.put( "key", expected );
123 final RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse =
124 new RemoteCacheResponse<>();
125 remoteHttpCacheResponse.setPayload( expectedMap );
126
127 mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
128
129
130 final Map<String, ICacheElement<String, String>> result = client.getMatching( cacheName, pattern );
131
132
133 assertEquals( "Wrong result.", expected, result.get( "key" ) );
134 assertEquals( "Wrong type.", RemoteRequestType.GET_MATCHING,
135 mockDispatcher.lastRemoteCacheRequest.getRequestType() );
136 }
137
138
139
140
141
142
143 public void testGetMultiple_normal()
144 throws IOException
145 {
146
147 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
148 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
149
150 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
151 client.setRemoteDispatcher( mockDispatcher );
152
153 final String cacheName = "test";
154 final Set<String> keys = Collections.emptySet();
155
156 final ICacheElement<String, String> expected = new CacheElement<>( cacheName, "key", "value" );
157 final Map<String, ICacheElement<String, String>> expectedMap = new HashMap<>();
158 expectedMap.put( "key", expected );
159 final RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse =
160 new RemoteCacheResponse<>();
161 remoteHttpCacheResponse.setPayload( expectedMap );
162
163 mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
164
165
166 final Map<String, ICacheElement<String, String>> result = client.getMultiple( cacheName, keys );
167
168
169 assertEquals( "Wrong result.", expected, result.get( "key" ) );
170 assertEquals( "Wrong type.", RemoteRequestType.GET_MULTIPLE,
171 mockDispatcher.lastRemoteCacheRequest.getRequestType() );
172 }
173
174
175
176
177
178
179 public void testRemove_normal()
180 throws IOException
181 {
182
183 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
184 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
185
186 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
187 client.setRemoteDispatcher( mockDispatcher );
188
189 final String cacheName = "test";
190 final String key = "key";
191
192
193 client.remove( cacheName, key );
194
195
196 assertEquals( "Wrong type.", RemoteRequestType.REMOVE, mockDispatcher.lastRemoteCacheRequest
197 .getRequestType() );
198 }
199
200
201
202
203
204
205 public void testRemoveAll_normal()
206 throws IOException
207 {
208
209 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
210 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
211
212 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
213 client.setRemoteDispatcher( mockDispatcher );
214
215 final String cacheName = "test";
216
217
218 client.removeAll( cacheName );
219
220
221 assertEquals( "Wrong type.", RemoteRequestType.REMOVE_ALL, mockDispatcher.lastRemoteCacheRequest
222 .getRequestType() );
223 }
224
225
226
227
228
229
230 public void testUpdate_normal()
231 throws IOException
232 {
233
234 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
235 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
236
237 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
238 client.setRemoteDispatcher( mockDispatcher );
239
240 final String cacheName = "test";
241
242 final ICacheElement<String, String> element = new CacheElement<>( cacheName, "key", "value" );
243
244
245 client.update( element );
246
247
248 assertEquals( "Wrong type.", RemoteRequestType.UPDATE, mockDispatcher.lastRemoteCacheRequest
249 .getRequestType() );
250 }
251
252
253
254
255
256
257 public void testDispose_normal()
258 throws IOException
259 {
260
261 final RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
262 final RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<>( attributes );
263
264 final MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
265 client.setRemoteDispatcher( mockDispatcher );
266
267 final String cacheName = "test";
268
269
270 client.dispose( cacheName );
271
272
273 assertEquals( "Wrong type.", RemoteRequestType.DISPOSE, mockDispatcher.lastRemoteCacheRequest
274 .getRequestType() );
275 }
276 }