<commit_before>package org.finra.herd.service.activiti.task;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import com.google.common.base.Objects;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

import org.finra.herd.dao.JobDefinitionDao;
import org.finra.herd.model.ObjectNotFoundException;
import org.finra.herd.model.dto.ApplicationUser;
import org.finra.herd.model.dto.SecurityUserWrapper;
import org.finra.herd.model.jpa.JobDefinitionEntity;
import org.finra.herd.service.AbstractServiceTest;
import org.finra.herd.service.ActivitiService;
import org.finra.herd.service.activiti.ActivitiHelper;
import org.finra.herd.service.activiti.ActivitiRuntimeHelper;
import org.finra.herd.service.helper.ConfigurationDaoHelper;
import org.finra.herd.service.helper.HerdErrorInformationExceptionHandler;
import org.finra.herd.service.helper.JobDefinitionDaoHelper;
import org.finra.herd.service.helper.UserNamespaceAuthorizationHelper;

/**
 * Test suite which targets BaseJavaDelegate specifically. This test suite uses Mockito injection annotations (ie. does not depend on Spring injection and
 * dependencies are CGLIB proxies created by Mockito).
 * <p/>
 * This test suite updated Spring security context which means it EACH TEST CASE MUST BE CLEANED UP after execution. Without doing so may cause subsequent tests
 * to not execute properly.
 */
public class BaseJavaDelegateTest extends AbstractServiceTest
{
    @Mock
    private ActivitiHelper activitiHelper;

    @Mock
    private ActivitiRuntimeHelper activitiRuntimeHelper;

    @Mock
    private ActivitiService activitiService;

    @Autowired
    @InjectMocks
    private MockJavaDelegate baseJavaDelegate;

    @Mock
    private ConfigurationDaoHelper configurationDaoHelper;

    @Mock
    private DelegateExecution delegateExecution;

    @Mock
    private HerdErrorInformationExceptionHandler errorInformationExceptionHandler;

    @Mock
    private JobDefinitionDao jobDefinitionDao;

    @Mock
    private JobDefinitionDaoHelper jobDefinitionDaoHelper;

    @Mock
    private UserNamespaceAuthorizationHelper userNamespaceAuthorizationHelper;

    @After
    public void after()
    {
        SecurityContextHolder.clearContext();
    }

    @Before
    public void before()
    {
        initMocks(this);
    }

