exceptionfactory commented on code in PR #10752:
URL: https://github.com/apache/nifi/pull/10752#discussion_r2747671905
##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGenerateRecord.java:
##########
@@ -287,4 +297,188 @@ public void
testGenerateNullableFieldsOneHundredNullPercentageSchemaText() throw
// null values should cause all fields to be empty in the output (2
top-level record fields in this case
flowFile.assertContentEquals(",\n");
}
+
+ @Test
+ public void testValidationFailsWithNoSchemaConfiguration() throws
InitializationException {
+ final MockRecordWriter recordWriter = new MockRecordWriter(null, true);
+ testRunner.addControllerService("record-writer", recordWriter);
+ testRunner.enableControllerService(recordWriter);
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "record-writer");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS, "1");
+
+ testRunner.assertNotValid();
+ }
+
+ @Test
+ public void testValidationFailsWithMultipleSchemaConfigurations() throws
Exception {
+ String schemaText = new
String(Files.readAllBytes(Paths.get("src/test/resources/TestGenerateRecord/nested_nullable.avsc")));
+
+ final MockRecordWriter recordWriter = new MockRecordWriter(null, true);
+ testRunner.addControllerService("record-writer", recordWriter);
+ testRunner.enableControllerService(recordWriter);
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "record-writer");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS, "1");
+
+ // Set both Schema Text and Predefined Schema - should be invalid
+ testRunner.setProperty(GenerateRecord.SCHEMA_TEXT, schemaText);
+ testRunner.setProperty(GenerateRecord.PREDEFINED_SCHEMA,
PredefinedRecordSchema.PERSON.name());
+
+ testRunner.assertNotValid();
+ }
+
+ @Test
+ public void testValidationFailsWithPredefinedSchemaAndDynamicProperties()
throws Exception {
+ final MockRecordWriter recordWriter = new MockRecordWriter(null, true);
+ testRunner.addControllerService("record-writer", recordWriter);
+ testRunner.enableControllerService(recordWriter);
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "record-writer");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS, "1");
+
+ // Set both Predefined Schema and dynamic property - should be invalid
+ testRunner.setProperty(GenerateRecord.PREDEFINED_SCHEMA,
PredefinedRecordSchema.PERSON.name());
+ testRunner.setProperty("myField", "Address.fullAddress");
+
+ testRunner.assertNotValid();
+ }
+
+ @Test
+ public void testPredefinedSchemaPerson() throws Exception {
+ // Field names aligned with schema.org/Person and
schema.org/PostalAddress
+ testPredefinedSchema(PredefinedRecordSchema.PERSON, 5,
+ "identifier", "givenName", "familyName", "email", "telephone",
"birthDate", "age", "active", "address");
Review Comment:
Maintaining this list of expected field names seems cumbersome. I think it
is sufficient to avoid expecting any particular field names and do a simpler
check.
##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGenerateRecord.java:
##########
@@ -21,8 +21,13 @@
import org.apache.nifi.avro.AvroRecordSetWriter;
import org.apache.nifi.avro.AvroTypeUtil;
import org.apache.nifi.components.AllowableValue;
+import org.apache.nifi.json.JsonRecordSetWriter;
Review Comment:
I recommend avoid use of specific format types for testing, since
`GenerateRecord` is agnostic to the particular format.
##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestGenerateRecord.java:
##########
@@ -287,4 +297,188 @@ public void
testGenerateNullableFieldsOneHundredNullPercentageSchemaText() throw
// null values should cause all fields to be empty in the output (2
top-level record fields in this case
flowFile.assertContentEquals(",\n");
}
+
+ @Test
+ public void testValidationFailsWithNoSchemaConfiguration() throws
InitializationException {
+ final MockRecordWriter recordWriter = new MockRecordWriter(null, true);
+ testRunner.addControllerService("record-writer", recordWriter);
+ testRunner.enableControllerService(recordWriter);
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "record-writer");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS, "1");
+
+ testRunner.assertNotValid();
+ }
+
+ @Test
+ public void testValidationFailsWithMultipleSchemaConfigurations() throws
Exception {
+ String schemaText = new
String(Files.readAllBytes(Paths.get("src/test/resources/TestGenerateRecord/nested_nullable.avsc")));
+
+ final MockRecordWriter recordWriter = new MockRecordWriter(null, true);
+ testRunner.addControllerService("record-writer", recordWriter);
+ testRunner.enableControllerService(recordWriter);
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "record-writer");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS, "1");
+
+ // Set both Schema Text and Predefined Schema - should be invalid
+ testRunner.setProperty(GenerateRecord.SCHEMA_TEXT, schemaText);
+ testRunner.setProperty(GenerateRecord.PREDEFINED_SCHEMA,
PredefinedRecordSchema.PERSON.name());
+
+ testRunner.assertNotValid();
+ }
+
+ @Test
+ public void testValidationFailsWithPredefinedSchemaAndDynamicProperties()
throws Exception {
+ final MockRecordWriter recordWriter = new MockRecordWriter(null, true);
+ testRunner.addControllerService("record-writer", recordWriter);
+ testRunner.enableControllerService(recordWriter);
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "record-writer");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS, "1");
+
+ // Set both Predefined Schema and dynamic property - should be invalid
+ testRunner.setProperty(GenerateRecord.PREDEFINED_SCHEMA,
PredefinedRecordSchema.PERSON.name());
+ testRunner.setProperty("myField", "Address.fullAddress");
+
+ testRunner.assertNotValid();
+ }
+
+ @Test
+ public void testPredefinedSchemaPerson() throws Exception {
+ // Field names aligned with schema.org/Person and
schema.org/PostalAddress
+ testPredefinedSchema(PredefinedRecordSchema.PERSON, 5,
+ "identifier", "givenName", "familyName", "email", "telephone",
"birthDate", "age", "active", "address");
+ }
+
+ @Test
+ public void testPredefinedSchemaOrder() throws Exception {
+ // Field names aligned with schema.org/Order
+ testPredefinedSchema(PredefinedRecordSchema.ORDER, 5,
+ "orderNumber", "customer", "customerName", "customerEmail",
"orderDate", "orderTime",
+ "orderDelivery", "totalPrice", "priceCurrency", "orderStatus",
"isGift", "itemCount", "orderedItem");
+ }
+
+ @Test
+ public void testPredefinedSchemaEvent() throws Exception {
+ // Field names aligned with schema.org/Event
+ testPredefinedSchema(PredefinedRecordSchema.EVENT, 5,
+ "identifier", "additionalType", "startDate", "startTime",
"endDate", "organizer",
+ "eventStatus", "description", "isAccessibleForFree",
"attendeeCount", "duration", "keywords", "additionalProperty");
+ }
+
+ @Test
+ public void testPredefinedSchemaSensor() throws Exception {
+ // Field names aligned with schema.org conventions (GeoCoordinates for
location)
+ testPredefinedSchema(PredefinedRecordSchema.SENSOR, 5,
+ "identifier", "additionalType", "manufacturer", "dateCreated",
"temperature",
+ "humidity", "pressure", "batteryLevel", "signalStrength",
"isActive", "geo");
+ }
+
+ @Test
+ public void testPredefinedSchemaProduct() throws Exception {
+ // Field names aligned with schema.org/Product
+ testPredefinedSchema(PredefinedRecordSchema.PRODUCT, 5,
+ "identifier", "sku", "name", "description", "category",
"brand", "price",
+ "priceCurrency", "availability", "inventoryLevel",
"ratingValue", "reviewCount", "dateCreated",
+ "dateModified", "keywords", "additionalProperty");
+ }
+
+ @Test
+ public void testPredefinedSchemaStockTrade() throws Exception {
+ // Field names aligned with schema.org conventions
+ testPredefinedSchema(PredefinedRecordSchema.STOCK_TRADE, 5,
+ "identifier", "tickerSymbol", "name", "exchange",
"actionType", "dateCreated",
+ "price", "orderQuantity", "totalPrice", "priceCurrency",
"bidPrice", "askPrice",
+ "highPrice", "lowPrice", "marketCap", "isSettled");
+ }
+
+ @Test
+ public void testPredefinedSchemaCompleteExample() throws Exception {
+ // Field names aligned with schema.org conventions
+ testPredefinedSchema(PredefinedRecordSchema.COMPLETE_EXAMPLE, 3,
+ "identifier", "isActive", "score", "count", "ratingValue",
"price", "balance", "initial",
+ "flags", "position", "dateCreated", "lastLogin",
"dateModified", "keywords",
+ "scores", "additionalProperty", "person", "orderedItem");
+ }
+
+ private void testPredefinedSchema(PredefinedRecordSchema predefinedSchema,
int numRecords, String... expectedFields) throws Exception {
+ final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
+ testRunner.addControllerService("json-writer", jsonWriter);
+ testRunner.setProperty(jsonWriter,
SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY,
SchemaAccessUtils.INHERIT_RECORD_SCHEMA);
+ testRunner.enableControllerService(jsonWriter);
+
+ testRunner.setProperty(GenerateRecord.RECORD_WRITER, "json-writer");
+ testRunner.setProperty(GenerateRecord.PREDEFINED_SCHEMA,
predefinedSchema.name());
+ testRunner.setProperty(GenerateRecord.NULLABLE_FIELDS, "true");
+ testRunner.setProperty(GenerateRecord.NULL_PERCENTAGE, "0");
+ testRunner.setProperty(GenerateRecord.NUM_RECORDS,
String.valueOf(numRecords));
+
+ testRunner.assertValid();
+ testRunner.run();
+
+ testRunner.assertTransferCount(GenerateRecord.REL_SUCCESS, 1);
+ MockFlowFile flowFile =
testRunner.getFlowFilesForRelationship(GenerateRecord.REL_SUCCESS).get(0);
+
+ // Verify record count attribute
+ flowFile.assertAttributeEquals("record.count",
String.valueOf(numRecords));
+ flowFile.assertAttributeEquals("mime.type", "application/json");
+
+ // Verify content is valid JSON and contains expected fields
+ final String content = flowFile.getContent();
+ assertNotNull(content);
+ assertTrue(content.startsWith("["), "Content should be a JSON array");
Review Comment:
This check for JSON array seems unnecessary since it is specific to the
expected test behavior, and is effectively confirmed using the `JsonTreeReader`.
--
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]