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

The following examples show how to use org.apache.nifi.util.TestRunner#setIncomingConnection() . 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: TestDeleteHDFS.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessfulDelete() throws Exception {
    Path filePath = new Path("/some/path/to/file.txt");
    when(mockFileSystem.exists(any(Path.class))).thenReturn(true);
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(false);
    runner.assertNotValid();
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, filePath.toString());
    runner.assertValid();
    runner.run();
    runner.assertAllFlowFilesTransferred(DeleteHDFS.REL_SUCCESS);
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, 1);
    FlowFile flowFile = runner.getFlowFilesForRelationship(DeleteHDFS.REL_SUCCESS).get(0);
    assertEquals(filePath.getName(), flowFile.getAttribute("filename"));
    assertEquals(filePath.getParent().toString(), flowFile.getAttribute("path"));
}
 
Example 2
Source File: TestDeleteHDFS.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsuccessfulDelete() throws Exception {
    Path filePath = new Path("/some/path/to/file.txt");
    when(mockFileSystem.exists(any(Path.class))).thenReturn(false);
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(false);
    runner.assertNotValid();
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, filePath.toString());
    runner.assertValid();
    runner.run();
    runner.assertAllFlowFilesTransferred(DeleteHDFS.REL_FAILURE);
    runner.assertTransferCount(DeleteHDFS.REL_FAILURE, 1);
    FlowFile flowFile = runner.getFlowFilesForRelationship(DeleteHDFS.REL_FAILURE).get(0);
    assertEquals(filePath.getName(), flowFile.getAttribute("filename"));
    assertEquals(filePath.getParent().toString(), flowFile.getAttribute("path"));
}
 
Example 3
Source File: TestDeleteHDFS.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessfulDelete() throws Exception {
    Path filePath = new Path("/some/path/to/file.txt");
    when(mockFileSystem.exists(any(Path.class))).thenReturn(true);
    when(mockFileSystem.getUri()).thenReturn(new URI("hdfs://0.example.com:8020"));
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(false);
    runner.assertNotValid();
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, filePath.toString());
    runner.assertValid();
    runner.run();
    // Even if there's no incoming relationship, a FlowFile is created to indicate which path is deleted.
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, 1);
    runner.assertTransferCount(DeleteHDFS.REL_FAILURE, 0);

    final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
    assertEquals(1, provenanceEvents.size());
    assertEquals(ProvenanceEventType.REMOTE_INVOCATION, provenanceEvents.get(0).getEventType());
    assertEquals("hdfs://0.example.com:8020/some/path/to/file.txt", provenanceEvents.get(0).getTransitUri());
}
 
Example 4
Source File: TestDeleteHDFS.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testGlobDelete() throws Exception {
    Path glob = new Path("/data/for/2017/08/05/*");
    int fileCount = 300;
    FileStatus[] fileStatuses = new FileStatus[fileCount];
    for (int i = 0; i < fileCount; i++) {
        Path file = new Path("/data/for/2017/08/05/file" + i);
        FileStatus fileStatus = mock(FileStatus.class);
        when(fileStatus.getPath()).thenReturn(file);
        fileStatuses[i] = fileStatus;
    }
    when(mockFileSystem.exists(any(Path.class))).thenReturn(true);
    when(mockFileSystem.globStatus(any(Path.class))).thenReturn(fileStatuses);
    when(mockFileSystem.getUri()).thenReturn(new URI("hdfs://0.example.com:8020"));
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(false);
    runner.assertNotValid();
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, glob.toString());
    runner.assertValid();
    runner.run();
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, 1);
}
 
Example 5
Source File: TestDeleteHDFS.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testGlobDeleteFromIncomingFlowFile() throws Exception {
    Path glob = new Path("/data/for/2017/08/05/*");
    int fileCount = 300;
    FileStatus[] fileStatuses = new FileStatus[fileCount];
    for (int i = 0; i < fileCount; i++) {
        Path file = new Path("/data/for/2017/08/05/file" + i);
        FileStatus fileStatus = mock(FileStatus.class);
        when(fileStatus.getPath()).thenReturn(file);
        fileStatuses[i] = fileStatus;
    }
    when(mockFileSystem.exists(any(Path.class))).thenReturn(true);
    when(mockFileSystem.globStatus(any(Path.class))).thenReturn(fileStatuses);
    when(mockFileSystem.getUri()).thenReturn(new URI("hdfs://0.example.com:8020"));
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(true);
    Map<String, String> attributes = Maps.newHashMap();
    runner.enqueue("foo", attributes);
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, glob.toString());
    runner.assertValid();
    runner.run();
    runner.assertAllFlowFilesTransferred(DeleteHDFS.REL_SUCCESS);
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, 1);
}
 
Example 6
Source File: TestDeleteHDFS.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoFlowFilesWithIncomingConnection() throws Exception {
    Path filePath = new Path("${hdfs.file}");
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, filePath.toString());
    runner.setIncomingConnection(true);
    runner.run();
    runner.assertQueueEmpty();
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, 0);
    runner.assertTransferCount(DeleteHDFS.REL_FAILURE, 0);
}
 
