jira-plugin-intellij /src/main/resources/com/onresolve/jira/groovy/test/BaseSubTaskTest.groovy

Language Groovy Lines 189
MD5 Hash fec302ba8b2773dc2ff05fdf8e02d6d6
Repository https://bitbucket.org/sorin/jira-plugin-intellij View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.onresolve.jira.groovy.test

import org.apache.log4j.Category
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager
import com.atlassian.core.action.ActionUtils
import com.atlassian.jira.action.ActionNames
import com.atlassian.core.ofbiz.CoreFactory
import webwork.dispatcher.ActionResult
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.User

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.event.api.EventPublisher
import com.onresolve.jira.groovy.ScriptManagerImpl
import static junit.framework.Assert.assertNotNull
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.query.Query
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.bc.JiraServiceContext

import static junit.framework.Assert.assertFalse
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.issue.index.IssueIndexManager

/**
 * User: jechlin2
 * Date: 20/12/10
 * Time: 14:49
 */

// todo: the impl is because classes don't seem reloable
class BaseSubTaskTest implements GroovyObject {

    Category log = Category.getInstance(this.class)
    UserUtil userUtil = ComponentManager.getInstance().getUserUtil()
    User adminUser = userUtil.getUser("admin")
    MutableIssue issue
    ComponentManager componentManager = ComponentManager.getInstance()
    Project project = componentManager.getProjectManager().getProjectObjByKey('JRA')
    ConstantsManager constantsManager = componentManager.getConstantsManager()
    IssueType issueType = constantsManager.getIssueTypeObject('1')
    IssueManager issueManager = ManagerFactory.getIssueManager()
    CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
    ProjectComponentManager projectComponentManager = componentManager.getProjectComponentManager()
    VersionManager versionManager = componentManager.getVersionManager()
    IssueLinkManager issueLinkManager = componentManager.getIssueLinkManager()
    CommentManager commentManager = componentManager.getCommentManager()
    ProjectManager projectManager = componentManager.getProjectManager()
    def labelManager = ComponentManager.getComponentInstanceOfType(LabelManager.class)
    def searchRequestManager = componentManager.getSearchRequestManager()
    def searchRequestService = componentManager.getSearchRequestService()
    IssueIndexManager indexManager = ComponentManager.getInstance().getIndexManager()

    public def resolveIssue(MutableIssue theIssue) {
        resolveIssue(theIssue, '1')
    }

    public def resolveIssue(MutableIssue theIssue, String resolution) {
        boolean wasIndexing = ImportUtils.isIndexIssues();
        ImportUtils.setIndexIssues(true);

        WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
        workflowTransitionUtil.setIssue(theIssue);
        workflowTransitionUtil.setUsername(adminUser.getName());
        // assume standard workflow
        workflowTransitionUtil.setAction(5);

        workflowTransitionUtil.setParams([resolution: resolution])
        // validate and transition issue
        workflowTransitionUtil.validate();
        workflowTransitionUtil.progress();

        // set indexing back to previous
        ImportUtils.setIndexIssues(wasIndexing)
    }

    public Issue createSubTask(MutableIssue parent) {
        MutableIssue subtask = componentManager.getIssueFactory().getIssue()
        subtask.setProject(project.getGenericValue())
        subtask.setSummary "subtask summary"
        subtask.setReporterId "admin"
        subtask.setIssueType constantsManager.getAllIssueTypeObjects().find {it.name == 'Sub-task'}.genericValue
        subtask.setAssigneeId "admin"
        subtask.setParentId(parent.id)
        issueManager.createIssue(adminUser, subtask);
        log.debug(subtask.id)

        // note: for some reason we seem to need both the setParent, and creating a subtask link
        componentManager.getSubTaskManager().createSubTaskIssueLink(parent, subtask, adminUser)
        log.debug "Created subtask ${subtask.getKey()} as child of ${parent.getKey()}"

        assert subtask.isSubTask()
        subtask
    }

    public def deleteIssue(Issue theIssue) {
        log.debug("Delete issue $theIssue")
        if (theIssue) {
            Map parameters = [
                    issue: theIssue.getGenericValue(),
                    remoteUser: userUtil.getUser("admin"),
                    dispatchEvent: false,
                    permissionOverride: true,
            ]
            ActionResult aResult = CoreFactory.getActionDispatcher().execute(ActionNames.ISSUE_DELETE, parameters);
            ActionUtils.checkForErrors(aResult)
        }
    }

    def getScriptManager() {
        def eventPublisher = ComponentAccessor.getComponentOfType(EventPublisher.class)
        new ScriptManagerImpl(eventPublisher)
    }

    public deleteAllIssues() {
        deleteAllIssues(project)
    }

    public deleteAllIssues(Project fromProject) {
        JqlQueryBuilder jqlQueryBuilder = JqlQueryBuilder.newBuilder()
        Query query = jqlQueryBuilder.where().project(fromProject.id).buildQuery()
        log.debug("Execute query: " + query.toString())
        SearchRequest sr = new SearchRequest(query)
        SearchProvider searchProvider = componentManager.getSearchProvider()
        SearchResults results = searchProvider.search(sr.getQuery(), componentManager.getJiraAuthenticationContext().getUser(), PagerFilter.getUnlimitedFilter())
        results.getIssues().each {Issue issue ->
            def theIssue = issueManager.getIssueObject(issue.id)
            if (theIssue) {
                deleteIssue(theIssue)
            }
        }
    }

    public void createIssue(String assigneeId, Project overrideProject = null) {
        log.debug("setUp")


        assertNotNull(project)

        issue = componentManager.getIssueFactory().getIssue()
        issue.setProject(overrideProject?.genericValue ?: project.getGenericValue())
        if (overrideProject) {
            issue.affectedVersions = versionManager.getVersions(overrideProject.id)
        }

        issue.setSummary "ss"
        issue.setReporterId "admin"
        issue.setIssueType issueType.getGenericValue()
        if (assigneeId) {
            issue.setAssigneeId(assigneeId)
        }
        issue.setPriorityId('2')
        issueManager.createIssue(adminUser, issue);
        log.debug("Created issue: $issue")
    }

    public def deleteFilter(JiraServiceContext ctx, SearchRequest sr) {
        searchRequestService.validateForDelete(ctx, sr.id)
        if (ctx.errorCollection.hasAnyErrors()) {
            log.warn(ctx.errorCollection)
        }
        assertFalse(ctx.errorCollection.hasAnyErrors())
        searchRequestService.deleteFilter(ctx, sr.id)
    }

}
Back to Top