Java Code Examples for org.apache.nifi.util.TestRunner#getContentAsByteArray()

The following examples show how to use org.apache.nifi.util.TestRunner#getContentAsByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreprocessingTest.java    From scalable-ocr with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    // Generate a test runner to mock a processor in a flow
    TestRunner runner = TestRunners.newTestRunner(new PreprocessingProcessor());
    File inputFile = new File("../preprocessing/src/test/resources/images/brscan_original_r90.jpg");
    // Add properties
    runner.setProperty(PreprocessingProcessor.CONVERT_PATH, Util.Locations.CONVERT.find().get().getAbsolutePath());
    runner.setProperty(PreprocessingProcessor.TEMP_DIR, "/tmp");
    runner.setProperty(PreprocessingProcessor.DEFINITIONS, "-g -e normalize -f 15 -o 10 -u -s 2 -T -p 20");
    // Add the content to the runner
    runner.enqueue(new FileInputStream(inputFile));

    // Run the enqueued content, it also takes an int = number of contents queued
    runner.run(1);

    // All results were processed with out failure
    runner.assertQueueEmpty();

    // If you need to read or do additional tests on results you can access the content
    List<MockFlowFile> results = runner.getFlowFilesForRelationship(PreprocessingProcessor.SUCCESS);
    Assert.assertEquals(1, results.size());
    byte[] value = runner.getContentAsByteArray(results.get(0));
    BufferedImage bi = ImageUtils.INSTANCE.readImage(value);
    Assert.assertEquals(1074, bi.getHeight());
    Assert.assertEquals(812, bi.getWidth());
}
 
Example 2
Source File: TestXMLRecordSetWriter.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testRootAndRecordNaming() throws IOException, InitializationException {
    XMLRecordSetWriter writer = new XMLRecordSetWriter();
    TestRunner runner = setup(writer);

    runner.setProperty(writer, XMLRecordSetWriter.ROOT_TAG_NAME, "ROOT_NODE");
    runner.setProperty(writer, XMLRecordSetWriter.RECORD_TAG_NAME, "RECORD_NODE");

    runner.enableControllerService(writer);
    runner.enqueue("");
    runner.run();
    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(TestXMLRecordSetWriterProcessor.SUCCESS, 1);

    String expected = "<ROOT_NODE><RECORD_NODE><array_field>1</array_field><array_field></array_field><array_field>3</array_field>" +
            "<name1>val1</name1><name2></name2></RECORD_NODE>" +
            "<RECORD_NODE><array_field>1</array_field><array_field></array_field><array_field>3</array_field>" +
            "<name1>val1</name1><name2></name2></RECORD_NODE></ROOT_NODE>";
    String actual = new String(runner.getContentAsByteArray(runner.getFlowFilesForRelationship(TestXMLRecordSetWriterProcessor.SUCCESS).get(0)));
    assertThat(expected, CompareMatcher.isSimilarTo(actual).ignoreWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)));
}
 
Example 3
Source File: TestEvaluateXQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchesMultipleStringContent() throws XPathFactoryConfigurationException, IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateXQuery());
    testRunner.setProperty(EvaluateXQuery.DESTINATION, EvaluateXQuery.DESTINATION_CONTENT);
    testRunner.setProperty("some.property", "//fruit/name/text()");

    testRunner.enqueue(XML_SNIPPET);
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(EvaluateXQuery.REL_MATCH, 7);

    final List<MockFlowFile> flowFilesForRelMatch = testRunner.getFlowFilesForRelationship(EvaluateXQuery.REL_MATCH);
    for (int i = 0; i < flowFilesForRelMatch.size(); i++) {

        final MockFlowFile out = flowFilesForRelMatch.get(i);
        final byte[] outData = testRunner.getContentAsByteArray(out);
        final String outXml = new String(outData, "UTF-8");
        assertEquals(fruitNames[i], outXml.trim());
    }
}
 