Example 7
Source File: TestDeleteHDFS.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testGlobDelete() throws Exception {
    Path glob = new Path("/data/for/2017/08/05/*");
    int fileCount = 300;
    FileStatus[] fileStatuses = new FileStatus[fileCount];
    for (int i = 0; i < fileCount; i++) {
        Path file = new Path("/data/for/2017/08/05/file" + i);
        FileStatus fileStatus = mock(FileStatus.class);
        when(fileStatus.getPath()).thenReturn(file);
        fileStatuses[i] = fileStatus;
    }
    when(mockFileSystem.exists(any(Path.class))).thenReturn(true);
    when(mockFileSystem.globStatus(any(Path.class))).thenReturn(fileStatuses);
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(false);
    runner.assertNotValid();
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, glob.toString());
    runner.assertValid();
    runner.run();
    runner.assertAllFlowFilesTransferred(DeleteHDFS.REL_SUCCESS);
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, fileCount);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(DeleteHDFS.REL_SUCCESS);
    for (int i = 0; i < fileCount; i++) {
        FlowFile flowFile = flowFiles.get(i);
        assertEquals("file" + i, flowFile.getAttribute("filename"));
        assertEquals("/data/for/2017/08/05", flowFile.getAttribute("path"));
    }
}
 
Example 8
Source File: TestDeleteHDFS.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoFlowFilesWithIncomingConnection() throws Exception {
    Path filePath = new Path("${hdfs.file}");
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, filePath.toString());
    runner.setIncomingConnection(true);
    runner.run();
    runner.assertQueueEmpty();
    runner.assertTransferCount(DeleteHDFS.REL_SUCCESS, 0);
    runner.assertTransferCount(DeleteHDFS.REL_FAILURE, 0);
}
 
Example 9
Source File: TestDeleteHDFS.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsuccessfulDelete() throws Exception {
    Path filePath = new Path("/some/path/to/file.txt");
    when(mockFileSystem.exists(any(Path.class))).thenReturn(false);
    DeleteHDFS deleteHDFS = new TestableDeleteHDFS(kerberosProperties, mockFileSystem);
    TestRunner runner = TestRunners.newTestRunner(deleteHDFS);
    runner.setIncomingConnection(false);
    runner.assertNotValid();
    runner.setProperty(DeleteHDFS.FILE_OR_DIRECTORY, filePath.toString());
    runner.assertValid();
    runner.run();
    runner.assertTransferCount(DeleteHDFS.REL_FAILURE, 0);
}
 
Example 10
Source File: GetMongoIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testDatabaseEL() {
    runner.clearTransferState();
    runner.removeVariable("collection");
    runner.removeVariable("db");
    runner.setIncomingConnection(true);

    String[] collections = new String[] { "a", "b", "c" };
    String[] dbs = new String[] { "el_db_1", "el_db_2", "el_db_3" };
    String query = "{}";

    for (int x = 0; x < collections.length; x++) {
        MongoDatabase db = mongoClient.getDatabase(dbs[x]);
        db.getCollection(collections[x])
            .insertOne(new Document().append("msg", "Hello, World"));

        Map<String, String> attrs = new HashMap<>();
        attrs.put("db", dbs[x]);
        attrs.put("collection", collections[x]);
        runner.enqueue(query, attrs);
        runner.run();

        db.drop();

        runner.assertTransferCount(GetMongo.REL_SUCCESS, 1);
        runner.assertTransferCount(GetMongo.REL_ORIGINAL, 1);
        runner.assertTransferCount(GetMongo.REL_FAILURE, 0);
        runner.clearTransferState();
    }

    Map<String, Map<String, String>> vals = new HashMap<String, Map<String, String>>(){{
        put("Collection", new HashMap<String, String>(){{
            put("db", "getmongotest");
            put("collection", "");
        }});
        put("Database", new HashMap<String, String>(){{
            put("db", "");
            put("collection", "test");
        }});
    }};

    TestRunner tmpRunner;

    for (Map.Entry<String, Map<String, String>> entry : vals.entrySet()) {
        // Creating a new runner for each set of attributes map since every subsequent runs will attempt to take the top most enqueued FlowFile
        tmpRunner = TestRunners.newTestRunner(GetMongo.class);
        tmpRunner.setProperty(AbstractMongoProcessor.URI, MONGO_URI);
        tmpRunner.setProperty(AbstractMongoProcessor.DATABASE_NAME, DB_NAME);
        tmpRunner.setProperty(AbstractMongoProcessor.COLLECTION_NAME, COLLECTION_NAME);
        tmpRunner.setIncomingConnection(true);

        tmpRunner.enqueue("{ }", entry.getValue());

        try {
            tmpRunner.run();
        } catch (Throwable ex) {
            Throwable cause = ex.getCause();
            Assert.assertTrue(cause instanceof ProcessException);
            Assert.assertTrue(entry.getKey(), ex.getMessage().contains(entry.getKey()));
        }
        tmpRunner.clearTransferState();

    }
}