Search in sources :

Example 1 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project kylo by Teradata.

the class GetTableDataTest method testAvro.

/**
 * Verify Avro output.
 */
@Test
public void testAvro() throws IOException {
    // Trigger processor
    runner.setProperty(GetTableData.OUTPUT_TYPE, GetTableDataSupport.OutputType.AVRO.toString());
    runner.enqueue(new byte[0]);
    runner.run();
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(CommonProperties.REL_SUCCESS);
    Assert.assertEquals(0, runner.getFlowFilesForRelationship(CommonProperties.REL_FAILURE).size());
    Assert.assertEquals(0, runner.getFlowFilesForRelationship(GetTableData.REL_NO_DATA).size());
    Assert.assertEquals(1, flowFiles.size());
    Assert.assertEquals("2", flowFiles.get(0).getAttribute(GetTableData.RESULT_ROW_COUNT));
    Assert.assertEquals("2", flowFiles.get(0).getAttribute(ComponentAttributes.NUM_SOURCE_RECORDS.key()));
    // Build Avro record reader
    final SeekableInput avroInput = new SeekableByteArrayInput(flowFiles.get(0).toByteArray());
    final Schema schema = SchemaBuilder.record("NiFi_ExecuteSQL_Record").namespace("any.data").fields().name("id").type().nullable().intType().noDefault().name("first_name").type().nullable().stringType().noDefault().name("last_name").type().nullable().stringType().noDefault().name("email").type().nullable().stringType().noDefault().name("last_updated").type().nullable().stringType().noDefault().endRecord();
    final DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(schema);
    final DataFileReader<GenericRecord> dataReader = new DataFileReader<>(avroInput, datumReader);
    // Verify Avro records
    List<GenericRecord> records = StreamSupport.stream(dataReader.spliterator(), false).collect(Collectors.toList());
    Assert.assertEquals(2, records.size());
    Assert.assertEquals(1, records.get(0).get(0));
    Assert.assertEquals(new Utf8("Mike"), records.get(0).get(1));
    Assert.assertEquals(new Utf8("Hillyer"), records.get(0).get(2));
    Assert.assertEquals(new Utf8("Mike.Hillyer@sakilastaff.com"), records.get(0).get(3));
    Assert.assertEquals(new Utf8("2006-02-15T03:57:16.000Z"), records.get(0).get(4));
    Assert.assertEquals(2, records.get(1).get(0));
    Assert.assertEquals(new Utf8("Jon"), records.get(1).get(1));
    Assert.assertEquals(new Utf8("Stephens"), records.get(1).get(2));
    Assert.assertEquals(new Utf8("Jon.Stephens@sakilastaff.com"), records.get(1).get(3));
    Assert.assertEquals(new Utf8("2006-02-15T03:57:16.000Z"), records.get(1).get(4));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) DataFileReader(org.apache.avro.file.DataFileReader) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) Utf8(org.apache.avro.util.Utf8) SeekableInput(org.apache.avro.file.SeekableInput) GenericRecord(org.apache.avro.generic.GenericRecord) SeekableByteArrayInput(org.apache.avro.file.SeekableByteArrayInput) Test(org.junit.Test)

Example 2 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project kylo by Teradata.

the class StripHeaderTest method testBypass.

@Test
public void testBypass() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new StripHeader());
    runner.setProperty(StripHeader.HEADER_LINE_COUNT, "1");
    runner.setProperty(StripHeader.ENABLED, "false");
    runner.enqueue(file);
    runner.run();
    runner.assertTransferCount(StripHeader.REL_CONTENT, 1);
    runner.assertTransferCount(StripHeader.REL_HEADER, 0);
    runner.assertTransferCount(StripHeader.REL_ORIGINAL, 1);
    List<MockFlowFile> originalFlows = runner.getFlowFilesForRelationship(StripHeader.REL_ORIGINAL);
    String originalContent = new String(originalFlows.get(0).toByteArray());
    assertTrue(originalContent.startsWith("name") && originalContent.endsWith("94550\n"));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Test(org.junit.Test)

Example 3 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project kylo by Teradata.

the class GetFeedsHistoryReindexTest method testTwoFeedsToReindex.