Example 4
Source File: TestEvaluateXPath.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessForEmbeddedDocTypeValidation() throws XPathFactoryConfigurationException, IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateXPath());
    testRunner.setProperty(EvaluateXPath.DESTINATION, EvaluateXPath.DESTINATION_CONTENT);
    testRunner.setProperty(EvaluateXPath.RETURN_TYPE, EvaluateXPath.RETURN_TYPE_STRING);
    testRunner.setProperty(EvaluateXPath.VALIDATE_DTD, "true");
    testRunner.setProperty("some.property", "/*:bundle/node/subNode[1]/value/text()");

    testRunner.enqueue(XML_SNIPPET_EMBEDDED_DOCTYPE);
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(EvaluateXPath.REL_MATCH, 1);
    final MockFlowFile out = testRunner.getFlowFilesForRelationship(EvaluateXPath.REL_MATCH).get(0);
    final byte[] outData = testRunner.getContentAsByteArray(out);
    final String outXml = new String(outData, "UTF-8");
    assertTrue(outXml.trim().equals("Hello"));
}
 
Example 5
Source File: TestXMLRecordSetWriter.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultSingleRecord() throws IOException, InitializationException {
    XMLRecordSetWriter writer = new XMLRecordSetWriter();
    TestRunner runner = setup(writer);

    runner.setProperty(TestXMLRecordSetWriterProcessor.MULTIPLE_RECORDS, "false");

    runner.enableControllerService(writer);
    runner.enqueue("");
    runner.run();
    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(TestXMLRecordSetWriterProcessor.SUCCESS, 1);

    String expected = "<array_record><array_field>1</array_field><array_field></array_field><array_field>3</array_field>" +
            "<name1>val1</name1><name2></name2></array_record>";

    String actual = new String(runner.getContentAsByteArray(runner.getFlowFilesForRelationship(TestXMLRecordSetWriterProcessor.SUCCESS).get(0)));
    assertThat(expected, CompareMatcher.isSimilarTo(actual).ignoreWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)));
}
 
Example 6
Source File: TestJSONToAvroProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicConversionWithCompression() throws IOException {
    TestRunner runner = TestRunners.newTestRunner(ConvertJSONToAvro.class);
    runner.assertNotValid();
    runner.setProperty(ConvertJSONToAvro.SCHEMA, SCHEMA.toString());
    runner.setProperty(AbstractKiteConvertProcessor.COMPRESSION_TYPE, CodecType.NONE.toString());
    runner.assertValid();

    runner.enqueue(streamFor(JSON_CONTENT));
    runner.run();

    long converted = runner.getCounterValue("Converted records");
    long errors = runner.getCounterValue("Conversion errors");
    Assert.assertEquals("Should convert 2 rows", 2, converted);
    Assert.assertEquals("Should reject 3 rows", 3, errors);

    runner.assertTransferCount("success", 1);
    runner.assertTransferCount("failure", 0);
    runner.assertTransferCount("incompatible", 1);

    MockFlowFile incompatible = runner.getFlowFilesForRelationship("incompatible").get(0);
    String failureContent = new String(runner.getContentAsByteArray(incompatible),
            StandardCharsets.UTF_8);
    Assert.assertEquals("Should reject an invalid string and double",
            JSON_CONTENT, failureContent);
    Assert.assertEquals("Should accumulate error messages",
            FAILURE_SUMMARY, incompatible.getAttribute("errors"));
}
 
Example 7
Source File: TestConsumeEmail.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsumePOP3() throws Exception {

    final TestRunner runner = TestRunners.newTestRunner(new ConsumePOP3());
    runner.setProperty(ConsumeIMAP.HOST, ServerSetupTest.POP3.getBindAddress());
    runner.setProperty(ConsumeIMAP.PORT, String.valueOf(ServerSetupTest.POP3.getPort()));
    runner.setProperty(ConsumeIMAP.USER, "nifiUserPop");
    runner.setProperty(ConsumeIMAP.PASSWORD, "nifiPassword");
    runner.setProperty(ConsumeIMAP.FOLDER, "INBOX");
    runner.setProperty(ConsumeIMAP.USE_SSL, "false");

    addMessage("testConsumePop1", popUser);
    addMessage("testConsumePop2", popUser);

    runner.run();

    runner.assertTransferCount(ConsumePOP3.REL_SUCCESS, 2);
    final List<MockFlowFile> messages = runner.getFlowFilesForRelationship(ConsumePOP3.REL_SUCCESS);
    String result = new String(runner.getContentAsByteArray(messages.get(0)));

    // Verify body
    Assert.assertTrue(result.contains("test test test chocolate"));

    // Verify sender
    Assert.assertTrue(result.contains("[email protected]"));

    // Verify subject
    Assert.assertTrue(result.contains("Pop1"));

}
 
