Search in sources :

Example 11 with ExpiringMapCache

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

the class CacheConditionTest method testExists.

@Test
public void testExists() throws Exception {
    CacheConnection conn = new CacheConnection().withCacheInstance(new ExpiringMapCache());
    ExistsInCache condition = new ExistsInCache().withConnection(conn).withKey("%message{key}");
    try {
        AdaptrisMessage message = DefaultMessageFactory.getDefaultInstance().newMessage();
        LifecycleHelper.initAndStart(condition);
        Cache cache = conn.retrieveCache();
        cache.put("key", "VALUE");
        message.addMessageHeader("key", "key");
        assertTrue(condition.evaluate(message));
    } finally {
        LifecycleHelper.stopAndClose(condition);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) CacheConnection(com.adaptris.core.services.cache.CacheConnection) Cache(com.adaptris.core.cache.Cache) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) Test(org.junit.Test)

Example 12 with ExpiringMapCache

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

the class CacheConditionTest method testDoesNotExist.

@Test
public void testDoesNotExist() throws Exception {
    CacheConnection conn = new CacheConnection().withCacheInstance(new ExpiringMapCache());
    ExistsInCache condition = new ExistsInCache().withConnection(conn).withKey("does-not-exist");
    try {
        AdaptrisMessage message = DefaultMessageFactory.getDefaultInstance().newMessage();
        LifecycleHelper.initAndStart(condition);
        Cache cache = conn.retrieveCache();
        cache.put("key", "VALUE");
        assertFalse(condition.evaluate(message));
    } finally {
        LifecycleHelper.stopAndClose(condition);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) CacheConnection(com.adaptris.core.services.cache.CacheConnection) Cache(com.adaptris.core.cache.Cache) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) Test(org.junit.Test)

Example 13 with ExpiringMapCache

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

the class XmlValidationServiceTest method testValidXmlSchema_Expression.

@Test
public void testValidXmlSchema_Expression() throws Exception {
    String schemaUrl = PROPERTIES.getProperty(KEY_WILL_VALIDATE_SCHEMA);
    // if (!ExternalResourcesHelper.isExternalServerAvailable(new URLString(schemaUrl))) {
    // log.debug(schemaUrl + " not available, ignoring testMetadataSchemaKeyIsEmpty test");
    // return;
    // }
    BasicXmlSchemaValidator validator = new BasicXmlSchemaValidator().withSchema("%message{schema-key}").withSchemaCache(new CacheConnection(new ExpiringMapCache().withMaxEntries(1)));
    AdaptrisMessage msg = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_INPUT_FILE));
    msg.addMetadata("schema-key", schemaUrl);
    XmlValidationService service = new XmlValidationService(validator);
    try {
        LifecycleHelper.initAndStart(service);
        service.doService(msg);
        // Hits the cache the 2nd time round
        service.doService(msg);
    } finally {
        LifecycleHelper.stopAndClose(service);
    }
}
Also used : BasicXmlSchemaValidator(com.adaptris.core.transform.schema.BasicXmlSchemaValidator) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) CacheConnection(com.adaptris.core.services.cache.CacheConnection) Test(org.junit.Test)

Example 14 with ExpiringMapCache

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

the class BasicSchemaValidationTest method testBasicSchemaValidator.

@Test
public void testBasicSchemaValidator() throws Exception {
    String schemaUrl = PROPERTIES.getProperty(KEY_WILL_VALIDATE_SCHEMA);
    BasicXmlSchemaValidator validator = new BasicXmlSchemaValidator().withSchema(schemaUrl).withSchemaCache(new CacheConnection().withCacheInstance(new ExpiringMapCache()));
    XmlValidationService service = new XmlValidationService(validator);
    try {
        LifecycleHelper.initAndStart(service);
        AdaptrisMessage m1 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_INPUT_FILE));
        AdaptrisMessage m2 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_INPUT_FILE));
        service.doService(m1);
        service.doService(m2);
    } finally {
        LifecycleHelper.stopAndClose(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) XmlValidationService(com.adaptris.core.transform.XmlValidationService) CacheConnection(com.adaptris.core.services.cache.CacheConnection) Test(org.junit.Test)

Example 15 with ExpiringMapCache

use of com.adaptris.core.cache.ExpiringMapCache 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)

Aggregations

ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)23 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)21 Test (org.junit.Test)21 CacheConnection (com.adaptris.core.services.cache.CacheConnection)10 MetadataElement (com.adaptris.core.MetadataElement)8 TimeInterval (com.adaptris.util.TimeInterval)8 MyCacheEventListener (com.adaptris.core.cache.MyCacheEventListener)5 StringPayloadCacheTranslator (com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)5 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)3 Cache (com.adaptris.core.cache.Cache)2 XmlValidationService (com.adaptris.core.transform.XmlValidationService)2 CoreException (com.adaptris.core.CoreException)1 ServiceException (com.adaptris.core.ServiceException)1 ServiceList (com.adaptris.core.ServiceList)1 LogMessageService (com.adaptris.core.services.LogMessageService)1 DefectiveMessageFactory (com.adaptris.core.stubs.DefectiveMessageFactory)1 BasicXmlSchemaValidator (com.adaptris.core.transform.schema.BasicXmlSchemaValidator)1 Date (java.util.Date)1