Search in sources :

Example 26 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class AddToCacheServiceTest method testDoService_EnforceSerializable_Serializable.

@Test
public void testDoService_EnforceSerializable_Serializable() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(JmsConstants.JMS_CORRELATION_ID, CORRELATION_ID), new MetadataElement(SRC_KEY, SRC_VALUE) }));
    Cache cache = createCacheInstanceForTests();
    AddToCacheService service = createServiceForTests();
    CacheEntryEvaluator eval = service.getCacheEntryEvaluators().get(0);
    eval.setValueTranslator(new MetadataCacheValueTranslator(SRC_KEY));
    try {
        service.setConnection(new CacheConnection(cache));
        service.setEnforceSerializable(true);
        start(service);
        service.doService(msg);
        Object value = cache.get(CORRELATION_ID);
        assertEquals(SRC_VALUE, value.toString());
    } finally {
        stop(service);
    }
}
Also used : MetadataCacheValueTranslator(com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 27 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class AddToCacheServiceTest method testDoService_NoErrorOnEmpty_NeitherKeyOrValue.

@Test
public void testDoService_NoErrorOnEmpty_NeitherKeyOrValue() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(JmsConstants.JMS_CORRELATION_ID, CORRELATION_ID) }));
    Cache cache = createCacheInstanceForTests();
    AddToCacheService service = createServiceForTests();
    CacheEntryEvaluator eval = service.getCacheEntryEvaluators().get(0);
    eval.setErrorOnEmptyKey(false);
    eval.setErrorOnEmptyValue(false);
    eval.setValueTranslator(new MetadataCacheValueTranslator(SRC_KEY));
    eval.setKeyTranslator(new MetadataCacheValueTranslator(SRC_KEY));
    try {
        service.setConnection(new CacheConnection(cache));
        start(service);
        service.doService(msg);
        assertNull(cache.get(SRC_VALUE));
    } finally {
        stop(service);
    }
}
Also used : MetadataCacheValueTranslator(com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 28 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_WithError.

@Test
public void testDoService_WithError() throws Exception {
    AdaptrisMessage msg = new DefectiveMessageFactory(EnumSet.of(WhenToBreak.METADATA_GET)).newMessage("Hello World");
    msg.addMetadata("metadataKey", "value");
    Cache cache = createCacheInstanceForTests();
    AddValueToCache service = new AddValueToCache().withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{metadataKey}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        service.doService(msg);
        fail();
    } catch (ServiceException expected) {
    } finally {
        stop(service);
    }
}
Also used : DefectiveMessageFactory(com.adaptris.core.stubs.DefectiveMessageFactory) ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 29 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class AddValueToCacheTest method testDoService.

@Test
public void testDoService() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    Cache cache = createCacheInstanceForTests();
    AddValueToCache service = new AddValueToCache().withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Example 30 with Cache

use of com.adaptris.core.cache.Cache in project interlok by adaptris.

the class ClearCacheServiceTest method testDoService_NotQuietly.

@Test(expected = ServiceException.class)
public void testDoService_NotQuietly() throws Exception {
    Cache mockCache = Mockito.mock(Cache.class);
    Mockito.doThrow(new UnsupportedOperationException()).when(mockCache).clear();
    CacheConnection cache = new CacheConnection(mockCache);
    ClearCacheService service = new ClearCacheService().withConnection(cache);
    ExampleServiceCase.execute(service, AdaptrisMessageFactory.getDefaultInstance().newMessage());
}
Also used : Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Aggregations

Cache (com.adaptris.core.cache.Cache)36 Test (org.junit.Test)27 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)23 ServiceException (com.adaptris.core.ServiceException)15 MetadataElement (com.adaptris.core.MetadataElement)14 ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)6 MetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator)5 StringPayloadCacheTranslator (com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)5 CacheConnection (com.adaptris.core.services.cache.CacheConnection)4 CoreException (com.adaptris.core.CoreException)2 StaticCacheValueTranslator (com.adaptris.core.services.cache.translators.StaticCacheValueTranslator)2 DefectiveMessageFactory (com.adaptris.core.stubs.DefectiveMessageFactory)2 UnresolvedMetadataException (com.adaptris.core.UnresolvedMetadataException)1 CacheExpiry (com.adaptris.core.cache.CacheExpiry)1 Expiry (com.adaptris.core.cache.CacheExpiry.Expiry)1 CacheProvider (com.adaptris.core.cache.CacheProvider)1 TimeInterval (com.adaptris.util.TimeInterval)1 Serializable (java.io.Serializable)1 URL (java.net.URL)1 Queue (javax.jms.Queue)1