Java Code Examples for org.apache.nifi.util.TestRunners#newTestRunner()

The following examples show how to use org.apache.nifi.util.TestRunners#newTestRunner() . 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: TestSplitAvro.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecordSplitBareOutputWithSingleRecords() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new SplitAvro());
    runner.setProperty(SplitAvro.OUTPUT_STRATEGY, SplitAvro.BARE_RECORD_OUTPUT);

    runner.enqueue(users.toByteArray());
    runner.run();

    runner.assertTransferCount(SplitAvro.REL_SPLIT, 100);
    runner.assertTransferCount(SplitAvro.REL_ORIGINAL, 1);
    runner.assertTransferCount(SplitAvro.REL_FAILURE, 0);

    runner.getFlowFilesForRelationship(SplitAvro.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), "100");
    final List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(SplitAvro.REL_SPLIT);

    checkBareRecordsSplitSize(flowFiles, 1, true);
}
 
Example 2
Source File: TestExecuteStreamCommand.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testIgnoredStdin() throws IOException {
    File exJar = new File("src/test/resources/ExecuteCommand/TestIngestAndUpdate.jar");
    File dummy = new File("src/test/resources/ExecuteCommand/1000bytes.txt");
    String jarPath = exJar.getAbsolutePath();
    exJar.setExecutable(true);
    final TestRunner controller = TestRunners.newTestRunner(ExecuteStreamCommand.class);
    controller.setValidateExpressionUsage(false);
    controller.enqueue(dummy.toPath());
    controller.setProperty(ExecuteStreamCommand.WORKING_DIR, "target");
    controller.setProperty(ExecuteStreamCommand.EXECUTION_COMMAND, "java");
    controller.setProperty(ExecuteStreamCommand.EXECUTION_ARGUMENTS, "-jar;" + jarPath);
    controller.setProperty(ExecuteStreamCommand.IGNORE_STDIN, "true");
    controller.run(1);
    controller.assertTransferCount(ExecuteStreamCommand.ORIGINAL_RELATIONSHIP, 1);
    controller.assertTransferCount(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP, 1);
    List<MockFlowFile> flowFiles = controller.getFlowFilesForRelationship(ExecuteStreamCommand.OUTPUT_STREAM_RELATIONSHIP);
    byte[] byteArray = flowFiles.get(0).toByteArray();
    String result = new String(byteArray);
    assertTrue("TestIngestAndUpdate.jar should not have received anything to modify",
        Pattern.compile("target:ModifiedResult\r?\n$").matcher(result).find());
}
 
Example 3
Source File: TestScrollElasticsearchHttp.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testScrollElasticsearchOnTriggerWithIOException() throws IOException {
    ScrollElasticsearchHttpTestProcessor processor = new ScrollElasticsearchHttpTestProcessor();
    processor.setExceptionToThrow(new IOException("Error reading from disk"));
    runner = TestRunners.newTestRunner(processor); // simulate doc not found
    runner.setProperty(AbstractElasticsearchHttpProcessor.ES_URL, "http://127.0.0.1:9200");
    runner.setProperty(ScrollElasticsearchHttp.INDEX, "doc");
    runner.setProperty(ScrollElasticsearchHttp.TYPE, "status");
    runner.setValidateExpressionUsage(true);
    runner.setProperty(ScrollElasticsearchHttp.QUERY, "${doc_id}");

    runner.enqueue("".getBytes(), new HashMap<String, String>() {
        {
            put("identifier", "28039652140");
        }
    });

    runner.run(1, true, true);

    // This test generates a HTTP 100 "Should fail"
    runner.assertTransferCount(ScrollElasticsearchHttp.REL_SUCCESS, 0);
    runner.assertTransferCount(ScrollElasticsearchHttp.REL_FAILURE, 0);
}
 