    @Test
    public void testExecute() throws Exception
    {
        // Set up expected values.
        String jobDefinitionNamespace = "jobDefinitionNamespace";
        String jobDefinitionName = "jobDefinitionName";
        String processDefinitionId = "processDefinitionId";
        String processDefinitionKey = String.format("%s.%s", jobDefinitionNamespace, jobDefinitionName);
        String updatedBy = "updatedBy";
        JobDefinitionEntity jobDefinitionEntity = new JobDefinitionEntity();
        jobDefinitionEntity.setUpdatedBy(updatedBy);

        // Mock dependency methods.
        when(delegateExecution.getProcessDefinitionId()).thenReturn(processDefinitionId);
        ProcessDefinition processDefinition = mock(ProcessDefinition.class);
        when(processDefinition.getKey()).thenReturn(processDefinitionKey);
        when(activitiService.getProcessDefinitionById(any())).thenReturn(processDefinition);
        when(jobDefinitionDaoHelper.getJobDefinitionEntity(any(), any())).thenReturn(jobDefinitionEntity);

        // Execute test method.
        baseJavaDelegate.execute(delegateExecution);

        // Verify dependencies were invoked correctly.
        InOrder inOrder = inOrder(configurationDaoHelper, activitiService, jobDefinitionDaoHelper, userNamespaceAuthorizationHelper, activitiRuntimeHelper);
        inOrder.verify(configurationDaoHelper).checkNotAllowedMethod(baseJavaDelegate.getClass().getCanonicalName());
        inOrder.verify(activitiService).getProcessDefinitionById(processDefinitionId);
        inOrder.verify(jobDefinitionDaoHelper).getJobDefinitionEntity(jobDefinitionNamespace, jobDefinitionName);
        inOrder.verify(userNamespaceAuthorizationHelper).buildNamespaceAuthorizations(applicationUserUserIdEq(updatedBy));<commit_msg>DM-9410 fix unit test error<commit_after>package org.finra.herd.service.activiti.task;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import com.google.common.base.Objects;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

import org.finra.herd.dao.JobDefinitionDao;
import org.finra.herd.model.ObjectNotFoundException;
import org.finra.herd.model.dto.ApplicationUser;
import org.finra.herd.model.dto.SecurityUserWrapper;
import org.finra.herd.model.jpa.JobDefinitionEntity;
import org.finra.herd.service.AbstractServiceTest;
import org.finra.herd.service.ActivitiService;
import org.finra.herd.service.activiti.ActivitiHelper;
import org.finra.herd.service.activiti.ActivitiRuntimeHelper;
import org.finra.herd.service.helper.ConfigurationDaoHelper;
import org.finra.herd.service.helper.HerdErrorInformationExceptionHandler;
import org.finra.herd.service.helper.JobDefinitionDaoHelper;
import org.finra.herd.service.helper.UserNamespaceAuthorizationHelper;

/**
 * Test suite which targets BaseJavaDelegate specifically. This test suite uses Mockito injection annotations (ie. does not depend on Spring injection and
 * dependencies are CGLIB proxies created by Mockito).
 * <p/>
 * This test suite updated Spring security context which means it EACH TEST CASE MUST BE CLEANED UP after execution. Without doing so may cause subsequent tests
 * to not execute properly.
 */
public class BaseJavaDelegateTest extends AbstractServiceTest
{
    @Mock
    private ActivitiHelper activitiHelper;

    @Mock
    private ActivitiRuntimeHelper activitiRuntimeHelper;

    @Mock
    private ActivitiService activitiService;

    @Autowired
    @InjectMocks
    private MockJavaDelegate baseJavaDelegate;

    @Mock
    private ConfigurationDaoHelper configurationDaoHelper;

    @Mock
    private DelegateExecution delegateExecution;

    @Mock
    private HerdErrorInformationExceptionHandler errorInformationExceptionHandler;

    @Mock
    private JobDefinitionDao jobDefinitionDao;

    @Mock
    private JobDefinitionDaoHelper jobDefinitionDaoHelper;

    @Mock
    private UserNamespaceAuthorizationHelper userNamespaceAuthorizationHelper;

    @After
    public void after()
    {
        SecurityContextHolder.clearContext();
    }

    @Before
    public void before()
    {
        initMocks(this);
    }

    @Test
    public void testExecute() throws Exception
    {
        // Set up expected values.
        String jobDefinitionNamespace = "jobDefinitionNamespace";
        String jobDefinitionName = "jobDefinitionName";
        String processDefinitionId = "processDefinitionId";
        String processDefinitionKey = String.format("%s.%s", jobDefinitionNamespace, jobDefinitionName);
        String updatedBy = "updatedBy";
        JobDefinitionEntity jobDefinitionEntity = new JobDefinitionEntity();
        jobDefinitionEntity.setUpdatedBy(updatedBy);

        // Mock dependency methods.
        when(delegateExecution.getProcessDefinitionId()).thenReturn(processDefinitionId);
        ProcessDefinition processDefinition = mock(ProcessDefinition.class);
        when(processDefinition.getKey()).thenReturn(processDefinitionKey);
        when(activitiService.getProcessDefinitionById(any())).thenReturn(processDefinition);
        when(jobDefinitionDaoHelper.getJobDefinitionEntity(any(), any())).thenReturn(jobDefinitionEntity);

        // Execute test method.
        baseJavaDelegate.execute(delegateExecution);

        // Verify dependencies were invoked correctly.
        InOrder inOrder = inOrder(configurationDaoHelper, activitiService, jobDefinitionDaoHelper, userNamespaceAuthorizationHelper, activitiRuntimeHelper);
        inOrder.verify(configurationDaoHelper).checkNotAllowedMethod(baseJavaDelegate.getClass().getCanonicalName());
        inOrder.verify(activitiService).getProcessDefinitionById(processDefinitionId);
        inOrder.verify(jobDefinitionDaoHelper).getJobDefinitionEntity(jobDefinitionNamespace, jobDefinitionName);
        inOrder.verify(userNamespaceAuthorizationHelper).buildNamespaceAuthorizations(applicationUserUserIdEq(updatedBy));
        inOrder.verify(activitiRuntimeHelper).setTaskEnvironmentVarialbe(delegateExecution);
{'fraction_non_alphanumeric': 0.07796775769418661, 'fraction_numerical': 0.00039081582804103567, 'mean_word_length': 5.13301378070701, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3408575', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2631, 'n_words': 540}