use of org.infinispan.rest.framework.impl.SimpleRequest in project infinispan by infinispan.
the class RestCacheManagerTest method shouldReuseEncodedCaches.
@Test
public void shouldReuseEncodedCaches() {
EmbeddedCacheManager embeddedCacheManager = Mockito.spy(cacheManager);
RestCacheManager<Object> restCacheManager = new RestCacheManager<>(embeddedCacheManager, c -> Boolean.FALSE);
Map<String, CacheInfo<Object, Object>> knownCaches = TestingUtil.extractField(restCacheManager, "knownCaches");
// Request cache by simple name
SimpleRequest request = new SimpleRequest.Builder().setPath("/test").build();
restCacheManager.getCache("cache1", request);
restCacheManager.getCache("cache2", request);
// Verify they are stored internally
assertEquals(knownCaches.size(), 2);
assertEquals(cachesSize(knownCaches.get("cache1")), 1);
assertEquals(cachesSize(knownCaches.get("cache2")), 1);
// Requesting again should not cause interaction with the cache manager
Mockito.reset(embeddedCacheManager);
restCacheManager.getCache("cache1", request);
restCacheManager.getCache("cache2", request);
Mockito.verify(embeddedCacheManager, never()).getCache("cache1");
Mockito.verify(embeddedCacheManager, never()).getCache("cache2");
assertEquals(cachesSize(knownCaches.get("cache1")), 1);
assertEquals(cachesSize(knownCaches.get("cache2")), 1);
// Request caches with a different media type
restCacheManager.getCache("cache1", MediaType.MATCH_ALL, MediaType.APPLICATION_JSON, request);
restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.TEXT_PLAIN, request);
// Verify they are stored internally
assertEquals(knownCaches.size(), 2);
assertEquals(cachesSize(knownCaches.get("cache1")), 2);
assertEquals(cachesSize(knownCaches.get("cache2")), 2);
// Requesting again with same media type but different parameters should not reuse internal instance
Mockito.reset(embeddedCacheManager);
restCacheManager.getCache("cache1", MediaType.MATCH_ALL, MediaType.fromString("application/json; charset=UTF-8"), request);
restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.fromString("text/plain; charset=SHIFT-JIS"), request);
assertEquals(knownCaches.size(), 2);
assertEquals(cachesSize(knownCaches.get("cache1")), 3);
assertEquals(cachesSize(knownCaches.get("cache2")), 3);
Mockito.verify(embeddedCacheManager, never()).getCache("cache1");
Mockito.verify(embeddedCacheManager, never()).getCache("cache2");
// Requesting with same params should reuse
restCacheManager.getCache("cache1", MediaType.MATCH_ALL, MediaType.fromString("application/json; charset=UTF-8"), request);
restCacheManager.getCache("cache2", MediaType.MATCH_ALL, MediaType.fromString("text/plain; charset=SHIFT-JIS"), request);
assertEquals(cachesSize(knownCaches.get("cache1")), 3);
assertEquals(cachesSize(knownCaches.get("cache2")), 3);
Mockito.verify(embeddedCacheManager, never()).getCache("cache1");
Mockito.verify(embeddedCacheManager, never()).getCache("cache2");
}
Aggregations