<commit_before>package pro.taskana.impl;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import pro.taskana.ClassificationService;
import pro.taskana.WorkbasketService;
import pro.taskana.configuration.TaskanaEngineConfiguration;
import pro.taskana.exceptions.ClassificationNotFoundException;
import pro.taskana.exceptions.NotAuthorizedException;
import pro.taskana.exceptions.TaskNotFoundException;
import pro.taskana.exceptions.WorkbasketNotFoundException;
import pro.taskana.model.*;
import pro.taskana.model.mappings.ObjectReferenceMapper;
import pro.taskana.model.mappings.TaskMapper;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
 * Unit Test for TaskServiceImpl.
 * @author EH
 */
@RunWith(MockitoJUnitRunner.class)
public class TaskServiceImplTest {

    private static final int SLEEP_TIME = 100;

    @InjectMocks
    private TaskServiceImpl cut;

    @Mock
    private TaskanaEngineConfiguration taskanaEngineConfigurationMock;

    @Mock
    private TaskanaEngineImpl taskanaEngineMock;

    @Mock
    private TaskanaEngineImpl taskanaEngineImpl;

    @Mock
    private TaskMapper taskMapperMock;

    @Mock
    private ObjectReferenceMapper objectReferenceMapperMock;

    @Mock
    private WorkbasketService workbasketServiceMock;

