001    package org.apache.jcs.auxiliary.remote.http.client;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.IOException;
023    import java.util.Collections;
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Set;
027    
028    import junit.framework.TestCase;
029    
030    import org.apache.jcs.auxiliary.remote.value.RemoteCacheResponse;
031    import org.apache.jcs.auxiliary.remote.value.RemoteRequestType;
032    import org.apache.jcs.engine.CacheElement;
033    import org.apache.jcs.engine.behavior.ICacheElement;
034    
035    /** Unit tests for the client. */
036    public class RemoteHttpCacheClientUnitTest
037        extends TestCase
038    {
039        /**
040         * Verify get functionality
041         * <p>
042         * @throws IOException
043         */
044        public void testGet_nullFromDispatcher()
045            throws IOException
046        {
047            // SETUP
048            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
049            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
050    
051            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
052            client.setRemoteDispatcher( mockDispatcher );
053    
054            String cacheName = "test";
055            String key = "key";
056    
057            mockDispatcher.setupRemoteCacheResponse = null;
058    
059            // DO WORK
060            ICacheElement<String, String> result = client.get( cacheName, key );
061    
062            // VERIFY
063            assertNull( "Wrong result.", result );
064            assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
065                .getRequestType() );
066        }
067    
068        /**
069         * Verify get functionality
070         * <p>
071         * @throws IOException
072         */
073        public void testGet_normal()
074            throws IOException
075        {
076            // SETUP
077            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
078            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
079    
080            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
081            client.setRemoteDispatcher( mockDispatcher );
082    
083            String cacheName = "test";
084            String key = "key";
085    
086            ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, key, "value" );
087            RemoteCacheResponse<ICacheElement<String, String>> remoteHttpCacheResponse =
088                new RemoteCacheResponse<ICacheElement<String,String>>();
089            remoteHttpCacheResponse.setPayload( expected );
090    
091            mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
092    
093            // DO WORK
094            ICacheElement<String, String> result = client.get( cacheName, key );
095    
096            // VERIFY
097            assertEquals( "Wrong result.", expected, result );
098            assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest
099                .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            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
112            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
113    
114            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
115            client.setRemoteDispatcher( mockDispatcher );
116    
117            String cacheName = "test";
118            String pattern = "key";
119    
120            ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" );
121            Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>();
122            expectedMap.put( "key", expected );
123            RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse =
124                new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>();
125            remoteHttpCacheResponse.setPayload( expectedMap );
126    
127            mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
128    
129            // DO WORK
130            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            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
148            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
149    
150            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
151            client.setRemoteDispatcher( mockDispatcher );
152    
153            String cacheName = "test";
154            Set<String> keys = Collections.emptySet();
155    
156            ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" );
157            Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>();
158            expectedMap.put( "key", expected );
159            RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse =
160                new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>();
161            remoteHttpCacheResponse.setPayload( expectedMap );
162    
163            mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse;
164    
165            // DO WORK
166            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            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
184            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
185    
186            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
187            client.setRemoteDispatcher( mockDispatcher );
188    
189            String cacheName = "test";
190            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            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
210            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
211    
212            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
213            client.setRemoteDispatcher( mockDispatcher );
214    
215            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            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
235            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
236    
237            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
238            client.setRemoteDispatcher( mockDispatcher );
239    
240            String cacheName = "test";
241    
242            ICacheElement<String, String> element = new CacheElement<String, String>( 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            RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes();
262            RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes );
263    
264            MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher();
265            client.setRemoteDispatcher( mockDispatcher );
266    
267            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    }