Polished. This closes #1008
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a24158b4 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a24158b4 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a24158b4 Branch: refs/heads/master Commit: a24158b4579f98cf8156ab1c1565b633b39d74b2 Parents: bead4e3 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Jun 2 08:38:25 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Jun 2 08:38:25 2016 +0200 ---------------------------------------------------------------------- .../idempotent/MongoDbIdempotentRepository.java | 32 +++++++++++--------- .../MongoDbIdempotentRepositoryTest.java | 18 ++--------- 2 files changed, 20 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a24158b4/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 index 56fc378..328a605 100644 --- 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 @@ -16,7 +16,6 @@ */ package org.apache.camel.component.mongodb.processor.idempotent; - import com.mongodb.ErrorCategory; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; @@ -25,31 +24,31 @@ 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.apache.camel.util.ObjectHelper; 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 MongoClient mongoClient; private String collectionName; private String dbName; - private MongoCollection<Document> collection; + public MongoDbIdempotentRepository() { + } - public MongoDbIdempotentRepository(MongoClient cli, String collectionName, String dbName) { - this.cli = cli; + public MongoDbIdempotentRepository(MongoClient mongoClient, String collectionName, String dbName) { + this.mongoClient = mongoClient; this.collectionName = collectionName; this.dbName = dbName; - collection = cli.getDatabase(dbName).getCollection(collectionName); + this.collection = mongoClient.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); @@ -57,7 +56,6 @@ public class MongoDbIdempotentRepository<E> extends ServiceSupport implements Id if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) { return false; } - throw ex; } return true; @@ -92,20 +90,26 @@ public class MongoDbIdempotentRepository<E> extends ServiceSupport implements Id @Override protected void doStart() throws Exception { + ObjectHelper.notNull(mongoClient, "cli"); + ObjectHelper.notNull(dbName, "dbName"); + ObjectHelper.notNull(collectionName, "collectionName"); + if (collection == null) { + this.collection = mongoClient.getDatabase(dbName).getCollection(collectionName); + } } @Override protected void doStop() throws Exception { - + // noop } - public MongoClient getCli() { - return cli; + public MongoClient getMongoClient() { + return mongoClient; } - public void setCli(MongoClient cli) { - this.cli = cli; + public void setMongoClient(MongoClient mongoClient) { + this.mongoClient = mongoClient; } public String getCollectionName() { http://git-wip-us.apache.org/repos/asf/camel/blob/a24158b4/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 index 84156a1..f10b5a4 100644 --- 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 @@ -21,16 +21,13 @@ import java.util.UUID; import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import org.apache.camel.component.mongodb.AbstractMongoDbTest; +import org.apache.camel.util.ServiceHelper; import org.junit.After; import org.junit.Before; import org.junit.Test; - - - public class MongoDbIdempotentRepositoryTest extends AbstractMongoDbTest { - MongoDbIdempotentRepository<String> repo; @Before @@ -47,17 +44,15 @@ public class MongoDbIdempotentRepositoryTest extends AbstractMongoDbTest { @Test public void add() throws Exception { - String randomUUIDString = UUID.randomUUID().toString(); boolean added = repo.add(randomUUIDString); - assertEquals("Driver inserted document" , 1, testCollection.count()); + 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); @@ -69,7 +64,6 @@ public class MongoDbIdempotentRepositoryTest extends AbstractMongoDbTest { @Test public void addAndRemove() throws Exception { - String randomUUIDString = UUID.randomUUID().toString(); repo.add(randomUUIDString); @@ -82,7 +76,6 @@ public class MongoDbIdempotentRepositoryTest extends AbstractMongoDbTest { @Test public void addDuplicatedFails() throws Exception { - String randomUUIDString = UUID.randomUUID().toString(); repo.add(randomUUIDString); @@ -95,34 +88,27 @@ public class MongoDbIdempotentRepositoryTest extends AbstractMongoDbTest { @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); - } }