PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/matticala/apache-camel
Java | 120 lines | 81 code | 20 blank | 19 comment | 0 complexity | d8747d706d5ac0971d70543e9a69de89 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;
  18. import java.util.Date;
  19. import com.atlassian.jira.rest.client.domain.BasicIssue;
  20. import com.atlassian.jira.rest.client.domain.Comment;
  21. import org.apache.camel.Exchange;
  22. import org.apache.camel.Message;
  23. import org.apache.camel.Processor;
  24. import org.apache.camel.builder.RouteBuilder;
  25. import org.apache.camel.component.jira.mocks.MockJerseyJiraRestClientFactory;
  26. import org.apache.camel.component.jira.mocks.MockJiraRestClient;
  27. import org.apache.camel.component.jira.mocks.MockSearchRestClient;
  28. import org.apache.camel.component.mock.MockEndpoint;
  29. import org.apache.camel.impl.JndiRegistry;
  30. import org.apache.camel.test.junit4.CamelTestSupport;
  31. import org.junit.Test;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. public class CommentConsumerTest extends CamelTestSupport {
  35. public static final Logger LOG = LoggerFactory.getLogger(CommentConsumerTest.class);
  36. private static final String URL = "https://somerepo.atlassian.net";
  37. private static final String USERNAME = "someguy";
  38. private static final String PASSWORD = "xU3xjhay9yjEaZq";
  39. private static final String JIRA_CREDENTIALS = URL + "&username=" + USERNAME + "&password=" + PASSWORD;
  40. protected MockJerseyJiraRestClientFactory factory;
  41. @Override
  42. protected JndiRegistry createRegistry() throws Exception {
  43. JndiRegistry registry = super.createRegistry();
  44. factory = new MockJerseyJiraRestClientFactory();
  45. registry.bind("JerseyJiraRestClientFactory", factory);
  46. return registry;
  47. }
  48. @Override
  49. protected RouteBuilder createRouteBuilder() throws Exception {
  50. return new RouteBuilder() {
  51. @Override
  52. public void configure() throws Exception {
  53. from("jira://newComment?serverUrl=" + JIRA_CREDENTIALS
  54. + "&jql=RAW(project=CAMELJIRA)" + "&delay=500")
  55. .process(new NewCommentProcessor())
  56. .to("mock:result");
  57. }
  58. };
  59. }
  60. @Test
  61. public void emptyAtStartupTest() throws Exception {
  62. MockEndpoint mockResultEndpoint = getMockEndpoint("mock:result");
  63. mockResultEndpoint.expectedMessageCount(0);
  64. mockResultEndpoint.assertIsSatisfied();
  65. }
  66. @Test
  67. public void singleIssueTest() throws Exception {
  68. MockEndpoint mockResultEndpoint = getMockEndpoint("mock:result");
  69. MockJiraRestClient jiraRestClient = factory.getClient();
  70. MockSearchRestClient searchRestClient = (MockSearchRestClient) jiraRestClient.getSearchClient();
  71. BasicIssue issue1 = searchRestClient.addIssue();
  72. String commentText = "Comment added at " + new Date();
  73. Comment comment1 = searchRestClient.addCommentToIssue(issue1, commentText);
  74. mockResultEndpoint.expectedBodiesReceived(comment1);
  75. mockResultEndpoint.assertIsSatisfied();
  76. }
  77. @Test
  78. public void multiIssueTest() throws Exception {
  79. MockEndpoint mockResultEndpoint = getMockEndpoint("mock:result");
  80. MockJiraRestClient jiraRestClient = factory.getClient();
  81. MockSearchRestClient searchRestClient = (MockSearchRestClient) jiraRestClient.getSearchClient();
  82. BasicIssue issue1 = searchRestClient.addIssue();
  83. Comment comment1 = searchRestClient.addCommentToIssue(issue1, "Comment added at " + new Date());
  84. BasicIssue issue2 = searchRestClient.addIssue();
  85. Comment comment2 = searchRestClient.addCommentToIssue(issue2, "Comment added at " + new Date());
  86. mockResultEndpoint.expectedBodiesReceivedInAnyOrder(comment1, comment2);
  87. mockResultEndpoint.assertIsSatisfied();
  88. }
  89. /**
  90. * Log new comments. Not really needed for this test, but useful for debugging.
  91. */
  92. public class NewCommentProcessor implements Processor {
  93. @Override
  94. public void process(Exchange exchange) throws Exception {
  95. Message in = exchange.getIn();
  96. Comment comment = (Comment) in.getBody();
  97. LOG.debug("Got comment with id " + comment.getId() + " Body " + comment.getBody());
  98. }
  99. }
  100. }