Example 8
Source File: TestEvaluateXQuery.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteXmlToContent() throws XPathFactoryConfigurationException, IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateXQuery());
    testRunner.setProperty(EvaluateXQuery.DESTINATION, EvaluateXQuery.DESTINATION_CONTENT);
    testRunner.setProperty("some.property", "/*:fruitbasket/fruit[1]/name");

    testRunner.enqueue(XML_SNIPPET);
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(EvaluateXQuery.REL_MATCH, 1);
    final MockFlowFile out = testRunner.getFlowFilesForRelationship(EvaluateXQuery.REL_MATCH).get(0);
    final byte[] outData = testRunner.getContentAsByteArray(out);
    final String outXml = new String(outData, "UTF-8");
    assertTrue(outXml.contains("<name xmlns:ns=\"http://namespace/1\">apple</name>"));
}
 
Example 9
Source File: TestRouteText.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testJson() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new RouteText());
    runner.setProperty(RouteText.MATCH_STRATEGY, RouteText.STARTS_WITH);
    runner.setProperty(RouteText.ROUTE_STRATEGY, RouteText.ROUTE_TO_MATCHING_PROPERTY_NAME);
    runner.setProperty("greeting", "\"greeting\"");
    runner.setProperty("address", "\"address\"");

    runner.enqueue(Paths.get("src/test/resources/TestJson/json-sample.json"));
    runner.run();

    runner.assertTransferCount("greeting", 1);
    runner.assertTransferCount("address", 1);
    runner.assertTransferCount("unmatched", 1);
    runner.assertTransferCount("original", 1);

    // Verify text is trimmed
    final MockFlowFile outGreeting = runner.getFlowFilesForRelationship("greeting").get(0);
    String outGreetingString = new String(runner.getContentAsByteArray(outGreeting));
    assertEquals(7, countLines(outGreetingString));
    final MockFlowFile outAddress = runner.getFlowFilesForRelationship("address").get(0);
    String outAddressString = new String(runner.getContentAsByteArray(outAddress));
    assertEquals(7, countLines(outAddressString));
    final MockFlowFile outUnmatched = runner.getFlowFilesForRelationship("unmatched").get(0);
    String outUnmatchedString = new String(runner.getContentAsByteArray(outUnmatched));
    assertEquals(400, countLines(outUnmatchedString));

    final MockFlowFile outOriginal = runner.getFlowFilesForRelationship("original").get(0);
    outOriginal.assertContentEquals(Paths.get("src/test/resources/TestJson/json-sample.json"));

}
 
Example 10
Source File: TestQuerySolr.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testStats() throws IOException {
    SolrClient solrClient = createSolrClient();
    TestRunner runner = createRunnerWithSolrClient(solrClient);

    runner.setProperty("stats", "true");
    runner.setProperty("stats.field", "integer_single");

    runner.enqueue(new ByteArrayInputStream(new byte[0]));
    runner.run();

    runner.assertTransferCount(QuerySolr.STATS, 1);
    JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(
            runner.getContentAsByteArray(runner.getFlowFilesForRelationship(QuerySolr.STATS).get(0)))));
    reader.beginObject();
    assertEquals(reader.nextName(), "stats_fields");
    reader.beginObject();
    assertEquals(reader.nextName(), "integer_single");
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
            case "min": assertEquals(reader.nextString(), "0.0"); break;
            case "max": assertEquals(reader.nextString(), "9.0"); break;
            case "count": assertEquals(reader.nextInt(), 10); break;
            case "sum": assertEquals(reader.nextString(), "45.0"); break;
            default: reader.skipValue(); break;
        }
    }
    reader.endObject();
    reader.endObject();
    reader.endObject();

    reader.close();
    solrClient.close();
}
 
