Search in sources :

Example 6 with HalResource

use of org.camunda.bpm.engine.rest.hal.HalResource 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());
}
Also used : DefaultHalResourceCache(org.camunda.bpm.engine.rest.hal.cache.DefaultHalResourceCache) HalUser(org.camunda.bpm.engine.rest.hal.user.HalUser) User(org.camunda.bpm.engine.identity.User) HashMap(java.util.HashMap) HalLinkResolver(org.camunda.bpm.engine.rest.hal.HalLinkResolver) HalResource(org.camunda.bpm.engine.rest.hal.HalResource) Matchers.anyString(org.mockito.Matchers.anyString) UserQuery(org.camunda.bpm.engine.identity.UserQuery) HalRelationCacheConfiguration(org.camunda.bpm.engine.rest.hal.cache.HalRelationCacheConfiguration) HalUser(org.camunda.bpm.engine.rest.hal.user.HalUser) Test(org.junit.Test) AbstractRestServiceTest(org.camunda.bpm.engine.rest.AbstractRestServiceTest)

Example 7 with HalResource

use of org.camunda.bpm.engine.rest.hal.HalResource in project camunda-bpm-platform by camunda.

the class HalCachingLinkResolver method resolveLinks.

/**
 * Resolve resources for linked ids, if configured uses a cache.
 */
public List<HalResource<?>> resolveLinks(String[] linkedIds, ProcessEngine processEngine) {
    Cache cache = getCache();
    if (cache == null) {
        return resolveNotCachedLinks(linkedIds, processEngine);
    } else {
        ArrayList<String> notCachedLinkedIds = new ArrayList<String>();
        List<HalResource<?>> resolvedResources = resolveCachedLinks(linkedIds, cache, notCachedLinkedIds);
        if (!notCachedLinkedIds.isEmpty()) {
            List<HalResource<?>> notCachedResources = resolveNotCachedLinks(notCachedLinkedIds.toArray(new String[notCachedLinkedIds.size()]), processEngine);
            resolvedResources.addAll(notCachedResources);
            putIntoCache(notCachedResources);
        }
        sortResolvedResources(resolvedResources);
        return resolvedResources;
    }
}
Also used : ArrayList(java.util.ArrayList) HalResource(org.camunda.bpm.engine.rest.hal.HalResource) Cache(org.camunda.bpm.engine.rest.cache.Cache)

Example 8 with HalResource

use of org.camunda.bpm.engine.rest.hal.HalResource in project camunda-bpm-platform by camunda.

the class HalCaseDefinitionResolver method resolveNotCachedLinks.

protected List<HalResource<?>> resolveNotCachedLinks(String[] linkedIds, ProcessEngine processEngine) {
    RepositoryService repositoryService = processEngine.getRepositoryService();
    List<CaseDefinition> caseDefinitions = repositoryService.createCaseDefinitionQuery().caseDefinitionIdIn(linkedIds).listPage(0, linkedIds.length);
    List<HalResource<?>> resolved = new ArrayList<HalResource<?>>();
    for (CaseDefinition caseDefinition : caseDefinitions) {
        resolved.add(HalCaseDefinition.fromCaseDefinition(caseDefinition, processEngine));
    }
    return resolved;
}
Also used : CaseDefinition(org.camunda.bpm.engine.repository.CaseDefinition) ArrayList(java.util.ArrayList) HalResource(org.camunda.bpm.engine.rest.hal.HalResource) RepositoryService(org.camunda.bpm.engine.RepositoryService)

Example 9 with HalResource

use of org.camunda.bpm.engine.rest.hal.HalResource in project camunda-bpm-platform by camunda.

the class HalGroupResolver method resolveNotCachedLinks.

protected List<HalResource<?>> resolveNotCachedLinks(String[] linkedIds, ProcessEngine processEngine) {
    IdentityService identityService = processEngine.getIdentityService();
    List<Group> groups = identityService.createGroupQuery().groupIdIn(linkedIds).listPage(0, linkedIds.length);
    List<HalResource<?>> resolvedGroups = new ArrayList<HalResource<?>>();
    for (Group group : groups) {
        resolvedGroups.add(HalGroup.fromGroup(group));
    }
    return resolvedGroups;
}
Also used : IdentityService(org.camunda.bpm.engine.IdentityService) Group(org.camunda.bpm.engine.identity.Group) ArrayList(java.util.ArrayList) HalResource(org.camunda.bpm.engine.rest.hal.HalResource)

Example 10 with HalResource

use of org.camunda.bpm.engine.rest.hal.HalResource in project camunda-bpm-platform by camunda.

the class HalProcessDefinitionResolver method resolveNotCachedLinks.

protected List<HalResource<?>> resolveNotCachedLinks(String[] linkedIds, ProcessEngine processEngine) {
    RepositoryService repositoryService = processEngine.getRepositoryService();
    List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().processDefinitionIdIn(linkedIds).listPage(0, linkedIds.length);
    List<HalResource<?>> resolved = new ArrayList<HalResource<?>>();
    for (ProcessDefinition procDef : processDefinitions) {
        resolved.add(HalProcessDefinition.fromProcessDefinition(procDef, processEngine));
    }
    return resolved;
}
Also used : ArrayList(java.util.ArrayList) ProcessDefinition(org.camunda.bpm.engine.repository.ProcessDefinition) HalResource(org.camunda.bpm.engine.rest.hal.HalResource) RepositoryService(org.camunda.bpm.engine.RepositoryService)

Aggregations

HalResource (org.camunda.bpm.engine.rest.hal.HalResource)11 ArrayList (java.util.ArrayList)9 IdentityService (org.camunda.bpm.engine.IdentityService)3 User (org.camunda.bpm.engine.identity.User)3 HashMap (java.util.HashMap)2 RepositoryService (org.camunda.bpm.engine.RepositoryService)2 AbstractRestServiceTest (org.camunda.bpm.engine.rest.AbstractRestServiceTest)2 HalLinkResolver (org.camunda.bpm.engine.rest.hal.HalLinkResolver)2 DefaultHalResourceCache (org.camunda.bpm.engine.rest.hal.cache.DefaultHalResourceCache)2 HalRelationCacheConfiguration (org.camunda.bpm.engine.rest.hal.cache.HalRelationCacheConfiguration)2 IdentityLink (org.camunda.bpm.engine.task.IdentityLink)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 TaskService (org.camunda.bpm.engine.TaskService)1 Group (org.camunda.bpm.engine.identity.Group)1 Tenant (org.camunda.bpm.engine.identity.Tenant)1 UserQuery (org.camunda.bpm.engine.identity.UserQuery)1 CaseDefinition (org.camunda.bpm.engine.repository.CaseDefinition)1 ProcessDefinition (org.camunda.bpm.engine.repository.ProcessDefinition)1 Cache (org.camunda.bpm.engine.rest.cache.Cache)1