@Test
public void testTwoFeedsToReindex() throws Exception {
    final String METADATA_SERVICE_IDENTIFIER = "MockMetadataProviderService_TwoFeedsToReindex";
    final TestRunner runner = TestRunners.newTestRunner(GetFeedsHistoryReindex.class);
    final MetadataProviderService metadataService = new MockMetadataProviderService_TwoFeedsToReindex();
    runner.addControllerService(METADATA_SERVICE_IDENTIFIER, metadataService);
    runner.enableControllerService(metadataService);
    runner.setProperty(GetFeedsHistoryReindex.METADATA_SERVICE, METADATA_SERVICE_IDENTIFIER);
    runner.run(1);
    runner.assertQueueEmpty();
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_FOUND, 2);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_NOT_FOUND, 0);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_FAILURE, 0);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_ORIGINAL, 1);
    List<MockFlowFile> results = runner.getFlowFilesForRelationship(GetFeedsHistoryReindex.REL_FOUND);
    for (int i = 0; i < results.size(); i++) {
        MockFlowFile resultFlowFile = results.get(i);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEED_ID_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEED_SYSTEM_NAME_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEED_CATEGORY_SYSTEM_NAME_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEED_STATUS_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEED_LAST_MODIFIED_UTC_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEEDS_TOTAL_COUNT_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEEDS_TOTAL_IDS_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeExists(GetFeedsHistoryReindex.FEEDS_CHECK_TIME_UTC_FOR_HISTORY_REINDEX_KEY);
        resultFlowFile.assertAttributeEquals(GetFeedsHistoryReindex.FEED_ID_FOR_HISTORY_REINDEX_KEY, "feed-" + i + "-id");
        resultFlowFile.assertAttributeEquals(GetFeedsHistoryReindex.FEED_SYSTEM_NAME_FOR_HISTORY_REINDEX_KEY, "feed-" + i + "-systemName");
        resultFlowFile.assertAttributeEquals(GetFeedsHistoryReindex.FEED_CATEGORY_SYSTEM_NAME_FOR_HISTORY_REINDEX_KEY, "cat-" + i + "-systemName");
        resultFlowFile.assertAttributeEquals(GetFeedsHistoryReindex.FEED_STATUS_FOR_HISTORY_REINDEX_KEY, "DIRTY");
        if (i == 0) {
            String feedLastModifiedUtcForHistoryReindexActualValue = resultFlowFile.getAttribute(GetFeedsHistoryReindex.FEED_LAST_MODIFIED_UTC_FOR_HISTORY_REINDEX_KEY);
            String feedLastModifiedUtcForHistoryReindexExpectedValuePrefixWithoutTimeZone = "2017-12-18T14:53:24.013";
            assertTrue(feedLastModifiedUtcForHistoryReindexActualValue.contains(feedLastModifiedUtcForHistoryReindexExpectedValuePrefixWithoutTimeZone));
        } else if (i == 1) {
            String feedLastModifiedUtcForHistoryReindexActualValue = resultFlowFile.getAttribute(GetFeedsHistoryReindex.FEED_LAST_MODIFIED_UTC_FOR_HISTORY_REINDEX_KEY);
            String feedLastModifiedUtcForHistoryReindexExpectedValuePrefixWithoutTimeZone = "2017-12-17T09:35:45.335";
            assertTrue(feedLastModifiedUtcForHistoryReindexActualValue.contains(feedLastModifiedUtcForHistoryReindexExpectedValuePrefixWithoutTimeZone));
        }
        resultFlowFile.assertAttributeEquals(GetFeedsHistoryReindex.FEEDS_TOTAL_COUNT_FOR_HISTORY_REINDEX_KEY, String.valueOf(2));
        resultFlowFile.assertAttributeEquals(GetFeedsHistoryReindex.FEEDS_TOTAL_IDS_FOR_HISTORY_REINDEX_KEY, "[feed-0-id, feed-1-id]");
        resultFlowFile.assertAttributeNotEquals(GetFeedsHistoryReindex.FEEDS_CHECK_TIME_UTC_FOR_HISTORY_REINDEX_KEY, null);
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MetadataProviderService(com.thinkbiganalytics.nifi.core.api.metadata.MetadataProviderService) Test(org.junit.Test)

Example 4 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project kylo by Teradata.

the class GetFeedsHistoryReindexTest method testGeneralExceptionGettingFeedsToReindex.

@Test
public void testGeneralExceptionGettingFeedsToReindex() throws Exception {
    final String METADATA_SERVICE_IDENTIFIER = "MockMetadataProviderService_GeneralExceptionGettingFeedsToReindex";
    final TestRunner runner = TestRunners.newTestRunner(GetFeedsHistoryReindex.class);
    final MetadataProviderService metadataService = new MockMetadataProviderService_GeneralExceptionGettingFeedsToReindex();
    runner.addControllerService(METADATA_SERVICE_IDENTIFIER, metadataService);
    runner.enableControllerService(metadataService);
    runner.setProperty(GetFeedsHistoryReindex.METADATA_SERVICE, METADATA_SERVICE_IDENTIFIER);
    runner.run(1);
    runner.assertQueueEmpty();
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_FOUND, 0);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_NOT_FOUND, 0);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_FAILURE, 1);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_ORIGINAL, 0);
    List<MockFlowFile> results = runner.getFlowFilesForRelationship(GetFeedsHistoryReindex.REL_FAILURE);
    MockFlowFile resultFlowFile = results.get(0);
    resultFlowFile.assertAttributeNotExists(GetFeedsHistoryReindex.FEEDS_TOTAL_COUNT_FOR_HISTORY_REINDEX_KEY);
    resultFlowFile.assertAttributeNotExists(GetFeedsHistoryReindex.FEEDS_TOTAL_IDS_FOR_HISTORY_REINDEX_KEY);
    resultFlowFile.assertAttributeNotExists(GetFeedsHistoryReindex.FEEDS_CHECK_TIME_UTC_FOR_HISTORY_REINDEX_KEY);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MetadataProviderService(com.thinkbiganalytics.nifi.core.api.metadata.MetadataProviderService) Test(org.junit.Test)