Example 11
Source File: TestEvaluateXQuery.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteXmlToContent() throws XPathFactoryConfigurationException, IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateXQuery());
    testRunner.setProperty(EvaluateXQuery.DESTINATION, EvaluateXQuery.DESTINATION_CONTENT);
    testRunner.setProperty("some.property", "/*:fruitbasket/fruit[1]/name");

    testRunner.enqueue(XML_SNIPPET);
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(EvaluateXQuery.REL_MATCH, 1);
    final MockFlowFile out = testRunner.getFlowFilesForRelationship(EvaluateXQuery.REL_MATCH).get(0);
    final byte[] outData = testRunner.getContentAsByteArray(out);
    final String outXml = new String(outData, "UTF-8");
    assertTrue(outXml.contains("<name xmlns:ns=\"http://namespace/1\">apple</name>"));
}
 
Example 12
Source File: TestCSVToAvroProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicConversion() throws IOException {
    TestRunner runner = TestRunners.newTestRunner(ConvertCSVToAvro.class);
    runner.assertNotValid();
    runner.setProperty(ConvertCSVToAvro.SCHEMA, SCHEMA.toString());
    runner.assertValid();

    runner.enqueue(streamFor(CSV_CONTENT));
    runner.run();

    long converted = runner.getCounterValue("Converted records");
    long errors = runner.getCounterValue("Conversion errors");
    Assert.assertEquals("Should convert 2 rows", 2, converted);
    Assert.assertEquals("Should reject 1 row", 1, errors);

    runner.assertTransferCount("success", 1);
    runner.assertTransferCount("failure", 0);
    runner.assertTransferCount("incompatible", 1);

    MockFlowFile incompatible = runner.getFlowFilesForRelationship("incompatible").get(0);
    String failureContent = new String(runner.getContentAsByteArray(incompatible),
            StandardCharsets.UTF_8);
    Assert.assertEquals("Should reject an invalid string and double",
            CSV_CONTENT, failureContent);
    Assert.assertEquals("Should accumulate error messages",
            FAILURE_SUMMARY, incompatible.getAttribute("errors"));
}
 
Example 13
Source File: TestCSVToAvroProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicConversionWithCompression() throws IOException {
    TestRunner runner = TestRunners.newTestRunner(ConvertCSVToAvro.class);
    runner.assertNotValid();
    runner.setProperty(ConvertCSVToAvro.SCHEMA, SCHEMA.toString());
    runner.setProperty(AbstractKiteConvertProcessor.COMPRESSION_TYPE, CodecType.DEFLATE.toString());
    runner.assertValid();

    runner.enqueue(streamFor(CSV_CONTENT));
    runner.run();

    long converted = runner.getCounterValue("Converted records");
    long errors = runner.getCounterValue("Conversion errors");
    Assert.assertEquals("Should convert 2 rows", 2, converted);
    Assert.assertEquals("Should reject 1 row", 1, errors);

    runner.assertTransferCount("success", 1);
    runner.assertTransferCount("failure", 0);
    runner.assertTransferCount("incompatible", 1);

    MockFlowFile incompatible = runner.getFlowFilesForRelationship("incompatible").get(0);
    String failureContent = new String(runner.getContentAsByteArray(incompatible),
            StandardCharsets.UTF_8);
    Assert.assertEquals("Should reject an invalid string and double",
            CSV_CONTENT, failureContent);
    Assert.assertEquals("Should accumulate error messages",
            FAILURE_SUMMARY, incompatible.getAttribute("errors"));
}
 