Example 4
Source File: TestInvokeJython.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that has a Jython Processor that that reads the first line of text from the flowfiles content and stores the value in an attribute of the outgoing flowfile.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttribute() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new InvokeScriptedProcessor());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/jython/test_reader.py");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/jython");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("success");
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example 5
Source File: ITPutS3Object.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEndpointOverride() {
    // remove leading "/" from filename to avoid duplicate separators
    final String TESTKEY = AbstractS3IT.SAMPLE_FILE_RESOURCE_NAME.substring(1);

    final PutS3Object processor = new TestablePutS3Object();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    final ProcessContext context = runner.getProcessContext();

    runner.setProperty(PutS3Object.ENDPOINT_OVERRIDE, TEST_ENDPOINT);
    runner.setProperty(PutS3Object.BUCKET, BUCKET_NAME);
    runner.setProperty(PutS3Object.KEY, TESTKEY);

    runner.run();

    Assert.assertEquals(BUCKET_NAME, context.getProperty(PutS3Object.BUCKET).toString());
    Assert.assertEquals(TESTKEY, context.getProperty(PutS3Object.KEY).evaluateAttributeExpressions().toString());
    Assert.assertEquals(TEST_ENDPOINT, context.getProperty(PutS3Object.ENDPOINT_OVERRIDE).toString());

    String s3url = ((TestablePutS3Object)processor).testable_getClient().getResourceUrl(BUCKET_NAME, TESTKEY);
    Assert.assertEquals(TEST_ENDPOINT + "/" + BUCKET_NAME + "/" + TESTKEY, s3url);
}
 
Example 6
Source File: TestReplaceText.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegexNoCaptureDefaultReplacement() throws IOException {
    // Test the old Default Regex and new Default Regex with the default replacement.  This should fail
    // because the regex does not create a capture group.
    final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(ReplaceText.SEARCH_VALUE, "(?s:^.*$)");
    runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "$1");
    runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.REGEX_REPLACE);
    runner.setProperty(ReplaceText.EVALUATION_MODE, ReplaceText.ENTIRE_TEXT);

    exception.expect(AssertionError.class);
    exception.expectMessage("java.lang.IndexOutOfBoundsException: No group 1");
    runner.enqueue("testing\n123".getBytes());
    runner.run();
}
 
Example 7
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithSortrPopulatedSpec() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.SORTR);
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, "abcd");
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
    transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/sortrOutput.json")));
    String transformedJsonString = JsonUtils.toJsonString(transformedJson);
    String compareJsonString = JsonUtils.toJsonString(compareJson);
    assertTrue(compareJsonString.equals(transformedJsonString));
}
 
Example 8
Source File: TestConvertAvroToJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleAvroMessagesContainerNone() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));

    runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE);

    final GenericRecord user1 = new GenericData.Record(schema);
    user1.put("name", "Alyssa");
    user1.put("favorite_number", 256);

    final GenericRecord user2 = new GenericData.Record(schema);
    user2.put("name", "George");
    user2.put("favorite_number", 1024);
    user2.put("favorite_color", "red");

    final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    final ByteArrayOutputStream out1 = serializeAvroRecord(schema, datumWriter, user1, user2);
    runner.enqueue(out1.toByteArray());

    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}\n{\"name\": \"George\", \"favorite_number\": 1024, \"favorite_color\": \"red\"}");
}
 
Example 9
Source File: TestEncodeContent.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailDecodeNotBase32() throws IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new EncodeContent());

    testRunner.setProperty(EncodeContent.MODE, EncodeContent.DECODE_MODE);
    testRunner.setProperty(EncodeContent.ENCODING, EncodeContent.BASE32_ENCODING);

    testRunner.enqueue(Paths.get("src/test/resources/hello.txt"));
    testRunner.clearTransferState();
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(EncodeContent.REL_FAILURE, 1);
}
 
