Search in sources :

Example 6 with MockRecordParser

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

the class TestConvertRecord method testWriteFailure.

@Test
public void testWriteFailure() throws InitializationException {
    final MockRecordParser readerService = new MockRecordParser();
    final MockRecordWriter writerService = new MockRecordWriter("header", false, 2);
    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);
    final MockFlowFile original = runner.enqueue("hello");
    runner.run();
    // Original FlowFile should be routed to 'failure' relationship without modification
    runner.assertAllFlowFilesTransferred(ConvertRecord.REL_FAILURE, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertRecord.REL_FAILURE).get(0);
    assertTrue(original == out);
}
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)

Example 7 with MockRecordParser

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

the class TestLookupRecord method testAddFieldsToExistingRecord.

@Test
public void testAddFieldsToExistingRecord() throws InitializationException, IOException {
    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("favorite", RecordFieldType.STRING);
    recordReader.addSchemaField("least", RecordFieldType.STRING);
    recordReader.addRecord("John Doe", 48, null, "baseball");
    runner.addControllerService("reader", recordReader);
    runner.enableControllerService(recordReader);
    runner.setProperty("lookup", "/name");
    runner.setProperty(LookupRecord.RESULT_RECORD_PATH, "/");
    runner.setProperty(LookupRecord.RESULT_CONTENTS, LookupRecord.RESULT_RECORD_FIELDS);
    runner.enqueue("");
    runner.run();
    final MockFlowFile out = runner.getFlowFilesForRelationship(LookupRecord.REL_MATCHED).get(0);
    out.assertContentEquals("John Doe,48,basketball,soccer\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 8 with MockRecordParser

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

the class TestLookupRecord method testAddFieldsToNonExistentRecord.

/**
 * If the output fields are added to a record that doesn't exist, the result should be that a Record is
 * created and the results added to it.
 */
@Test
public void testAddFieldsToNonExistentRecord() 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.RECORD);
    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 9 with MockRecordParser

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

the class TestPartitionRecord method setup.

@Before
public void setup() throws InitializationException {
    readerService = new MockRecordParser();
    writerService = new MockRecordWriter(null, false);
    runner = TestRunners.newTestRunner(PartitionRecord.class);
    runner.addControllerService("reader", readerService);
    runner.enableControllerService(readerService);
    runner.addControllerService("writer", writerService);
    runner.enableControllerService(writerService);
    runner.setProperty(PartitionRecord.RECORD_READER, "reader");
    runner.setProperty(PartitionRecord.RECORD_WRITER, "writer");
    readerService.addSchemaField("name", RecordFieldType.STRING);
    readerService.addSchemaField("age", RecordFieldType.INT);
    readerService.addSchemaField("sports", RecordFieldType.ARRAY);
}
Also used : MockRecordWriter(org.apache.nifi.serialization.record.MockRecordWriter) MockRecordParser(org.apache.nifi.serialization.record.MockRecordParser) Before(org.junit.Before)

Example 10 with MockRecordParser

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

the class TestQueryRecord method testAggregateFunction.

@Test
public void testAggregateFunction() throws InitializationException, IOException {
    final MockRecordParser parser = new MockRecordParser();
    parser.addSchemaField("name", RecordFieldType.STRING);
    parser.addSchemaField("points", RecordFieldType.INT);
    parser.addRecord("Tom", 1);
    parser.addRecord("Jerry", 2);
    parser.addRecord("Tom", 99);
    final MockRecordWriter writer = new MockRecordWriter("\"name\",\"points\"");
    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 name, sum(points) as points from FLOWFILE GROUP BY name");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");
    runner.enqueue("");
    runner.run();
    runner.assertTransferCount(REL_NAME, 1);
    final MockFlowFile flowFileOut = runner.getFlowFilesForRelationship(ExecuteProcess.REL_SUCCESS).get(0);
    flowFileOut.assertContentEquals("\"name\",\"points\"\n\"Tom\",\"100\"\n\"Jerry\",\"2\"\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