[ https://issues.apache.org/jira/browse/NIFI-4759?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16324174#comment-16324174 ]
ASF GitHub Bot commented on NIFI-4759: -------------------------------------- Github user mgaido91 commented on a diff in the pull request: https://github.com/apache/nifi/pull/2401#discussion_r161262183 --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java --- @@ -256,4 +257,74 @@ public void testUpsertWithOperators() throws Exception { Assert.assertEquals("Msg had wrong value", msg, "Hi"); } } + + /* + * Start NIFI-4759 Regression Tests + * + * 2 issues with ID field: + * + * * Assumed _id is the update key, causing failures when the user configured a different one in the UI. + * * Treated _id as a string even when it is an ObjectID sent from another processor as a string value. + * + * Expected behavior: + * + * * update key field should work no matter what (legal) value it is set to be. + * * _ids that are ObjectID should become real ObjectIDs when added to Mongo. + * * _ids that are arbitrary strings should be still go in as strings. + * + */ + @Test + public void testNiFi_4759_Regressions() { + String[] upserts = new String[]{ + "{\n" + + "\t\"_id\": \"12345\",\n" + + "\t\"$set\": {\n" + + "\t\t\"msg\": \"Hello, world\"\n" + + "\t}\n" + + "}", + + "{\n" + + "\t\"_id\": \"5a5617b9c1f5de6d8276e87d\",\n" + + "\t\"$set\": {\n" + + "\t\t\"msg\": \"Hello, world\"\n" + + "\t}\n" + + "}", + + "{\n" + + "\t\"updateKey\": \"12345\",\n" + + "\t\"$set\": {\n" + + "\t\t\"msg\": \"Hello, world\"\n" + + "\t}\n" + + "}", + }; + + String[] updateKeyProps = new String[] { "_id", "_id", "updateKey" }; + Object[] updateKeys = new Object[] { "12345", new ObjectId("5a5617b9c1f5de6d8276e87d"), "12345" }; + int index = 0; + + runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS); + runner.setProperty(PutMongo.MODE, "update"); + runner.setProperty(PutMongo.UPSERT, "true"); + + for (String upsert : upserts) { + runner.setProperty(PutMongo.UPDATE_QUERY_KEY, updateKeyProps[index]); + for (int x = 0; x < 5; x++) { + runner.enqueue(upsert); + } + runner.run(5, true, true); + runner.assertTransferCount(PutMongo.REL_FAILURE, 0); + runner.assertTransferCount(PutMongo.REL_SUCCESS, 5); + + Document query = new Document(updateKeyProps[index], updateKeys[index]); + Document result = collection.find(query).first(); + Assert.assertNotNull("Result was null", result); + Assert.assertEquals("Count was wrong", 1, collection.count(query)); + runner.clearTransferState(); + index++; + } + } + + /* --- End diff -- Nit: I'd remove this and the empty line above... > PutMongo does not handle updateKey field correctly > -------------------------------------------------- > > Key: NIFI-4759 > URL: https://issues.apache.org/jira/browse/NIFI-4759 > Project: Apache NiFi > Issue Type: Bug > Reporter: Mike Thomsen > Assignee: Mike Thomsen > > Two issues: > * The updateKey field is ignored in favor of _id in the update code block of > PutMongo. > * _id fields are always treated as strings, even if they're valid ObjectIds > represented as a string. PutMongo should be able to handle these as ObjectIds. > Regarding the first point, this works: > {code:java} > { > "_id": "1234", > "$set": { "msg": "Hello, world" } > } > {code} > This does not: > {code:java} > { > "uniqueKey": "12345", > "$set": { "msg": "Hello, World" } > } > {code} -- This message was sent by Atlassian JIRA (v6.4.14#64029)