PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/groovy/com/ceilfors/jenkins/plugins/jiratrigger/webhook/JiraWebhookTest.groovy

https://gitlab.com/github-cloud-corp/jira-trigger-plugin
Groovy | 121 lines | 91 code | 27 blank | 3 comment | 0 complexity | 076f6e110635b795041a22ca85b1843a MD5 | raw file
  1. package com.ceilfors.jenkins.plugins.jiratrigger.webhook
  2. import com.atlassian.jira.rest.client.api.domain.ChangelogItem
  3. import com.atlassian.jira.rest.client.api.domain.FieldType
  4. import org.kohsuke.stapler.StaplerRequest
  5. import spock.lang.Specification
  6. import static org.hamcrest.Matchers.equalTo
  7. import static org.hamcrest.Matchers.is
  8. import static spock.util.matcher.HamcrestSupport.expect
  9. /**
  10. * @author ceilfors
  11. */
  12. @SuppressWarnings("GroovyAssignabilityCheck")
  13. class JiraWebhookTest extends Specification {
  14. String createIssueCreatedEvent() {
  15. this.class.getResourceAsStream("issue_created.json").text
  16. }
  17. String createIssueUpdatedEvent() {
  18. this.class.getResourceAsStream("issue_updated_without_comment.json").text
  19. }
  20. String createIssueUpdatedWithCommentEvent() {
  21. this.class.getResourceAsStream("issue_updated_with_comment.json").text
  22. }
  23. String createIssueStatusUpdatedEvent() {
  24. this.class.getResourceAsStream("issue_updated_status_updated.json").text
  25. }
  26. @SuppressWarnings("GrReassignedInClosureLocalVar")
  27. def "Should fire changelog created event when status field is updated"() {
  28. WebhookChangelogEvent changelogEvent = null
  29. given:
  30. JiraWebhook jiraWebhook = new JiraWebhook()
  31. def listener = Mock(JiraWebhookListener)
  32. jiraWebhook.setJiraWebhookListener(listener)
  33. when:
  34. jiraWebhook.processEvent(Mock(StaplerRequest), createIssueStatusUpdatedEvent())
  35. then:
  36. 1 * listener.changelogCreated(_) >> { args -> changelogEvent = args[0] }
  37. expect changelogEvent.changelog.items.toList(), equalTo([
  38. new ChangelogItem(FieldType.JIRA, "resolution", "1", "Fixed", "10000", "Done"),
  39. new ChangelogItem(FieldType.JIRA, "status", "10000", "To Do", "10001", "Done")
  40. ])
  41. }
  42. @SuppressWarnings("GrReassignedInClosureLocalVar")
  43. def "Should store request parameter in context"() {
  44. WebhookCommentEvent commentEvent = null
  45. given:
  46. def listener = Mock(JiraWebhookListener)
  47. def staplerRequest = Mock(StaplerRequest)
  48. staplerRequest.getParameter("user_id") >> "adminId"
  49. staplerRequest.getParameter("user_key") >> "adminKey"
  50. JiraWebhook jiraWebhook = new JiraWebhook()
  51. jiraWebhook.setJiraWebhookListener(listener)
  52. when:
  53. jiraWebhook.processEvent(staplerRequest, createIssueUpdatedWithCommentEvent())
  54. then:
  55. 1 * listener.commentCreated(_) >> { args -> commentEvent = args[0] }
  56. expect commentEvent.userId, equalTo("adminId")
  57. expect commentEvent.userKey, equalTo("adminKey")
  58. }
  59. def "Should not notify listener when the event type is issue created"() {
  60. given:
  61. JiraWebhook jiraWebhook = new JiraWebhook()
  62. def listener = Mock(JiraWebhookListener)
  63. jiraWebhook.setJiraWebhookListener(listener)
  64. when:
  65. jiraWebhook.processEvent(Mock(StaplerRequest), createIssueCreatedEvent())
  66. then:
  67. 0 * listener.commentCreated(_)
  68. }
  69. @SuppressWarnings("GrReassignedInClosureLocalVar")
  70. def "Should not notify listener when issue is updated with comment"() {
  71. WebhookCommentEvent commentEvent = null
  72. given:
  73. JiraWebhook jiraWebhook = new JiraWebhook()
  74. def listener = Mock(JiraWebhookListener)
  75. jiraWebhook.setJiraWebhookListener(listener)
  76. when:
  77. jiraWebhook.processEvent(Mock(StaplerRequest), createIssueUpdatedWithCommentEvent())
  78. then:
  79. 1 * listener.commentCreated(_) >> { args -> commentEvent = args[0] }
  80. expect commentEvent.comment.body, is("comment body")
  81. expect commentEvent.comment.author.name, is("admin")
  82. expect commentEvent.webhookEventType, is(JiraWebhook.WEBHOOK_EVENT)
  83. }
  84. def "Should not fire comment created event when a comment is added to an issue"() {
  85. given:
  86. JiraWebhook jiraWebhook = new JiraWebhook()
  87. def listener = Mock(JiraWebhookListener)
  88. jiraWebhook.setJiraWebhookListener(listener)
  89. when:
  90. jiraWebhook.processEvent(Mock(StaplerRequest), createIssueUpdatedEvent())
  91. then:
  92. 0 * listener.commentCreated(_)
  93. }
  94. }