You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by MikeThomsen <gi...@git.apache.org> on 2018/01/10 13:36:43 UTC

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

GitHub user MikeThomsen opened a pull request:

    https://github.com/apache/nifi/pull/2392

    NIFI-4759 Fixed a bug that left a hard-coded reference to _id in as t…

    …he update key for MongoDB upserts.
    
    Thank you for submitting a contribution to Apache NiFi.
    
    In order to streamline the review of the contribution we ask you
    to ensure the following steps have been taken:
    
    ### For all changes:
    - [ ] Is there a JIRA ticket associated with this PR? Is it referenced 
         in the commit message?
    
    - [ ] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
    
    - [ ] Has your PR been rebased against the latest commit within the target branch (typically master)?
    
    - [ ] Is your initial contribution a single, squashed commit?
    
    ### For code changes:
    - [ ] Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
    - [ ] Have you written or updated unit tests to verify your changes?
    - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
    - [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
    - [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
    - [ ] If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?
    
    ### For documentation related changes:
    - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
    
    ### Note:
    Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/MikeThomsen/nifi NIFI-4759

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/nifi/pull/2392.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #2392
    
----
commit 931ded7d39c63fdaf94d781323e8585b440c71c2
Author: Mike Thomsen <mi...@...>
Date:   2018-01-10T13:35:09Z

    NIFI-4759 Fixed a bug that left a hard-coded reference to _id in as the update key for MongoDB upserts.

----


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160973717
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java ---
    @@ -173,13 +166,25 @@ public void process(final InputStream in) throws IOException {
                     // update
                     final boolean upsert = context.getProperty(UPSERT).asBoolean();
                     final String updateKey = context.getProperty(UPDATE_QUERY_KEY).getValue();
    -                final Document query = new Document(updateKey, ((Map)doc).get(updateKey));
    +                final Document query;
    +
    +                Object keyVal = ((Map)doc).get(updateKey);
    +                if (updateKey.equals("_id")) {
    +                    try {
    +                        keyVal = new ObjectId((String) keyVal);
    +                    } catch (Exception ex) {
    +                        getLogger().error("{} is not a valid ObjectID, using raw value.", new Object[]{keyVal});
    --- End diff --
    
    what about `keyVal + "is not a valid ObjectID, using raw value."`


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160972295
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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" +
    --- End diff --
    
    I think having the 3 JSONs one for each row would improve readability


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160975264
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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.
    +     *
    +     */
    +
    --- End diff --
    
    nit: useless blank line


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161213509
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -166,6 +167,7 @@ public void testUpdateDoesNotInsert() throws Exception {
             byte[] bytes = documentToByteArray(doc);
     
             runner.setProperty(PutMongo.MODE, "update");
    +        runner.setProperty(PutMongo.UPSERT, "false");
    --- End diff --
    
    can we remove it then, please?


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161213199
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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;
    +
    +        for (String upsert : upserts) {
    +            runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS);
    +            runner.setProperty(PutMongo.MODE, "update");
    +            runner.setProperty(PutMongo.UPSERT, "true");
    +            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 --
    
    I prefer to leave it in to explicitly mark the ending of the regression testing.


---

[GitHub] nifi issue #2392: NIFI-4759 Fixed a bug that left a hard-coded reference to ...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2392
  
    @mgaido91 
    @mattyb149 
    
    Ok, added a UT that really kicks the tires on both problems. I updated the ticket with a new title and an explanation of both issues. Most of the code here is focused on handling ObjectIDs correctly.
    
    https://issues.apache.org/jira/browse/NIFI-4759


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160975782
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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;
    +
    +        for (String upsert : upserts) {
    +            runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS);
    +            runner.setProperty(PutMongo.MODE, "update");
    +            runner.setProperty(PutMongo.UPSERT, "true");
    +            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 --
    
    this comment can be removed, it seems useless to me


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160971847
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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;
    +
    +        for (String upsert : upserts) {
    +            runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS);
    --- End diff --
    
    this and the following two can be set outside the for loop


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160982784
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -166,6 +167,7 @@ public void testUpdateDoesNotInsert() throws Exception {
             byte[] bytes = documentToByteArray(doc);
     
             runner.setProperty(PutMongo.MODE, "update");
    +        runner.setProperty(PutMongo.UPSERT, "false");
    --- End diff --
    
    why do we need to add this?


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160974686
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java ---
    @@ -173,13 +166,25 @@ public void process(final InputStream in) throws IOException {
                     // update
                     final boolean upsert = context.getProperty(UPSERT).asBoolean();
                     final String updateKey = context.getProperty(UPDATE_QUERY_KEY).getValue();
    -                final Document query = new Document(updateKey, ((Map)doc).get(updateKey));
    +                final Document query;
    +
    +                Object keyVal = ((Map)doc).get(updateKey);
    +                if (updateKey.equals("_id")) {
    --- End diff --
    
    add ` && ObjectId.isValid(keyVal)`


---

[GitHub] nifi issue #2392: NIFI-4759 Fixed a bug that left a hard-coded reference to ...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2392
  
    Thanks @mgaido91. Unfortunately, the user got back to me and told me they noticed another bug that is related to this so I have to expand on it. I'll incorporate your feedback into that.


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen closed the pull request at:

    https://github.com/apache/nifi/pull/2392


---

[GitHub] nifi issue #2392: NIFI-4759 Fixed a bug that left a hard-coded reference to ...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2392
  
    @mgaido91 
    
    I screwed up the pull request because of a mistake with a rebase. New PR is here:
    
    https://github.com/apache/nifi/pull/2401


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160975171
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java ---
    @@ -173,13 +166,25 @@ public void process(final InputStream in) throws IOException {
                     // update
                     final boolean upsert = context.getProperty(UPSERT).asBoolean();
                     final String updateKey = context.getProperty(UPDATE_QUERY_KEY).getValue();
    -                final Document query = new Document(updateKey, ((Map)doc).get(updateKey));
    +                final Document query;
    --- End diff --
    
    this can be moved later where it is created


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160685547
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java ---
    @@ -155,12 +155,7 @@ public void onTrigger(final ProcessContext context, final ProcessSession session
             try {
                 // Read the contents of the FlowFile into a byte array
                 final byte[] content = new byte[(int) flowFile.getSize()];
    -            session.read(flowFile, new InputStreamCallback() {
    -                @Override
    -                public void process(final InputStream in) throws IOException {
    -                    StreamUtils.fillBuffer(in, content, true);
    -                }
    -            });
    +            session.read(flowFile, in -> StreamUtils.fillBuffer(in, content, true));
    --- End diff --
    
    this is causing the error on Ci because of the unused import which introduces. Since this is not related with the PR, can we get it back to its original state?


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161212208
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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;
    +
    +        for (String upsert : upserts) {
    +            runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS);
    +            runner.setProperty(PutMongo.MODE, "update");
    +            runner.setProperty(PutMongo.UPSERT, "true");
    +            runner.setProperty(PutMongo.UPDATE_QUERY_KEY, updateKeyProps[index]);
    +            for (int x = 0; x < 5; x++) {
    --- End diff --
    
    Just a validation that if we run things multiple times the same result happens.


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160973857
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java ---
    @@ -173,13 +166,25 @@ public void process(final InputStream in) throws IOException {
                     // update
                     final boolean upsert = context.getProperty(UPSERT).asBoolean();
                     final String updateKey = context.getProperty(UPDATE_QUERY_KEY).getValue();
    -                final Document query = new Document(updateKey, ((Map)doc).get(updateKey));
    +                final Document query;
    +
    +                Object keyVal = ((Map)doc).get(updateKey);
    +                if (updateKey.equals("_id")) {
    +                    try {
    +                        keyVal = new ObjectId((String) keyVal);
    +                    } catch (Exception ex) {
    +                        getLogger().error("{} is not a valid ObjectID, using raw value.", new Object[]{keyVal});
    +                    }
    +                }
    +
    +                query = new Document(updateKey, keyVal);
     
                     if (updateMode.equals(UPDATE_WITH_DOC.getValue())) {
    +
    --- End diff --
    
    nit: useless blank line


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r160975388
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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;
    +
    +        for (String upsert : upserts) {
    +            runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS);
    +            runner.setProperty(PutMongo.MODE, "update");
    +            runner.setProperty(PutMongo.UPSERT, "true");
    +            runner.setProperty(PutMongo.UPDATE_QUERY_KEY, updateKeyProps[index]);
    +            for (int x = 0; x < 5; x++) {
    --- End diff --
    
    why doing this 5 times?


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161213967
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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" +
    --- End diff --
    
    I just meant that is hard to read and understand the content of this JSON. I think that something like:
    ```
    String[] upserts = new String[]{
        "{...}",
        "{...}",
        "{...}"};
    ```
    would improve readability.


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161213119
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -166,6 +167,7 @@ public void testUpdateDoesNotInsert() throws Exception {
             byte[] bytes = documentToByteArray(doc);
     
             runner.setProperty(PutMongo.MODE, "update");
    +        runner.setProperty(PutMongo.UPSERT, "false");
    --- End diff --
    
    We don't.


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161212684
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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" +
    --- End diff --
    
    Not following you here.


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by mgaido91 <gi...@git.apache.org>.
Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161213577
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/test/java/org/apache/nifi/processors/mongodb/PutMongoTest.java ---
    @@ -256,4 +258,72 @@ 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;
    +
    +        for (String upsert : upserts) {
    +            runner.setProperty(PutMongo.UPDATE_MODE, PutMongo.UPDATE_WITH_OPERATORS);
    +            runner.setProperty(PutMongo.MODE, "update");
    +            runner.setProperty(PutMongo.UPSERT, "true");
    +            runner.setProperty(PutMongo.UPDATE_QUERY_KEY, updateKeyProps[index]);
    +            for (int x = 0; x < 5; x++) {
    --- End diff --
    
    then 2 is enough, isn't it?


---

[GitHub] nifi pull request #2392: NIFI-4759 Fixed a bug that left a hard-coded refere...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2392#discussion_r161212008
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java ---
    @@ -173,13 +166,25 @@ public void process(final InputStream in) throws IOException {
                     // update
                     final boolean upsert = context.getProperty(UPSERT).asBoolean();
                     final String updateKey = context.getProperty(UPDATE_QUERY_KEY).getValue();
    -                final Document query = new Document(updateKey, ((Map)doc).get(updateKey));
    +                final Document query;
    +
    +                Object keyVal = ((Map)doc).get(updateKey);
    +                if (updateKey.equals("_id")) {
    +                    try {
    +                        keyVal = new ObjectId((String) keyVal);
    +                    } catch (Exception ex) {
    +                        getLogger().error("{} is not a valid ObjectID, using raw value.", new Object[]{keyVal});
    --- End diff --
    
    That's the general pattern that is used in other places for building log statements.


---