From 3921ab827b650da9142e0b6986c6986d69fdb42d Mon Sep 17 00:00:00 2001 From: trkurc Date: Sat, 19 Mar 2016 14:48:44 -0400 Subject: [PATCH] NIFI-1651 unit tests work on windows. removed contributor name/package from data and schema Reviewed by Aldrin Piri (aldrin@apache.org) and Mike Moser (mosermw@apache.org). This closes #291 --- .../processors/kite/TestInferAvroSchema.java | 52 +++++++++++++------ .../src/test/resources/Shapes.json.avro | 2 +- .../src/test/resources/Shapes_header.csv.avro | 2 +- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java index 510eaf97ad..125a631da6 100644 --- a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java +++ b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/java/org/apache/nifi/processors/kite/TestInferAvroSchema.java @@ -18,24 +18,29 @@ */ package org.apache.nifi.processors.kite; -import org.junit.Assert; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.HashMap; +import java.util.Map; + import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.nifi.flowfile.attributes.CoreAttributes; import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import java.io.File; -import java.io.StringWriter; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.HashMap; -import java.util.Map; - public class TestInferAvroSchema { private TestRunner runner = null; @@ -53,7 +58,7 @@ public class TestInferAvroSchema { runner.setProperty(InferAvroSchema.HEADER_LINE_SKIP_COUNT, "0"); runner.setProperty(InferAvroSchema.ESCAPE_STRING, "\\"); runner.setProperty(InferAvroSchema.QUOTE_STRING, "'"); - runner.setProperty(InferAvroSchema.RECORD_NAME, "com.jeremydyer.contact"); + runner.setProperty(InferAvroSchema.RECORD_NAME, "org.apache.nifi.contact"); runner.setProperty(InferAvroSchema.CHARSET, "UTF-8"); runner.setProperty(InferAvroSchema.PRETTY_AVRO_OUTPUT, "true"); } @@ -74,7 +79,7 @@ public class TestInferAvroSchema { runner.assertTransferCount(InferAvroSchema.REL_SUCCESS, 1); MockFlowFile flowFile = runner.getFlowFilesForRelationship(InferAvroSchema.REL_SUCCESS).get(0); - flowFile.assertContentEquals(new File("src/test/resources/Shapes_header.csv.avro").toPath()); + flowFile.assertContentEquals(unix2PlatformSpecificLineEndings(new File("src/test/resources/Shapes_header.csv.avro"))); flowFile.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "application/avro-binary"); } @@ -101,7 +106,9 @@ public class TestInferAvroSchema { MockFlowFile data = runner.getFlowFilesForRelationship(InferAvroSchema.REL_SUCCESS).get(0); String avroSchema = data.getAttribute(InferAvroSchema.AVRO_SCHEMA_ATTRIBUTE_NAME); - String knownSchema = FileUtils.readFileToString(new File("src/test/resources/Shapes.json.avro")); + String knownSchema = new String(unix2PlatformSpecificLineEndings( + new File("src/test/resources/Shapes.json.avro")), + StandardCharsets.UTF_8); Assert.assertEquals(avroSchema, knownSchema); //Since that avro schema is written to an attribute this should be teh same as the original @@ -130,7 +137,7 @@ public class TestInferAvroSchema { runner.assertTransferCount(InferAvroSchema.REL_SUCCESS, 1); MockFlowFile data = runner.getFlowFilesForRelationship(InferAvroSchema.REL_SUCCESS).get(0); - data.assertContentEquals(Paths.get("src/test/resources/Shapes_header.csv.avro")); + data.assertContentEquals(unix2PlatformSpecificLineEndings(new File("src/test/resources/Shapes_header.csv.avro"))); data.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "application/avro-binary"); } @@ -149,7 +156,7 @@ public class TestInferAvroSchema { Map attributes = new HashMap<>(); attributes.put(CoreAttributes.MIME_TYPE.key(), "text/csv"); - runner.enqueue((CSV_HEADER_LINE + "\nJeremy,Dyer,29,55555").getBytes(), attributes); + runner.enqueue((CSV_HEADER_LINE + "\nJane,Doe,29,55555").getBytes(), attributes); runner.run(); runner.assertTransferCount(InferAvroSchema.REL_FAILURE, 0); @@ -191,7 +198,7 @@ public class TestInferAvroSchema { runner.assertTransferCount(InferAvroSchema.REL_SUCCESS, 1); MockFlowFile flowFile = runner.getFlowFilesForRelationship(InferAvroSchema.REL_SUCCESS).get(0); - flowFile.assertContentEquals(new File("src/test/resources/Shapes_header.csv.avro").toPath()); + flowFile.assertContentEquals(unix2PlatformSpecificLineEndings(new File("src/test/resources/Shapes_header.csv.avro"))); flowFile.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "application/avro-binary"); } @@ -216,4 +223,19 @@ public class TestInferAvroSchema { flowFile.assertContentEquals(new File("src/test/resources/Shapes_Header_TabDelimited.csv").toPath()); flowFile.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "text/csv"); } + static byte [] unix2PlatformSpecificLineEndings(final File file) throws IOException { + try ( final BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); + final ByteArrayOutputStream out = new ByteArrayOutputStream()) { + byte eol[] = System.lineSeparator().getBytes(StandardCharsets.UTF_8); + int justRead; + while ((justRead = in.read()) != -1) { + if (justRead == '\n'){ + out.write(eol); + } else { + out.write(justRead); + } + } + return out.toByteArray(); + } + } } diff --git a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes.json.avro b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes.json.avro index e5f91e40ee..cdbf0481c8 100644 --- a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes.json.avro +++ b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes.json.avro @@ -1,7 +1,7 @@ { "type" : "record", "name" : "contact", - "namespace" : "com.jeremydyer", + "namespace" : "org.apache.nifi", "fields" : [ { "name" : "shapes", "type" : { diff --git a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes_header.csv.avro b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes_header.csv.avro index 2c35ea346f..300b069e05 100644 --- a/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes_header.csv.avro +++ b/nifi-nar-bundles/nifi-kite-bundle/nifi-kite-processors/src/test/resources/Shapes_header.csv.avro @@ -1,7 +1,7 @@ { "type" : "record", "name" : "contact", - "namespace" : "com.jeremydyer", + "namespace" : "org.apache.nifi", "doc" : "Schema generated by Kite", "fields" : [ { "name" : "shape",