From 1f8af1bde394e2ab4df4191fcb251555817e470a Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Thu, 8 Mar 2018 13:29:54 +0100 Subject: [PATCH] NIFI-4948 - MongoDB Lookup Service throws an exception if there is no match Signed-off-by: Matthew Burgess This closes #2522 --- .../nifi/mongodb/MongoDBControllerService.java | 4 +++- .../apache/nifi/mongodb/MongoDBLookupService.java | 8 +++++--- .../nifi/mongodb/TestMongoDBLookupService.java | 14 ++++++-------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java index fe86429997..0faed0d525 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java @@ -22,6 +22,7 @@ import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.UpdateOptions; + import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.annotation.lifecycle.OnDisabled; @@ -69,9 +70,10 @@ public class MongoDBControllerService extends AbstractMongoDBControllerService i return this.col.count(query) > 0; } + @Override public Document findOne(Document query) { MongoCursor cursor = this.col.find(query).limit(1).iterator(); - Document retVal = cursor.next(); + Document retVal = cursor.tryNext(); cursor.close(); return retVal; diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java index a816ca0aed..fba228738a 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java @@ -32,6 +32,7 @@ import org.apache.nifi.serialization.record.Record; import org.apache.nifi.serialization.record.RecordField; import org.apache.nifi.serialization.record.RecordFieldType; import org.apache.nifi.serialization.record.RecordSchema; +import org.apache.nifi.util.StringUtils; import org.bson.Document; import java.io.IOException; @@ -43,7 +44,6 @@ import java.util.Map; import java.util.Optional; import java.util.Set; - @Tags({"mongo", "mongodb", "lookup", "record"}) @CapabilityDescription( "Provides a lookup service based around MongoDB. Each key that is specified \n" + @@ -86,7 +86,9 @@ public class MongoDBLookupService extends MongoDBControllerService implements Lo try { Document result = this.findOne(query); - if (lookupValueField != null && !lookupValueField.equals("")) { + if(result == null) { + return Optional.empty(); + } else if (!StringUtils.isEmpty(lookupValueField)) { return Optional.ofNullable(result.get(lookupValueField)); } else { final List fields = new ArrayList<>(); @@ -107,6 +109,7 @@ public class MongoDBLookupService extends MongoDBControllerService implements Lo } } + @Override @OnEnabled public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, InterruptedException { this.lookupValueField = context.getProperty(LOOKUP_VALUE_FIELD).getValue(); @@ -125,7 +128,6 @@ public class MongoDBLookupService extends MongoDBControllerService implements Lo @Override protected List getSupportedPropertyDescriptors() { - return lookupDescriptors; } } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java index 16beefe903..b2b1495a01 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java @@ -82,14 +82,13 @@ public class TestMongoDBLookupService { clean.putAll(criteria); service.delete(new Document(clean)); - boolean error = false; try { - service.lookup(criteria); + result = service.lookup(criteria); } catch (LookupFailureException ex) { - error = true; + Assert.fail(); } - Assert.assertTrue("An error should have been thrown.", error); + Assert.assertTrue(!result.isPresent()); } @Test @@ -113,14 +112,13 @@ public class TestMongoDBLookupService { clean.putAll(criteria); service.delete(new Document(clean)); - boolean error = false; try { - service.lookup(criteria); + result = service.lookup(criteria); } catch (LookupFailureException ex) { - error = true; + Assert.fail(); } - Assert.assertTrue("An error should have been thrown.", error); + Assert.assertTrue(!result.isPresent()); } @Test