Example 10
Source File: TestControlRate.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCountWithGrouping() throws InterruptedException {
    final TestRunner runner = TestRunners.newTestRunner(new ControlRate());
    runner.setProperty(ControlRate.RATE_CONTROL_CRITERIA, ControlRate.FLOWFILE_RATE);
    runner.setProperty(ControlRate.MAX_RATE, "2");
    runner.setProperty(ControlRate.TIME_PERIOD, "1 sec");
    runner.setProperty(ControlRate.GROUPING_ATTRIBUTE_NAME, "group");

    createFlowFileWithGroup(runner, "one");
    createFlowFileWithGroup(runner, "two");
    createFlowFileWithGroup(runner, "one");
    createFlowFileWithGroup(runner, "two");
    createFlowFileWithGroup(runner, "one");
    createFlowFileWithGroup(runner, "two");

    runner.run(6, false);

    runner.assertAllFlowFilesTransferred(ControlRate.REL_SUCCESS, 4);
    runner.clearTransferState();

    runner.run(50, false);
    runner.assertTransferCount(ControlRate.REL_SUCCESS, 0);
    runner.assertTransferCount(ControlRate.REL_FAILURE, 0);
    runner.assertQueueNotEmpty();

    // we have sent 2 files per group and after 1 second, we should be able to send the remaining 1 file per group
    Thread.sleep(1100L);
    runner.run(2);
    runner.assertAllFlowFilesTransferred(ControlRate.REL_SUCCESS, 2);
    runner.assertQueueEmpty();
}
 
Example 11
Source File: SSLContextServiceTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBad3() throws InitializationException {
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    final SSLContextService service = new StandardSSLContextService();
    final Map<String, String> properties = new HashMap<String, String>();
    properties.put(StandardSSLContextService.KEYSTORE.getName(), "src/test/resources/localhost-ks.jks");
    properties.put(StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
    properties.put(StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
    properties.put(StandardSSLContextService.TRUSTSTORE.getName(), "src/test/resources/localhost-ts.jks");
    runner.addControllerService("test-bad3", service, properties);
    runner.assertNotValid(service);
}
 
Example 12
Source File: TestGetHTTP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public final void testDynamicHeaders() throws Exception {
    // set up web service
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(UserAgentTestingServlet.class, "/*");

    // create the service
    TestServer server = new TestServer();
    server.addHandler(handler);

    try {
        server.startServer();

        String destination = server.getUrl();

        // set up NiFi mock controller
        controller = TestRunners.newTestRunner(GetHTTP.class);
        controller.setProperty(GetHTTP.CONNECTION_TIMEOUT, "5 secs");
        controller.setProperty(GetHTTP.URL, destination);
        controller.setProperty(GetHTTP.FILENAME, "testFile");
        controller.setProperty(GetHTTP.ACCEPT_CONTENT_TYPE, "application/json");
        controller.setProperty(GetHTTP.USER_AGENT, "testUserAgent");
        controller.setProperty("Static-Header", "StaticHeaderValue");
        controller.setProperty("EL-Header", "${now()}");

        controller.run();
        controller.assertTransferCount(GetHTTP.REL_SUCCESS, 1);

        // shutdown web service
    } finally {
        server.shutdownServer();
    }
}
 
Example 13
Source File: TestExtractText.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoFlowFile() throws UnsupportedEncodingException {
    final TestRunner testRunner = TestRunners.newTestRunner(new ExtractText());
    testRunner.run();
    testRunner.assertAllFlowFilesTransferred(ExtractText.REL_MATCH, 0);

}
 
Example 14
Source File: TestPutSQL.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleStatementsWithinFlowFile() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(PutSQL.class);
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);
    runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp");

    final String sql = "INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?); " +
            "UPDATE PERSONS SET NAME='George' WHERE ID=?; ";
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("sql.args.1.type", String.valueOf(Types.INTEGER));
    attributes.put("sql.args.1.value", "1");

    attributes.put("sql.args.2.type", String.valueOf(Types.VARCHAR));
    attributes.put("sql.args.2.value", "Mark");

    attributes.put("sql.args.3.type", String.valueOf(Types.INTEGER));
    attributes.put("sql.args.3.value", "84");

    attributes.put("sql.args.4.type", String.valueOf(Types.INTEGER));
    attributes.put("sql.args.4.value", "1");

    runner.enqueue(sql.getBytes(), attributes);
    runner.run();

    // should fail because of the semicolon
    runner.assertAllFlowFilesTransferred(PutSQL.REL_FAILURE, 1);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            final ResultSet rs = stmt.executeQuery("SELECT * FROM PERSONS");
            assertFalse(rs.next());
        }
    }
}
 