Example 14
Source File: TestRouteText.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testXml() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new RouteText());
    runner.setProperty(RouteText.MATCH_STRATEGY, RouteText.CONTAINS);
    runner.setProperty(RouteText.ROUTE_STRATEGY, RouteText.ROUTE_TO_MATCHING_PROPERTY_NAME);
    runner.setProperty("NodeType", "name=\"NodeType\"");
    runner.setProperty("element", "<xs:element");
    runner.setProperty("name", "name=");

    runner.enqueue(Paths.get("src/test/resources/TestXml/XmlBundle.xsd"));
    runner.run();

    runner.assertTransferCount("NodeType", 1);
    runner.assertTransferCount("element", 1);
    runner.assertTransferCount("name", 1);
    runner.assertTransferCount("unmatched", 1);
    runner.assertTransferCount("original", 1);


    // Verify text is trimmed
    final MockFlowFile outNode = runner.getFlowFilesForRelationship("NodeType").get(0);
    String outNodeString = new String(runner.getContentAsByteArray(outNode));
    assertEquals(1, countLines(outNodeString));
    final MockFlowFile outElement = runner.getFlowFilesForRelationship("element").get(0);
    String outElementString = new String(runner.getContentAsByteArray(outElement));
    assertEquals(4, countLines(outElementString));
    final MockFlowFile outName = runner.getFlowFilesForRelationship("name").get(0);
    String outNameString = new String(runner.getContentAsByteArray(outName));
    assertEquals(7, countLines(outNameString));
    final MockFlowFile outUnmatched = runner.getFlowFilesForRelationship("unmatched").get(0);
    String outUnmatchedString = new String(runner.getContentAsByteArray(outUnmatched));
    assertEquals(26, countLines(outUnmatchedString));

    final MockFlowFile outOriginal = runner.getFlowFilesForRelationship("original").get(0);
    outOriginal.assertContentEquals(Paths.get("src/test/resources/TestXml/XmlBundle.xsd"));
}
 
Example 15
Source File: TestAttributesToCSV.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoAttrListCoreNullOffToContent() throws IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToCSV());
    testRunner.setProperty(AttributesToCSV.DESTINATION, OUTPUT_OVERWRITE_CONTENT);
    testRunner.setProperty(AttributesToCSV.INCLUDE_CORE_ATTRIBUTES, "true");
    testRunner.setProperty(AttributesToCSV.NULL_VALUE_FOR_EMPTY_STRING, "false");

    final Map<String, String> attrs = new HashMap<String, String>(){{
        put("beach-name", "Malibu Beach");
        put("beach-location", "California, US");
        put("beach-endorsement", "This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim");
    }};

    testRunner.enqueue(new byte[0], attrs);
    testRunner.run();

    List<MockFlowFile> flowFilesForRelationship = testRunner.getFlowFilesForRelationship(AttributesToCSV.REL_SUCCESS);

    testRunner.assertTransferCount(AttributesToCSV.REL_FAILURE, 0);
    testRunner.assertTransferCount(AttributesToCSV.REL_SUCCESS, 1);

    MockFlowFile flowFile = flowFilesForRelationship.get(0);

    assertEquals(OUTPUT_MIME_TYPE, flowFile.getAttribute(CoreAttributes.MIME_TYPE.key()));

    final byte[] contentData = testRunner.getContentAsByteArray(flowFile);

    final String contentDataString = new String(contentData, "UTF-8");

    Set<String> contentValues = new HashSet<>(getStrings(contentDataString));

    assertEquals(6, contentValues.size());

    assertTrue(contentValues.contains("Malibu Beach"));
    assertTrue(contentValues.contains("\"California, US\""));
    assertTrue(contentValues.contains("\"This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim\""));
    assertTrue(contentValues.contains(flowFile.getAttribute("filename")));
    assertTrue(contentValues.contains(flowFile.getAttribute("path")));
    assertTrue(contentValues.contains(flowFile.getAttribute("uuid")));
}
 
Example 16
Source File: TestEvaluateXQuery.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteStringToContent() throws XPathFactoryConfigurationException, IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateXQuery());
    testRunner.setProperty(EvaluateXQuery.DESTINATION, EvaluateXQuery.DESTINATION_CONTENT);
    testRunner.setProperty("some.property", "/*:fruitbasket/fruit[1]/name/text()");

    testRunner.enqueue(XML_SNIPPET);
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(EvaluateXQuery.REL_MATCH, 1);
    final MockFlowFile out = testRunner.getFlowFilesForRelationship(EvaluateXQuery.REL_MATCH).get(0);
    final byte[] outData = testRunner.getContentAsByteArray(out);
    final String outXml = new String(outData, "UTF-8");
    assertTrue(outXml.trim().equals("apple"));
}
 
