Search in sources :

Example 61 with MockFlowFile

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

the class TestConvertAvroToJSON method testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer.

@Test
public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE);
    runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true));
    Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    String stringSchema = schema.toString();
    runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema);
    final GenericRecord user1 = new GenericData.Record(schema);
    user1.put("name", "Alyssa");
    user1.put("favorite_number", 256);
    final ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null);
    final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    datumWriter.write(user1, encoder);
    encoder.flush();
    out1.flush();
    byte[] test = out1.toByteArray();
    runner.enqueue(test);
    runner.run();
    runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0);
    out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}");
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) Schema(org.apache.avro.Schema) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) MockFlowFile(org.apache.nifi.util.MockFlowFile) BinaryEncoder(org.apache.avro.io.BinaryEncoder) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File) MockFlowFile(org.apache.nifi.util.MockFlowFile) Test(org.junit.Test)

Example 62 with MockFlowFile

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

the class TestConvertAvroToJSON method testZeroRecords.

@Test
public void testZeroRecords() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    final ByteArrayOutputStream out1 = serializeAvroRecord(schema, datumWriter);
    runner.enqueue(out1.toByteArray());
    runner.run();
    runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0);
    out.assertContentEquals("{}");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Schema(org.apache.avro.Schema) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) GenericRecord(org.apache.avro.generic.GenericRecord) File(java.io.File) MockFlowFile(org.apache.nifi.util.MockFlowFile) Test(org.junit.Test)

Example 63 with MockFlowFile

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

the class TestExtractAvroMetadata method testExtractionWithZeroUsers.

@Test
public void testExtractionWithZeroUsers() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractAvroMetadata());
    runner.setProperty(ExtractAvroMetadata.COUNT_ITEMS, "true");
    final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    final ByteArrayOutputStream out = getOutputStreamWithMultipleUsers(schema, 0);
    runner.enqueue(out.toByteArray());
    runner.run();
    runner.assertAllFlowFilesTransferred(ExtractAvroMetadata.REL_SUCCESS, 1);
    final MockFlowFile flowFile = runner.getFlowFilesForRelationship(ExtractAvroMetadata.REL_SUCCESS).get(0);
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_FINGERPRINT_ATTR, "b2d1d8d3de2833ce");
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_TYPE_ATTR, Schema.Type.RECORD.getName());
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_NAME_ATTR, "User");
    flowFile.assertAttributeEquals(ExtractAvroMetadata.ITEM_COUNT_ATTR, "0");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Schema(org.apache.avro.Schema) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) File(java.io.File) MockFlowFile(org.apache.nifi.util.MockFlowFile) Test(org.junit.Test)

Example 64 with MockFlowFile

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

the class TestExtractAvroMetadata method testExtractionWithSHA256.

@Test
public void testExtractionWithSHA256() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractAvroMetadata());
    runner.setProperty(ExtractAvroMetadata.FINGERPRINT_ALGORITHM, ExtractAvroMetadata.SHA_256);
    final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    final ByteArrayOutputStream out = getOutputStreamWithOneUser(schema);
    runner.enqueue(out.toByteArray());
    runner.run();
    runner.assertAllFlowFilesTransferred(ExtractAvroMetadata.REL_SUCCESS, 1);
    final MockFlowFile flowFile = runner.getFlowFilesForRelationship(ExtractAvroMetadata.REL_SUCCESS).get(0);
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_FINGERPRINT_ATTR, "683f8f51ecd208038f4f0d39820ee9dd0ef3e32a3bee9371de0a2016d501b113");
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_TYPE_ATTR, Schema.Type.RECORD.getName());
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_NAME_ATTR, "User");
    flowFile.assertAttributeNotExists(AVRO_SCHEMA_ATTR);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) TestRunner(org.apache.nifi.util.TestRunner) Schema(org.apache.avro.Schema) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) File(java.io.File) MockFlowFile(org.apache.nifi.util.MockFlowFile) Test(org.junit.Test)

Example 65 with MockFlowFile

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

the class TestExtractAvroMetadata method testExtractionWithCodec.

@Test
public void testExtractionWithCodec() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractAvroMetadata());
    // test dynamic attribute avro.codec
    runner.setProperty(ExtractAvroMetadata.METADATA_KEYS, AVRO_CODEC_ATTR);
    final Schema schema = new Schema.Parser().parse(new File("src/test/resources/array.avsc"));
    final GenericData.Array<String> data = new GenericData.Array<>(schema, Arrays.asList("one", "two", "three"));
    final DatumWriter<GenericData.Array<String>> datumWriter = new GenericDatumWriter<>(schema);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final DataFileWriter<GenericData.Array<String>> dataFileWriter = new DataFileWriter<>(datumWriter);
    dataFileWriter.setCodec(CodecFactory.deflateCodec(1));
    dataFileWriter.create(schema, out);
    dataFileWriter.append(data);
    dataFileWriter.close();
    runner.enqueue(out.toByteArray());
    runner.run();
    runner.assertAllFlowFilesTransferred(ExtractAvroMetadata.REL_SUCCESS, 1);
    final MockFlowFile flowFile = runner.getFlowFilesForRelationship(ExtractAvroMetadata.REL_SUCCESS).get(0);
    flowFile.assertAttributeEquals("avro.codec", "deflate");
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) Schema(org.apache.avro.Schema) DataFileWriter(org.apache.avro.file.DataFileWriter) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) GenericData(org.apache.avro.generic.GenericData) MockFlowFile(org.apache.nifi.util.MockFlowFile) File(java.io.File) MockFlowFile(org.apache.nifi.util.MockFlowFile) 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