camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject git commit: CAME-7902 Added test for new ClosePullRequestProducer
Date Tue, 14 Oct 2014 01:31:04 GMT
Repository: camel
Updated Branches:
  refs/heads/master 402e5a1d0 -> a58a4fa43


CAME-7902 Added test for new ClosePullRequestProducer


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a58a4fa4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a58a4fa4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a58a4fa4

Branch: refs/heads/master
Commit: a58a4fa43d64f20e9c1a84aebe5f7ad651127472
Parents: 402e5a1
Author: Kevin Earls <kevin@kevinearls.com>
Authored: Mon Oct 13 12:27:05 2014 +0200
Committer: Kevin Earls <kevin@kevinearls.com>
Committed: Mon Oct 13 12:27:05 2014 +0200

----------------------------------------------------------------------
 .../producer/ClosePullRequestProducer.java      |  9 +-
 .../github/ClosePullRequestProducerTest.java    | 96 ++++++++++++++++++++
 .../github/consumer/MockPullRequestService.java | 32 +++++--
 3 files changed, 130 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a58a4fa4/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
old mode 100644
new mode 100755
index cf50fd9..a09517b
--- a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
+++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java
@@ -20,6 +20,7 @@ import java.util.Calendar;
 
 import org.apache.camel.Exchange;
 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;
 
@@ -34,7 +35,13 @@ public class ClosePullRequestProducer extends AbstractGitHubProducer {
     public ClosePullRequestProducer(GitHubEndpoint endpoint) throws Exception {
         super(endpoint);
         
-        pullRequestService = new PullRequestService();
+        Registry registry = endpoint.getCamelContext().getRegistry();
+        Object service = registry.lookupByName("githubPullRequestService");
+        if (service != null) {
+            pullRequestService = (PullRequestService) service;
+        } else {
+            pullRequestService = new PullRequestService();
+        }
         initService(pullRequestService);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a58a4fa4/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
new file mode 100755
index 0000000..217a6c7
--- /dev/null
+++ b/components/camel-github/src/test/java/org/apache/camel/component/github/ClosePullRequestProducerTest.java
@@ -0,0 +1,96 @@
+/**
+ * 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.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;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+public class ClosePullRequestProducerTest extends GitHubComponentTestBase {
+    protected static final Logger LOG = LoggerFactory.getLogger(ClosePullRequestProducerTest.class);
+    private long latestPullRequestId;
+    public static final String PULL_REQUEST_PRODUCER_ENDPOINT = "direct:validPullRequest";
+
+    @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/a58a4fa4/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
old mode 100644
new mode 100755
index db9427e..1800f55
--- 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
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.github.consumer;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -33,7 +34,7 @@ import org.slf4j.LoggerFactory;
 public class MockPullRequestService extends PullRequestService {
     protected static final Logger LOG = LoggerFactory.getLogger(MockPullRequestService.class);
 
-    private List<PullRequest> pullRequestsList = new ArrayList<>();
+    private Map<Long, PullRequest> pullRequests = new HashMap<>();
     private List<CommitComment> emptyComments = new ArrayList<>();
     private AtomicInteger pullRequestNumber = new AtomicInteger(101);
     private AtomicInteger commentId = new AtomicInteger(500);
@@ -68,7 +69,6 @@ public class MockPullRequestService extends PullRequestService {
         commitComment.setBody(bodyText);
         commitComment.setBodyText(bodyText);
 
-
         List<CommitComment> comments;
         if (allComments.containsKey(pullRequestId)) {
             comments = allComments.get(pullRequestId);
@@ -90,17 +90,37 @@ public class MockPullRequestService extends PullRequestService {
         pullRequest.setTitle(title);
         pullRequest.setNumber(pullRequestNumber.get());
         pullRequest.setId(pullRequestNumber.get());
+        pullRequest.setState("open");
+        pullRequests.put(pullRequest.getId(), pullRequest);
+
         pullRequestNumber.incrementAndGet();
+        return pullRequest;
+    }
 
-        pullRequestsList.add(pullRequest);
+    @Override
+    public PullRequest getPullRequest(IRepositoryIdProvider repository, int id) throws IOException
{
+        PullRequest pullRequest = pullRequests.get((long) id);
         return pullRequest;
     }
 
     @Override
-    public synchronized List<PullRequest> getPullRequests(IRepositoryIdProvider repository,
String state) {
-        LOG.debug("Returning list of " + pullRequestsList.size() + " pull requests");
-        return pullRequestsList;
+    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<>();
 
+        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;
+    }
 }


Mime
View raw message