Example 17
Source File: TestAttributesToCSV.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoAttrListNoCoreNullOffToContent() throws IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToCSV());
    testRunner.setProperty(AttributesToCSV.DESTINATION, OUTPUT_OVERWRITE_CONTENT);
    testRunner.setProperty(AttributesToCSV.INCLUDE_CORE_ATTRIBUTES, "false");
    testRunner.setProperty(AttributesToCSV.NULL_VALUE_FOR_EMPTY_STRING, "false");

    Map<String, String> attrs = new HashMap<String, String>(){{
        put("beach-name", "Malibu Beach");
        put("beach-location", "California, US");
        put("beach-endorsement", "This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim");
    }};

    testRunner.enqueue(new byte[0], attrs);
    testRunner.run();

    List<MockFlowFile> flowFilesForRelationship = testRunner.getFlowFilesForRelationship(AttributesToCSV.REL_SUCCESS);

    testRunner.assertTransferCount(AttributesToCSV.REL_FAILURE, 0);
    testRunner.assertTransferCount(AttributesToCSV.REL_SUCCESS, 1);

    MockFlowFile flowFile = flowFilesForRelationship.get(0);

    assertEquals(OUTPUT_MIME_TYPE, flowFile.getAttribute(CoreAttributes.MIME_TYPE.key()));

    final byte[] contentData = testRunner.getContentAsByteArray(flowFile);

    final String contentDataString = new String(contentData, "UTF-8");
    Set<String> contentValues = new HashSet<>(getStrings(contentDataString));

    assertEquals(3, contentValues.size());

    assertTrue(contentValues.contains("Malibu Beach"));
    assertTrue(contentValues.contains("\"California, US\""));
    assertTrue(contentValues.contains("\"This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim\""));

}
 
Example 18
Source File: TestMergeContent.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testTar() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new MergeContent());
    runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec");
    runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_TAR);

    final Map<String, String> attributes = new HashMap<>();
    attributes.put(CoreAttributes.MIME_TYPE.key(), "application/plain-text");

    attributes.put(CoreAttributes.FILENAME.key(), "AShortFileName");
    runner.enqueue("Hello".getBytes("UTF-8"), attributes);
    attributes.put(CoreAttributes.FILENAME.key(), "ALongerrrFileName");
    runner.enqueue(", ".getBytes("UTF-8"), attributes);
    attributes.put(CoreAttributes.FILENAME.key(), "AReallyLongggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggFileName");
    runner.enqueue("World!".getBytes("UTF-8"), attributes);
    runner.run();

    runner.assertQueueEmpty();
    runner.assertTransferCount(MergeContent.REL_MERGED, 1);
    runner.assertTransferCount(MergeContent.REL_FAILURE, 0);
    runner.assertTransferCount(MergeContent.REL_ORIGINAL, 3);

    final MockFlowFile bundle = runner.getFlowFilesForRelationship(MergeContent.REL_MERGED).get(0);
    try (final InputStream rawIn = new ByteArrayInputStream(runner.getContentAsByteArray(bundle)); final TarArchiveInputStream in = new TarArchiveInputStream(rawIn)) {
        ArchiveEntry entry = in.getNextEntry();
        Assert.assertNotNull(entry);
        assertEquals("AShortFileName", entry.getName());
        final byte[] part1 = IOUtils.toByteArray(in);
        Assert.assertTrue(Arrays.equals("Hello".getBytes("UTF-8"), part1));

        entry = in.getNextEntry();
        assertEquals("ALongerrrFileName", entry.getName());
        final byte[] part2 = IOUtils.toByteArray(in);
        Assert.assertTrue(Arrays.equals(", ".getBytes("UTF-8"), part2));

        entry = in.getNextEntry();
        assertEquals("AReallyLongggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggFileName", entry.getName());
        final byte[] part3 = IOUtils.toByteArray(in);
        Assert.assertTrue(Arrays.equals("World!".getBytes("UTF-8"), part3));
    }
    bundle.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "application/tar");
}
 
