[GitHub] [nifi] HorizonNet commented on a change in pull request #4231: NIFI-7037 Split off username and password fields for GetMongo processor

2020-04-27 Thread GitBox


HorizonNet commented on a change in pull request #4231:
URL: https://github.com/apache/nifi/pull/4231#discussion_r415698289



##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/GetMongoIT.java
##
@@ -668,4 +671,52 @@ public void testSendEmpty() throws Exception {
 MockFlowFile flowFile = flowFiles.get(0);
 Assert.assertEquals(0, flowFile.getSize());
 }
+
+
+@Test
+public void testReadUserPaswd() throws Exception {
+final String username = "myuser";
+final String password = "password";
+mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));
+final MongoDatabase db = mongoClient.getDatabase(DB_NAME);
+final BasicDBObject createUserCommand = new 
BasicDBObject("createUser", username).append("pwd", password).append("roles",
+java.util.Collections.singletonList(new BasicDBObject("role", 
"dbOwner").append("db", DB_NAME)));
+
+BasicDBObject getUsersInfoCommand = new BasicDBObject("usersInfo", new 
BasicDBObject("user", username).append("db", DB_NAME));
+Document result = db.runCommand(getUsersInfoCommand);
+BasicDBObject dropUserCommand = new BasicDBObject("dropUser", 
username);
+
+ArrayList users = (ArrayList) result.get("users");
+if (!users.isEmpty()) {
+db.runCommand(dropUserCommand);
+}
+db.runCommand(createUserCommand);
+
+//setting new property
+runner.removeProperty(AbstractMongoProcessor.URI);
+runner.setVariable("uri", "mongodb://localhost:27017/?authSource=" + 
DB_NAME);
+runner.setProperty(AbstractMongoProcessor.URI, "${uri}");
+runner.setProperty(GetMongo.PASSWORD, password);
+runner.setProperty(GetMongo.USER_NAME, username);
+
+runner.setVariable("query", "{\"_id\": \"doc_2\"}");
+runner.setProperty(GetMongo.QUERY, "${query}");
+runner.setProperty(GetMongo.JSON_TYPE, GetMongo.JSON_STANDARD);
+runner.run();
+runner.assertAllFlowFilesTransferred(GetMongo.REL_SUCCESS, 1);
+
+List flowFiles = 
runner.getFlowFilesForRelationship(GetMongo.REL_SUCCESS);
+byte[] raw = runner.getContentAsByteArray(flowFiles.get(0));
+ObjectMapper mapper = new ObjectMapper();
+Map parsed = mapper.readValue(raw, Map.class);
+SimpleDateFormat format = new SimpleDateFormat("-MM-dd");
+
+// Drop the user which was created
+db.runCommand(dropUserCommand);
+
+Assert.assertTrue(parsed.get("date_field").getClass() == String.class);
+Assert.assertTrue(((String) 
parsed.get("date_field")).startsWith(format.format(CAL.getTime(;
+

Review comment:
   Remove empty line.

##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/GetMongo.java
##
@@ -253,7 +278,13 @@ public void onTrigger(final ProcessContext context, final 
ProcessSession session
 });
 
 outgoingFlowFile = 
session.putAllAttributes(outgoingFlowFile, attributes);
-session.getProvenanceReporter().receive(outgoingFlowFile, 
getURI(context));
+String uriPass="";

Review comment:
   Please add spaces around the equals sing.

##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/GetMongoIT.java
##
@@ -668,4 +671,52 @@ public void testSendEmpty() throws Exception {
 MockFlowFile flowFile = flowFiles.get(0);
 Assert.assertEquals(0, flowFile.getSize());
 }
+
+
+@Test
+public void testReadUserPaswd() throws Exception {
+final String username = "myuser";
+final String password = "password";
+mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));
+final MongoDatabase db = mongoClient.getDatabase(DB_NAME);
+final BasicDBObject createUserCommand = new 
BasicDBObject("createUser", username).append("pwd", password).append("roles",
+java.util.Collections.singletonList(new BasicDBObject("role", 
"dbOwner").append("db", DB_NAME)));
+
+BasicDBObject getUsersInfoCommand = new BasicDBObject("usersInfo", new 
BasicDBObject("user", username).append("db", DB_NAME));
+Document result = db.runCommand(getUsersInfoCommand);
+BasicDBObject dropUserCommand = new BasicDBObject("dropUser", 
username);
+
+ArrayList users = (ArrayList) result.get("users");
+if (!users.isEmpty()) {
+db.runCommand(dropUserCommand);
+}
+db.runCommand(createUserCommand);
+
+//setting new property
+runner.removeProperty(AbstractMongoProcessor.URI);
+runner.setVariable("uri", "mongodb://localhost:27017/?authSource=" + 
DB_NAME);
+

[GitHub] [nifi] HorizonNet commented on a change in pull request #4231: NIFI-7037 Split off username and password fields for GetMongo processor

2020-04-24 Thread GitBox


HorizonNet commented on a change in pull request #4231:
URL: https://github.com/apache/nifi/pull/4231#discussion_r414690223



##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/GetMongoIT.java
##
@@ -61,9 +64,9 @@
 static {
 CAL = Calendar.getInstance();
 DOCUMENTS = Lists.newArrayList(
-new Document("_id", "doc_1").append("a", 1).append("b", 
2).append("c", 3),
-new Document("_id", "doc_2").append("a", 1).append("b", 
2).append("c", 4).append("date_field", CAL.getTime()),
-new Document("_id", "doc_3").append("a", 1).append("b", 3)
+new Document("_id", "doc_1").append("a", 1).append("b", 
2).append("c", 3),

Review comment:
   Was this done by IDE auto-indentation or because the earlier version did 
not match the general indentation? There are also similar places in the other 
classes.

##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/GetMongoIT.java
##
@@ -668,4 +671,53 @@ public void testSendEmpty() throws Exception {
 MockFlowFile flowFile = flowFiles.get(0);
 Assert.assertEquals(0, flowFile.getSize());
 }
+
+
+@Test
+public void testReadUserPaswd() throws Exception {
+final String username = "myuser";
+final String password = "password";
+mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));
+final MongoDatabase db = mongoClient.getDatabase(DB_NAME);
+final BasicDBObject createUserCommand = new 
BasicDBObject("createUser", username).append("pwd", password).append("roles",
+java.util.Collections.singletonList(new BasicDBObject("role", 
"dbOwner").append("db", DB_NAME)));
+
+BasicDBObject getUsersInfoCommand = new BasicDBObject("usersInfo", new 
BasicDBObject("user", username).append("db", DB_NAME));
+Document result = db.runCommand(getUsersInfoCommand);
+BasicDBObject dropUserCommand = new BasicDBObject("dropUser", 
username);
+
+ArrayList users = (ArrayList) result.get("users");
+if (!users.isEmpty()) {
+db.runCommand(dropUserCommand);
+System.out.println("dropping user");

Review comment:
   Better would be to use a logger.

##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/GetMongo.java
##
@@ -253,7 +278,14 @@ public void onTrigger(final ProcessContext context, final 
ProcessSession session
 });
 
 outgoingFlowFile = 
session.putAllAttributes(outgoingFlowFile, attributes);
-session.getProvenanceReporter().receive(outgoingFlowFile, 
getURI(context));
+String uriPass="";
+if (context.getProperty(USER_NAME).getValue() != null) {
+uriPass = "mongodb://" + 
context.getProperty(USER_NAME).getValue() + ":" + 
context.getProperty(PASSWORD).getValue() + "@" + getURI(context).substring(10);
+

Review comment:
   Remove this empty line.

##
File path: 
nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/GetMongoIT.java
##
@@ -668,4 +671,53 @@ public void testSendEmpty() throws Exception {
 MockFlowFile flowFile = flowFiles.get(0);
 Assert.assertEquals(0, flowFile.getSize());
 }
+
+
+@Test
+public void testReadUserPaswd() throws Exception {
+final String username = "myuser";
+final String password = "password";
+mongoClient = new MongoClient(new MongoClientURI(MONGO_URI));
+final MongoDatabase db = mongoClient.getDatabase(DB_NAME);
+final BasicDBObject createUserCommand = new 
BasicDBObject("createUser", username).append("pwd", password).append("roles",
+java.util.Collections.singletonList(new BasicDBObject("role", 
"dbOwner").append("db", DB_NAME)));
+
+BasicDBObject getUsersInfoCommand = new BasicDBObject("usersInfo", new 
BasicDBObject("user", username).append("db", DB_NAME));
+Document result = db.runCommand(getUsersInfoCommand);
+BasicDBObject dropUserCommand = new BasicDBObject("dropUser", 
username);
+
+ArrayList users = (ArrayList) result.get("users");
+if (!users.isEmpty()) {
+db.runCommand(dropUserCommand);
+System.out.println("dropping user");
+}
+db.runCommand(createUserCommand);
+
+//setting new property
+runner.removeProperty(AbstractMongoProcessor.URI);
+runner.setVariable("uri", 
"mongodb://localhost:27017/?authSource="+DB_NAME);
+runner.setProperty(AbstractMongoProcessor.URI, "${uri}");
+runner.setProperty(GetMongo.PASSWORD,password);

Review comment:
   To stay consistent, add a space