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