Skip to content

Commit

Permalink
CAMEL-7902 New test for camel-github
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinearls authored and WillemJiang committed Oct 10, 2014
1 parent 2275e28 commit f1d4afe
Show file tree
Hide file tree
Showing 16 changed files with 781 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import org.apache.camel.Processor;
import org.apache.camel.component.github.GitHubEndpoint;
import org.apache.camel.impl.ScheduledPollConsumer;
import org.apache.camel.spi.Registry;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.service.GitHubService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractGitHubConsumer extends ScheduledPollConsumer {

private static final transient Logger LOG = LoggerFactory.getLogger(AbstractGitHubConsumer.class);

private final GitHubEndpoint endpoint;

private RepositoryService repositoryService;
Expand All @@ -34,8 +38,16 @@ public abstract class AbstractGitHubConsumer extends ScheduledPollConsumer {
public AbstractGitHubConsumer(GitHubEndpoint endpoint, Processor processor) throws Exception {
super(endpoint, processor);
this.endpoint = endpoint;

repositoryService = new RepositoryService();

Registry registry = endpoint.getCamelContext().getRegistry();
Object service = registry.lookupByName("githubRepositoryService");
if (service !=null) {
LOG.debug("Using RepositoryService found in registry " + service.getClass().getCanonicalName());
repositoryService = (RepositoryService) service;
} else {
repositoryService = new RepositoryService();
}

initService(repositoryService);
repository = repositoryService.getRepository(endpoint.getRepoOwner(), endpoint.getRepoName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.github.GitHubEndpoint;
import org.apache.camel.spi.Registry;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.service.CommitService;
import org.slf4j.Logger;
Expand All @@ -37,8 +38,15 @@ public class CommitConsumer extends AbstractGitHubConsumer {

public CommitConsumer(String branchName, GitHubEndpoint endpoint, Processor processor) throws Exception {
super(endpoint, processor);

commitService = new CommitService();

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);

LOG.info("GitHub CommitConsumer: Indexing current commits...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.github.GitHubEndpoint;
import org.apache.camel.spi.Registry;
import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.CommitComment;
import org.eclipse.egit.github.core.PullRequest;
Expand All @@ -42,10 +43,23 @@ public class PullRequestCommentConsumer extends AbstractGitHubConsumer {

public PullRequestCommentConsumer(GitHubEndpoint endpoint, Processor processor) throws Exception {
super(endpoint, processor);

pullRequestService = new PullRequestService();

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);
issueService = new IssueService();

service = registry.lookupByName("githbIssueService");
if (service != null) {
issueService = (IssueService) service;
} else {
issueService = new IssueService();
}
initService(issueService);

LOG.info("GitHub PullRequestCommentConsumer: Indexing current pull request comments...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.github.GitHubEndpoint;
import org.apache.camel.spi.Registry;
import org.eclipse.egit.github.core.PullRequest;
import org.eclipse.egit.github.core.service.PullRequestService;
import org.slf4j.Logger;
Expand All @@ -36,8 +37,16 @@ public class PullRequestConsumer extends AbstractGitHubConsumer {

public PullRequestConsumer(GitHubEndpoint endpoint, Processor processor) throws Exception {
super(endpoint, processor);

pullRequestService = new PullRequestService();

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);

LOG.info("GitHub PullRequestConsumer: Indexing current pull requests...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.component.github.GitHubEndpoint;
import org.apache.camel.impl.DefaultProducer;
import org.apache.camel.spi.Registry;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.service.GitHubService;
import org.eclipse.egit.github.core.service.RepositoryService;
Expand All @@ -34,9 +35,14 @@ public abstract class AbstractGitHubProducer extends DefaultProducer {
public AbstractGitHubProducer(GitHubEndpoint endpoint) throws Exception {
super(endpoint);
this.endpoint = endpoint;

repositoryService = new RepositoryService();
initService(repositoryService);

Registry registry = endpoint.getCamelContext().getRegistry();
Object service = registry.lookupByName("githubRepositoryService");
if (service !=null) {
repositoryService = (RepositoryService) service;
} else {
repositoryService = new RepositoryService();
}
repository = repositoryService.getRepository(endpoint.getRepoOwner(), endpoint.getRepoName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import org.apache.camel.Exchange;
import org.apache.camel.component.github.GitHubEndpoint;
import org.apache.camel.component.github.consumer.PullRequestCommentConsumer;
import org.apache.camel.spi.Registry;
import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.service.IssueService;
import org.eclipse.egit.github.core.service.PullRequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Producer endpoint that adds one of two types of comments on a GitHub pull request:
Expand All @@ -32,16 +36,30 @@
* Both endpoints require the "GitHubPullRequest" header, identifying the pull request number (integer).
*/
public class PullRequestCommentProducer extends AbstractGitHubProducer {
private static final transient Logger LOG = LoggerFactory.getLogger(PullRequestCommentProducer.class);
private PullRequestService pullRequestService;

private IssueService issueService;

public PullRequestCommentProducer(GitHubEndpoint endpoint) throws Exception {
super(endpoint);

pullRequestService = new PullRequestService();
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);
issueService = new IssueService();

service = registry.lookupByName("githbIssueService");
if (service != null) {
issueService = (IssueService) service;
} else {
issueService = new IssueService();
}
initService(issueService);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

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());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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.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.mock.MockEndpoint;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

public abstract class GitHubComponentTestBase extends CamelTestSupport {
public String USERNAME = "someguy";
public String PASSWORD = "apassword";
public String REPO_OWNER = "anotherguy";
public String REPO_NAME = "somerepo";
public String GITHUB_CREDENTIALS_STRING = "username=" + USERNAME + "&password=" + PASSWORD + "&repoOwner=" + REPO_OWNER + "&repoName=" + REPO_NAME;

@Rule
public TestName testName = new TestName();

protected MockCommitService commitService;
protected MockRepositoryService repositoryService;
protected MockPullRequestService pullRequestService;
protected MockIssueService issueService;

@EndpointInject(uri = "mock:result")
protected MockEndpoint mockResultEndpoint;

@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
commitService = new MockCommitService();
registry.bind("githubCommitService", commitService);

repositoryService = new MockRepositoryService();
registry.bind("githubRepositoryService", repositoryService);

pullRequestService = new MockPullRequestService();
registry.bind("githubPullRequestService", pullRequestService);

issueService = new MockIssueService(pullRequestService);
registry.bind("githbIssueService", issueService);

return registry;
}

@Before
public void setUp() throws Exception {
super.setUp();
log.debug("Starting test " + testName.getMethodName());
}

@Test
public void emptyAtStartupTest() throws Exception {
mockResultEndpoint.expectedMessageCount(0);
mockResultEndpoint.assertIsSatisfied();
}

}
Loading

0 comments on commit f1d4afe

Please sign in to comment.