    @Mock
    private ClassificationService classificationServiceMock;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        doReturn(workbasketServiceMock).when(taskanaEngineMock).getWorkbasketService();
        doReturn(classificationServiceMock).when(taskanaEngineMock).getClassificationService();
        try {
            Mockito.doNothing().when(workbasketServiceMock).checkAuthorization(any(), any());
        } catch (NotAuthorizedException e) {
             e.printStackTrace();
        }
        Mockito.doNothing().when(taskanaEngineImpl).openConnection();
        Mockito.doNothing().when(taskanaEngineImpl).returnConnection();
    }

    @Test
    public void testCreateSimpleTask() throws NotAuthorizedException, WorkbasketNotFoundException {
        Mockito.doNothing().when(taskMapperMock).insert(any());
        Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1");

        Task actualTask = cut.create(expectedTask);

        verify(taskanaEngineImpl, times(1)).openConnection();
        verify(taskanaEngineMock, times(1)).getWorkbasketService();
        verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any());
        verify(taskMapperMock, times(1)).insert(expectedTask);
        verify(taskanaEngineImpl, times(1)).returnConnection();
        verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl,
                taskMapperMock, objectReferenceMapperMock, workbasketServiceMock);
        assertNull(actualTask.getOwner());
        assertNotNull(actualTask.getCreated());
        assertNotNull(actualTask.getModified());
        assertNull(actualTask.getCompleted());
        assertThat(actualTask.getWorkbasketId(), equalTo(expectedTask.getWorkbasketId()));
        assertThat(actualTask.getName(), equalTo(expectedTask.getName()));
        assertThat(actualTask.getState(), equalTo(TaskState.READY));
    }

    @Test
    public void testCreateSimpleTaskWithObjectReference() throws NotAuthorizedException, WorkbasketNotFoundException {
        ObjectReference expectedObjectReference = new ObjectReference();
        expectedObjectReference.setId("1");
        expectedObjectReference.setType("DUMMY");

        Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1");
        expectedTask.setPrimaryObjRef(new ObjectReference());

        Mockito.doNothing().when(taskMapperMock).insert(any());
        Mockito.doReturn(expectedObjectReference).when(objectReferenceMapperMock).findByObjectReference(any());

        Task actualTask = cut.create(expectedTask);

        verify(taskanaEngineImpl, times(1)).openConnection();
        verify(taskanaEngineMock, times(1)).getWorkbasketService();
        verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any());
        verify(objectReferenceMapperMock, times(1)).findByObjectReference(any());
        verify(taskMapperMock, times(1)).insert(expectedTask);
        verify(taskanaEngineImpl, times(1)).returnConnection();
        verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl,
                taskMapperMock, objectReferenceMapperMock, workbasketServiceMock);

        assertNull(actualTask.getOwner());
        assertNotNull(actualTask.getCreated());
        assertNotNull(actualTask.getModified());
        assertNull(actualTask.getCompleted());
        assertThat(actualTask.getWorkbasketId(), equalTo(expectedTask.getWorkbasketId()));
        assertThat(actualTask.getName(), equalTo(expectedTask.getName()));
        assertThat(actualTask.getState(), equalTo(TaskState.READY));
        assertThat(actualTask.getPrimaryObjRef(), equalTo(expectedObjectReference));
    }

    @Test
    public void testCreateSimpleTaskWithObjectReferenceIsNull() throws NotAuthorizedException, WorkbasketNotFoundException {
        ObjectReference expectedObjectReference = new ObjectReference();
        expectedObjectReference.setId("1");
        expectedObjectReference.setType("DUMMY");

        Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1");
        expectedTask.setPrimaryObjRef(expectedObjectReference);

        Mockito.doNothing().when(taskMapperMock).insert(any());
        Mockito.doNothing().when(objectReferenceMapperMock).insert(any());
        Mockito.doReturn(null).when(objectReferenceMapperMock).findByObjectReference(any());

        Task actualTask = cut.create(expectedTask);
        expectedTask.getPrimaryObjRef().setId(actualTask.getPrimaryObjRef().getId());   // get only new ID

        verify(taskanaEngineImpl, times(1)).openConnection();
        verify(taskanaEngineMock, times(1)).getWorkbasketService();
        verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any());
        verify(objectReferenceMapperMock, times(1)).findByObjectReference(any());
        verify(objectReferenceMapperMock, times(1)).insert(any());
        verify(taskMapperMock, times(1)).insert(expectedTask);
        verify(taskanaEngineImpl, times(1)).returnConnection();
        verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl,
                taskMapperMock, objectReferenceMapperMock, workbasketServiceMock);

        assertNull(actualTask.getOwner());
        assertNotNull(actualTask.getCreated());
        assertNotNull(actualTask.getModified());
        assertNull(actualTask.getCompleted());
        assertThat(actualTask.getWorkbasketId(), equalTo(expectedTask.getWorkbasketId()));
        assertThat(actualTask.getName(), equalTo(expectedTask.getName()));
        assertThat(actualTask.getState(), equalTo(TaskState.READY));
        assertThat(actualTask.getPrimaryObjRef(), equalTo(expectedObjectReference));
    }

    @Test
    public void testCreateManualTask() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException {
        ObjectReference expectedObjectReference = new ObjectReference();
        expectedObjectReference.setId("1");
        expectedObjectReference.setType("DUMMY");

        Workbasket workbasket = new Workbasket();
        Classification classification = new Classification();
        classification.setName("Name");
        classification.setCategory("MANUAL");

        Mockito.doReturn(classification).when(classificationServiceMock).getClassification(any(), any());
        Mockito.doNothing().when(taskMapperMock).insert(any());
        Mockito.doNothing().when(objectReferenceMapperMock).insert(any());
<commit_msg>TSK-43 Add businessProcessId and parentBusinessProcessId to Task and TaskMapper<commit_after>package pro.taskana.impl;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import pro.taskana.ClassificationService;
import pro.taskana.WorkbasketService;
import pro.taskana.configuration.TaskanaEngineConfiguration;
import pro.taskana.exceptions.ClassificationNotFoundException;
import pro.taskana.exceptions.NotAuthorizedException;
import pro.taskana.exceptions.TaskNotFoundException;
import pro.taskana.exceptions.WorkbasketNotFoundException;
import pro.taskana.model.*;
import pro.taskana.model.mappings.ObjectReferenceMapper;
import pro.taskana.model.mappings.TaskMapper;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
 * Unit Test for TaskServiceImpl.
 * @author EH
 */
@RunWith(MockitoJUnitRunner.class)
public class TaskServiceImplTest {

    private static final int SLEEP_TIME = 100;

    @InjectMocks
    private TaskServiceImpl cut;

    @Mock
    private TaskanaEngineConfiguration taskanaEngineConfigurationMock;

    @Mock
    private TaskanaEngineImpl taskanaEngineMock;

    @Mock
    private TaskanaEngineImpl taskanaEngineImpl;

    @Mock
    private TaskMapper taskMapperMock;

    @Mock
    private ObjectReferenceMapper objectReferenceMapperMock;

    @Mock
    private WorkbasketService workbasketServiceMock;

    @Mock
    private ClassificationService classificationServiceMock;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        doReturn(workbasketServiceMock).when(taskanaEngineMock).getWorkbasketService();
        doReturn(classificationServiceMock).when(taskanaEngineMock).getClassificationService();
        try {
            Mockito.doNothing().when(workbasketServiceMock).checkAuthorization(any(), any());
        } catch (NotAuthorizedException e) {
             e.printStackTrace();
        }
        Mockito.doNothing().when(taskanaEngineImpl).openConnection();
        Mockito.doNothing().when(taskanaEngineImpl).returnConnection();
    }

    @Test
    public void testCreateSimpleTask() throws NotAuthorizedException, WorkbasketNotFoundException {
        Mockito.doNothing().when(taskMapperMock).insert(any());
        Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1");

        Task actualTask = cut.create(expectedTask);

        verify(taskanaEngineImpl, times(1)).openConnection();
        verify(taskanaEngineMock, times(1)).getWorkbasketService();
        verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any());
        verify(taskMapperMock, times(1)).insert(expectedTask);
        verify(taskanaEngineImpl, times(1)).returnConnection();
        verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl,
                taskMapperMock, objectReferenceMapperMock, workbasketServiceMock);
        assertNull(actualTask.getOwner());
        assertNotNull(actualTask.getCreated());
        assertNotNull(actualTask.getModified());
        assertNull(actualTask.getCompleted());
        assertThat(actualTask.getWorkbasketId(), equalTo(expectedTask.getWorkbasketId()));
        assertThat(actualTask.getName(), equalTo(expectedTask.getName()));
        assertThat(actualTask.getState(), equalTo(TaskState.READY));
    }

    @Test
    public void testCreateSimpleTaskWithObjectReference() throws NotAuthorizedException, WorkbasketNotFoundException {
        ObjectReference expectedObjectReference = new ObjectReference();
        expectedObjectReference.setId("1");
        expectedObjectReference.setType("DUMMY");

        Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1");
        expectedTask.setPrimaryObjRef(new ObjectReference());

        Mockito.doNothing().when(taskMapperMock).insert(any());
        Mockito.doReturn(expectedObjectReference).when(objectReferenceMapperMock).findByObjectReference(any());

        Task actualTask = cut.create(expectedTask);

        verify(taskanaEngineImpl, times(1)).openConnection();
        verify(taskanaEngineMock, times(1)).getWorkbasketService();
        verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any());
        verify(objectReferenceMapperMock, times(1)).findByObjectReference(any());
        verify(taskMapperMock, times(1)).insert(expectedTask);
        verify(taskanaEngineImpl, times(1)).returnConnection();
        verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl,
                taskMapperMock, objectReferenceMapperMock, workbasketServiceMock);

        assertNull(actualTask.getOwner());
        assertNotNull(actualTask.getCreated());
        assertNotNull(actualTask.getModified());
        assertNull(actualTask.getCompleted());
        assertThat(actualTask.getWorkbasketId(), equalTo(expectedTask.getWorkbasketId()));
        assertThat(actualTask.getName(), equalTo(expectedTask.getName()));
        assertThat(actualTask.getState(), equalTo(TaskState.READY));
        assertThat(actualTask.getPrimaryObjRef(), equalTo(expectedObjectReference));
    }

    @Test
    public void testCreateSimpleTaskWithObjectReferenceIsNull() throws NotAuthorizedException, WorkbasketNotFoundException {
        ObjectReference expectedObjectReference = new ObjectReference();
        expectedObjectReference.setId("1");
        expectedObjectReference.setType("DUMMY");

        Task expectedTask = createUnitTestTask("1", "DUMMYTASK", "1");
        expectedTask.setPrimaryObjRef(expectedObjectReference);

        Mockito.doNothing().when(taskMapperMock).insert(any());
        Mockito.doNothing().when(objectReferenceMapperMock).insert(any());
        Mockito.doReturn(null).when(objectReferenceMapperMock).findByObjectReference(any());

        Task actualTask = cut.create(expectedTask);
        expectedTask.getPrimaryObjRef().setId(actualTask.getPrimaryObjRef().getId());   // get only new ID

        verify(taskanaEngineImpl, times(1)).openConnection();
        verify(taskanaEngineMock, times(1)).getWorkbasketService();
        verify(workbasketServiceMock, times(1)).checkAuthorization(any(), any());
        verify(objectReferenceMapperMock, times(1)).findByObjectReference(any());
        verify(objectReferenceMapperMock, times(1)).insert(any());
        verify(taskMapperMock, times(1)).insert(expectedTask);
        verify(taskanaEngineImpl, times(1)).returnConnection();
        verifyNoMoreInteractions(taskanaEngineConfigurationMock, taskanaEngineMock, taskanaEngineImpl,
                taskMapperMock, objectReferenceMapperMock, workbasketServiceMock);

        assertNull(actualTask.getOwner());
        assertNotNull(actualTask.getCreated());
        assertNotNull(actualTask.getModified());
        assertNull(actualTask.getCompleted());
        assertThat(actualTask.getWorkbasketId(), equalTo(expectedTask.getWorkbasketId()));
        assertThat(actualTask.getName(), equalTo(expectedTask.getName()));
        assertThat(actualTask.getState(), equalTo(TaskState.READY));
        assertThat(actualTask.getPrimaryObjRef(), equalTo(expectedObjectReference));
    }

    @Test
    public void testCreateManualTask() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException {
        ObjectReference expectedObjectReference = new ObjectReference();
        expectedObjectReference.setId("1");
        expectedObjectReference.setType("DUMMY");

        Classification classification = new Classification();
        classification.setName("Name");
        classification.setCategory("MANUAL");

        Mockito.doReturn(classification).when(classificationServiceMock).getClassification(any(), any());
        Mockito.doNothing().when(taskMapperMock).insert(any());
        Mockito.doNothing().when(objectReferenceMapperMock).insert(any());

{'fraction_non_alphanumeric': 0.10796961659030624, 'fraction_numerical': 0.0037376416686761512, 'mean_word_length': 4.758070114543561, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022135', 'n_tokens_mistral': 4300, 'n_tokens_neox': 4259, 'n_words': 642}