Example 15
Source File: TestPutRiemann.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private TestRunner getTestRunner(final boolean failOnWrite) {
  RiemannClient riemannClient = mock(RiemannClient.class);
  when(riemannClient.sendEvents(anyListOf(Proto.Event.class))).thenAnswer(new Answer() {
    @Override
    public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
      List<Proto.Event> events = (List<Proto.Event>) invocationOnMock.getArguments()[0];
      for (Proto.Event event : events) {
        eventStream.add(event);
      }
      IPromise iPromise = mock(IPromise.class);
      if (!failOnWrite) {
        when(iPromise.deref(anyInt(), any(TimeUnit.class))).thenReturn(Proto.Msg.getDefaultInstance());
      } else {
        when(iPromise.deref(anyInt(), any(TimeUnit.class))).thenReturn(null);
      }
      return iPromise;
    }
  });
  when(riemannClient.isConnected()).thenReturn(true);
  PutRiemann riemannProcessor = new PutRiemann();
  riemannProcessor.riemannClient = riemannClient;
  riemannProcessor.transport = PutRiemann.Transport.TCP;

  TestRunner runner = TestRunners.newTestRunner(riemannProcessor);
  runner.setProperty(PutRiemann.RIEMANN_HOST, "localhost");
  runner.setProperty(PutRiemann.RIEMANN_PORT, "5555");
  runner.setProperty(PutRiemann.TRANSPORT_PROTOCOL, "TCP");
  runner.setProperty(PutRiemann.BATCH_SIZE, "100");
  runner.setProperty(PutRiemann.ATTR_SERVICE, "nifi-test-service");
  runner.setProperty(PutRiemann.ATTR_HOST, "${riemann.host}");
  runner.setProperty(PutRiemann.ATTR_TTL, "5");
  runner.setProperty(PutRiemann.ATTR_DESCRIPTION, "test");
  runner.setProperty(PutRiemann.ATTR_TAGS, "tag1, tag2, tag3");
  runner.setProperty(PutRiemann.ATTR_METRIC, "${riemann.metric}");
  runner.setProperty("custom-attribute-1", "${custom.attribute.1}");
  runner.setProperty("custom-attribute-2", "${custom.attribute.2}");
  runner.setProperty("custom-attribute-3", "${custom.attribute.3}");
  return runner;
}
 
Example 16
Source File: TestRouteText.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSeparationStrategyNotKnown() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new RouteText());
    runner.setProperty(RouteText.MATCH_STRATEGY, RouteText.STARTS_WITH);

    runner.assertNotValid();
}
 
Example 17
Source File: TestFetchElasticsearch.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFetchElasticsearchOnTrigger() throws IOException {
    runner = TestRunners.newTestRunner(new FetchElasticsearchTestProcessor(true)); // all docs are found
    runner.setValidateExpressionUsage(true);
    runner.setProperty(AbstractElasticsearchTransportClientProcessor.CLUSTER_NAME, "elasticsearch");
    runner.setProperty(AbstractElasticsearchTransportClientProcessor.HOSTS, "127.0.0.1:9300");
    runner.setProperty(AbstractElasticsearchTransportClientProcessor.PING_TIMEOUT, "5s");
    runner.setProperty(AbstractElasticsearchTransportClientProcessor.SAMPLER_INTERVAL, "5s");

    runner.setProperty(FetchElasticsearch.INDEX, "doc");
    runner.assertNotValid();
    runner.setProperty(FetchElasticsearch.TYPE, "status");
    runner.assertNotValid();
    runner.setProperty(FetchElasticsearch.DOC_ID, "${doc_id}");
    runner.assertValid();

    runner.enqueue(docExample, new HashMap<String, String>() {{
        put("doc_id", "28039652140");
    }});
    runner.run(1, true, true);

    runner.assertAllFlowFilesTransferred(FetchElasticsearch.REL_SUCCESS, 1);
    assertFalse(runner.getProvenanceEvents().isEmpty());
    runner.getProvenanceEvents().forEach(event -> { assertEquals(event.getEventType(), ProvenanceEventType.FETCH); });
    final MockFlowFile out = runner.getFlowFilesForRelationship(FetchElasticsearch.REL_SUCCESS).get(0);
    assertNotNull(out);
    out.assertAttributeEquals("doc_id", "28039652140");
}
 
