PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/gnodet/camel
Java | 210 lines | 163 code | 31 blank | 16 comment | 0 complexity | dd8f256bcf265528d0143a83e0655f7d 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.net.URI;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import com.atlassian.jira.rest.client.api.IssueRestClient;
  25. import com.atlassian.jira.rest.client.api.JiraRestClient;
  26. import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
  27. import com.atlassian.jira.rest.client.api.domain.Issue;
  28. import com.atlassian.jira.rest.client.api.domain.Worklog;
  29. import com.atlassian.jira.rest.client.api.domain.input.WorklogInput;
  30. import io.atlassian.util.concurrent.Promises;
  31. import org.apache.camel.CamelContext;
  32. import org.apache.camel.CamelExecutionException;
  33. import org.apache.camel.EndpointInject;
  34. import org.apache.camel.Produce;
  35. import org.apache.camel.ProducerTemplate;
  36. import org.apache.camel.builder.RouteBuilder;
  37. import org.apache.camel.component.jira.JiraComponent;
  38. import org.apache.camel.component.mock.MockEndpoint;
  39. import org.apache.camel.spi.Registry;
  40. import org.apache.camel.test.junit5.CamelTestSupport;
  41. import org.junit.jupiter.api.Test;
  42. import org.junit.jupiter.api.extension.ExtendWith;
  43. import org.mockito.Mock;
  44. import org.mockito.junit.jupiter.MockitoExtension;
  45. import org.mockito.stubbing.Answer;
  46. import static org.apache.camel.component.jira.JiraConstants.ISSUE_KEY;
  47. import static org.apache.camel.component.jira.JiraConstants.JIRA;
  48. import static org.apache.camel.component.jira.JiraConstants.JIRA_REST_CLIENT_FACTORY;
  49. import static org.apache.camel.component.jira.JiraConstants.MINUTES_SPENT;
  50. import static org.apache.camel.component.jira.JiraTestConstants.JIRA_CREDENTIALS;
  51. import static org.apache.camel.component.jira.Utils.createIssueWithComments;
  52. import static org.apache.camel.component.jira.Utils.createIssueWithWorkLogs;
  53. import static org.apache.camel.component.jira.Utils.newWorkLog;
  54. import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
  55. import static org.apache.camel.test.junit5.TestSupport.assertStringContains;
  56. import static org.junit.jupiter.api.Assertions.fail;
  57. import static org.mockito.ArgumentMatchers.any;
  58. import static org.mockito.ArgumentMatchers.eq;
  59. import static org.mockito.Mockito.lenient;
  60. import static org.mockito.Mockito.never;
  61. import static org.mockito.Mockito.verify;
  62. import static org.mockito.Mockito.when;
  63. @ExtendWith(MockitoExtension.class)
  64. public class AddWorkLogProducerTest extends CamelTestSupport {
  65. @Mock
  66. private JiraRestClient jiraClient;
  67. @Mock
  68. private JiraRestClientFactory jiraRestClientFactory;
  69. @Mock
  70. private IssueRestClient issueRestClient;
  71. @Produce("direct:start")
  72. private ProducerTemplate template;
  73. @EndpointInject("mock:result")
  74. private MockEndpoint mockResult;
  75. private Issue backendIssue;
  76. @Override
  77. protected void bindToRegistry(Registry registry) {
  78. registry.bind(JIRA_REST_CLIENT_FACTORY, jiraRestClientFactory);
  79. }
  80. public void setMocks() {
  81. lenient().when(jiraRestClientFactory.createWithBasicHttpAuthentication(any(), any(), any())).thenReturn(jiraClient);
  82. lenient().when(jiraClient.getIssueClient()).thenReturn(issueRestClient);
  83. backendIssue = createIssueWithComments(1, 1);
  84. lenient().when(issueRestClient.getIssue(any())).then(inv -> Promises.promise(backendIssue));
  85. }
  86. @Override
  87. protected CamelContext createCamelContext() throws Exception {
  88. setMocks();
  89. CamelContext camelContext = super.createCamelContext();
  90. camelContext.disableJMX();
  91. JiraComponent component = new JiraComponent(camelContext);
  92. camelContext.addComponent(JIRA, component);
  93. return camelContext;
  94. }
  95. @Override
  96. protected RouteBuilder createRouteBuilder() {
  97. return new RouteBuilder() {
  98. @Override
  99. public void configure() {
  100. from("direct:start")
  101. .to("jira://addWorkLog?jiraUrl=" + JIRA_CREDENTIALS)
  102. .to(mockResult);
  103. }
  104. };
  105. }
  106. @Test
  107. public void testAddWorkLog() throws InterruptedException {
  108. int minutesSpent = 10;
  109. Map<String, Object> headers = new HashMap<>();
  110. headers.put(ISSUE_KEY, backendIssue.getKey());
  111. headers.put(MINUTES_SPENT, minutesSpent);
  112. String comment = "A new test comment " + new Date();
  113. when(issueRestClient.addWorklog(any(URI.class), any(WorklogInput.class)))
  114. .then((Answer<Void>) inv -> {
  115. Collection<Worklog> workLogs = new ArrayList<>();
  116. workLogs.add(newWorkLog(backendIssue.getId(), minutesSpent, comment));
  117. backendIssue = createIssueWithWorkLogs(backendIssue.getId(), workLogs);
  118. return null;
  119. });
  120. template.sendBodyAndHeaders(comment, headers);
  121. mockResult.expectedMessageCount(1);
  122. mockResult.assertIsSatisfied();
  123. verify(issueRestClient).getIssue(backendIssue.getKey());
  124. verify(issueRestClient).addWorklog(eq(backendIssue.getWorklogUri()), any(WorklogInput.class));
  125. }
  126. @Test
  127. public void testAddWorkLogMissingIssueKey() throws InterruptedException {
  128. int minutesSpent = 3;
  129. Map<String, Object> headers = new HashMap<>();
  130. headers.put(MINUTES_SPENT, minutesSpent);
  131. String comment = "A new test comment " + new Date();
  132. try {
  133. template.sendBodyAndHeaders(comment, headers);
  134. fail("Should have thrown an exception");
  135. } catch (CamelExecutionException e) {
  136. IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
  137. assertStringContains(cause.getMessage(), ISSUE_KEY);
  138. }
  139. mockResult.expectedMessageCount(0);
  140. mockResult.assertIsSatisfied();
  141. verify(issueRestClient, never()).getIssue(any(String.class));
  142. verify(issueRestClient, never()).addWorklog(any(URI.class), any(WorklogInput.class));
  143. }
  144. @Test
  145. public void testAddWorkLogMissingMinutesSpent() throws InterruptedException {
  146. Map<String, Object> headers = new HashMap<>();
  147. headers.put(ISSUE_KEY, backendIssue.getKey());
  148. String comment = "A new test comment " + new Date();
  149. try {
  150. template.sendBodyAndHeaders(comment, headers);
  151. fail("Should have thrown an exception");
  152. } catch (CamelExecutionException e) {
  153. IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
  154. assertStringContains(cause.getMessage(), MINUTES_SPENT);
  155. }
  156. mockResult.expectedMessageCount(0);
  157. mockResult.assertIsSatisfied();
  158. verify(issueRestClient, never()).getIssue(any(String.class));
  159. verify(issueRestClient, never()).addWorklog(any(URI.class), any(WorklogInput.class));
  160. }
  161. @Test
  162. public void testAddWorkLogMissingComment() throws InterruptedException {
  163. int minutesSpent = 60;
  164. Map<String, Object> headers = new HashMap<>();
  165. headers.put(ISSUE_KEY, backendIssue.getKey());
  166. headers.put(MINUTES_SPENT, minutesSpent);
  167. try {
  168. template.sendBodyAndHeaders(null, headers);
  169. fail("Should have thrown an exception");
  170. } catch (CamelExecutionException e) {
  171. IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
  172. assertStringContains(cause.getMessage(), "Missing exchange body");
  173. }
  174. mockResult.expectedMessageCount(0);
  175. mockResult.assertIsSatisfied();
  176. verify(issueRestClient, never()).getIssue(any(String.class));
  177. verify(issueRestClient, never()).addWorklog(any(URI.class), any(WorklogInput.class));
  178. }
  179. }