Skip to content

Commit

Permalink
fix(process): delete remaining resources on deletion (#2592)
Browse files Browse the repository at this point in the history
  • Loading branch information
DumitruCorini authored Jun 7, 2023
1 parent d339106 commit 72fdbd5
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
**/
package org.bonitasoft.engine.process;

import static java.util.Collections.singletonList;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bonitasoft.engine.TestWithUser;
import org.bonitasoft.engine.bpm.category.Category;
import org.bonitasoft.engine.bpm.category.CategoryCriterion;
import org.bonitasoft.engine.bpm.comment.ArchivedComment;
import org.bonitasoft.engine.bpm.comment.Comment;
import org.bonitasoft.engine.bpm.document.Document;
Expand All @@ -38,13 +41,15 @@
import org.bonitasoft.engine.bpm.process.impl.DocumentDefinitionBuilder;
import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder;
import org.bonitasoft.engine.bpm.process.impl.SubProcessDefinitionBuilder;
import org.bonitasoft.engine.bpm.supervisor.ProcessSupervisorSearchDescriptor;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.exception.DeletionException;
import org.bonitasoft.engine.expression.Expression;
import org.bonitasoft.engine.expression.ExpressionBuilder;
import org.bonitasoft.engine.search.Order;
import org.bonitasoft.engine.search.SearchOptionsBuilder;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.engine.test.BuildTestUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -666,4 +671,92 @@ public void deleteArchivedProcessInstanceAndComments() throws Exception {
assertEquals(0, searchResult.getCount());
}

@Test
public void deleteShouldNotKeepResources() throws Exception {
final DesignProcessDefinition designProcessDefinition = BuildTestUtil
.buildProcessDefinitionWithHumanAndAutomaticSteps("My_Process", "1.0",
Arrays.asList("step1"), Arrays.asList(true));
final ProcessDefinition processDefinition = deployAndEnableProcessWithActor(designProcessDefinition, ACTOR_NAME,
user);

Category category = getProcessAPI().createCategory("processCategory", "category assigned to process");
getProcessAPI().addCategoriesToProcess(processDefinition.getId(), singletonList(category.getId()));

assertEquals(1, getProcessAPI()
.getCategoriesOfProcessDefinition(processDefinition.getId(), 0, 5, CategoryCriterion.NAME_ASC).size());

getProcessAPI().createProcessSupervisorForUser(processDefinition.getId(), user.getId());
getProcessAPI().createProcessSupervisorForGroup(processDefinition.getId(),
createGroup("supervisingGroup").getId());
getProcessAPI().createProcessSupervisorForRole(processDefinition.getId(),
createRole("supervisingRole").getId());

SearchOptionsBuilder builder = new SearchOptionsBuilder(0, 5);
builder.filter(ProcessSupervisorSearchDescriptor.PROCESS_DEFINITION_ID, processDefinition.getId());
assertEquals(3, getProcessAPI().searchProcessSupervisors(builder.done()).getCount());

disableAndDeleteProcess(processDefinition);

assertEquals(0, getProcessAPI()
.getCategoriesOfProcessDefinition(processDefinition.getId(), 0, 5, CategoryCriterion.NAME_ASC).size());
assertEquals(0, getProcessAPI().searchProcessSupervisors(builder.done()).getCount());

assertNotNull(getProcessAPI().getCategory(category.getId()));
}

@Test
public void deleteShouldNotModifyAnotherProcess() throws Exception {
final DesignProcessDefinition designProcessDefinition1 = BuildTestUtil
.buildProcessDefinitionWithHumanAndAutomaticSteps("My_Process1", "1.0",
Arrays.asList("step1"), Arrays.asList(true));
final ProcessDefinition processDefinition1 = deployAndEnableProcessWithActor(designProcessDefinition1,
ACTOR_NAME,
user);

final DesignProcessDefinition designProcessDefinition2 = BuildTestUtil
.buildProcessDefinitionWithHumanAndAutomaticSteps("My_Process2", "1.0",
Arrays.asList("step1"), Arrays.asList(true));
final ProcessDefinition processDefinition2 = deployAndEnableProcessWithActor(designProcessDefinition2,
ACTOR_NAME,
user);

Category category1 = getProcessAPI().createCategory("processCategory1", "category assigned to both processes");
getProcessAPI().addCategoriesToProcess(processDefinition1.getId(), singletonList(category1.getId()));
getProcessAPI().addCategoriesToProcess(processDefinition2.getId(), singletonList(category1.getId()));
Category category2 = getProcessAPI().createCategory("processCategory2",
"category assigned only to second process");
getProcessAPI().addCategoriesToProcess(processDefinition2.getId(), singletonList(category2.getId()));

assertEquals(1, getProcessAPI()
.getCategoriesOfProcessDefinition(processDefinition1.getId(), 0, 5, CategoryCriterion.NAME_ASC).size());
assertEquals(2, getProcessAPI()
.getCategoriesOfProcessDefinition(processDefinition2.getId(), 0, 5, CategoryCriterion.NAME_ASC).size());

getProcessAPI().createProcessSupervisorForUser(processDefinition1.getId(), user.getId());
getProcessAPI().createProcessSupervisorForUser(processDefinition2.getId(), user.getId());

SearchOptionsBuilder builder = new SearchOptionsBuilder(0, 5);
builder.filter(ProcessSupervisorSearchDescriptor.USER_ID, user.getId());
assertEquals(2, getProcessAPI().searchProcessSupervisors(builder.done()).getCount());

disableAndDeleteProcess(processDefinition1);

SearchOptionsBuilder builder2 = new SearchOptionsBuilder(0, 5);
builder2.filter(ProcessSupervisorSearchDescriptor.PROCESS_DEFINITION_ID, processDefinition1.getId());

assertEquals(0, getProcessAPI()
.getCategoriesOfProcessDefinition(processDefinition1.getId(), 0, 5, CategoryCriterion.NAME_ASC).size());
assertEquals(0, getProcessAPI().searchProcessSupervisors(builder2.done()).getCount());

SearchOptionsBuilder builder3 = new SearchOptionsBuilder(0, 5);
builder3.filter(ProcessSupervisorSearchDescriptor.PROCESS_DEFINITION_ID, processDefinition2.getId());

assertEquals(2, getProcessAPI().getCategoriesOfProcessDefinition(processDefinition2.getId(), 0, 5,
CategoryCriterion.NAME_ASC).size());
assertEquals(1, getProcessAPI().searchProcessSupervisors(builder3.done()).getCount());

assertNotNull(getProcessAPI().getCategory(category1.getId()));
assertNotNull(getProcessAPI().getCategory(category2.getId()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.bonitasoft.engine.actor.mapping.ActorMappingService;
Expand Down Expand Up @@ -198,6 +199,7 @@
import org.bonitasoft.engine.bpm.process.V6FormDeployException;
import org.bonitasoft.engine.bpm.process.impl.ProcessInstanceUpdater;
import org.bonitasoft.engine.bpm.supervisor.ProcessSupervisor;
import org.bonitasoft.engine.bpm.supervisor.ProcessSupervisorSearchDescriptor;
import org.bonitasoft.engine.builder.BuilderFactory;
import org.bonitasoft.engine.classloader.ClassLoaderService;
import org.bonitasoft.engine.classloader.SClassLoaderException;
Expand All @@ -210,6 +212,7 @@
import org.bonitasoft.engine.commons.transaction.TransactionContentWithResult;
import org.bonitasoft.engine.core.category.CategoryService;
import org.bonitasoft.engine.core.category.exception.SCategoryAlreadyExistsException;
import org.bonitasoft.engine.core.category.exception.SCategoryException;
import org.bonitasoft.engine.core.category.exception.SCategoryInProcessAlreadyExistsException;
import org.bonitasoft.engine.core.category.exception.SCategoryNotFoundException;
import org.bonitasoft.engine.core.category.model.SCategory;
Expand Down Expand Up @@ -353,14 +356,7 @@
import org.bonitasoft.engine.operation.LeftOperand;
import org.bonitasoft.engine.operation.Operation;
import org.bonitasoft.engine.operation.OperationBuilder;
import org.bonitasoft.engine.persistence.FilterOption;
import org.bonitasoft.engine.persistence.OrderAndField;
import org.bonitasoft.engine.persistence.OrderByOption;
import org.bonitasoft.engine.persistence.OrderByType;
import org.bonitasoft.engine.persistence.PersistentObject;
import org.bonitasoft.engine.persistence.QueryOptions;
import org.bonitasoft.engine.persistence.ReadPersistenceService;
import org.bonitasoft.engine.persistence.SBonitaReadException;
import org.bonitasoft.engine.persistence.*;
import org.bonitasoft.engine.recorder.model.EntityUpdateDescriptor;
import org.bonitasoft.engine.resources.BARResourceType;
import org.bonitasoft.engine.resources.ProcessResourcesService;
Expand Down Expand Up @@ -502,6 +498,10 @@ public void deleteProcessDefinition(final long processDefinitionId) throws Delet
checkIfItIsPossibleToDeleteProcessInstance(processDefinitionId, hasOpenProcessInstances);
final boolean hasArchivedProcessInstances = searchArchivedProcessInstances(searchOptions).getCount() > 0;
checkIfItIsPossibleToDeleteProcessInstance(processDefinitionId, hasArchivedProcessInstances);

removeAllCategoriesFromProcessDefinition(processDefinitionId);
deleteAllSupervisorsOfProcess(processDefinitionId);

processManagementAPIImplDelegate.deleteProcessDefinition(processDefinitionId);
} catch (final Exception e) {
throw new DeletionException(e);
Expand Down Expand Up @@ -2023,6 +2023,11 @@ public long removeCategoriesFromProcessDefinition(final long processDefinitionId
}
}

private void removeAllCategoriesFromProcessDefinition(final long processDefinitionId)
throws SCategoryException, DeletionException {
removeCategoriesFromProcessDefinition(processDefinitionId, 0, Integer.MAX_VALUE);
}

@Override
public long removeProcessDefinitionsFromCategory(final long categoryId, final int startIndex, final int maxResults)
throws DeletionException {
Expand Down Expand Up @@ -4355,6 +4360,23 @@ public void deleteSupervisor(final Long processDefinitionId, final Long userId,
}
}

private void deleteAllSupervisorsOfProcess(final long processDefinitionId) throws DeletionException {
final SearchOptionsBuilder builder = new SearchOptionsBuilder(0, Integer.MAX_VALUE);
builder.filter(ProcessSupervisorSearchDescriptor.PROCESS_DEFINITION_ID, processDefinitionId);
try {
SearchResult<ProcessSupervisor> processSupervisors = searchProcessSupervisors(builder.done());

List<Long> supervisorIds = processSupervisors.getResult().stream().map(ProcessSupervisor::getSupervisorId)
.collect(Collectors.toList());

for (Long id : supervisorIds) {
deleteSupervisor(id);
}
} catch (SearchException e) {
throw new DeletionException(e);
}
}

@Override
public ProcessSupervisor createProcessSupervisorForUser(final long processDefinitionId, final long userId)
throws CreationException, AlreadyExistsException {
Expand Down

0 comments on commit 72fdbd5

Please sign in to comment.