Example 19
Source File: QuerySolrIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllFacetCategories() throws IOException {
    SolrClient solrClient = createSolrClient();
    TestRunner runner = createRunnerWithSolrClient(solrClient);

    runner.setProperty("facet", "true");
    runner.setProperty("facet.field", "integer_multi");
    runner.setProperty("facet.interval", "integer_single");
    runner.setProperty("facet.interval.set.1", "[4,7]");
    runner.setProperty("facet.interval.set.2", "[5,7]");
    runner.setProperty("facet.range", "created");
    runner.setProperty("facet.range.start", "NOW/MINUTE");
    runner.setProperty("facet.range.end", "NOW/MINUTE+1MINUTE");
    runner.setProperty("facet.range.gap", "+20SECOND");
    runner.setProperty("facet.query.1", "*:*");
    runner.setProperty("facet.query.2", "integer_multi:2");
    runner.setProperty("facet.query.3", "integer_multi:3");

    runner.enqueue(new ByteArrayInputStream(new byte[0]));
    runner.run();
    runner.assertTransferCount(QuerySolr.FACETS, 1);

    JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(
            runner.getContentAsByteArray(runner.getFlowFilesForRelationship(QuerySolr.FACETS).get(0)))));
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("facet_queries")) {
            assertEquals(30, returnCheckSumForArrayOfJsonObjects(reader));
        } else if (name.equals("facet_fields")) {
            reader.beginObject();
            assertEquals(reader.nextName(), "integer_multi");
            assertEquals(returnCheckSumForArrayOfJsonObjects(reader), 30);
            reader.endObject();
        } else if (name.equals("facet_ranges")) {
            reader.beginObject();
            assertEquals(reader.nextName(), "created");
            assertEquals(returnCheckSumForArrayOfJsonObjects(reader), 10);
            reader.endObject();
        } else if (name.equals("facet_intervals")) {
            reader.beginObject();
            assertEquals(reader.nextName(), "integer_single");
            assertEquals(returnCheckSumForArrayOfJsonObjects(reader), 7);
            reader.endObject();
        }
    }
    reader.endObject();
    reader.close();
    solrClient.close();
}
 
Example 20
Source File: QuerySolrIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecordResponse() throws IOException, InitializationException {
    SolrClient solrClient = createSolrClient();
    TestRunner runner = createRunnerWithSolrClient(solrClient);

    runner.setProperty(QuerySolr.RETURN_TYPE, QuerySolr.MODE_REC.getValue());
    runner.setProperty(QuerySolr.SOLR_PARAM_FIELD_LIST, "id,created,integer_single");
    runner.setProperty(QuerySolr.SOLR_PARAM_ROWS, "10");

    final String outputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/test-schema.avsc")));

    final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
    runner.addControllerService("writer", jsonWriter);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
    runner.setProperty(jsonWriter, "Pretty Print JSON", "true");
    runner.setProperty(jsonWriter, "Schema Write Strategy", "full-schema-attribute");
    runner.enableControllerService(jsonWriter);
    runner.setProperty(SolrUtils.RECORD_WRITER, "writer");

    runner.setNonLoopConnection(false);

    runner.run(1);
    runner.assertQueueEmpty();
    runner.assertTransferCount(QuerySolr.RESULTS, 1);

    JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(
            runner.getContentAsByteArray(runner.getFlowFilesForRelationship(QuerySolr.RESULTS).get(0)))));
    reader.beginArray();
    int controlScore = 0;
    while (reader.hasNext()) {
        reader.beginObject();
        while (reader.hasNext()) {
            if (reader.nextName().equals("integer_single")) {
                controlScore += reader.nextInt();
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }
    reader.close();
    solrClient.close();

    assertEquals(controlScore, 45);
}