View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote.http.client;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  /** Unit tests for the client. */
36  public class RemoteHttpCacheClientUnitTest
37      extends TestCase
38  {
39      /**
40       * Verify get functionality
41       * <p>
42       * @throws IOException
43       */
44      public void testGet_nullFromDispatcher()
45          throws IOException
46      {
47          // SETUP
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          // DO WORK
60          final ICacheElement<String, String> result = client.get( cacheName, key );
61  
62          // VERIFY
63          assertNull( "Wrong result.", result );
64          assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
65              .getRequestType() );
66      }
67  
68      /**
69       * Verify get functionality
70       * <p>
71       * @throws IOException
72       */
73      public void testGet_normal()
74          throws IOException
75      {
76          // SETUP
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          // DO WORK
94          final ICacheElement<String, String> result = client.get( cacheName, key );
95  
96          // VERIFY
97          assertEquals( "Wrong result.", expected, result );
98          assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
99              .getRequestType() );
100     }
101 
102     /**
103      * Verify get functionality
104      * <p>
105      * @throws IOException
106      */
107     public void testGetMatching_normal()
108         throws IOException
109     {
110         // SETUP
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         // DO WORK
130         final Map<String, ICacheElement<String, String>> result = client.getMatching( cacheName, pattern );
131 
132         // VERIFY
133         assertEquals( "Wrong result.", expected, result.get( "key" ) );
134         assertEquals( "Wrong type.", RemoteRequestType.GET_MATCHING,
135                       mockDispatcher.lastRemoteCacheRequest.getRequestType() );
136     }
137 
138     /**
139      * Verify get functionality
140      * <p>
141      * @throws IOException
142      */
143     public void testGetMultiple_normal()
144         throws IOException
145     {
146         // SETUP
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         // DO WORK
166         final Map<String, ICacheElement<String, String>> result = client.getMultiple( cacheName, keys );
167 
168         // VERIFY
169         assertEquals( "Wrong result.", expected, result.get( "key" ) );
170         assertEquals( "Wrong type.", RemoteRequestType.GET_MULTIPLE,
171                       mockDispatcher.lastRemoteCacheRequest.getRequestType() );
172     }
173 
174     /**
175      * Verify remove functionality
176      * <p>
177      * @throws IOException
178      */
179     public void testRemove_normal()
180         throws IOException
181     {
182         // SETUP
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         // DO WORK
193         client.remove( cacheName, key );
194 
195         // VERIFY
196         assertEquals( "Wrong type.", RemoteRequestType.REMOVE, mockDispatcher.lastRemoteCacheRequest
197             .getRequestType() );
198     }
199 
200     /**
201      * Verify removeall functionality
202      * <p>
203      * @throws IOException
204      */
205     public void testRemoveAll_normal()
206         throws IOException
207     {
208         // SETUP
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         // DO WORK
218         client.removeAll( cacheName );
219 
220         // VERIFY
221         assertEquals( "Wrong type.", RemoteRequestType.REMOVE_ALL, mockDispatcher.lastRemoteCacheRequest
222             .getRequestType() );
223     }
224 
225     /**
226      * Verify update functionality
227      * <p>
228      * @throws IOException
229      */
230     public void testUpdate_normal()
231         throws IOException
232     {
233         // SETUP
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         // DO WORK
245         client.update( element );
246 
247         // VERIFY
248         assertEquals( "Wrong type.", RemoteRequestType.UPDATE, mockDispatcher.lastRemoteCacheRequest
249             .getRequestType() );
250     }
251 
252     /**
253      * Verify dispose functionality
254      * <p>
255      * @throws IOException
256      */
257     public void testDispose_normal()
258         throws IOException
259     {
260         // SETUP
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         // DO WORK
270         client.dispose( cacheName );
271 
272         // VERIFY
273         assertEquals( "Wrong type.", RemoteRequestType.DISPOSE, mockDispatcher.lastRemoteCacheRequest
274             .getRequestType() );
275     }
276 }