/*
* Copyright 2010-2020 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.engine.test.api.task;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.api.runtime.shared.identity.UserGroupManager;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.VariableInstanceEntity;
import org.activiti.engine.impl.test.PluggableActivitiTestCase;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.DelegationState;
import org.activiti.engine.task.IdentityLinkType;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.activiti.engine.test.Deployment;
import org.mockito.Mockito;
/**
*/
public class TaskQueryTest extends PluggableActivitiTestCase {
private List<String> taskIds;
private static final String KERMIT = "kermit";
private static final List<String> KERMITSGROUPS = asList("management","accountancy");
private static final String GONZO = "gonzo";
private static final List<String> GONZOSGROUPS = asList();
private static final String FOZZIE = "fozzie";
private static final List<String> FOZZIESGROUPS = asList("management");
private static final String SCOOTER = "scooter";
private static final List<String> SCOOTERSGROUPS = null;
private UserGroupManager userGroupManager = Mockito.mock(UserGroupManager.class);
public void setUp() throws Exception {
ProcessEngineConfigurationImpl engineConfiguration = (ProcessEngineConfigurationImpl)cachedProcessEngine.getProcessEngineConfiguration();
engineConfiguration.setUserGroupManager(userGroupManager);
taskIds = generateTestTasks();
}
public void tearDown() throws Exception {
taskService.deleteTasks(taskIds, true);
}
public void testBasicTaskPropertiesNotNull() {
Task task = taskService.createTaskQuery().taskId(taskIds.get(0)).singleResult();
assertThat(task.getDescription()).isNotNull();
assertThat(task.getId()).isNotNull();
assertThat(task.getName()).isNotNull();
assertThat(task.getCreateTime()).isNotNull();
}
public void testQueryNoCriteria() {
TaskQuery query = taskService.createTaskQuery();
assertThat(query.count()).isEqualTo(12);
assertThat(query.list()).hasSize(12);
assertThatExceptionOfType(ActivitiException.class)
.isThrownBy(() -> query.singleResult());
}
public void testQueryByTaskId() {
TaskQuery query = taskService.createTaskQuery().taskId(taskIds.get(0));
assertThat(query.singleResult()).isNotNull();
assertThat(query.list()).hasSize(1);
assertThat(query.count()).isEqualTo(1);
}
public void testQueryByTaskIdOr() {
TaskQuery query = taskService.createTaskQuery().or().taskId(taskIds.get(0)).taskName("INVALID NAME").endOr();
assertThat(query.singleResult()).isNotNull();
assertThat(query.list()).hasSize(1);
assertThat(query.count()).isEqualTo(1);
}
public void testQueryByInvalidTaskId() {
TaskQuery query = taskService.createTaskQuery().taskId("invalid");
assertThat(query.singleResult()).isNull();
assertThat(query.list()).hasSize(0);
assertThat(query.count()).isEqualTo(0);
assertThatExceptionOfType(ActivitiIllegalArgumentException.class)
.isThrownBy(() -> taskService.createTaskQuery().taskId(null));
}
public void testQueryByInvalidTaskIdOr() {
TaskQuery query = taskService.createTaskQuery().or().taskId("invalid").taskName("invalid");
assertThat(query.singleResult()).isNull();
assertThat(query.list()).hasSize(0);
assertThat(query.count()).isEqualTo(0);
assertThatExceptionOfType(ActivitiIllegalArgumentException.class)
.isThrownBy(() -> taskService.createTaskQuery().taskId(null));
}
public void testQueryByName() {
TaskQuery query = taskService.createTaskQuery().taskName("testTask");
assertThat(query.list()).hasSize(6);
assertThat(query.count()).isEqualTo(6);
assertThatExceptionOfType(ActivitiException.class)
.isThrownBy(() -> query.singleResult());
}
public void testQueryByNameOr() {
TaskQuery query = taskService.createTaskQuery().or().taskName("testTask").taskId("invalid");
assertThat(query.list()).hasSize(6);
assertThat(query.count()).isEqualTo(6);
assertThatExceptionOfType(ActivitiException.class)
.isThrownBy(() -> query.singleResult());
}
public void testQueryByInvalidName() {
TaskQuery query = taskService.createTaskQuery().taskName("invalid");
assertThat(query.singleResult()).isNull();
assertThat(query.list()).hasSize(0);
assertThat(query.count()).isEqualTo(0);
assertThatExceptionOfType(ActivitiIllegalArgumentException.class)
.isThrownBy(() -> taskService.createTaskQuery().or().taskId("invalid").taskName(null).singleResult());
}
public void testQueryByInvalidNameOr() {
TaskQuery query = taskService.createTaskQuery().or().taskId("invalid").taskName("invalid");
assertThat(query.singleResult()).isNull();
assertThat(query.list()).hasSize(0);
assertThat(query.count()).isEqualTo(0);
assertThatExceptionOfType(ActivitiIllegalArgumentException.class)
.isThrownBy(() -> taskService.createTaskQuery().or().taskId("invalid").taskName(null).singleResult());
}
public void testQueryByNameIn() {
final List<String> taskNameList = new ArrayList<String>(2);
taskNameList.add("testTask");
taskNameList.add("gonzoTask");
TaskQuery query = taskService.createTaskQuery().taskNameIn(taskNameList);
assertThat(query.list()).hasSize(7);
assertThat(query.count()).isEqualTo(7);
assertThatExceptionOfType(ActivitiException.class)
.isThrownBy(() -> query.singleResult());
}
public void testQueryByNameInIgnoreCase() {
final List<String> taskNameList = new ArrayList<String>(2);
taskNameList.add("testtask");
taskNameList.add("gonzotask");
TaskQuery query = taskService.createTaskQuery().taskNameInIgnoreCase(taskNameList);
assertThat(query.list()).hasSize(7);
assertThat(query.count()).isEqualTo(7);
assertThatExceptionOfType(ActivitiException.class)
.isThrownBy(() -> query.singleResult());
}
public void testQueryByNameInOr() {
final List<String> taskNameList = new ArrayList<String>(2);
taskNameList.add("testTask");
taskNameList.add("gonzoTask");
TaskQuery query = taskService.createTaskQuery().or().taskNameIn(taskNameList).taskId("invalid");
assertThat(query.list()).hasSize(7);
assertThat(query.count()).isEqualTo(7);
assertThatExceptionOfType(ActivitiException.class)
.isThrownBy(() -> query.singleResult());
}
public void testQueryByNameInIgnoreCaseOr() {
final List<String> taskNameList = new ArrayList<String>(2);
taskNameList.add