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.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<Document> cursor = this.col.find(query).limit(1).iterator();
Document retVal = cursor.next();
Document retVal = cursor.tryNext();
cursor.close();
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.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<RecordField> 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<PropertyDescriptor> getSupportedPropertyDescriptors() {
return lookupDescriptors;
}
}

View File

@ -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