use of org.camunda.bpm.engine.rest.hal.HalLinkResolver in project camunda-bpm-platform by camunda.
the class HalResourceCacheTest method testIdentityLinkCaching.
@Test
public void testIdentityLinkCaching() {
String[] taskIds = new String[] { "test" };
// mock identityLinks and query
IdentityLink link1 = mock(IdentityLink.class);
when(link1.getTaskId()).thenReturn(taskIds[0]);
IdentityLink link2 = mock(IdentityLink.class);
when(link2.getTaskId()).thenReturn(taskIds[0]);
when(processEngine.getTaskService().getIdentityLinksForTask(anyString())).thenReturn(Arrays.asList(link1, link2));
// configure cache
HalRelationCacheConfiguration configuration = new HalRelationCacheConfiguration();
configuration.setCacheImplementationClass(DefaultHalResourceCache.class);
Map<String, Object> halIdentityLinkConfig = new HashMap<String, Object>();
halIdentityLinkConfig.put("capacity", 100);
halIdentityLinkConfig.put("secondsToLive", 10000);
configuration.addCacheConfiguration(HalIdentityLink.class, halIdentityLinkConfig);
contextListener.configureCaches(configuration);
// cache exists and is empty
DefaultHalResourceCache cache = (DefaultHalResourceCache) Hal.getInstance().getHalRelationCache(HalIdentityLink.class);
assertNotNull(cache);
assertEquals(0, cache.size());
// get link resolver and resolve identity link
HalLinkResolver linkResolver = Hal.getInstance().getLinkResolver(IdentityRestService.class);
List<HalResource<?>> halIdentityLinks = linkResolver.resolveLinks(taskIds, processEngine);
assertEquals(2, halIdentityLinks.size());
assertEquals(1, cache.size());
assertEquals(halIdentityLinks, cache.get(taskIds[0]));
}
use of org.camunda.bpm.engine.rest.hal.HalLinkResolver in project camunda-bpm-platform by camunda.
the class HalResourceCacheTest method testEntityCaching.
@Test
public void testEntityCaching() {
String[] userIds = new String[] { "test" };
// mock user and query
User user = mock(User.class);
when(user.getId()).thenReturn(userIds[0]);
when(user.getFirstName()).thenReturn("kermit");
UserQuery userQuery = mock(UserQuery.class);
when(userQuery.userIdIn(Matchers.<String[]>anyVararg())).thenReturn(userQuery);
when(userQuery.listPage(anyInt(), anyInt())).thenReturn(Arrays.asList(user));
when(processEngine.getIdentityService().createUserQuery()).thenReturn(userQuery);
// configure cache
HalRelationCacheConfiguration configuration = new HalRelationCacheConfiguration();
configuration.setCacheImplementationClass(DefaultHalResourceCache.class);
Map<String, Object> halUserConfig = new HashMap<String, Object>();
halUserConfig.put("capacity", 100);
halUserConfig.put("secondsToLive", 10000);
configuration.addCacheConfiguration(HalUser.class, halUserConfig);
contextListener.configureCaches(configuration);
// cache exists and is empty
DefaultHalResourceCache cache = (DefaultHalResourceCache) Hal.getInstance().getHalRelationCache(HalUser.class);
assertNotNull(cache);
assertEquals(0, cache.size());
// get link resolver and resolve user
HalLinkResolver linkResolver = Hal.getInstance().getLinkResolver(UserRestService.class);
List<HalResource<?>> halUsers = linkResolver.resolveLinks(userIds, processEngine);
// mocked user was resolved
assertNotNull(halUsers);
assertEquals(1, halUsers.size());
HalUser halUser = (HalUser) halUsers.get(0);
assertEquals("kermit", halUser.getFirstName());
// cache contains user
assertEquals(1, cache.size());
// change user mock
when(user.getFirstName()).thenReturn("fritz");
// resolve users again
halUsers = linkResolver.resolveLinks(userIds, processEngine);
// cached mocked user was resolved with old name
assertNotNull(halUsers);
assertEquals(1, halUsers.size());
halUser = (HalUser) halUsers.get(0);
assertEquals("kermit", halUser.getFirstName());
forwardTime(cache.getSecondsToLive() * 3);
// resolve users again
halUsers = linkResolver.resolveLinks(userIds, processEngine);
// new mocked user was resolved with old name
assertNotNull(halUsers);
assertEquals(1, halUsers.size());
halUser = (HalUser) halUsers.get(0);
assertEquals("fritz", halUser.getFirstName());
}
Aggregations