PageRenderTime 72ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/components/camel-jira/src/test/java/org/apache/camel/component/jira/Utils.java

https://github.com/apache/camel
Java | 194 lines | 149 code | 26 blank | 19 comment | 3 complexity | 6df3e217e181a57444180dcff77f2692 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.jira;
  18. import java.net.URI;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Map;
  23. import com.atlassian.jira.rest.client.api.StatusCategory;
  24. import com.atlassian.jira.rest.client.api.domain.Attachment;
  25. import com.atlassian.jira.rest.client.api.domain.BasicComponent;
  26. import com.atlassian.jira.rest.client.api.domain.BasicPriority;
  27. import com.atlassian.jira.rest.client.api.domain.BasicWatchers;
  28. import com.atlassian.jira.rest.client.api.domain.Comment;
  29. import com.atlassian.jira.rest.client.api.domain.Issue;
  30. import com.atlassian.jira.rest.client.api.domain.IssueLink;
  31. import com.atlassian.jira.rest.client.api.domain.IssueLinkType;
  32. import com.atlassian.jira.rest.client.api.domain.IssueType;
  33. import com.atlassian.jira.rest.client.api.domain.Priority;
  34. import com.atlassian.jira.rest.client.api.domain.Resolution;
  35. import com.atlassian.jira.rest.client.api.domain.Status;
  36. import com.atlassian.jira.rest.client.api.domain.User;
  37. import com.atlassian.jira.rest.client.api.domain.Worklog;
  38. import com.google.common.collect.ImmutableMap;
  39. import org.apache.commons.lang3.StringUtils;
  40. import org.joda.time.DateTime;
  41. import org.slf4j.LoggerFactory;
  42. import static com.atlassian.jira.rest.client.api.domain.User.S48_48;
  43. import static org.apache.camel.component.jira.JiraTestConstants.KEY;
  44. import static org.apache.camel.component.jira.JiraTestConstants.TEST_JIRA_URL;
  45. public final class Utils {
  46. public static User userAssignee;
  47. static {
  48. try {
  49. userAssignee = new User(
  50. null, "user-test", "User Test", "user@test", true, null, buildUserAvatarUris("user-test", 10082L), null);
  51. } catch (Exception e) {
  52. LoggerFactory.getLogger(Utils.class).debug("Failed to build test user: {}", e.getMessage(), e);
  53. }
  54. }
  55. private static IssueType issueType = new IssueType(null, 1L, "Bug", false, "Bug", null);
  56. private Utils() {
  57. }
  58. public static Issue createIssue(long id) {
  59. return createIssueWithComments(id, 0);
  60. }
  61. public static Issue createIssue(
  62. long id, String summary, String key, IssueType issueType, String description,
  63. BasicPriority priority, User assignee, Collection<BasicComponent> components, BasicWatchers watchers) {
  64. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + id);
  65. return new Issue(
  66. summary, selfUri, KEY + "-" + id, id, null, issueType, null, description, priority, null, null, null,
  67. assignee, null, null, null, null, null, components, null, null, null, null, null, null, null, watchers,
  68. null, null, null, null, null);
  69. }
  70. public static Issue transitionIssueDone(Issue issue, Status status, Resolution resolution) {
  71. return new Issue(
  72. issue.getSummary(), issue.getSelf(), issue.getKey(), issue.getId(), null, issue.getIssueType(),
  73. status, issue.getDescription(), issue.getPriority(), resolution, null, null,
  74. issue.getAssignee(), null, null, null, null, null, null, null, null, null, null, null, null, null, null,
  75. null, null, null, null, null);
  76. }
  77. public static Issue setPriority(Issue issue, Priority p) {
  78. return new Issue(
  79. issue.getSummary(), issue.getSelf(), issue.getKey(), issue.getId(), null, issue.getIssueType(),
  80. issue.getStatus(), issue.getDescription(), p, issue.getResolution(), null, null,
  81. issue.getAssignee(), null, null, null, null, null, null, null, null, null, null, null, null, null, null,
  82. null, null, null, null, null);
  83. }
  84. public static Issue transitionIssueDone(Issue issue) {
  85. URI doneStatusUri = URI.create(TEST_JIRA_URL + "/rest/api/2/status/1");
  86. URI doneResolutionUri = URI.create(TEST_JIRA_URL + "/rest/api/2/resolution/1");
  87. StatusCategory sc = new StatusCategory(doneResolutionUri, "statusCategory", 1L, "SC-1", "GREEN");
  88. Status status = new Status(doneStatusUri, 1L, "Done", "Done", null, sc);
  89. Resolution resolution = new Resolution(doneResolutionUri, 5L, "Resolution", "Resolution");
  90. return transitionIssueDone(issue, status, resolution);
  91. }
  92. public static Issue createIssueWithAttachment(
  93. long id, String summary, String key, IssueType issueType, String description,
  94. BasicPriority priority, User assignee, Collection<Attachment> attachments) {
  95. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + id);
  96. return new Issue(
  97. summary, selfUri, KEY + "-" + id, id, null, issueType, null, description, priority, null, attachments, null,
  98. assignee, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
  99. null, null, null, null, null);
  100. }
  101. public static Issue createIssueWithComments(long id, int numComments) {
  102. Collection<Comment> comments = new ArrayList<>();
  103. if (numComments > 0) {
  104. for (int idx = 1; idx < numComments + 1; idx++) {
  105. Comment c = newComment(id, idx, "A test comment " + idx + " for " + KEY + "-" + id);
  106. comments.add(c);
  107. }
  108. }
  109. return createIssueWithComments(id, comments);
  110. }
  111. public static Issue createIssueWithComments(long id, Collection<Comment> comments) {
  112. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + id);
  113. return new Issue(
  114. "jira summary test " + id, selfUri, KEY + "-" + id, id, null, issueType, null, "Description " + id,
  115. null, null, null, null, userAssignee, null, null, null, null, null, null, null, null, comments, null, null,
  116. null, null, null, null, null, null, null, null);
  117. }
  118. public static Issue createIssueWithLinks(long id, Collection<IssueLink> issueLinks) {
  119. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + id);
  120. return new Issue(
  121. "jira summary test " + id, selfUri, KEY + "-" + id, id, null, issueType, null, "Description " + id,
  122. null, null, null, null, userAssignee, null, null, null, null, null, null, null, null, null, null, issueLinks,
  123. null, null, null, null, null, null, null, null);
  124. }
  125. public static Issue createIssueWithWorkLogs(long id, Collection<Worklog> worklogs) {
  126. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + id);
  127. return new Issue(
  128. "jira summary test " + id, selfUri, KEY + "-" + id, id, null, issueType, null, "Description " + id,
  129. null, null, null, null, userAssignee, null, null, null, null, null, null, null, null, null, null, null,
  130. null, worklogs, null, null, null, null, null, null);
  131. }
  132. public static Comment newComment(long issueId, int newCommentId, String comment) {
  133. DateTime now = DateTime.now();
  134. long id = Long.parseLong(issueId + "0" + newCommentId);
  135. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + issueId + "/comment");
  136. return new Comment(selfUri, comment, null, null, now, null, null, id);
  137. }
  138. public static IssueLink newIssueLink(long issueId, int newLinkId, String comment) {
  139. long id = Long.parseLong(issueId + "0" + newLinkId);
  140. URI issueUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + id);
  141. IssueLinkType relatesTo = new IssueLinkType("Relates", "relates to", IssueLinkType.Direction.OUTBOUND);
  142. return new IssueLink(KEY, issueUri, relatesTo);
  143. }
  144. public static Worklog newWorkLog(long issueId, Integer minutesSpent, String comment) {
  145. DateTime now = DateTime.now();
  146. URI issueUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + issueId);
  147. URI selfUri = URI.create(TEST_JIRA_URL + "/rest/api/latest/issue/" + issueId + "/comment");
  148. return new Worklog(selfUri, issueUri, null, null, comment, now, null, null, minutesSpent, null);
  149. }
  150. private static Map<String, URI> buildUserAvatarUris(String user, Long avatarId) throws Exception {
  151. final ImmutableMap.Builder<String, URI> builder = ImmutableMap.builder();
  152. builder.put(S48_48, buildUserAvatarUri(user, avatarId));
  153. return builder.build();
  154. }
  155. private static URI buildUserAvatarUri(String userName, Long avatarId) throws Exception {
  156. // secure/useravatar?size=small&ownerId=admin&avatarId=10054
  157. final StringBuilder sb = new StringBuilder("secure/useravatar?");
  158. // Optional user name
  159. if (StringUtils.isNotBlank(userName)) {
  160. sb.append("ownerId=").append(userName).append("&");
  161. }
  162. // avatar Id
  163. sb.append("avatarId=").append(avatarId);
  164. String relativeAvatarUrl = sb.toString();
  165. URI avatarUrl = new URL(TEST_JIRA_URL + "/" + relativeAvatarUrl).toURI();
  166. return avatarUrl;
  167. }
  168. }