tpalfy commented on a change in pull request #3646: NIFI-6546 - Add JsonPath set value support URL: https://github.com/apache/nifi/pull/3646#discussion_r317581202
########## File path: nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java ########## @@ -384,12 +385,121 @@ public void testJsonPathDeleteHomePhoneNumber() throws IOException { verifyEquals("${json:jsonPath('$.firstName')}", attributes, "John"); verifyEquals("${json:jsonPath('$.lastName')}", attributes, "Smith"); verifyEquals("${json:jsonPath('$.age')}", attributes, "25"); + verifyEquals("${json:jsonPath('$.voter')}", attributes, "true"); verifyEquals("${json:jsonPath('$.address.postalCode')}", attributes, "10021-3100"); verifyEquals("${json:jsonPath(\"$.phoneNumbers[?(@.type=='home')].number\")}", attributes, "[]"); verifyEquals("${json:jsonPath('$.phoneNumbers')}", attributes, "{\"type\":\"office\",\"number\":\"646 555-4567\"}"); } + @Test + public void testJsonPathSetFirstNameAttribute() throws IOException { + final Map<String, String> attributes = new HashMap<>(); Review comment: In fact basically the whole test could be refactored out and that is true for almost all tests. In general what these tests do is 1. Check (for some reason) that an attribute is the original before the change 2. Change the value of an attribute 3. Check if the attribute changes 4. Check if the other attributes did not change This can be fairly simply encapsulated in a common method, like this: ```java private void testJsonPathExpression(String readExpression, Object expectedOriginalValue, String writeExpression, Object expectedChangedValue) throws IOException { final Map<String, String> attributes = new HashMap<>(); String addressBook = getResourceAsString("/json/address-book.json"); attributes.put("json", addressBook); verifyEquals(readExpression, attributes, expectedOriginalValue); String addressBookAfterDelete = Query.evaluateExpressions(writeExpression, attributes, ParameterLookup.EMPTY); attributes.clear(); attributes.put("json", addressBookAfterDelete); verifyCommonAddressBookAttributes(addressBook, attributes, readExpression); verifyEquals(readExpression, attributes, expectedChangedValue); } ``` All the tests could use this, even the delete ones. A couple of examples: ```java @Test public void testJsonPathDeleteMissingPath() throws IOException { testJsonPathExpression( "${json:jsonPath('$.missing-path')}", "", "${json:jsonPathDelete('$.missing-path')}", "" ); } @Test public void testJsonPathDeleteFirstNameAttribute() throws IOException { testJsonPathExpression( ADDRESS_BOOK_JSON_PATH_FIRST_NAME, "John", "${json:jsonPathDelete('$.firstName')}", "" ); } @Test public void testJsonPathDeleteHomePhoneNumber() throws IOException { testJsonPathExpression( ADDRESS_BOOK_JSON_PATH_PHONE_NUMBERS_TYPE_HOME_NUMBER, "212 555-1234", "${json:jsonPathDelete(\"$.phoneNumbers[?(@.type=='home')]\")}", "[]" ); } @Test public void testJsonPathSetVoterBooleanAttribute() throws IOException { testJsonPathExpression( ADDRESS_BOOK_JSON_PATH_VOTER, "true", "${json:jsonPathSet('$.voter', false)}", "false" ); } ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services