camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [08/23] camel git commit: CAMEL-7982: camel-git - A generic git component, add commit consumer example
Date Sat, 18 Jul 2015 09:09:25 GMT
CAMEL-7982: camel-git - A generic git component, add commit consumer example


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

Branch: refs/heads/master
Commit: 8205bbef8defa20e1be90dd8ce5c9bab1b88b1b2
Parents: f3731ed
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sat Jul 18 11:06:45 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sat Jul 18 11:08:15 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/component/git/GitConsumer.java |  65 ++++++++++
 .../apache/camel/component/git/GitEndpoint.java |   5 +-
 .../github/consumer/GitConsumerTest.java        | 126 +++++++++++++++++++
 .../github/producer/GitTestSupport.java         |   2 +
 4 files changed, 195 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8205bbef/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
new file mode 100644
index 0000000..ac3a5f4
--- /dev/null
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConsumer.java
@@ -0,0 +1,65 @@
+package org.apache.camel.component.git;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.impl.ScheduledPollConsumer;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+public class GitConsumer extends ScheduledPollConsumer {
+	
+	private GitEndpoint endpoint;
+	
+	private Repository repository;
+	
+	private Git git;
+	
+	private List used;
+
+	public GitConsumer(GitEndpoint endpoint, Processor processor) {
+		super(endpoint, processor);
+		this.endpoint = endpoint;
+		this.repository = getLocalRepository();
+		this.git = new Git(repository);
+		this.used = new ArrayList();
+	}
+
+	@Override
+	protected int poll() throws Exception {
+		int count = 0;
+		Iterable<RevCommit> commits = git.log().all().call();
+        for (RevCommit commit : commits) {
+        	if (!used.contains(commit.getId())) {
+            Exchange e = getEndpoint().createExchange();
+            e.getOut().setBody(commit.getShortMessage());
+            getProcessor().process(e);
+            used.add(commit.getId());
+            count++;
+        	}
+        }
+        return count;
+	}
+	
+    private Repository getLocalRepository(){
+        FileRepositoryBuilder builder = new FileRepositoryBuilder();
+        Repository repo = null;
+		try {
+			repo = builder.setGitDir(new File(endpoint.getLocalPath(), ".git"))
+			        .readEnvironment() // scan environment GIT_* variables
+			        .findGitDir() // scan up the file system tree
+			        .build();
+		} catch (IOException e) {
+			//LOG.error("There was an error, cannot open " + endpoint.getLocalPath() + " repository");
+			e.printStackTrace();
+		}
+		return repo;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8205bbef/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
index cbaed51..de09f95 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
@@ -25,7 +25,7 @@ import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
 
-@UriEndpoint(scheme = "git", title = "Git", syntax = "git:localpath", label = "api,file")
+@UriEndpoint(scheme = "git", title = "Git", syntax = "git://localpath", label = "api,file")
 public class GitEndpoint extends DefaultEndpoint {
 
     @UriPath @Metadata(required = "true")
@@ -52,8 +52,7 @@ public class GitEndpoint extends DefaultEndpoint {
 
 	@Override
 	public Consumer createConsumer(Processor processor) throws Exception {
-		// TODO Auto-generated method stub
-		return null;
+        return new GitConsumer(this, processor);
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/camel/blob/8205bbef/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
b/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
new file mode 100644
index 0000000..6071d23
--- /dev/null
+++ b/components/camel-git/src/test/java/org/apache/camel/component/github/consumer/GitConsumerTest.java
@@ -0,0 +1,126 @@
+/**
+ * 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.File;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.git.GitConstants;
+import org.apache.camel.component.github.producer.GitTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.Status;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.junit.Test;
+
+public class GitConsumerTest extends GitTestSupport {
+    
+    @Test
+    public void commitTest() throws Exception {
+
+    	Repository repository = getTestRepository();
+        MockEndpoint added = getMockEndpoint("mock:result");
+        
+        File fileToAdd = new File(GIT_LOCAL_REPO, FILENAME_TO_ADD);
+        fileToAdd.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, FILENAME_TO_ADD);
+            }
+        });
+        File gitDir = new File(GIT_LOCAL_REPO, ".git");
+        assertEquals(gitDir.exists(), true);
+        
+        Status status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(FILENAME_TO_ADD));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, COMMIT_MESSAGE);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+            count++;
+        }
+        assertEquals(count, 1);
+        
+        File fileToAdd1 = new File(GIT_LOCAL_REPO, FILENAME_BRANCH_TO_ADD);
+        fileToAdd1.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, FILENAME_BRANCH_TO_ADD);
+            }
+        });
+        
+        status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(FILENAME_BRANCH_TO_ADD));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, "Test test Commit");
+            }
+        });
+        logs  = new Git(repository).log()
+                .call();
+        count = 0;
+        for (RevCommit rev : logs) {
+            count++;
+        }
+        assertEquals(count, 2);
+        
+        Thread.sleep(1 * 5000);
+        Exchange ex1 = added.getExchanges().get(0);
+        Exchange ex2 = added.getExchanges().get(1);
+        assertEquals(COMMIT_MESSAGE, ex2.getOut().getBody());
+        assertEquals("Test test Commit", ex1.getOut().getBody());
+        repository.close();
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {            
+            @Override
+            public void configure() throws Exception {
+                from("direct:clone")
+                        .to("git://" + GIT_LOCAL_REPO + "?remotePath=https://github.com/oscerd/json-webserver-example.git&operation=clone");
+                from("direct:init")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=init");
+                from("direct:add")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=add");
+                from("direct:commit")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=commit");
+                from("git://" + GIT_LOCAL_REPO)
+                        .to("mock:result");
+            } 
+        };
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/8205bbef/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
index 0aa3946..7c15386 100644
--- a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
+++ b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitTestSupport.java
@@ -19,6 +19,8 @@ package org.apache.camel.component.github.producer;
 import java.io.File;
 import java.io.IOException;
 
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;


Mime
View raw message