/** * Creates a remove Request. * <p> * @param cacheName * @param key * @param requesterId * @return RemoteHttpCacheRequest */ public static <K, V> RemoteCacheRequest<K, V> createRemoveRequest( String cacheName, K key, long requesterId ) { RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>(); request.setCacheName( cacheName ); request.setKey( key ); request.setRequesterId( requesterId ); request.setRequestType( RemoteRequestType.REMOVE ); if ( log.isDebugEnabled() ) { log.debug( "Created: " + request ); } return request; }
/** @return string */ @Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append( "\nRemoteHttpCacheRequest" ); buf.append( "\n requesterId [" + getRequesterId() + "]" ); buf.append( "\n requestType [" + getRequestType() + "]" ); buf.append( "\n cacheName [" + getCacheName() + "]" ); buf.append( "\n key [" + getKey() + "]" ); buf.append( "\n keySet [" + getKeySet() + "]" ); buf.append( "\n pattern [" + getPattern() + "]" ); buf.append( "\n cacheElement [" + getCacheElement() + "]" ); return buf.toString(); } }
/** * Creates an alive check Request. * <p> * @param requesterId * @return RemoteHttpCacheRequest */ public static <K, V> RemoteCacheRequest<K, V> createAliveCheckRequest( long requesterId ) { RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>(); request.setRequesterId( requesterId ); request.setRequestType( RemoteRequestType.ALIVE_CHECK ); if ( log.isDebugEnabled() ) { log.debug( "Created: " + request ); } return request; }
/** Simple test */ public void testCreateGetMultipleRequest_Normal() { // SETUP String cacheName = "test"; Set<Serializable> keys = Collections.emptySet(); long requesterId = 2; // DO WORK RemoteCacheRequest<Serializable, Serializable> result = RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId ); // VERIFY assertNotNull( "Should have a result", result ); assertEquals( "Wrong cacheName", cacheName, result.getCacheName() ); assertEquals( "Wrong type", RemoteRequestType.GET_MULTIPLE, result.getRequestType() ); }
/** * Verify that we don't get two ?'s */ public void testAddParameters_withQueryString() { // SETUP RemoteHttpCacheAttributes remoteHttpCacheAttributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheDispatcher dispatcher = new RemoteHttpCacheDispatcher( remoteHttpCacheAttributes ); RemoteCacheRequest<String, String> remoteCacheRequest = new RemoteCacheRequest<String, String>(); remoteCacheRequest.setRequestType( RemoteRequestType.REMOVE_ALL ); String cacheName = "myCache"; remoteCacheRequest.setCacheName( cacheName ); String baseUrl = "http://localhost?thishasaquestionmark"; // DO WORK String result = dispatcher.addParameters( remoteCacheRequest, baseUrl ); // VERIFY assertEquals( "Wrong url", baseUrl + "&CacheName=" + cacheName + "&Key=&RequestType=REMOVE_ALL", result ); } }
/** * 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() ); }
/** * Creates an Update Request. * <p> * @param cacheElement * @param requesterId * @return RemoteHttpCacheRequest */ public static <K, V> RemoteCacheRequest<K, V> createUpdateRequest( ICacheElement<K, V> cacheElement, long requesterId ) { RemoteCacheRequest<K, V> request = createRequest(null, RemoteRequestType.UPDATE, requesterId); if ( cacheElement != null ) { request.setCacheName( cacheElement.getCacheName() ); request.setCacheElement( cacheElement ); request.setKey( cacheElement.getKey() ); } else { log.error( "Can't create a proper update request for a null cache element." ); } return request; }
/** Simple test */ public void testCreateGetMultipleRequest_Normal() { // SETUP String cacheName = "test"; Set<Serializable> keys = Collections.emptySet(); long requesterId = 2; // DO WORK RemoteCacheRequest<Serializable, Serializable> result = RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId ); // VERIFY assertNotNull( "Should have a result", result ); assertEquals( "Wrong cacheName", cacheName, result.getCacheName() ); assertEquals( "Wrong type", RemoteRequestType.GET_MULTIPLE, result.getRequestType() ); }
/** * Verify that we don't get two ?'s */ public void testAddParameters_withQueryString() { // SETUP RemoteHttpCacheAttributes remoteHttpCacheAttributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheDispatcher dispatcher = new RemoteHttpCacheDispatcher( remoteHttpCacheAttributes ); RemoteCacheRequest<String, String> remoteCacheRequest = new RemoteCacheRequest<String, String>(); remoteCacheRequest.setRequestType( RemoteRequestType.REMOVE_ALL ); String cacheName = "myCache"; remoteCacheRequest.setCacheName( cacheName ); String baseUrl = "http://localhost?thishasaquestionmark"; // DO WORK String result = dispatcher.addParameters( remoteCacheRequest, baseUrl ); // VERIFY assertEquals( "Wrong url", baseUrl + "&CacheName=" + cacheName + "&Key=&RequestType=REMOVE_ALL", result ); } }
/** * 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() ); }
/** * Create generic request * @param cacheName cache name * @param requestType type of request * @param requesterId id of requester * @return the request */ private static <K, V> RemoteCacheRequest<K, V> createRequest(String cacheName, RemoteRequestType requestType, long requesterId) { RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>(); request.setCacheName( cacheName ); request.setRequestType( requestType ); request.setRequesterId( requesterId ); if ( log.isDebugEnabled() ) { log.debug( "Created: " + request ); } return request; }
/** @return string */ @Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append( "\nRemoteHttpCacheRequest" ); buf.append( "\n requesterId [" + getRequesterId() + "]" ); buf.append( "\n requestType [" + getRequestType() + "]" ); buf.append( "\n cacheName [" + getCacheName() + "]" ); buf.append( "\n key [" + getKey() + "]" ); buf.append( "\n keySet [" + getKeySet() + "]" ); buf.append( "\n pattern [" + getPattern() + "]" ); buf.append( "\n cacheElement [" + getCacheElement() + "]" ); return buf.toString(); } }
/** Simple test */ public void testCreateRemoveAllRequest_Normal() { // SETUP String cacheName = "test"; long requesterId = 2; // DO WORK RemoteCacheRequest<Serializable, Serializable> result = RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId ); // VERIFY assertNotNull( "Should have a result", result ); assertEquals( "Wrong cacheName", cacheName, result.getCacheName() ); assertEquals( "Wrong type", RemoteRequestType.REMOVE_ALL, result.getRequestType() ); }
/** * 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() ); }
/** * Creates a dispose Request. * <p> * @param cacheName * @param requesterId * @return RemoteHttpCacheRequest */ public static <K, V> RemoteCacheRequest<K, V> createDisposeRequest( String cacheName, long requesterId ) { RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>(); request.setCacheName( cacheName ); request.setRequesterId( requesterId ); request.setRequestType( RemoteRequestType.DISPOSE ); if ( log.isDebugEnabled() ) { log.debug( "Created: " + request ); } return request; }
if ( remoteCacheRequest.getCacheName() != null ) + URLEncoder.encode( remoteCacheRequest.getCacheName(), DEFAULT_ENCODING ) ); switch ( remoteCacheRequest.getRequestType() ) keyValue = remoteCacheRequest.getKey() + ""; break; case GET_MATCHING: keyValue = remoteCacheRequest.getPattern(); break; case GET_MULTIPLE: keyValue = remoteCacheRequest.getKeySet() + ""; break; case UPDATE: keyValue = remoteCacheRequest.getCacheElement().getKey() + ""; break; default: + PARAMETER_REQUEST_TYPE + "=" + URLEncoder.encode( remoteCacheRequest.getRequestType().toString(), DEFAULT_ENCODING ) );
/** Simple test */ public void testCreateRemoveAllRequest_Normal() { // SETUP String cacheName = "test"; long requesterId = 2; // DO WORK RemoteCacheRequest<Serializable, Serializable> result = RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId ); // VERIFY assertNotNull( "Should have a result", result ); assertEquals( "Wrong cacheName", cacheName, result.getCacheName() ); assertEquals( "Wrong type", RemoteRequestType.REMOVE_ALL, result.getRequestType() ); }
/** * 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() ); }
/** * Creates a removeAll Request. * <p> * @param cacheName * @param requesterId * @return RemoteHttpCacheRequest */ public static <K, V> RemoteCacheRequest<K, V> createRemoveAllRequest( String cacheName, long requesterId ) { RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>(); request.setCacheName( cacheName ); request.setRequesterId( requesterId ); request.setRequestType( RemoteRequestType.REMOVE_ALL ); if ( log.isDebugEnabled() ) { log.debug( "Created: " + request ); } return request; }
if ( remoteCacheRequest.getCacheName() != null ) + URLEncoder.encode( remoteCacheRequest.getCacheName(), "UTF-8" ) ); switch ( remoteCacheRequest.getRequestType() ) keyValue = remoteCacheRequest.getKey() + ""; break; case REMOVE: keyValue = remoteCacheRequest.getKey() + ""; break; case GET_MATCHING: keyValue = remoteCacheRequest.getPattern(); break; case GET_MULTIPLE: keyValue = remoteCacheRequest.getKeySet() + ""; break; case GET_KEYSET: keyValue = remoteCacheRequest.getKey() + ""; break; case UPDATE: keyValue = remoteCacheRequest.getCacheElement().getKey() + ""; break; default: + PARAMETER_REQUEST_TYPE + "=" + URLEncoder.encode( remoteCacheRequest.getRequestType().toString(), "UTF-8" ) );