PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/gnodet/camel
Java | 234 lines | 181 code | 37 blank | 16 comment | 0 complexity | 0919b258ab55e846a205767e0f7ce663 MD5 | raw file
  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.producer;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import com.atlassian.jira.rest.client.api.IssueRestClient;
  24. import com.atlassian.jira.rest.client.api.JiraRestClient;
  25. import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
  26. import com.atlassian.jira.rest.client.api.domain.Issue;
  27. import com.atlassian.jira.rest.client.api.domain.IssueLink;
  28. import com.atlassian.jira.rest.client.api.domain.input.LinkIssuesInput;
  29. import org.apache.camel.CamelContext;
  30. import org.apache.camel.CamelExecutionException;
  31. import org.apache.camel.EndpointInject;
  32. import org.apache.camel.Produce;
  33. import org.apache.camel.ProducerTemplate;
  34. import org.apache.camel.builder.RouteBuilder;
  35. import org.apache.camel.component.jira.JiraComponent;
  36. import org.apache.camel.component.mock.MockEndpoint;
  37. import org.apache.camel.spi.Registry;
  38. import org.apache.camel.test.junit5.CamelTestSupport;
  39. import org.junit.jupiter.api.Test;
  40. import org.junit.jupiter.api.extension.ExtendWith;
  41. import org.mockito.Mock;
  42. import org.mockito.junit.jupiter.MockitoExtension;
  43. import org.mockito.stubbing.Answer;
  44. import static org.apache.camel.component.jira.JiraConstants.CHILD_ISSUE_KEY;
  45. import static org.apache.camel.component.jira.JiraConstants.JIRA;
  46. import static org.apache.camel.component.jira.JiraConstants.JIRA_REST_CLIENT_FACTORY;
  47. import static org.apache.camel.component.jira.JiraConstants.LINK_TYPE;
  48. import static org.apache.camel.component.jira.JiraConstants.PARENT_ISSUE_KEY;
  49. import static org.apache.camel.component.jira.JiraTestConstants.JIRA_CREDENTIALS;
  50. import static org.apache.camel.component.jira.Utils.createIssue;
  51. import static org.apache.camel.component.jira.Utils.createIssueWithLinks;
  52. import static org.apache.camel.component.jira.Utils.newIssueLink;
  53. import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
  54. import static org.apache.camel.test.junit5.TestSupport.assertStringContains;
  55. import static org.junit.jupiter.api.Assertions.fail;
  56. import static org.mockito.ArgumentMatchers.any;
  57. import static org.mockito.Mockito.lenient;
  58. import static org.mockito.Mockito.never;
  59. import static org.mockito.Mockito.verify;
  60. import static org.mockito.Mockito.when;
  61. @ExtendWith(MockitoExtension.class)
  62. public class AddIssueLinkProducerTest extends CamelTestSupport {
  63. @Mock
  64. private JiraRestClient jiraClient;
  65. @Mock
  66. private JiraRestClientFactory jiraRestClientFactory;
  67. @Mock
  68. private IssueRestClient issueRestClient;
  69. @Produce("direct:start")
  70. private ProducerTemplate template;
  71. @EndpointInject("mock:result")
  72. private MockEndpoint mockResult;
  73. private Issue parentIssue;
  74. private Issue childIssue;
  75. @Override
  76. protected void bindToRegistry(Registry registry) {
  77. registry.bind(JIRA_REST_CLIENT_FACTORY, jiraRestClientFactory);
  78. }
  79. @Override
  80. protected CamelContext createCamelContext() throws Exception {
  81. setMocks();
  82. CamelContext camelContext = super.createCamelContext();
  83. camelContext.disableJMX();
  84. JiraComponent component = new JiraComponent(camelContext);
  85. camelContext.addComponent(JIRA, component);
  86. return camelContext;
  87. }
  88. public void setMocks() {
  89. lenient().when(jiraRestClientFactory.createWithBasicHttpAuthentication(any(), any(), any())).thenReturn(jiraClient);
  90. lenient().when(jiraClient.getIssueClient()).thenReturn(issueRestClient);
  91. parentIssue = createIssue(1);
  92. childIssue = createIssue(2);
  93. }
  94. @Override
  95. protected RouteBuilder createRouteBuilder() {
  96. return new RouteBuilder() {
  97. @Override
  98. public void configure() {
  99. from("direct:start")
  100. .to("jira://addIssueLink?jiraUrl=" + JIRA_CREDENTIALS)
  101. .to(mockResult);
  102. }
  103. };
  104. }
  105. @Test
  106. public void testAddIssueLink() throws InterruptedException {
  107. String comment = "A new test comment " + new Date();
  108. String linkType = "Relates";
  109. Map<String, Object> headers = new HashMap<>();
  110. headers.put(PARENT_ISSUE_KEY, parentIssue.getKey());
  111. headers.put(CHILD_ISSUE_KEY, childIssue.getKey());
  112. headers.put(LINK_TYPE, linkType);
  113. when(issueRestClient.linkIssue(any(LinkIssuesInput.class)))
  114. .then((Answer<Void>) inv -> {
  115. Collection<IssueLink> links = new ArrayList<>();
  116. links.add(newIssueLink(childIssue.getId(), 1, comment));
  117. parentIssue = createIssueWithLinks(parentIssue.getId(), links);
  118. return null;
  119. });
  120. template.sendBodyAndHeaders(comment, headers);
  121. mockResult.expectedMessageCount(1);
  122. mockResult.assertIsSatisfied();
  123. verify(issueRestClient).linkIssue(any(LinkIssuesInput.class));
  124. }
  125. @Test
  126. public void testAddIssueLinkNoComment() throws InterruptedException {
  127. String linkType = "Relates";
  128. Map<String, Object> headers = new HashMap<>();
  129. headers.put(PARENT_ISSUE_KEY, parentIssue.getKey());
  130. headers.put(CHILD_ISSUE_KEY, childIssue.getKey());
  131. headers.put(LINK_TYPE, linkType);
  132. when(issueRestClient.linkIssue(any(LinkIssuesInput.class)))
  133. .then((Answer<Void>) inv -> {
  134. Collection<IssueLink> links = new ArrayList<>();
  135. links.add(newIssueLink(childIssue.getId(), 1, null));
  136. parentIssue = createIssueWithLinks(parentIssue.getId(), links);
  137. return null;
  138. });
  139. template.sendBodyAndHeaders(null, headers);
  140. mockResult.expectedMessageCount(1);
  141. mockResult.assertIsSatisfied();
  142. verify(issueRestClient).linkIssue(any(LinkIssuesInput.class));
  143. }
  144. @Test
  145. public void testAddIssueLinkMissingParentIssueKey() throws InterruptedException {
  146. String comment = "A new test comment " + new Date();
  147. String linkType = "Relates";
  148. Map<String, Object> headers = new HashMap<>();
  149. headers.put(CHILD_ISSUE_KEY, childIssue.getKey());
  150. headers.put(LINK_TYPE, linkType);
  151. try {
  152. template.sendBodyAndHeaders(comment, headers);
  153. fail("Should have thrown an exception");
  154. } catch (CamelExecutionException e) {
  155. IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
  156. assertStringContains(cause.getMessage(), PARENT_ISSUE_KEY);
  157. }
  158. mockResult.expectedMessageCount(0);
  159. mockResult.assertIsSatisfied();
  160. verify(issueRestClient, never()).linkIssue(any(LinkIssuesInput.class));
  161. }
  162. @Test
  163. public void testAddIssueLinkMissingChildIssueKey() throws InterruptedException {
  164. String comment = "A new test comment " + new Date();
  165. String linkType = "Relates";
  166. Map<String, Object> headers = new HashMap<>();
  167. headers.put(PARENT_ISSUE_KEY, parentIssue.getKey());
  168. headers.put(LINK_TYPE, linkType);
  169. try {
  170. template.sendBodyAndHeaders(comment, headers);
  171. fail("Should have thrown an exception");
  172. } catch (CamelExecutionException e) {
  173. IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
  174. assertStringContains(cause.getMessage(), CHILD_ISSUE_KEY);
  175. }
  176. mockResult.expectedMessageCount(0);
  177. mockResult.assertIsSatisfied();
  178. verify(issueRestClient, never()).linkIssue(any(LinkIssuesInput.class));
  179. }
  180. @Test
  181. public void testAddIssueLinkMissingLinkType() throws InterruptedException {
  182. String comment = "A new test comment " + new Date();
  183. Map<String, Object> headers = new HashMap<>();
  184. headers.put(PARENT_ISSUE_KEY, parentIssue.getKey());
  185. headers.put(CHILD_ISSUE_KEY, childIssue.getKey());
  186. try {
  187. template.sendBodyAndHeaders(comment, headers);
  188. fail("Should have thrown an exception");
  189. } catch (CamelExecutionException e) {
  190. IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
  191. assertStringContains(cause.getMessage(), LINK_TYPE);
  192. }
  193. mockResult.expectedMessageCount(0);
  194. mockResult.assertIsSatisfied();
  195. verify(issueRestClient, never()).linkIssue(any(LinkIssuesInput.class));
  196. }
  197. }