camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/3] camel git commit: Created mongodb idempotent repository. https://issues.apache.org/jira/browse/CAMEL-10006
Date Thu, 02 Jun 2016 06:38:40 GMT
Created mongodb idempotent repository.
https://issues.apache.org/jira/browse/CAMEL-10006


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

Branch: refs/heads/master
Commit: eb36d34bcda07824fe284805904f02a936cb51d1
Parents: f897449
Author: Joseluis Pedrosa <joseluis.pedrosa@elephanttalk.com>
Authored: Wed Jun 1 15:41:49 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Jun 2 08:15:42 2016 +0200

----------------------------------------------------------------------
 .../idempotent/MongoDbIdempotentRepository.java | 127 ++++++++++++++++++
 .../component/mongodb/AbstractMongoDbTest.java  |   9 +-
 .../MongoDbIdempotentRepositoryTest.java        | 129 +++++++++++++++++++
 3 files changed, 259 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/eb36d34b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
new file mode 100644
index 0000000..56fc378
--- /dev/null
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepository.java
@@ -0,0 +1,127 @@
+/**
+ * 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.mongodb.processor.idempotent;
+
+
+import com.mongodb.ErrorCategory;
+import com.mongodb.MongoClient;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.result.DeleteResult;
+import org.apache.camel.api.management.ManagedOperation;
+import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.spi.IdempotentRepository;
+import org.apache.camel.support.ServiceSupport;
+import org.bson.BsonDocument;
+import org.bson.Document;
+
+
+@ManagedResource(description = "Mongo db based message id repository")
+public class MongoDbIdempotentRepository<E> extends ServiceSupport implements IdempotentRepository<E>
{
+
+    private MongoClient cli;
+    private String collectionName;
+    private String dbName;
+
+    private MongoCollection<Document> collection;
+
+
+    public MongoDbIdempotentRepository(MongoClient cli, String collectionName, String dbName)
{
+        this.cli = cli;
+        this.collectionName = collectionName;
+        this.dbName = dbName;
+        collection = cli.getDatabase(dbName).getCollection(collectionName);
+    }
+
+    @ManagedOperation(description = "Adds the key to the store")
+    @Override
+    public boolean add(E key) {
+
+        Document document = new Document("_id", key);
+        try {
+            collection.insertOne(document);
+        } catch (com.mongodb.MongoWriteException ex) {
+            if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
+                return false;
+            }
+
+            throw ex;
+        }
+        return true;
+    }
+
+    @ManagedOperation(description = "Does the store contain the given key")
+    @Override
+    public boolean contains(E key) {
+        Document document = new Document("_id", key);
+        long count =  collection.count(document);
+        return count > 0;
+    }
+
+    @ManagedOperation(description = "Remove the key from the store")
+    @Override
+    public boolean remove(E key) {
+        Document document = new Document("_id", key);
+        DeleteResult res = collection.deleteOne(document);
+        return  res.getDeletedCount() > 0;
+    }
+
+    @Override
+    public boolean confirm(E key) {
+        return true;
+    }
+
+    @ManagedOperation(description = "Clear the store")
+    @Override
+    public void clear() {
+        collection.deleteMany(new BsonDocument());
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+
+    }
+
+    public MongoClient getCli() {
+        return cli;
+    }
+
+    public void setCli(MongoClient cli) {
+        this.cli = cli;
+    }
+
+    public String getCollectionName() {
+        return collectionName;
+    }
+
+    public void setCollectionName(String collectionName) {
+        this.collectionName = collectionName;
+    }
+
+    public String getDbName() {
+        return dbName;
+    }
+
+    public void setDbName(String dbName) {
+        this.dbName = dbName;
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/eb36d34b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
index bf9e892..661fced 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java
@@ -18,13 +18,8 @@ package org.apache.camel.component.mongodb;
 
 import java.util.Formatter;
 
-import com.mongodb.DB;
-import com.mongodb.DBCollection;
-import com.mongodb.DBObject;
-import com.mongodb.Mongo;
-import com.mongodb.WriteConcern;
+import com.mongodb.*;
 import com.mongodb.util.JSON;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.component.properties.PropertiesComponent;
@@ -35,6 +30,8 @@ import org.apache.camel.util.ObjectHelper;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
+
+
 public abstract class AbstractMongoDbTest extends CamelTestSupport {
 
     protected static Mongo mongo;

http://git-wip-us.apache.org/repos/asf/camel/blob/eb36d34b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepositoryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepositoryTest.java
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepositoryTest.java
new file mode 100644
index 0000000..84156a1
--- /dev/null
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/processor/idempotent/MongoDbIdempotentRepositoryTest.java
@@ -0,0 +1,129 @@
+/**
+ * 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.mongodb.processor.idempotent;
+
+import java.util.UUID;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.MongoClient;
+import org.apache.camel.component.mongodb.AbstractMongoDbTest;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+
+
+
+public class MongoDbIdempotentRepositoryTest extends AbstractMongoDbTest {
+
+
+    MongoDbIdempotentRepository<String> repo;
+
+    @Before
+    @After
+    public void clearDB() {
+        testCollection.remove(new BasicDBObject());
+    }
+
+    @Override
+    public void doPostSetup() {
+        super.doPostSetup();
+        repo = new MongoDbIdempotentRepository<>((MongoClient) mongo, testCollectionName,
dbName);
+    }
+
+    @Test
+    public void add() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+
+        boolean added = repo.add(randomUUIDString);
+        assertEquals("Driver inserted document" , 1, testCollection.count());
+        assertTrue("Add ui returned true", added);
+    }
+
+    @Test
+    public void addAndContains() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+
+        repo.add(randomUUIDString);
+        assertEquals(1, testCollection.count());
+
+        boolean found =  repo.contains(randomUUIDString);
+        assertTrue("Added uid was found", found);
+    }
+
+    @Test
+    public void addAndRemove() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+
+        repo.add(randomUUIDString);
+        assertEquals(1, testCollection.count());
+
+        boolean removed =  repo.remove(randomUUIDString);
+        assertTrue("Added uid was removed correctly", removed);
+        assertEquals(0, testCollection.count());
+    }
+
+    @Test
+    public void addDuplicatedFails() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+
+        repo.add(randomUUIDString);
+        assertEquals(1, testCollection.count());
+
+        boolean added =  repo.add(randomUUIDString);
+        assertTrue("Duplicated entry was not added", !added);
+        assertEquals(1, testCollection.count());
+    }
+
+    @Test
+    public void deleteMissingiIsFailse() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+        assertEquals(0, testCollection.count());
+        boolean removed =  repo.remove(randomUUIDString);
+        assertTrue("Non exisint uid returns false", !removed);
+    }
+
+
+    @Test
+    public void containsMissingReturnsFalse() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+        boolean found =  repo.contains(randomUUIDString);
+        assertTrue("Non existing item is not found", !found);
+    }
+
+
+    @Test
+    public void confirmAllwaysReturnsTrue() throws Exception {
+
+        String randomUUIDString = UUID.randomUUID().toString();
+        boolean found =  repo.confirm(randomUUIDString);
+        assertTrue("Confirm always returns true", found);
+
+
+        found =  repo.confirm(null);
+        assertTrue("Confirm always returns true, even with null", found);
+
+    }
+
+}
+


Mime
View raw message