Search in sources :

Example 26 with MockRecordParser

use of org.apache.nifi.serialization.record.MockRecordParser in project nifi by apache.

the class TestLookupRecord method testAddFieldsToNonRecordField.

/**
 * If the output fields are added to a non-record field, then the result should be that the field
 * becomes a UNION that does allow the Record and the value is set to a Record.
 */
@Test
public void testAddFieldsToNonRecordField() throws InitializationException {
    final RecordLookup lookupService = new RecordLookup();
    runner.addControllerService("lookup", lookupService);
    runner.enableControllerService(lookupService);
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("favorite", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("least", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    final Record sports = new MapRecord(schema, new HashMap<String, Object>());
    sports.setValue("favorite", "basketball");
    sports.setValue("least", "soccer");
    lookupService.addValue("John Doe", sports);
    recordReader = new MockRecordParser();
    recordReader.addSchemaField("name", RecordFieldType.STRING);
    recordReader.addSchemaField("age", RecordFieldType.INT);
    recordReader.addSchemaField("sport", RecordFieldType.STRING);
    recordReader.addRecord("John Doe", 48, null);
    runner.addControllerService("reader", recordReader);
    runner.enableControllerService(recordReader);
    runner.setProperty("lookup", "/name");
    runner.setProperty(LookupRecord.RESULT_RECORD_PATH, "/sport");
    runner.setProperty(LookupRecord.RESULT_CONTENTS, LookupRecord.RESULT_RECORD_FIELDS);
    runner.enqueue("");
    runner.run();
    final MockFlowFile out = runner.getFlowFilesForRelationship(LookupRecord.REL_MATCHED).get(0);
    // We can't be sure of the order of the fields in the record, so we allow either 'least' or 'favorite' to be first
    final String outputContents = new String(out.toByteArray());
    assertTrue(outputContents.equals("John Doe,48,MapRecord[{favorite=basketball, least=soccer}]\n") || outputContents.equals("John Doe,48,MapRecord[{least=soccer, favorite=basketball}]\n"));
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) MockFlowFile(org.apache.nifi.util.MockFlowFile) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Example 27 with MockRecordParser

use of org.apache.nifi.serialization.record.MockRecordParser in project nifi by apache.

the class TestQueryRecord method testColumnNames.

@Test
public void testColumnNames() throws InitializationException, IOException {
    final MockRecordParser parser = new MockRecordParser();
    parser.addSchemaField("name", RecordFieldType.STRING);
    parser.addSchemaField("points", RecordFieldType.INT);
    parser.addSchemaField("greeting", RecordFieldType.STRING);
    parser.addRecord("Tom", 1, "Hello");
    parser.addRecord("Jerry", 2, "Hi");
    parser.addRecord("Tom", 99, "Howdy");
    final List<String> colNames = new ArrayList<>();
    colNames.add("name");
    colNames.add("points");
    colNames.add("greeting");
    colNames.add("FAV_GREETING");
    final ResultSetValidatingRecordWriter writer = new ResultSetValidatingRecordWriter(colNames);
    final TestRunner runner = TestRunners.newTestRunner(QueryRecord.class);
    runner.addControllerService("parser", parser);
    runner.enableControllerService(parser);
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    runner.setProperty(REL_NAME, "select *, greeting AS FAV_GREETING from FLOWFILE");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");
    runner.enqueue("");
    runner.run();
    runner.assertTransferCount(REL_NAME, 1);
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ArrayList(java.util.ArrayList) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Example 28 with MockRecordParser

use of org.apache.nifi.serialization.record.MockRecordParser in project nifi by apache.

the class TestConvertRecord method testSuccessfulConversion.

@Test
public void testSuccessfulConversion() throws InitializationException {
    final MockRecordParser readerService = new MockRecordParser();
    final MockRecordWriter writerService = new MockRecordWriter("header", false);
    final TestRunner runner = TestRunners.newTestRunner(ConvertRecord.class);
    runner.addControllerService("reader", readerService);
    runner.enableControllerService(readerService);
    runner.addControllerService("writer", writerService);
    runner.enableControllerService(writerService);
    runner.setProperty(ConvertRecord.RECORD_READER, "reader");
    runner.setProperty(ConvertRecord.RECORD_WRITER, "writer");
    readerService.addSchemaField("name", RecordFieldType.STRING);
    readerService.addSchemaField("age", RecordFieldType.INT);
    readerService.addRecord("John Doe", 48);
    readerService.addRecord("Jane Doe", 47);
    readerService.addRecord("Jimmy Doe", 14);
    runner.enqueue("");
    runner.run();
    runner.assertAllFlowFilesTransferred(ConvertRecord.REL_SUCCESS, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertRecord.REL_SUCCESS).get(0);
    out.assertAttributeEquals("record.count", "3");
    out.assertAttributeEquals("mime.type", "text/plain");
    out.assertContentEquals("header\nJohn Doe,48\nJane Doe,47\nJimmy Doe,14\n");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Test(org.junit.Test)

Aggregations

MockRecordParser (org.apache.nifi.serialization.record.MockRecordParser)28 Test (org.junit.Test)18 MockRecordWriter (org.apache.nifi.serialization.record.MockRecordWriter)16 MockFlowFile (org.apache.nifi.util.MockFlowFile)16 TestRunner (org.apache.nifi.util.TestRunner)13 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 RecordField (org.apache.nifi.serialization.record.RecordField)4 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)4 InitializationException (org.apache.nifi.reporting.InitializationException)3 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)3 MapRecord (org.apache.nifi.serialization.record.MapRecord)3 Record (org.apache.nifi.serialization.record.Record)3 IOException (java.io.IOException)2 List (java.util.List)1 MockDruidTranquilityController (org.apache.nifi.controller.druid.MockDruidTranquilityController)1 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)1 TestRunners (org.apache.nifi.util.TestRunners)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1