Example 18
Source File: TestHandleHttpRequest.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout=10000)
public void testRequestAddedToService() throws InitializationException, MalformedURLException, IOException, InterruptedException {
    final TestRunner runner = TestRunners.newTestRunner(HandleHttpRequest.class);
    runner.setProperty(HandleHttpRequest.PORT, "0");

    final MockHttpContextMap contextMap = new MockHttpContextMap();
    runner.addControllerService("http-context-map", contextMap);
    runner.enableControllerService(contextMap);
    runner.setProperty(HandleHttpRequest.HTTP_CONTEXT_MAP, "http-context-map");

    // trigger processor to stop but not shutdown.
    runner.run(1, false);
    try {
        final Thread httpThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final int port = ((HandleHttpRequest) runner.getProcessor()).getPort();
                    final HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:"
                            + port + "/my/path?query=true&value1=value1&value2=&value3&value4=apple=orange").openConnection();
                    connection.setDoOutput(false);
                    connection.setRequestMethod("GET");
                    connection.setRequestProperty("header1", "value1");
                    connection.setRequestProperty("header2", "");
                    connection.setRequestProperty("header3", "apple=orange");
                    connection.setConnectTimeout(3000);
                    connection.setReadTimeout(3000);

                    StreamUtils.copy(connection.getInputStream(), new NullOutputStream());
                } catch (final Throwable t) {
                    t.printStackTrace();
                    Assert.fail(t.toString());
                }
            }
        });
        httpThread.start();

        while ( runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).isEmpty() ) {
            // process the request.
            runner.run(1, false, false);
        }

        runner.assertAllFlowFilesTransferred(HandleHttpRequest.REL_SUCCESS, 1);
        assertEquals(1, contextMap.size());

        final MockFlowFile mff = runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).get(0);
        mff.assertAttributeEquals("http.query.param.query", "true");
        mff.assertAttributeEquals("http.query.param.value1", "value1");
        mff.assertAttributeEquals("http.query.param.value2", "");
        mff.assertAttributeEquals("http.query.param.value3", "");
        mff.assertAttributeEquals("http.query.param.value4", "apple=orange");
        mff.assertAttributeEquals("http.headers.header1", "value1");
        mff.assertAttributeEquals("http.headers.header3", "apple=orange");
    } finally {
        // shut down the server
        runner.run(1, true);
    }
}
 
