/** @return string */ @Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append( "\nRemoteHttpCacheResponse" ); buf.append( "\n success [" + isSuccess() + "]" ); buf.append( "\n payload [" + getPayload() + "]" ); buf.append( "\n errorMessage [" + getErrorMessage() + "]" ); return buf.toString(); } }
/** * Verify get functionality * <p> * @throws IOException */ public void testGetMultiple_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; Set<String> keys = Collections.emptySet(); ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" ); Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>(); expectedMap.put( "key", expected ); RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse = new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>(); remoteHttpCacheResponse.setPayload( expectedMap ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK Map<String, ICacheElement<String, String>> result = client.getMultiple( cacheName, keys ); // VERIFY assertEquals( "Wrong result.", expected, result.get( "key" ) ); assertEquals( "Wrong type.", RemoteRequestType.GET_MULTIPLE, mockDispatcher.lastRemoteCacheRequest.getRequestType() ); }
RemoteCacheResponse<Object> response = new RemoteCacheResponse<Object>(); response.setSuccess( false ); response.setErrorMessage( message ); ICacheElement<Serializable, Serializable> element = remoteCacheService.get( request.getCacheName(), request.getKey(), request.getRequesterId() ); response.setPayload(element); break; case GET_MULTIPLE: response.setPayload(map); response.setPayload(map); case ALIVE_CHECK: case DISPOSE: response.setSuccess( true ); response.setPayload( keys ); break; default: String message = "Unknown event type. Cannot process " + request; log.warn( message ); response.setSuccess( false ); response.setErrorMessage( message ); break; response.setSuccess( false );
/** Verify that we balk and return an error. */ public void testProcessRequest_null() { RemoteCacheRequest<Serializable, Serializable> request = null; // DO WORK RemoteCacheResponse<Object> result = servlet.processRequest( request ); // VERIFY assertNotNull( "Should have a result.", result ); assertTrue( "Should have 'The request is null' in the errorMessage", result.getErrorMessage().indexOf( "The request is null" ) != -1 ); assertTrue( "Should have 'The request is null' in the toString", result.toString().indexOf( "The request is null" ) != -1 ); }
/** * Return the keys in this cache. * <p> * @param cacheName the name of the cache * @see org.apache.commons.jcs.auxiliary.AuxiliaryCache#getKeySet() */ @Override public Set<K> getKeySet( String cacheName ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<String, String> remoteHttpCacheRequest = RemoteCacheRequestFactory.createGetKeySetRequest(cacheName, 0 ); RemoteCacheResponse<Set<K>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( remoteHttpCacheResponse != null && remoteHttpCacheResponse.getPayload() != null ) { return remoteHttpCacheResponse.getPayload(); } return Collections.emptySet(); }
/** * Make and alive request. * <p> * @return true if we make a successful alive request. * @throws IOException */ @Override public boolean isAlive() throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createAliveCheckRequest( 0 ); RemoteCacheResponse<String> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( remoteHttpCacheResponse != null ) { return remoteHttpCacheResponse.isSuccess(); } return false; }
/** * Verify get functionality * <p> * @throws IOException */ public void testGetMultiple_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; Set<String> keys = Collections.emptySet(); ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" ); Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>(); expectedMap.put( "key", expected ); RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse = new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>(); remoteHttpCacheResponse.setPayload( expectedMap ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK Map<String, ICacheElement<String, String>> result = client.getMultiple( cacheName, keys ); // VERIFY assertEquals( "Wrong result.", expected, result.get( "key" ) ); assertEquals( "Wrong type.", RemoteRequestType.GET_MULTIPLE, mockDispatcher.lastRemoteCacheRequest.getRequestType() ); }
RemoteCacheResponse<Object> response = new RemoteCacheResponse<Object>(); response.setSuccess( false ); response.setErrorMessage( message ); ICacheElement<K, V> element = getRemoteCacheService().get( request.getCacheName(), request.getKey(), request.getRequesterId() ); response.setPayload(element); break; case GET_MULTIPLE: response.setPayload(map); response.setPayload(map); break; case ALIVE_CHECK: response.setSuccess( true ); break; case DISPOSE: response.setSuccess( true ); response.setPayload( keys ); break; default: String message = "Unknown event type. Cannot process " + request; log.warn( message ); response.setSuccess( false ); response.setErrorMessage( message );
/** Verify that we balk and return an error. */ public void testProcessRequest_null() { // SETUP RemoteCacheServiceAdaptor<String, String> adaptor = new RemoteCacheServiceAdaptor<String, String>(); MockRemoteCacheService<String, String> remoteHttpCacheService = new MockRemoteCacheService<String, String>(); adaptor.setRemoteCacheService( remoteHttpCacheService ); RemoteCacheRequest<String, String> request = null; // DO WORK RemoteCacheResponse<String> result = adaptor.processRequest( request ); // VERIFY assertNotNull( "Should have a result.", result ); assertTrue( "Should have 'The request is null' in the errorMessage", result.getErrorMessage().indexOf( "The request is null" ) != -1 ); assertTrue( "Should have 'The request is null' in the toString", result.toString().indexOf( "The request is null" ) != -1 ); }
/** * Return the keys in this cache. * <p> * @param cacheName the name of the cache * @see org.apache.commons.jcs.auxiliary.AuxiliaryCache#getKeySet() */ @Override public Set<K> getKeySet( String cacheName ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<String, String> remoteHttpCacheRequest = RemoteCacheRequestFactory.createGetKeySetRequest(cacheName, 0 ); RemoteCacheResponse<Set<K>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( remoteHttpCacheResponse != null && remoteHttpCacheResponse.getPayload() != null ) { return remoteHttpCacheResponse.getPayload(); } return Collections.emptySet(); }
/** * Make and alive request. * <p> * @return true if we make a successful alive request. * @throws IOException */ @Override public boolean isAlive() throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createAliveCheckRequest( 0 ); RemoteCacheResponse<String> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( remoteHttpCacheResponse != null ) { return remoteHttpCacheResponse.isSuccess(); } return false; }
/** * Verify get functionality * <p> * @throws IOException */ public void testGetMatching_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String pattern = "key"; ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" ); Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>(); expectedMap.put( "key", expected ); RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse = new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>(); remoteHttpCacheResponse.setPayload( expectedMap ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK Map<String, ICacheElement<String, String>> result = client.getMatching( cacheName, pattern ); // VERIFY assertEquals( "Wrong result.", expected, result.get( "key" ) ); assertEquals( "Wrong type.", RemoteRequestType.GET_MATCHING, mockDispatcher.lastRemoteCacheRequest.getRequestType() ); }
/** @return string */ @Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append( "\nRemoteHttpCacheResponse" ); buf.append( "\n success [" + isSuccess() + "]" ); buf.append( "\n payload [" + getPayload() + "]" ); buf.append( "\n errorMessage [" + getErrorMessage() + "]" ); return buf.toString(); } }
/** * Gets multiple items from the cache matching the pattern. * <p> * @param cacheName * @param pattern * @param requesterId * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache matching the pattern. * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId ); RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( log.isDebugEnabled() ) { log.debug( "GetMatching [" + pattern + "] = " + remoteHttpCacheResponse ); } return remoteHttpCacheResponse.getPayload(); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGetMatching_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String pattern = "key"; ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" ); Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>(); expectedMap.put( "key", expected ); RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse = new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>(); remoteHttpCacheResponse.setPayload( expectedMap ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK Map<String, ICacheElement<String, String>> result = client.getMatching( cacheName, pattern ); // VERIFY assertEquals( "Wrong result.", expected, result.get( "key" ) ); assertEquals( "Wrong type.", RemoteRequestType.GET_MATCHING, mockDispatcher.lastRemoteCacheRequest.getRequestType() ); }
/** * Gets multiple items from the cache matching the pattern. * <p> * @param cacheName * @param pattern * @param requesterId * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache matching the pattern. * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId ); RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( log.isDebugEnabled() ) { log.debug( "GetMatching [" + pattern + "] = " + remoteHttpCacheResponse ); } return remoteHttpCacheResponse.getPayload(); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGet_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String key = "key"; ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, key, "value" ); RemoteCacheResponse<ICacheElement<String, String>> remoteHttpCacheResponse = new RemoteCacheResponse<ICacheElement<String,String>>(); remoteHttpCacheResponse.setPayload( expected ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK ICacheElement<String, String> result = client.get( cacheName, key ); // VERIFY assertEquals( "Wrong result.", expected, result ); assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Gets multiple items from the cache based on the given set of keys. * <p> * @param cacheName * @param keys * @param requesterId * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache for any of these keys * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId ); RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( log.isDebugEnabled() ) { log.debug( "GetMultiple [" + keys + "] = " + remoteHttpCacheResponse ); } return remoteHttpCacheResponse.getPayload(); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGet_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String key = "key"; ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, key, "value" ); RemoteCacheResponse<ICacheElement<String, String>> remoteHttpCacheResponse = new RemoteCacheResponse<ICacheElement<String,String>>(); remoteHttpCacheResponse.setPayload( expected ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK ICacheElement<String, String> result = client.get( cacheName, key ); // VERIFY assertEquals( "Wrong result.", expected, result ); assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Gets multiple items from the cache based on the given set of keys. * <p> * @param cacheName * @param keys * @param requesterId * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache for any of these keys * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId ); RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); if ( log.isDebugEnabled() ) { log.debug( "GetMultiple [" + keys + "] = " + remoteHttpCacheResponse ); } return remoteHttpCacheResponse.getPayload(); }