Example 5 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project kylo by Teradata.

the class GetFeedsHistoryReindexTest method testErrorGettingFeedsToReindex.

@Test
public void testErrorGettingFeedsToReindex() throws Exception {
    final String METADATA_SERVICE_IDENTIFIER = "MockMetadataProviderService_ErrorGettingFeedsToReindex";
    final TestRunner runner = TestRunners.newTestRunner(GetFeedsHistoryReindex.class);
    final MetadataProviderService metadataService = new MockMetadataProviderService_ErrorGettingFeedsToReindex();
    runner.addControllerService(METADATA_SERVICE_IDENTIFIER, metadataService);
    runner.enableControllerService(metadataService);
    runner.setProperty(GetFeedsHistoryReindex.METADATA_SERVICE, METADATA_SERVICE_IDENTIFIER);
    runner.run(1);
    runner.assertQueueEmpty();
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_FOUND, 0);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_NOT_FOUND, 0);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_FAILURE, 1);
    runner.assertTransferCount(GetFeedsHistoryReindex.REL_ORIGINAL, 0);
    List<MockFlowFile> results = runner.getFlowFilesForRelationship(GetFeedsHistoryReindex.REL_FAILURE);
    MockFlowFile resultFlowFile = results.get(0);
    resultFlowFile.assertAttributeNotExists(GetFeedsHistoryReindex.FEEDS_TOTAL_COUNT_FOR_HISTORY_REINDEX_KEY);
    resultFlowFile.assertAttributeNotExists(GetFeedsHistoryReindex.FEEDS_TOTAL_IDS_FOR_HISTORY_REINDEX_KEY);
    resultFlowFile.assertAttributeNotExists(GetFeedsHistoryReindex.FEEDS_CHECK_TIME_UTC_FOR_HISTORY_REINDEX_KEY);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) MetadataProviderService(com.thinkbiganalytics.nifi.core.api.metadata.MetadataProviderService) Test(org.junit.Test)

Aggregations

MockFlowFile (org.apache.nifi.util.MockFlowFile)1228 Test (org.junit.Test)1198 TestRunner (org.apache.nifi.util.TestRunner)725 HashMap (java.util.HashMap)279 File (java.io.File)155 Matchers.anyString (org.mockito.Matchers.anyString)59 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)48 FlowFile (org.apache.nifi.flowfile.FlowFile)42 ByteArrayInputStream (java.io.ByteArrayInputStream)41 ProcessContext (org.apache.nifi.processor.ProcessContext)37 InputStream (java.io.InputStream)34 Connection (java.sql.Connection)31 GenericRecord (org.apache.avro.generic.GenericRecord)31 Statement (java.sql.Statement)30 Schema (org.apache.avro.Schema)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)27 SQLException (java.sql.SQLException)25 DBCPService (org.apache.nifi.dbcp.DBCPService)25 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)24