CAMEL-8497 Add support for setting a commit status, getting list of files associated with PR and retrieving a specific file content.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1cb3c092 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1cb3c092 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1cb3c092 Branch: refs/heads/camel-2.15.x Commit: 1cb3c092655646f0c0465a67091b41e6df32b083 Parents: b08443a Author: objectiser <g...@brownuk.com> Authored: Tue Mar 10 11:49:19 2015 +0000 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Thu Mar 19 15:01:29 2015 +0800 ---------------------------------------------------------------------- components/camel-github/pom.xml | 5 + .../camel/component/github/GitHubEndpoint.java | 53 ++++++- .../camel/component/github/GitHubType.java | 3 +- .../github/consumer/PullRequestConsumer.java | 12 +- .../github/producer/GetCommitFileProducer.java | 82 +++++++++++ .../producer/PullRequestFilesProducer.java | 61 ++++++++ .../producer/PullRequestStateProducer.java | 83 +++++++++++ .../github/ClosePullRequestProducerTest.java | 95 ------------- .../component/github/CommitConsumerTest.java | 64 --------- .../github/GitHubComponentTestBase.java | 8 +- .../github/PullRequestCommentConsumerTest.java | 69 ---------- .../github/PullRequestCommentProducerTest.java | 87 ------------ .../github/PullRequestConsumerTest.java | 72 ---------- .../camel/component/github/TagConsumerTest.java | 62 --------- .../github/consumer/CommitConsumerTest.java | 66 +++++++++ .../github/consumer/MockCommitService.java | 57 -------- .../github/consumer/MockIssueService.java | 47 ------- .../github/consumer/MockPullRequestService.java | 126 ----------------- .../github/consumer/MockRepositoryService.java | 55 -------- .../PullRequestCommentConsumerTest.java | 71 ++++++++++ .../consumer/PullRequestConsumerTest.java | 74 ++++++++++ .../github/consumer/TagConsumerTest.java | 64 +++++++++ .../producer/ClosePullRequestProducerTest.java | 97 +++++++++++++ .../PullRequestCommentProducerTest.java | 89 ++++++++++++ .../producer/PullRequestFilesProducerTest.java | 88 ++++++++++++ .../producer/PullRequestStateProducerTest.java | 94 +++++++++++++ .../github/services/MockCommitService.java | 77 +++++++++++ .../github/services/MockIssueService.java | 47 +++++++ .../github/services/MockPullRequestService.java | 138 +++++++++++++++++++ .../github/services/MockRepositoryService.java | 55 ++++++++ 30 files changed, 1251 insertions(+), 750 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-github/pom.xml b/components/camel-github/pom.xml index 819e283..cb9a4bc 100644 --- a/components/camel-github/pom.xml +++ b/components/camel-github/pom.xml @@ -39,6 +39,11 @@ <version>${egit-github-core-version}</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>commons-codec</groupId> + <artifactId>commons-codec</artifactId> + <version>${commons-codec-version}</version> + </dependency> <!-- testing --> <dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java index a2e8b30..e29c932 100644 --- a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubEndpoint.java @@ -24,7 +24,10 @@ import org.apache.camel.component.github.consumer.PullRequestCommentConsumer; import org.apache.camel.component.github.consumer.PullRequestConsumer; import org.apache.camel.component.github.consumer.TagConsumer; import org.apache.camel.component.github.producer.ClosePullRequestProducer; +import org.apache.camel.component.github.producer.GetCommitFileProducer; import org.apache.camel.component.github.producer.PullRequestCommentProducer; +import org.apache.camel.component.github.producer.PullRequestFilesProducer; +import org.apache.camel.component.github.producer.PullRequestStateProducer; import org.apache.camel.impl.DefaultEndpoint; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; @@ -34,19 +37,19 @@ import org.apache.camel.util.ObjectHelper; /** * The endpoint encapsulates portions of the GitHub API, relying on the org.eclipse.egit.github.core Java SDK. * Available endpoint URIs include: - * + * * CONSUMERS * github://pullRequest (new pull requests) * github://pullRequestComment (new pull request comments) * github://commit/[branch] (new commits) * github://tag (new tags) - * + * * PRODUCERS * github://pullRequestComment (create a new pull request comment; see PullRequestCommentProducer for header requirements) - * + * * The endpoints will respond with org.eclipse.egit.github.core-provided POJOs (PullRequest, CommitComment, * RepositoryTag, RepositoryCommit, etc.) - * + * * Note: Rather than webhooks, this endpoint relies on simple polling. Reasons include: * - concerned about reliability/stability if this somehow relied on an exposed, embedded server (Jetty?) * - the types of payloads we're polling aren't typically large (plus, paging is available in the API) @@ -69,7 +72,13 @@ public class GitHubEndpoint extends DefaultEndpoint { private String repoOwner; @UriParam private String repoName; - + @UriParam + private String state; + @UriParam + private String targetUrl; + @UriParam + private String encoding; + public GitHubEndpoint(String uri, GitHubComponent component) { super(uri, component); } @@ -79,10 +88,16 @@ public class GitHubEndpoint extends DefaultEndpoint { return new ClosePullRequestProducer(this); } else if (type == GitHubType.PULLREQUESTCOMMENT) { return new PullRequestCommentProducer(this); + } else if (type == GitHubType.PULLREQUESTSTATE) { + return new PullRequestStateProducer(this); + } else if (type == GitHubType.PULLREQUESTFILES) { + return new PullRequestFilesProducer(this); + } else if (type == GitHubType.GETCOMMITFILE) { + return new GetCommitFileProducer(this); } throw new IllegalArgumentException("Cannot create producer with type " + type); } - + public Consumer createConsumer(Processor processor) throws Exception { if (type == GitHubType.COMMIT) { ObjectHelper.notEmpty(branchName, "branchName", this); @@ -140,7 +155,7 @@ public class GitHubEndpoint extends DefaultEndpoint { public void setOauthToken(String oauthToken) { this.oauthToken = oauthToken; } - + public boolean hasOauth() { return oauthToken != null && oauthToken.length() > 0; } @@ -160,4 +175,28 @@ public class GitHubEndpoint extends DefaultEndpoint { public void setRepoName(String repoName) { this.repoName = repoName; } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getTargetUrl() { + return targetUrl; + } + + public void setTargetUrl(String targetUrl) { + this.targetUrl = targetUrl; + } + + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java index c6e07de..cc831d0 100644 --- a/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/GitHubType.java @@ -18,6 +18,7 @@ package org.apache.camel.component.github; public enum GitHubType { - CLOSEPULLREQUEST, PULLREQUESTCOMMENT, COMMIT, PULLREQUEST, TAG + CLOSEPULLREQUEST, PULLREQUESTCOMMENT, COMMIT, PULLREQUEST, TAG, PULLREQUESTSTATE, + PULLREQUESTFILES, GETCOMMITFILE } http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java index 0d62f6d..32d1577 100644 --- a/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/consumer/PullRequestConsumer.java @@ -32,7 +32,7 @@ public class PullRequestConsumer extends AbstractGitHubConsumer { private static final transient Logger LOG = LoggerFactory.getLogger(PullRequestConsumer.class); private PullRequestService pullRequestService; - + private int lastOpenPullRequest; public PullRequestConsumer(GitHubEndpoint endpoint, Processor processor) throws Exception { @@ -68,7 +68,7 @@ public class PullRequestConsumer extends AbstractGitHubConsumer { break; } } - + if (newPullRequests.size() > 0) { lastOpenPullRequest = openPullRequests.get(0).getNumber(); } @@ -76,11 +76,15 @@ public class PullRequestConsumer extends AbstractGitHubConsumer { while (!newPullRequests.empty()) { PullRequest newPullRequest = newPullRequests.pop(); Exchange e = getEndpoint().createExchange(); + e.getIn().setBody(newPullRequest); - + // Required by the producers. Set it here for convenience. e.getIn().setHeader("GitHubPullRequest", newPullRequest.getNumber()); - + if (newPullRequest.getHead() != null) { + e.getIn().setHeader("GitHubPullRequestHeadCommitSHA", newPullRequest.getHead().getSha()); + } + getProcessor().process(e); } return newPullRequests.size(); http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/main/java/org/apache/camel/component/github/producer/GetCommitFileProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/GetCommitFileProducer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/GetCommitFileProducer.java new file mode 100644 index 0000000..92375b9 --- /dev/null +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/GetCommitFileProducer.java @@ -0,0 +1,82 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.component.github.GitHubEndpoint; +import org.apache.camel.spi.Registry; +import org.apache.commons.codec.binary.Base64; +import org.eclipse.egit.github.core.Blob; +import org.eclipse.egit.github.core.CommitFile; +import org.eclipse.egit.github.core.service.DataService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Producer endpoint that gets the file associated with a CommitFile object. + * + */ +public class GetCommitFileProducer extends AbstractGitHubProducer { + private static final transient Logger LOG = LoggerFactory.getLogger(GetCommitFileProducer.class); + + private DataService dataService; + + private String encoding=Blob.ENCODING_UTF8; + + public GetCommitFileProducer(GitHubEndpoint endpoint) throws Exception { + super(endpoint); + + Registry registry = endpoint.getCamelContext().getRegistry(); + Object service = registry.lookupByName("githubDataService"); + if (service != null) { + LOG.debug("Using DataService found in registry " + service.getClass().getCanonicalName()); + dataService = (DataService) service; + } else { + dataService = new DataService(); + } + initService(dataService); + + if (endpoint.getEncoding() != null) { + encoding = endpoint.getEncoding(); + + if (!encoding.equalsIgnoreCase(Blob.ENCODING_BASE64) + && !encoding.equalsIgnoreCase(Blob.ENCODING_UTF8)) { + throw new IllegalArgumentException("Unknown encoding '"+encoding+"'"); + } + } + } + + public void process(Exchange exchange) throws Exception { + CommitFile file=exchange.getIn().getBody(CommitFile.class); + + Blob response=dataService.getBlob(getRepository(), file.getSha()); + + String text=response.getContent(); + + // By default, if blob encoding is base64 then we convert to UTF-8. If + // base64 encoding is required, then must be explicitly requested + if (response.getEncoding().equals(Blob.ENCODING_BASE64) && + encoding != null && encoding.equalsIgnoreCase(Blob.ENCODING_UTF8)) { + text = new String(Base64.decodeBase64(text)); + } + + // copy the header of in message to the out message + exchange.getOut().copyFrom(exchange.getIn()); + exchange.getOut().setBody(text); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestFilesProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestFilesProducer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestFilesProducer.java new file mode 100644 index 0000000..f69bd3f --- /dev/null +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestFilesProducer.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.component.github.GitHubEndpoint; +import org.apache.camel.spi.Registry; +import org.eclipse.egit.github.core.CommitFile; +import org.eclipse.egit.github.core.service.PullRequestService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Producer endpoint that gets the files associated with a pull request. + * + * The endpoint requires the "GitHubPullRequest" header, identifying the pull request number (integer). + */ +public class PullRequestFilesProducer extends AbstractGitHubProducer { + private static final transient Logger LOG = LoggerFactory.getLogger(PullRequestFilesProducer.class); + + private PullRequestService pullRequestService; + + public PullRequestFilesProducer(GitHubEndpoint endpoint) throws Exception { + super(endpoint); + + Registry registry = endpoint.getCamelContext().getRegistry(); + Object service = registry.lookupByName("githubPullRequestService"); + if (service != null) { + LOG.debug("Using PullRequestService found in registry " + service.getClass().getCanonicalName()); + pullRequestService = (PullRequestService) service; + } else { + pullRequestService = new PullRequestService(); + } + initService(pullRequestService); + } + + public void process(Exchange exchange) throws Exception { + Integer pullRequestNumber = exchange.getIn().getHeader("GitHubPullRequest", Integer.class); + + java.util.List<CommitFile> response=pullRequestService.getFiles(getRepository(), pullRequestNumber); + + // copy the header of in message to the out message + exchange.getOut().copyFrom(exchange.getIn()); + exchange.getOut().setBody(response); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestStateProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestStateProducer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestStateProducer.java new file mode 100644 index 0000000..b9218b6 --- /dev/null +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestStateProducer.java @@ -0,0 +1,83 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.producer; + +import org.apache.camel.Exchange; +import org.apache.camel.component.github.GitHubEndpoint; +import org.apache.camel.spi.Registry; +import org.eclipse.egit.github.core.CommitStatus; +import org.eclipse.egit.github.core.service.CommitService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Producer endpoint that sets the commit status. + * + * The endpoint requires the "GitHubPullRequest" header, identifying the pull request number (integer), + * and the "GitHubPullRequestHeadCommitSHA" header, identifying the commit SHA on which the state will be recorded. + */ +public class PullRequestStateProducer extends AbstractGitHubProducer { + private static final transient Logger LOG = LoggerFactory.getLogger(PullRequestStateProducer.class); + + private CommitService commitService; + + private String state; + private String targetUrl; + + public PullRequestStateProducer(GitHubEndpoint endpoint) throws Exception { + super(endpoint); + + Registry registry = endpoint.getCamelContext().getRegistry(); + Object service = registry.lookupByName("githubCommitService"); + if (service != null) { + LOG.debug("Using CommitService found in registry " + service.getClass().getCanonicalName()); + commitService = (CommitService) service; + } else { + commitService = new CommitService(); + } + initService(commitService); + + state = endpoint.getState(); + targetUrl = endpoint.getTargetUrl(); + } + + public void process(Exchange exchange) throws Exception { + String pullRequestNumberSHA = exchange.getIn().getHeader("GitHubPullRequestHeadCommitSHA", String.class); + String text = exchange.getIn().getBody(String.class); + + CommitStatus status=new CommitStatus(); + + if (state != null) { + status.setState(state); + } + + if (targetUrl != null) { + status.setTargetUrl(targetUrl); + } + + if (text != null) { + status.setDescription(text); + } + + CommitStatus response=commitService.createStatus(getRepository(), pullRequestNumberSHA, status); + + // copy the header of in message to the out message + exchange.getOut().copyFrom(exchange.getIn()); + exchange.getOut().setBody(response); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/ClosePullRequestProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/ClosePullRequestProducerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/ClosePullRequestProducerTest.java deleted file mode 100755 index 6b5a1a8..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/ClosePullRequestProducerTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github; - -import java.util.List; -import java.util.Map; - -import org.apache.camel.Endpoint; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.eclipse.egit.github.core.PullRequest; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ClosePullRequestProducerTest extends GitHubComponentTestBase { - public static final String PULL_REQUEST_PRODUCER_ENDPOINT = "direct:validPullRequest"; - protected static final Logger LOG = LoggerFactory.getLogger(ClosePullRequestProducerTest.class); - private long latestPullRequestId; - - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - context.addComponent("github", new GitHubComponent()); - from(PULL_REQUEST_PRODUCER_ENDPOINT) - .process(new ClosePullRequestProducerProcessor()) - .to("github://closePullRequest?" + GITHUB_CREDENTIALS_STRING); - } // end of configure - - - }; - } - - - @Test - public void testPullRequestCommentProducer() throws Exception { - // Create a pull request - PullRequest pullRequest = pullRequestService.addPullRequest("testPullRequestCommentProducer"); - latestPullRequestId = pullRequest.getId(); - - - // Close it - Endpoint closePullRequestEndpoint = getMandatoryEndpoint(PULL_REQUEST_PRODUCER_ENDPOINT); - Exchange exchange = closePullRequestEndpoint.createExchange(); - template.send(closePullRequestEndpoint, exchange); - - Thread.sleep(1 * 1000); - - // Verify that it was closed - - List<PullRequest> closedPullRequests = pullRequestService.getPullRequests(null, "closed"); - assertNotNull(closedPullRequests); - boolean found = false; - for (PullRequest pr : closedPullRequests) { - if (pr.getId() == latestPullRequestId) { - found = true; - break; - } - } - - assertTrue("Didn't find pull request " + latestPullRequestId, found); - } - - - public class ClosePullRequestProducerProcessor implements Processor { - @Override - public void process(Exchange exchange) throws Exception { - Message in = exchange.getIn(); - Map<String, Object> headers = in.getHeaders(); - headers.put("GitHubPullRequest", latestPullRequestId); - } - } - - -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/CommitConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/CommitConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/CommitConsumerTest.java deleted file mode 100644 index c9bd8b5..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/CommitConsumerTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.eclipse.egit.github.core.RepositoryCommit; -import org.eclipse.egit.github.core.User; -import org.junit.Test; - -public class CommitConsumerTest extends GitHubComponentTestBase { - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - context.addComponent("github", new GitHubComponent()); - from("github://commit/master?" + GITHUB_CREDENTIALS_STRING). - process(new GitHubCommitProcessor()) - .to(mockResultEndpoint); - } - }; - } - - - @Test - public void commitConsumerTest() throws Exception { - mockResultEndpoint.expectedMessageCount(2); - RepositoryCommit commit1 = commitService.addRepositoryCommit(); - RepositoryCommit commit2 = commitService.addRepositoryCommit(); - mockResultEndpoint.expectedBodiesReceivedInAnyOrder(commit1, commit2); - - Thread.sleep(1 * 1000); - - mockResultEndpoint.assertIsSatisfied(); - } - - public class GitHubCommitProcessor implements Processor { - @Override - public void process(Exchange exchange) throws Exception { - Message in = exchange.getIn(); - RepositoryCommit commit = (RepositoryCommit) in.getBody(); - User author = commit.getAuthor(); - log.debug("Got commit with author: " + author.getLogin() + ": " + author.getHtmlUrl() + " SHA " + commit.getSha()); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/GitHubComponentTestBase.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/GitHubComponentTestBase.java b/components/camel-github/src/test/java/org/apache/camel/component/github/GitHubComponentTestBase.java index ac93fb5..d0cba8c 100644 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/GitHubComponentTestBase.java +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/GitHubComponentTestBase.java @@ -17,10 +17,10 @@ package org.apache.camel.component.github; import org.apache.camel.EndpointInject; -import org.apache.camel.component.github.consumer.MockCommitService; -import org.apache.camel.component.github.consumer.MockIssueService; -import org.apache.camel.component.github.consumer.MockPullRequestService; -import org.apache.camel.component.github.consumer.MockRepositoryService; +import org.apache.camel.component.github.services.MockCommitService; +import org.apache.camel.component.github.services.MockIssueService; +import org.apache.camel.component.github.services.MockPullRequestService; +import org.apache.camel.component.github.services.MockRepositoryService; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.impl.JndiRegistry; import org.apache.camel.test.junit4.CamelTestSupport; http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentConsumerTest.java deleted file mode 100644 index ad489f6..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentConsumerTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.eclipse.egit.github.core.Comment; -import org.eclipse.egit.github.core.CommitComment; -import org.eclipse.egit.github.core.PullRequest; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PullRequestCommentConsumerTest extends GitHubComponentTestBase { - protected static final Logger LOG = LoggerFactory.getLogger(PullRequestCommentConsumerTest.class); - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - context.addComponent("github", new GitHubComponent()); - from("github://pullRequestComment?" + GITHUB_CREDENTIALS_STRING) - .process(new PullRequestCommentProcessor()) - .to(mockResultEndpoint); - } - }; - } - - @Test - public void pullRequestCommentTest() throws Exception { - PullRequest pr1 = pullRequestService.addPullRequest("First add"); - PullRequest pr2 = pullRequestService.addPullRequest("Second"); - CommitComment commitComment1 = pullRequestService.addComment(pr1.getId(), "First comment"); - CommitComment commitComment2 = pullRequestService.addComment(pr2.getId(), "Second comment"); - mockResultEndpoint.expectedBodiesReceivedInAnyOrder(commitComment1, commitComment2); - - Thread.sleep(1 * 1000); // TODO do I need this? - - mockResultEndpoint.assertIsSatisfied(); - } - - - public class PullRequestCommentProcessor implements Processor { - @Override - public void process(Exchange exchange) throws Exception { - Message in = exchange.getIn(); - Comment comment = (Comment) in.getBody(); - LOG.debug("Got Comment " + comment.getId() + " [" + comment.getBody() + "] from User [" + comment.getUser().getLogin() + "]"); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentProducerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentProducerTest.java deleted file mode 100644 index ef70c5e..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestCommentProducerTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github; - -import java.util.Date; -import java.util.List; -import java.util.Map; - -import org.apache.camel.Endpoint; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.eclipse.egit.github.core.CommitComment; -import org.eclipse.egit.github.core.PullRequest; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PullRequestCommentProducerTest extends GitHubComponentTestBase { - protected static final Logger LOG = LoggerFactory.getLogger(PullRequestCommentProducerTest.class); - private long latestPullRequestId; - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - context.addComponent("github", new GitHubComponent()); - from("direct:validPullRequest") - .process(new MockPullRequestCommentProducerProcessor()) - .to("github://pullRequestComment?" + GITHUB_CREDENTIALS_STRING); - } // end of configure - - - }; - } - - - @Test - public void testPullRequestCommentProducer() throws Exception { - PullRequest pullRequest = pullRequestService.addPullRequest("testPullRequestCommentProducer"); - latestPullRequestId = pullRequest.getId(); - - Endpoint commentProducerEndpoint = getMandatoryEndpoint("direct:validPullRequest"); - Exchange exchange = commentProducerEndpoint.createExchange(); - String commentText = "Pushed this comment at " + new Date(); - exchange.getIn().setBody(commentText); - template.send(commentProducerEndpoint, exchange); - - Thread.sleep(1 * 1000); - - // Verify that the mock pull request service received this comment. - List<CommitComment> commitComments = pullRequestService.getComments(null, (int) pullRequest.getId()); - assertEquals(1, commitComments.size()); - CommitComment commitComment = commitComments.get(0); - assertEquals("Commit IDs did not match ", Long.toString(pullRequest.getId()), commitComment.getCommitId()); - assertEquals("Comment text did not match ", commentText, commitComment.getBodyText()); - } - - - public class MockPullRequestCommentProducerProcessor implements Processor { - @Override - public void process(Exchange exchange) throws Exception { - Message in = exchange.getIn(); - Map<String, Object> headers = in.getHeaders(); - headers.put("GitHubPullRequest", latestPullRequestId); - } - } - - -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestConsumerTest.java deleted file mode 100644 index a2df1d3..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/PullRequestConsumerTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.eclipse.egit.github.core.PullRequest; -import org.eclipse.egit.github.core.User; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PullRequestConsumerTest extends GitHubComponentTestBase { - protected static final Logger LOG = LoggerFactory.getLogger(PullRequestConsumerTest.class); - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - context.addComponent("github", new GitHubComponent()); - from("github://pullRequest?" + GITHUB_CREDENTIALS_STRING) - .process(new MockPullRequestProcessor()) - .to(mockResultEndpoint); - } - }; - } - - - @Test - public void pullRequestTest() throws Exception { - PullRequest pr1 = pullRequestService.addPullRequest("First add"); - PullRequest pr2 = pullRequestService.addPullRequest("Second"); - mockResultEndpoint.expectedMessageCount(2); - mockResultEndpoint.expectedBodiesReceivedInAnyOrder(pr1, pr2); - Thread.sleep(1 * 1000); - - mockResultEndpoint.assertIsSatisfied(); - } - - public class MockPullRequestProcessor implements Processor { - @Override - public void process(Exchange exchange) throws Exception { - Message in = exchange.getIn(); - PullRequest pullRequest = (PullRequest) in.getBody(); - User pullRequestUser = pullRequest.getUser(); - - pullRequest.getTitle(); - pullRequest.getHtmlUrl(); - pullRequest.getUser().getLogin(); - pullRequest.getUser().getHtmlUrl(); - LOG.debug("Got PullRequest " + pullRequest.getHtmlUrl() + " [" + pullRequest.getTitle() + "] From " + pullRequestUser.getLogin()); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/TagConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/TagConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/TagConsumerTest.java deleted file mode 100644 index 6d69954..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/TagConsumerTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github; - -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.eclipse.egit.github.core.RepositoryTag; -import org.junit.Test; - -public class TagConsumerTest extends GitHubComponentTestBase { - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - context.addComponent("github", new GitHubComponent()); - from("github://tag?" + GITHUB_CREDENTIALS_STRING) - .process(new RepositoryTagProcessor()) - .to(mockResultEndpoint); - } - }; - } - - - @Test - public void tagConsumerTest() throws Exception { - RepositoryTag tag1 = repositoryService.addTag("TAG1"); - RepositoryTag tag2 = repositoryService.addTag("TAG2"); - RepositoryTag tag3 = repositoryService.addTag("TAG3"); - mockResultEndpoint.expectedBodiesReceivedInAnyOrder(tag1, tag2, tag3); - Thread.sleep(1 * 1000); - - mockResultEndpoint.assertIsSatisfied(); - } - - public class RepositoryTagProcessor implements Processor { - @Override - public void process(Exchange exchange) throws Exception { - Message in = exchange.getIn(); - RepositoryTag tag = (RepositoryTag) in.getBody(); - log.debug("Got TAG [" + tag.getName() + "]"); - } - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerTest.java new file mode 100644 index 0000000..196231c --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/CommitConsumerTest.java @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.consumer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.RepositoryCommit; +import org.eclipse.egit.github.core.User; +import org.junit.Test; + +public class CommitConsumerTest extends GitHubComponentTestBase { + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from("github://commit/master?" + GITHUB_CREDENTIALS_STRING). + process(new GitHubCommitProcessor()) + .to(mockResultEndpoint); + } + }; + } + + + @Test + public void commitConsumerTest() throws Exception { + mockResultEndpoint.expectedMessageCount(2); + RepositoryCommit commit1 = commitService.addRepositoryCommit(); + RepositoryCommit commit2 = commitService.addRepositoryCommit(); + mockResultEndpoint.expectedBodiesReceivedInAnyOrder(commit1, commit2); + + Thread.sleep(1 * 1000); + + mockResultEndpoint.assertIsSatisfied(); + } + + public class GitHubCommitProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + RepositoryCommit commit = (RepositoryCommit) in.getBody(); + User author = commit.getAuthor(); + log.debug("Got commit with author: " + author.getLogin() + ": " + author.getHtmlUrl() + " SHA " + commit.getSha()); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockCommitService.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockCommitService.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockCommitService.java deleted file mode 100644 index a7bc45a..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockCommitService.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github.consumer; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; - -import org.eclipse.egit.github.core.IRepositoryIdProvider; -import org.eclipse.egit.github.core.RepositoryCommit; -import org.eclipse.egit.github.core.User; -import org.eclipse.egit.github.core.service.CommitService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MockCommitService extends CommitService { - protected static final Logger LOG = LoggerFactory.getLogger(MockCommitService.class); - - private List<RepositoryCommit> commitsList = new ArrayList<RepositoryCommit>(); - private AtomicLong fakeSha = new AtomicLong(System.currentTimeMillis()); - - public synchronized RepositoryCommit addRepositoryCommit() { - User author = new User(); - author.setEmail("some...@gmail.com"); // TODO change - author.setHtmlUrl("http://github/someguy"); - author.setLogin("someguy"); - - RepositoryCommit rc = new RepositoryCommit(); - rc.setAuthor(author); - rc.setSha(fakeSha.incrementAndGet() + ""); - LOG.debug("In MockCommitService added commit with sha " + rc.getSha()); - commitsList.add(rc); - - return rc; - } - - @Override - public synchronized List<RepositoryCommit> getCommits(IRepositoryIdProvider repository, String sha, String path) throws IOException { - LOG.debug("Returning list of size " + commitsList.size()); - return commitsList; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockIssueService.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockIssueService.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockIssueService.java deleted file mode 100644 index 4106a8a..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockIssueService.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github.consumer; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.egit.github.core.Comment; -import org.eclipse.egit.github.core.IRepositoryIdProvider; -import org.eclipse.egit.github.core.service.IssueService; - -public class MockIssueService extends IssueService { - - private List<Comment> comments = new ArrayList<Comment>(); - private MockPullRequestService mockPullRequestService; - - public MockIssueService(MockPullRequestService mockPullRequestService) { - this.mockPullRequestService = mockPullRequestService; - - } - - @Override - public List<Comment> getComments(IRepositoryIdProvider repository, int issueNumber) { - return comments; - } - - @Override - public Comment createComment(IRepositoryIdProvider repository, int issueNumber, String commentText) throws IOException { - Comment addedComment = mockPullRequestService.addComment((long) issueNumber, commentText); - return addedComment; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockPullRequestService.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockPullRequestService.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockPullRequestService.java deleted file mode 100755 index b171e4a..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockPullRequestService.java +++ /dev/null @@ -1,126 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github.consumer; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -import org.eclipse.egit.github.core.CommitComment; -import org.eclipse.egit.github.core.IRepositoryIdProvider; -import org.eclipse.egit.github.core.PullRequest; -import org.eclipse.egit.github.core.User; -import org.eclipse.egit.github.core.service.PullRequestService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MockPullRequestService extends PullRequestService { - protected static final Logger LOG = LoggerFactory.getLogger(MockPullRequestService.class); - - private Map<Long, PullRequest> pullRequests = new HashMap<Long, PullRequest>(); - private List<CommitComment> emptyComments = new ArrayList<CommitComment>(); - private AtomicInteger pullRequestNumber = new AtomicInteger(101); - private AtomicInteger commentId = new AtomicInteger(500); - private Map<Long, List<CommitComment>> allComments = new HashMap<Long, List<CommitComment>>(); - - public List<CommitComment> getComments(IRepositoryIdProvider repository, int pullRequestId) { - Long id = new Long(pullRequestId); - if (allComments.containsKey(id)) { - List<CommitComment> comments = allComments.get(id); - return comments; - } else { - return emptyComments; - } - } - - private User createAuthor() { - User author = new User(); - author.setEmail("some...@gmail.com"); - author.setHtmlUrl("http://github/someguy"); - author.setLogin("someguy"); - - return author; - } - - public CommitComment addComment(Long pullRequestId, String bodyText) { - CommitComment commitComment = new CommitComment(); - - User author = createAuthor(); - commitComment.setUser(author); - commitComment.setCommitId("" + pullRequestId); - commitComment.setId(commentId.getAndIncrement()); - commitComment.setBody(bodyText); - commitComment.setBodyText(bodyText); - - List<CommitComment> comments; - if (allComments.containsKey(pullRequestId)) { - comments = allComments.get(pullRequestId); - } else { - comments = new ArrayList<CommitComment>(); - } - comments.add(commitComment); - allComments.put(pullRequestId, comments); - - return commitComment; - } - - public PullRequest addPullRequest(String title) { - User author = createAuthor(); - - PullRequest pullRequest = new PullRequest(); - pullRequest.setUser(author); - pullRequest.setHtmlUrl("https://github.com/someguy/somerepo/pull" + pullRequestNumber); - pullRequest.setTitle(title); - pullRequest.setNumber(pullRequestNumber.get()); - pullRequest.setId(pullRequestNumber.get()); - pullRequest.setState("open"); - pullRequests.put(pullRequest.getId(), pullRequest); - - pullRequestNumber.incrementAndGet(); - return pullRequest; - } - - @Override - public PullRequest getPullRequest(IRepositoryIdProvider repository, int id) throws IOException { - PullRequest pullRequest = pullRequests.get((long) id); - return pullRequest; - } - - @Override - public PullRequest editPullRequest(IRepositoryIdProvider repository, PullRequest request) throws IOException { - pullRequests.put(request.getId(), request); - return request; - } - - @Override - public synchronized List<PullRequest> getPullRequests(IRepositoryIdProvider repository, String state) { - List<PullRequest> result = new ArrayList<PullRequest>(); - - for (Long id : pullRequests.keySet()) { - PullRequest pr = pullRequests.get(id); - if (pr.getState().equals(state)) { - result.add(pr); - } - } - - LOG.debug("Returning list of " + result.size() + " pull requests with state " + state); - return result; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockRepositoryService.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockRepositoryService.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockRepositoryService.java deleted file mode 100644 index d42bcd4..0000000 --- a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/MockRepositoryService.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.github.consumer; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.egit.github.core.IRepositoryIdProvider; -import org.eclipse.egit.github.core.Repository; -import org.eclipse.egit.github.core.RepositoryTag; -import org.eclipse.egit.github.core.service.RepositoryService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class MockRepositoryService extends RepositoryService { - protected static final Logger LOG = LoggerFactory.getLogger(MockRepositoryService.class); - - private List<RepositoryTag> tags = new ArrayList<RepositoryTag>(); - - public RepositoryTag addTag(String tagName) { - RepositoryTag tag = new RepositoryTag(); - tag.setName(tagName); - tags.add(tag); - - return tag; - } - - @Override - public Repository getRepository(final String owner, final String name) throws IOException { - Repository repository = new Repository(); - repository.setName(name); - return repository; - } - - @Override - public List<RepositoryTag> getTags(IRepositoryIdProvider repository) throws IOException { - LOG.debug("in MockRepositoryService returning " + tags.size() + " tags"); - return tags; - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumerTest.java new file mode 100644 index 0000000..e20bd1a --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestCommentConsumerTest.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.consumer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.Comment; +import org.eclipse.egit.github.core.CommitComment; +import org.eclipse.egit.github.core.PullRequest; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PullRequestCommentConsumerTest extends GitHubComponentTestBase { + protected static final Logger LOG = LoggerFactory.getLogger(PullRequestCommentConsumerTest.class); + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from("github://pullRequestComment?" + GITHUB_CREDENTIALS_STRING) + .process(new PullRequestCommentProcessor()) + .to(mockResultEndpoint); + } + }; + } + + @Test + public void pullRequestCommentTest() throws Exception { + PullRequest pr1 = pullRequestService.addPullRequest("First add"); + PullRequest pr2 = pullRequestService.addPullRequest("Second"); + CommitComment commitComment1 = pullRequestService.addComment(pr1.getId(), "First comment"); + CommitComment commitComment2 = pullRequestService.addComment(pr2.getId(), "Second comment"); + mockResultEndpoint.expectedBodiesReceivedInAnyOrder(commitComment1, commitComment2); + + Thread.sleep(1 * 1000); // TODO do I need this? + + mockResultEndpoint.assertIsSatisfied(); + } + + + public class PullRequestCommentProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + Comment comment = (Comment) in.getBody(); + LOG.debug("Got Comment " + comment.getId() + " [" + comment.getBody() + "] from User [" + comment.getUser().getLogin() + "]"); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestConsumerTest.java new file mode 100644 index 0000000..c197085 --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/PullRequestConsumerTest.java @@ -0,0 +1,74 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.consumer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.PullRequest; +import org.eclipse.egit.github.core.User; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PullRequestConsumerTest extends GitHubComponentTestBase { + protected static final Logger LOG = LoggerFactory.getLogger(PullRequestConsumerTest.class); + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from("github://pullRequest?" + GITHUB_CREDENTIALS_STRING) + .process(new MockPullRequestProcessor()) + .to(mockResultEndpoint); + } + }; + } + + + @Test + public void pullRequestTest() throws Exception { + PullRequest pr1 = pullRequestService.addPullRequest("First add"); + PullRequest pr2 = pullRequestService.addPullRequest("Second"); + mockResultEndpoint.expectedMessageCount(2); + mockResultEndpoint.expectedBodiesReceivedInAnyOrder(pr1, pr2); + Thread.sleep(1 * 1000); + + mockResultEndpoint.assertIsSatisfied(); + } + + public class MockPullRequestProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + PullRequest pullRequest = (PullRequest) in.getBody(); + User pullRequestUser = pullRequest.getUser(); + + pullRequest.getTitle(); + pullRequest.getHtmlUrl(); + pullRequest.getUser().getLogin(); + pullRequest.getUser().getHtmlUrl(); + LOG.debug("Got PullRequest " + pullRequest.getHtmlUrl() + " [" + pullRequest.getTitle() + "] From " + pullRequestUser.getLogin()); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/TagConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/TagConsumerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/TagConsumerTest.java new file mode 100644 index 0000000..4b0ffab --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/consumer/TagConsumerTest.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.consumer; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.RepositoryTag; +import org.junit.Test; + +public class TagConsumerTest extends GitHubComponentTestBase { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from("github://tag?" + GITHUB_CREDENTIALS_STRING) + .process(new RepositoryTagProcessor()) + .to(mockResultEndpoint); + } + }; + } + + + @Test + public void tagConsumerTest() throws Exception { + RepositoryTag tag1 = repositoryService.addTag("TAG1"); + RepositoryTag tag2 = repositoryService.addTag("TAG2"); + RepositoryTag tag3 = repositoryService.addTag("TAG3"); + mockResultEndpoint.expectedBodiesReceivedInAnyOrder(tag1, tag2, tag3); + Thread.sleep(1 * 1000); + + mockResultEndpoint.assertIsSatisfied(); + } + + public class RepositoryTagProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + RepositoryTag tag = (RepositoryTag) in.getBody(); + log.debug("Got TAG [" + tag.getName() + "]"); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/producer/ClosePullRequestProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/producer/ClosePullRequestProducerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/ClosePullRequestProducerTest.java new file mode 100755 index 0000000..c9a8a94 --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/ClosePullRequestProducerTest.java @@ -0,0 +1,97 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.producer; + +import java.util.List; +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.PullRequest; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClosePullRequestProducerTest extends GitHubComponentTestBase { + public static final String PULL_REQUEST_PRODUCER_ENDPOINT = "direct:validPullRequest"; + protected static final Logger LOG = LoggerFactory.getLogger(ClosePullRequestProducerTest.class); + private long latestPullRequestId; + + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from(PULL_REQUEST_PRODUCER_ENDPOINT) + .process(new ClosePullRequestProducerProcessor()) + .to("github://closePullRequest?" + GITHUB_CREDENTIALS_STRING); + } // end of configure + + + }; + } + + + @Test + public void testPullRequestCommentProducer() throws Exception { + // Create a pull request + PullRequest pullRequest = pullRequestService.addPullRequest("testPullRequestCommentProducer"); + latestPullRequestId = pullRequest.getId(); + + + // Close it + Endpoint closePullRequestEndpoint = getMandatoryEndpoint(PULL_REQUEST_PRODUCER_ENDPOINT); + Exchange exchange = closePullRequestEndpoint.createExchange(); + template.send(closePullRequestEndpoint, exchange); + + Thread.sleep(1 * 1000); + + // Verify that it was closed + + List<PullRequest> closedPullRequests = pullRequestService.getPullRequests(null, "closed"); + assertNotNull(closedPullRequests); + boolean found = false; + for (PullRequest pr : closedPullRequests) { + if (pr.getId() == latestPullRequestId) { + found = true; + break; + } + } + + assertTrue("Didn't find pull request " + latestPullRequestId, found); + } + + + public class ClosePullRequestProducerProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + Map<String, Object> headers = in.getHeaders(); + headers.put("GitHubPullRequest", latestPullRequestId); + } + } + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestCommentProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestCommentProducerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestCommentProducerTest.java new file mode 100644 index 0000000..9396b3e --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestCommentProducerTest.java @@ -0,0 +1,89 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.producer; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.CommitComment; +import org.eclipse.egit.github.core.PullRequest; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PullRequestCommentProducerTest extends GitHubComponentTestBase { + protected static final Logger LOG = LoggerFactory.getLogger(PullRequestCommentProducerTest.class); + private long latestPullRequestId; + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from("direct:validPullRequest") + .process(new MockPullRequestCommentProducerProcessor()) + .to("github://pullRequestComment?" + GITHUB_CREDENTIALS_STRING); + } // end of configure + + + }; + } + + + @Test + public void testPullRequestCommentProducer() throws Exception { + PullRequest pullRequest = pullRequestService.addPullRequest("testPullRequestCommentProducer"); + latestPullRequestId = pullRequest.getId(); + + Endpoint commentProducerEndpoint = getMandatoryEndpoint("direct:validPullRequest"); + Exchange exchange = commentProducerEndpoint.createExchange(); + String commentText = "Pushed this comment at " + new Date(); + exchange.getIn().setBody(commentText); + template.send(commentProducerEndpoint, exchange); + + Thread.sleep(1 * 1000); + + // Verify that the mock pull request service received this comment. + List<CommitComment> commitComments = pullRequestService.getComments(null, (int) pullRequest.getId()); + assertEquals(1, commitComments.size()); + CommitComment commitComment = commitComments.get(0); + assertEquals("Commit IDs did not match ", Long.toString(pullRequest.getId()), commitComment.getCommitId()); + assertEquals("Comment text did not match ", commentText, commitComment.getBodyText()); + } + + + public class MockPullRequestCommentProducerProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + Map<String, Object> headers = in.getHeaders(); + headers.put("GitHubPullRequest", latestPullRequestId); + } + } + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/1cb3c092/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestFilesProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestFilesProducerTest.java b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestFilesProducerTest.java new file mode 100644 index 0000000..d975532 --- /dev/null +++ b/components/camel-github/src/test/java/org/apache/camel/component/github/producer/PullRequestFilesProducerTest.java @@ -0,0 +1,88 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.github.producer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.github.GitHubComponent; +import org.apache.camel.component.github.GitHubComponentTestBase; +import org.eclipse.egit.github.core.CommitFile; +import org.eclipse.egit.github.core.PullRequest; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PullRequestFilesProducerTest extends GitHubComponentTestBase { + protected static final Logger LOG = LoggerFactory.getLogger(PullRequestFilesProducerTest.class); + private int latestPullRequestNumber; + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + context.addComponent("github", new GitHubComponent()); + from("direct:validPullRequest") + .process(new MockPullFilesProducerProcessor()) + .to("github://pullRequestFiles?" + GITHUB_CREDENTIALS_STRING); + } // end of configure + + + }; + } + + + @Test + public void testPullRequestFilesProducer() throws Exception { + PullRequest pullRequest = pullRequestService.addPullRequest("testPullRequestFilesProducer"); + latestPullRequestNumber = pullRequest.getNumber(); + + CommitFile file=new CommitFile(); + file.setFilename("testfile"); + + List<CommitFile> commitFiles=new ArrayList<CommitFile>(); + commitFiles.add(file); + pullRequestService.setFiles(latestPullRequestNumber, commitFiles); + + Endpoint filesProducerEndpoint = getMandatoryEndpoint("direct:validPullRequest"); + Exchange exchange = filesProducerEndpoint.createExchange(); + + Exchange resp = template.send(filesProducerEndpoint, exchange); + + assertEquals(resp.getOut().getBody(), commitFiles); + } + + + public class MockPullFilesProducerProcessor implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + Map<String, Object> headers = in.getHeaders(); + headers.put("GitHubPullRequest", latestPullRequestNumber); + } + } + + +}