Search in sources :

Example 21 with ExpiringMapCache

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

the class CheckCacheServiceTest method testDoService_DoesNotUseKeys.

@Test
public void testDoService_DoesNotUseKeys() throws Exception {
    AdaptrisMessage msg = createMessage("Hello World", Arrays.asList(new MetadataElement[] { new MetadataElement(LOOKUP_VALUE, LOOKUP_VALUE) }));
    ExpiringMapCache cache = new KeysSizeUnsupportedCache().withMaxEntries(10).withExpiration(new TimeInterval(10L, TimeUnit.SECONDS));
    CheckCacheService service = createServiceForTests();
    try {
        service.setConnection(new CacheConnection(cache));
        service.setKeysFoundServiceId(FOUND);
        service.setKeysNotFoundServiceId(NOT_FOUND);
        start(service);
        cache.put(LOOKUP_VALUE, LOOKED_UP_VALUE);
        service.doService(msg);
        assertEquals(FOUND, msg.getNextServiceId());
    } finally {
        stop(service);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) MetadataElement(com.adaptris.core.MetadataElement) Test(org.junit.Test)

Example 22 with ExpiringMapCache

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

the class GetAndCacheOauthTokenTest method testService_Expiry.

@Test
public void testService_Expiry() throws Exception {
    ExpiringMapCache cache = new ExpiringMapCache().withExpiration(new TimeInterval(5L, TimeUnit.SECONDS));
    AccessToken t = new AccessToken(getName());
    GetAndCacheOauthToken service = new GetAndCacheOauthToken().withCacheKey("OauthToken").withConnection(new CacheConnection(cache)).withAccessTokenBuilder(new ExpiringAccessToken(t, 1000));
    try {
        AdaptrisMessage m1 = new DefaultMessageFactory().newMessage(TEXT);
        LifecycleHelper.initAndStart(service);
        service.doService(m1);
        AccessToken t1 = (AccessToken) cache.get("OauthToken");
        assertNotNull(t1);
        await().atMost(Duration.ofSeconds(3)).with().pollInterval(Duration.ofMillis(100)).until(() -> cache.size() == 0);
        AdaptrisMessage m2 = new DefaultMessageFactory().newMessage(TEXT);
        service.doService(m2);
        AccessToken t2 = (AccessToken) cache.get("OauthToken");
        assertNotNull(t2);
        assertNotSame(t1, t2);
    } finally {
        LifecycleHelper.stopAndClose(service);
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) CacheConnection(com.adaptris.core.services.cache.CacheConnection) Test(org.junit.Test)

Example 23 with ExpiringMapCache

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

the class GetAndCacheOauthTokenTest method testService.

@Test
public void testService() throws Exception {
    ExpiringMapCache cache = new ExpiringMapCache().withExpiration(new TimeInterval(5L, TimeUnit.SECONDS));
    AccessToken t = new AccessToken(getName());
    GetAndCacheOauthToken service = new GetAndCacheOauthToken().withCacheKey("OauthToken").withConnection(new CacheConnection(cache)).withAccessTokenBuilder(new DummyAccessTokenBuilder(t));
    try {
        AdaptrisMessage m1 = new DefaultMessageFactory().newMessage(TEXT);
        LifecycleHelper.initAndStart(service);
        service.doService(m1);
        assertTrue(m1.headersContainsKey("Authorization"));
        assertEquals("Bearer " + getName(), m1.getMetadataValue("Authorization"));
        AccessToken t1 = (AccessToken) cache.get("OauthToken");
        assertNotNull(t1);
        AdaptrisMessage m2 = new DefaultMessageFactory().newMessage(TEXT);
        service.doService(m2);
        assertTrue(m2.headersContainsKey("Authorization"));
        assertEquals("Bearer " + getName(), m1.getMetadataValue("Authorization"));
        AccessToken t2 = (AccessToken) cache.get("OauthToken");
        assertNotNull(t2);
        assertSame(t1, t2);
    } finally {
        LifecycleHelper.stopAndClose(service);
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) CacheConnection(com.adaptris.core.services.cache.CacheConnection) 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