Example 19
Source File: TestHBase_1_1_2_ClientService.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiplePutsDifferentRow() throws IOException, InitializationException {
    final String tableName = "nifi";
    final String row1 = "row1";
    final String row2 = "row2";
    final String columnFamily = "family1";
    final String columnQualifier = "qualifier1";
    final String content1 = "content1";
    final String content2 = "content2";

    final Collection<PutColumn> columns1 = Collections.singletonList(new PutColumn(columnFamily.getBytes(StandardCharsets.UTF_8),
            columnQualifier.getBytes(StandardCharsets.UTF_8),
            content1.getBytes(StandardCharsets.UTF_8)));
    final PutFlowFile putFlowFile1 = new PutFlowFile(tableName, row1.getBytes(StandardCharsets.UTF_8), columns1, null);

    final Collection<PutColumn> columns2 = Collections.singletonList(new PutColumn(columnFamily.getBytes(StandardCharsets.UTF_8),
            columnQualifier.getBytes(StandardCharsets.UTF_8),
            content2.getBytes(StandardCharsets.UTF_8)));
    final PutFlowFile putFlowFile2 = new PutFlowFile(tableName, row2.getBytes(StandardCharsets.UTF_8), columns2, null);

    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);

    // Mock an HBase Table so we can verify the put operations later
    final Table table = Mockito.mock(Table.class);
    when(table.getName()).thenReturn(TableName.valueOf(tableName));

    // create the controller service and link it to the test processor
    final HBaseClientService service = configureHBaseClientService(runner, table);
    runner.assertValid(service);

    // try to put a multiple cells with different rows
    final HBaseClientService hBaseClientService = runner.getProcessContext().getProperty(TestProcessor.HBASE_CLIENT_SERVICE)
            .asControllerService(HBaseClientService.class);

    hBaseClientService.put(tableName, Arrays.asList(putFlowFile1, putFlowFile2));

    // verify put was only called once
    ArgumentCaptor<List> capture = ArgumentCaptor.forClass(List.class);
    verify(table, times(1)).put(capture.capture());

    // verify there were two puts in the list
    final List<Put> puts = capture.getValue();
    assertEquals(2, puts.size());
}
 
Example 20
Source File: TestPutIgniteCache.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesOneNoKeySecondOkThirdNoBytes() throws IOException, InterruptedException {

    runner = TestRunners.newTestRunner(putIgniteCache);
    runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
    runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
    runner.setProperty(PutIgniteCache.DATA_STREAMER_PER_NODE_BUFFER_SIZE, "1");
    runner.setProperty(PutIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");

    runner.assertValid();
    runner.enqueue("test1".getBytes());
    runner.enqueue("test2".getBytes(),properties1);
    runner.enqueue("".getBytes(),properties2);
    runner.run(1, false, true);

    List<MockFlowFile> successfulFlowFiles = runner.getFlowFilesForRelationship(PutIgniteCache.REL_SUCCESS);
    assertEquals(1, successfulFlowFiles.size());
    List<MockFlowFile> failureFlowFiles = runner.getFlowFilesForRelationship(PutIgniteCache.REL_FAILURE);
    assertEquals(2, failureFlowFiles.size());

    final MockFlowFile out1 = runner.getFlowFilesForRelationship(PutIgniteCache.REL_FAILURE).get(0);

    out1.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_ITEM_NUMBER, "0");
    out1.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_TOTAL_COUNT, "3");
    out1.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_COUNT, "2");
    out1.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_REASON_ATTRIBUTE_KEY, PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_MISSING_KEY_MESSAGE);
    out1.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_ITEM_NUMBER, "0");

    out1.assertContentEquals("test1".getBytes());
    assertEquals("test2", new String(putIgniteCache.getIgniteCache().get("key1")));

    final MockFlowFile out2 = runner.getFlowFilesForRelationship(PutIgniteCache.REL_SUCCESS).get(0);

    out2.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_SUCCESSFUL_ITEM_NUMBER, "0");
    out2.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_TOTAL_COUNT, "3");
    out2.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_ITEM_NUMBER, "1");
    out2.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_SUCCESSFUL_COUNT, "1");

    out2.assertContentEquals("test2".getBytes());
    Assert.assertArrayEquals("test2".getBytes(),(byte[])putIgniteCache.getIgniteCache().get("key1"));

    final MockFlowFile out3 = runner.getFlowFilesForRelationship(PutIgniteCache.REL_FAILURE).get(1);

    out3.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_ITEM_NUMBER, "1");
    out3.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_TOTAL_COUNT, "3");
    out3.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_COUNT, "2");
    out3.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_REASON_ATTRIBUTE_KEY, PutIgniteCache.IGNITE_BATCH_FLOW_FILE_FAILED_ZERO_SIZE_MESSAGE);
    out3.assertAttributeEquals(PutIgniteCache.IGNITE_BATCH_FLOW_FILE_ITEM_NUMBER, "2");

    out3.assertContentEquals("".getBytes());
    assertNull((byte[])putIgniteCache.getIgniteCache().get("key2"));

    runner.shutdown();

}