RetrieveFromCacheService extends CacheServiceBase { @Override public void doService(AdaptrisMessage msg) throws ServiceException { try { for (CacheEntryEvaluator ceg : getCacheEntryEvaluators()) { String key = ceg.getKey(msg); if (isEmpty(key)) { log.warn("{} generated null value for the key, nothing to do", ceg.friendlyName()); continue; } addCacheValueToMessage(msg, key, ceg.valueTranslator(), !exceptionIfNotFound()); } } catch (Exception e) { throw ExceptionHelper.wrapServiceException(e); } } RetrieveFromCacheService(); @Override void doService(AdaptrisMessage msg); Boolean getExceptionIfNotFound(); void setExceptionIfNotFound(Boolean b);  }
@Test public void testDoService() throws Exception { AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(SRC_METADATA_KEY, LOOKUP_VALUE) })); Cache cache = createCacheInstanceForTests(); RetrieveFromCacheService service = createServiceForTests(); try { service.setConnection(new CacheConnection(cache)); start(service); cache.put(LOOKUP_VALUE, LOOKED_UP_VALUE); service.doService(msg); assertTrue(msg.headersContainsKey(TARGET_METADATA_KEY)); assertEquals(LOOKED_UP_VALUE, msg.getMetadataValue(TARGET_METADATA_KEY)); } finally { stop(service); } }
@Test public void testDoService_AddToMessageFailure() throws Exception { AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(SRC_METADATA_KEY, LOOKUP_VALUE) })); Cache cache = createCacheInstanceForTests(); RetrieveFromCacheService service = createServiceForTests(); CacheEntryEvaluator eval = service.getCacheEntryEvaluators().get(0); eval.setValueTranslator(new StaticCacheValueTranslator("dummy value")); try { service.setConnection(new CacheConnection(cache)); start(service); cache.put(LOOKUP_VALUE, LOOKED_UP_VALUE); service.doService(msg); fail("Expected a ServiceException"); } catch (ServiceException expected) { } finally { stop(service); } }
@Test public void testDoService_NullValueTranslator() throws Exception { AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(SRC_METADATA_KEY, LOOKUP_VALUE) })); Cache cache = createCacheInstanceForTests(); RetrieveFromCacheService service = createServiceForTests(); CacheEntryEvaluator eval = service.getCacheEntryEvaluators().get(0); eval.setValueTranslator(new StaticCacheValueTranslator("dummy value")); try { service.setConnection(new CacheConnection(cache)); start(service); cache.put(LOOKUP_VALUE, LOOKED_UP_VALUE); service.doService(msg); fail("Expected a ServiceException"); } catch (ServiceException expected) { } finally { stop(service); } }
@Test public void testDoService_KeyNotFound() throws Exception { AdaptrisMessage msg = createMessage("Hello World", new ArrayList<MetadataElement>()); Cache cache = createCacheInstanceForTests(); RetrieveFromCacheService service = createService(); CacheEntryEvaluator eval = new CacheEntryEvaluator(); eval.setErrorOnEmptyKey(false); service.addCacheEntryEvaluator(eval); try { service.setConnection(new CacheConnection(cache)); start(service); service.doService(msg); } finally { stop(service); } }