NIFI-4948 - MongoDB Lookup Service throws an exception if there is no match

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #2522
This commit is contained in:
Pierre Villard 2018-03-08 13:29:54 +01:00 committed by Matthew Burgess
parent dae2b73d94
commit 1f8af1bde3
3 changed files with 14 additions and 12 deletions

View File

@ -22,6 +22,7 @@ import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.model.UpdateOptions;
import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.annotation.lifecycle.OnDisabled; import org.apache.nifi.annotation.lifecycle.OnDisabled;
@ -69,9 +70,10 @@ public class MongoDBControllerService extends AbstractMongoDBControllerService i
return this.col.count(query) > 0; return this.col.count(query) > 0;
} }
@Override
public Document findOne(Document query) { public Document findOne(Document query) {
MongoCursor<Document> cursor = this.col.find(query).limit(1).iterator(); MongoCursor<Document> cursor = this.col.find(query).limit(1).iterator();
Document retVal = cursor.next(); Document retVal = cursor.tryNext();
cursor.close(); cursor.close();
return retVal; return retVal;

View File

@ -32,6 +32,7 @@ import org.apache.nifi.serialization.record.Record;
import org.apache.nifi.serialization.record.RecordField; import org.apache.nifi.serialization.record.RecordField;
import org.apache.nifi.serialization.record.RecordFieldType; import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.serialization.record.RecordSchema; import org.apache.nifi.serialization.record.RecordSchema;
import org.apache.nifi.util.StringUtils;
import org.bson.Document; import org.bson.Document;
import java.io.IOException; import java.io.IOException;
@ -43,7 +44,6 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@Tags({"mongo", "mongodb", "lookup", "record"}) @Tags({"mongo", "mongodb", "lookup", "record"})
@CapabilityDescription( @CapabilityDescription(
"Provides a lookup service based around MongoDB. Each key that is specified \n" + "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 { try {
Document result = this.findOne(query); 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)); return Optional.ofNullable(result.get(lookupValueField));
} else { } else {
final List<RecordField> fields = new ArrayList<>(); final List<RecordField> fields = new ArrayList<>();
@ -107,6 +109,7 @@ public class MongoDBLookupService extends MongoDBControllerService implements Lo
} }
} }
@Override
@OnEnabled @OnEnabled
public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, InterruptedException { public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, InterruptedException {
this.lookupValueField = context.getProperty(LOOKUP_VALUE_FIELD).getValue(); this.lookupValueField = context.getProperty(LOOKUP_VALUE_FIELD).getValue();
@ -125,7 +128,6 @@ public class MongoDBLookupService extends MongoDBControllerService implements Lo
@Override @Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return lookupDescriptors; return lookupDescriptors;
} }
} }

View File

@ -82,14 +82,13 @@ public class TestMongoDBLookupService {
clean.putAll(criteria); clean.putAll(criteria);
service.delete(new Document(clean)); service.delete(new Document(clean));
boolean error = false;
try { try {
service.lookup(criteria); result = service.lookup(criteria);
} catch (LookupFailureException ex) { } catch (LookupFailureException ex) {
error = true; Assert.fail();
} }
Assert.assertTrue("An error should have been thrown.", error); Assert.assertTrue(!result.isPresent());
} }
@Test @Test
@ -113,14 +112,13 @@ public class TestMongoDBLookupService {
clean.putAll(criteria); clean.putAll(criteria);
service.delete(new Document(clean)); service.delete(new Document(clean));
boolean error = false;
try { try {
service.lookup(criteria); result = service.lookup(criteria);
} catch (LookupFailureException ex) { } catch (LookupFailureException ex) {
error = true; Assert.fail();
} }
Assert.assertTrue("An error should have been thrown.", error); Assert.assertTrue(!result.isPresent());
} }
@Test @Test