pvillard31 commented on code in PR #10914:
URL: https://github.com/apache/nifi/pull/10914#discussion_r2821591995
##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertRecord.java:
##########
@@ -569,4 +570,57 @@ public void
testJSONWithDefinedFieldTypeBytesAndLogicalTypeDecimal() throws Exce
final MockFlowFile flowFile =
runner.getFlowFilesForRelationship(ConvertRecord.REL_SUCCESS).getFirst();
flowFile.assertContentEquals(expectedContent);
}
+
+ @Test
Review Comment:
Useful test for the end to end scenario but could we also add a unit test in
TestXMLRecordReader that directly tests XMLRecordReader with namespaced
attributes?
##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestConvertRecord.java:
##########
@@ -569,4 +570,57 @@ public void
testJSONWithDefinedFieldTypeBytesAndLogicalTypeDecimal() throws Exce
final MockFlowFile flowFile =
runner.getFlowFilesForRelationship(ConvertRecord.REL_SUCCESS).getFirst();
flowFile.assertContentEquals(expectedContent);
}
+
+ @Test
+ public void testXMLReaderWithNamespacedAttributesAndInferredSchema()
throws InitializationException {
+ final XMLReader xmlReader = new XMLReader();
+ runner.addControllerService(READER_ID, xmlReader);
+ runner.setProperty(xmlReader,
SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaInferenceUtil.INFER_SCHEMA);
+ runner.setProperty(xmlReader, XMLReader.RECORD_FORMAT,
XMLReader.RECORD_SINGLE.getValue());
+ runner.setProperty(xmlReader, XMLReader.PARSE_XML_ATTRIBUTES, "true");
+ runner.setProperty(xmlReader, XMLReader.CONTENT_FIELD_NAME,
"content_value");
+ runner.enableControllerService(xmlReader);
+
+ final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
+ runner.addControllerService(WRITER_ID, jsonWriter);
+ runner.setProperty(jsonWriter,
SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY,
SchemaAccessUtils.INHERIT_RECORD_SCHEMA);
+ runner.enableControllerService(jsonWriter);
+
+ runner.setProperty(ConvertRecord.RECORD_READER, READER_ID);
+ runner.setProperty(ConvertRecord.RECORD_WRITER, WRITER_ID);
+
+ final String xmlWithNamespacedAttributes = """
+ <?xml version='1.0' encoding='UTF-8'?>
+ <wd:data xmlns:wd="urn:com.wd.report/foo">
+ <wd:entry>
+ <wd:something wd:attr1="attr1 content">
+ <wd:record_with_attr2 wd:attr2="attr2 content
1">record_with_attr2 content 1</wd:record_with_attr2>
+ <wd:record_with_attr2 wd:attr2="attr2 content
2">record_with_attr2 content 2</wd:record_with_attr2>
+ </wd:something>
+ </wd:entry>
+ </wd:data>
+ """;
+ final String xmlWithPlainAttributes = """
+ <?xml version='1.0' encoding='UTF-8'?>
+ <wd:data xmlns:wd="urn:com.wd.report/foo">
+ <wd:entry>
+ <wd:something attr1="attr1 content">
+ <wd:record_with_attr2 attr2="attr2 content
1">record_with_attr2 content 1</wd:record_with_attr2>
+ <wd:record_with_attr2 attr2="attr2 content
2">record_with_attr2 content 2</wd:record_with_attr2>
+ </wd:something>
+ </wd:entry>
+ </wd:data>
+ """;
+
+ runner.enqueue(xmlWithNamespacedAttributes);
+ runner.enqueue(xmlWithPlainAttributes);
+ runner.run(2);
+
+ runner.assertAllFlowFilesTransferred(ConvertRecord.REL_SUCCESS, 2);
+ final MockFlowFile firstFlowFile =
runner.getFlowFilesForRelationship(ConvertRecord.REL_SUCCESS).getFirst();
+ final MockFlowFile secondFlowFile =
runner.getFlowFilesForRelationship(ConvertRecord.REL_SUCCESS).get(1);
+ assertFalse(firstFlowFile.getContent().contains("null"));
+ assertFalse(secondFlowFile.getContent().contains("null"));
+ assertEquals(firstFlowFile.getContent(), secondFlowFile.getContent());
Review Comment:
Can we be more extensive here? Something along the lines of
```java
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode firstJson = objectMapper.readTree(firstFlowFile.getContent());
assertTrue(firstJson.isArray());
assertEquals(1, firstJson.size());
final JsonNode record = firstJson.get(0);
final JsonNode something = record.get("something");
assertEquals("attr1 content", something.get("attr1").asText());
final JsonNode recordsWithAttr2 = something.get("record_with_attr2");
assertTrue(recordsWithAttr2.isArray());
assertEquals(2, recordsWithAttr2.size());
assertEquals("attr2 content 1",
recordsWithAttr2.get(0).get("attr2").asText());
assertEquals("record_with_attr2 content 1",
recordsWithAttr2.get(0).get("content_value").asText());
assertEquals("attr2 content 2",
recordsWithAttr2.get(1).get("attr2").asText());
assertEquals("record_with_attr2 content 2",
recordsWithAttr2.get(1).get("content_value").asText());
final JsonNode secondJson =
objectMapper.readTree(secondFlowFile.getContent());
assertEquals(firstJson, secondJson);
```
##########
nifi-extension-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/xml/XMLRecordReader.java:
##########
Review Comment:
Don't we have the same issue here?
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]