Search in sources :

Example 6 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_AbsoluteExpiry_Date.

@Test
public void testDoService_AbsoluteExpiry_Date() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests();
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // Expire in 2 seconds or so.
        Date expiry = new Date(System.currentTimeMillis() + ThreadLocalRandom.current().nextInt(1000) + 1000);
        msg.addMetadata("expiry", DateFormatUtil.format(expiry));
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        // Default expiration is > 5 seconds.
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Date(java.util.Date) Test(org.junit.Test)

Example 7 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_RelativeExpiry.

@Test
public void testDoService_RelativeExpiry() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests();
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // relative expiry, 500ms
        msg.addMetadata("expiry", "500");
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        // Default expiration is > 5 seconds.
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 8 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator 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 9 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator 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 10 with StringPayloadCacheTranslator

use of com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator in project interlok by adaptris.

the class GetValueFromCacheTest method testDoService_WithError.

@Test
public void testDoService_WithError() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    Cache cache = createCacheInstanceForTests();
    GetValueFromCache service = new GetValueFromCache().withExceptionIfNotFound(true).withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        service.doService(msg);
        fail();
    } catch (ServiceException expected) {
    } finally {
        stop(service);
    }
}
Also used : ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) Cache(com.adaptris.core.cache.Cache) Test(org.junit.Test)

Aggregations

StringPayloadCacheTranslator (com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)12 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)10 Test (org.junit.Test)10 ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)7 Cache (com.adaptris.core.cache.Cache)5 MyCacheEventListener (com.adaptris.core.cache.MyCacheEventListener)5 ServiceException (com.adaptris.core.ServiceException)3 CacheEntryEvaluator (com.adaptris.core.services.cache.CacheEntryEvaluator)2 JmsReplyToCacheValueTranslator (com.adaptris.core.services.cache.translators.JmsReplyToCacheValueTranslator)2 MetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.MetadataCacheValueTranslator)2 ObjectMetadataCacheValueTranslator (com.adaptris.core.services.cache.translators.ObjectMetadataCacheValueTranslator)2 TimeInterval (com.adaptris.util.TimeInterval)2 ArrayList (java.util.ArrayList)2 AddToCacheService (com.adaptris.core.services.cache.AddToCacheService)1 XpathCacheValueTranslator (com.adaptris.core.services.cache.translators.XpathCacheValueTranslator)1 DefectiveMessageFactory (com.adaptris.core.stubs.DefectiveMessageFactory)1 Date (java.util.Date)1