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

The following examples show how to use org.apache.nifi.util.TestRunner#addControllerService() . 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: TestConvertJSONToSQL.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteWithNullValue() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(ConvertJSONToSQL.class);
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);

    runner.setProperty(ConvertJSONToSQL.CONNECTION_POOL, "dbcp");
    runner.setProperty(ConvertJSONToSQL.TABLE_NAME, "PERSONS");
    runner.setProperty(ConvertJSONToSQL.STATEMENT_TYPE, "DELETE");
    runner.enqueue(Paths.get("src/test/resources/TestConvertJSONToSQL/person-with-null-code.json"));
    runner.run();

    runner.assertTransferCount(ConvertJSONToSQL.REL_ORIGINAL, 1);
    runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), "1");
    runner.assertTransferCount(ConvertJSONToSQL.REL_SQL, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_SQL).get(0);
    out.assertAttributeEquals("sql.args.1.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeEquals("sql.args.1.value", "1");
    out.assertAttributeEquals("sql.args.2.type", String.valueOf(java.sql.Types.VARCHAR));
    out.assertAttributeEquals("sql.args.2.value", "Mark");
    out.assertAttributeEquals("sql.args.3.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeNotExists("sql.args.3.value");

    out.assertContentEquals("DELETE FROM PERSONS WHERE ID = ? AND NAME = ? AND CODE = ?");
}
 
Example 2
Source File: GCPCredentialsServiceTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultingToApplicationDefault() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(MockCredentialsServiceProcessor.class);
    final GCPCredentialsControllerService serviceImpl = new GCPCredentialsControllerService();
    runner.addControllerService("gcpCredentialsProvider", serviceImpl);

    runner.enableControllerService(serviceImpl);

    runner.assertValid(serviceImpl);

    final GCPCredentialsService service = (GCPCredentialsService) runner.getProcessContext()
            .getControllerServiceLookup().getControllerService("gcpCredentialsProvider");

    assertNotNull(service);
    final GoogleCredentials credentials = service.getGoogleCredentials();
    assertNotNull(credentials);
}
 
Example 3
Source File: JMSConnectionFactoryProviderTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
@Test
public void validateFullConfigWithUserLib() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
    JMSConnectionFactoryProvider cfProvider = new JMSConnectionFactoryProvider();
    runner.addControllerService("cfProvider", cfProvider);
    runner.setProperty(cfProvider, JMSConnectionFactoryProvider.BROKER_URI, "myhost:1234");

    runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CLIENT_LIB_DIR_PATH,
            new File("test-lib").getAbsolutePath()); // see README in 'test-lib' dir for more info
    runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
            "org.apache.nifi.jms.testcflib.TestConnectionFactory");
    runner.setProperty(cfProvider, "Foo", "foo");
    runner.setProperty(cfProvider, "Bar", "3");

    runner.enableControllerService(cfProvider);
    runner.assertValid(cfProvider);
    ConnectionFactory cf = cfProvider.getConnectionFactory();
    assertNotNull(cf);
    assertEquals("org.apache.nifi.jms.testcflib.TestConnectionFactory", cf.getClass().getName());
    assertEquals("myhost", this.get("getHost", cf));
    assertEquals(1234, ((Integer) this.get("getPort", cf)).intValue());
    assertEquals("foo", this.get("getFoo", cf));
    assertEquals(3, ((Integer) this.get("getBar", cf)).intValue());
}
 
Example 4
Source File: TestQueryRecord.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamClosedWhenBadData() throws InitializationException {
    final MockRecordParser parser = new MockRecordParser();
    parser.failAfter(0);
    parser.addSchemaField("name", RecordFieldType.STRING);
    parser.addSchemaField("age", RecordFieldType.INT);
    parser.addRecord("Tom", 49);

    final MockRecordWriter writer = new MockRecordWriter("\"name\",\"points\"");

    TestRunner runner = getRunner();
    runner.addControllerService("parser", parser);
    runner.enableControllerService(parser);
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);

    runner.setProperty(REL_NAME, "select name, age from FLOWFILE WHERE name <> ''");
    runner.setProperty(QueryRecord.RECORD_READER_FACTORY, "parser");
    runner.setProperty(QueryRecord.RECORD_WRITER_FACTORY, "writer");

    runner.enqueue(new byte[0]);

    runner.run();

    runner.assertTransferCount(QueryRecord.REL_FAILURE, 1);
}
 
Example 5
Source File: ITPutCloudWatchMetric.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublishWithCredentialsProviderService() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(new PutCloudWatchMetric());
    runner.setValidateExpressionUsage(false);

    final AWSCredentialsProviderControllerService serviceImpl = new AWSCredentialsProviderControllerService();
    runner.addControllerService("awsCredentialsProvider", serviceImpl);

    runner.setProperty(serviceImpl, AbstractAWSCredentialsProviderProcessor.CREDENTIALS_FILE, System.getProperty("user.home") + "/aws-credentials.properties");
    runner.enableControllerService(serviceImpl);

    runner.assertValid(serviceImpl);

    runner.setProperty(PutCloudWatchMetric.NAMESPACE, "Test");
    runner.setProperty(PutCloudWatchMetric.METRIC_NAME, "Test");
    runner.setProperty(PutCloudWatchMetric.VALUE, "1.0");
    runner.setProperty(PutSNS.AWS_CREDENTIALS_PROVIDER_SERVICE, "awsCredentialsProvider");

    runner.enqueue(new byte[] {});
    runner.run();

    runner.assertAllFlowFilesTransferred(PutCloudWatchMetric.REL_SUCCESS, 1);
}
 
Example 6
Source File: DBCPServiceTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMinIdleCannotBeNegative() throws InitializationException {
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    final DBCPConnectionPool service = new DBCPConnectionPool();
    runner.addControllerService("test-good1", service);

    // set embedded Derby database connection url
    runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:derby:" + dbLocation + ";create=true");
    runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester");
    runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD, "testerp");
    runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.apache.derby.jdbc.EmbeddedDriver");
    runner.setProperty(service, DBCPConnectionPool.MAX_WAIT_TIME, "-1");
    runner.setProperty(service, DBCPConnectionPool.MIN_IDLE, "-1");

    runner.assertNotValid(service);
}
 
Example 7
Source File: ConnectionFactoryConfigIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void configureJndiJmsConnectionFactoryControllerService(TestRunner runner, String queueName) throws InitializationException {
    JndiJmsConnectionFactoryProvider cfProvider = new JndiJmsConnectionFactoryProvider();
    runner.addControllerService(CONTROLLER_SERVICE_ID, cfProvider);
    runner.setProperty(cfProvider, JndiJmsConnectionFactoryProperties.JNDI_INITIAL_CONTEXT_FACTORY, PROP_JNDI_INITIAL_CONTEXT_FACTORY);
    runner.setProperty(cfProvider, JndiJmsConnectionFactoryProperties.JNDI_PROVIDER_URL, PROP_JNDI_PROVIDER_URL);
    runner.setProperty(cfProvider, JndiJmsConnectionFactoryProperties.JNDI_CONNECTION_FACTORY_NAME, PROP_JNDI_CONNECTION_FACTORY_NAME);
    runner.enableControllerService(cfProvider);
    runner.setProperty(AbstractJMSProcessor.CF_SERVICE, CONTROLLER_SERVICE_ID);
    runner.setProperty(AbstractJMSProcessor.DESTINATION, queueName);
}
 
Example 8
Source File: JMSConnectionFactoryProviderTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void propertiesSetOnMultipleIbmMqColonPairBrokersConnectionFactory() throws InitializationException {
    TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));

    JMSConnectionFactoryProviderForTest cfProvider = new JMSConnectionFactoryProviderForTest();
    runner.addControllerService(controllerServiceId, cfProvider);

    runner.setProperty(cfProvider, JMSConnectionFactoryProperties.JMS_BROKER_URI, MULTIPLE_IBM_MQ_COLON_PAIR_BROKERS);
    runner.setProperty(cfProvider, JMSConnectionFactoryProperties.JMS_CLIENT_LIBRARIES, dummyResource);
    runner.setProperty(cfProvider, JMSConnectionFactoryProperties.JMS_CONNECTION_FACTORY_IMPL, IBM_MQ_CONNECTION_FACTORY_IMPL);

    runner.enableControllerService(cfProvider);

    assertEquals(cfProvider.getConfiguredProperties(), ImmutableMap.of("connectionNameList", MULTIPLE_IBM_MQ_BROKERS));
}
 
Example 9
Source File: JMSConnectionFactoryProviderTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateNotValidForNonDirectoryPath() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
    JMSConnectionFactoryProvider cfProvider = new JMSConnectionFactoryProvider();
    runner.addControllerService("cfProvider", cfProvider);
    runner.setProperty(cfProvider, JMSConnectionFactoryProvider.BROKER_URI, "myhost:1234");

    runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, "pom.xml");
    runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
            "org.apache.nifi.jms.testcflib.TestConnectionFactory");
    runner.assertNotValid(cfProvider);
}
 
Example 10
Source File: TestPutSQL.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryableFailure() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(PutSQL.class);
    final DBCPService service = new SQLExceptionService(null);
    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_RETRY, 1);
}
 
Example 11
Source File: TestPutHive3QL.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnknownFailureRollbackOnFailure() throws InitializationException, ProcessException {
    final TestRunner runner = TestRunners.newTestRunner(PutHive3QL.class);
    final SQLExceptionService service = new SQLExceptionService(null);
    service.setErrorCode(0);
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);

    runner.setProperty(PutHive3QL.HIVE_DBCP_SERVICE, "dbcp");
    runner.setProperty(RollbackOnFailure.ROLLBACK_ON_FAILURE, "true");

    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("hiveql.args.1.type", String.valueOf(Types.INTEGER));
    attributes.put("hiveql.args.1.value", "1");

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

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

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

    runner.enqueue(sql.getBytes(), attributes);
    try {
        runner.run();
        fail("Should throw ProcessException");
    } catch (AssertionError e) {
        assertTrue(e.getCause() instanceof ProcessException);
    }

    assertEquals(1, runner.getQueueSize().getObjectCount());
    runner.assertAllFlowFilesTransferred(PutHive3QL.REL_RETRY, 0);
}
 
Example 12
Source File: AbstractGCSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected static TestRunner buildNewRunner(Processor processor) throws Exception {
    final GCPCredentialsControllerService credentialsControllerService = new GCPCredentialsControllerService();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    runner.addControllerService("gcpCredentialsControllerService", credentialsControllerService);
    runner.enableControllerService(credentialsControllerService);

    runner.setProperty(AbstractGCSProcessor.GCP_CREDENTIALS_PROVIDER_SERVICE, "gcpCredentialsControllerService");
    runner.setProperty(AbstractGCSProcessor.PROJECT_ID, PROJECT_ID);
    runner.setProperty(AbstractGCSProcessor.RETRY_COUNT, String.valueOf(RETRIES));

    runner.assertValid(credentialsControllerService);

    return runner;
}
 
Example 13
Source File: TestPutSQL.java    From 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");

    recreateTable("PERSONS", createPersons);

    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 14
Source File: TestPutSQL.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithNullParameter() 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 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));

    runner.enqueue("INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?)".getBytes(), attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred(PutSQL.REL_SUCCESS, 1);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            final ResultSet rs = stmt.executeQuery("SELECT * FROM PERSONS");
            assertTrue(rs.next());
            assertEquals(1, rs.getInt(1));
            assertEquals("Mark", rs.getString(2));
            assertEquals(0, rs.getInt(3));
            assertFalse(rs.next());
        }
    }
}
 
Example 15
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulConsumeAndTransferToSuccess() throws Exception {
    final String destinationName = "cooQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        final Map<String, String> senderAttributes = new HashMap<>();
        senderAttributes.put("filename", "message.txt");
        senderAttributes.put("attribute_from_sender", "some value");
        sender.publish(destinationName, "Hey dude!".getBytes(), senderAttributes);
        TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());
        JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
        when(cs.getIdentifier()).thenReturn("cfProvider");
        when(cs.getConnectionFactory()).thenReturn(jmsTemplate.getConnectionFactory());
        runner.addControllerService("cfProvider", cs);
        runner.enableControllerService(cs);

        runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
        runner.setProperty(ConsumeJMS.DESTINATION, destinationName);
        runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.QUEUE);

        runner.run(1, false);
        //
        final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
        assertNotNull(successFF);
        successFF.assertAttributeExists(JmsHeaders.DESTINATION);
        successFF.assertAttributeEquals(JmsHeaders.DESTINATION, destinationName);
        successFF.assertAttributeExists("filename");
        successFF.assertAttributeEquals("filename", "message.txt");
        successFF.assertAttributeExists("attribute_from_sender");
        successFF.assertAttributeEquals("attribute_from_sender", "some value");
        successFF.assertAttributeExists("jms.messagetype");
        successFF.assertAttributeEquals("jms.messagetype", "BytesMessage");
        successFF.assertContentEquals("Hey dude!".getBytes());
        String sourceDestination = successFF.getAttribute(ConsumeJMS.JMS_SOURCE_DESTINATION_NAME);
        assertNotNull(sourceDestination);
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 16
Source File: TestPutHBaseCell.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleFlowFileWithELOneMissingAttributes() throws IOException, InitializationException {
    final PutHBaseCell proc = new PutHBaseCell();
    final TestRunner runner = getTestRunnerWithEL(proc);
    runner.setProperty(PutHBaseCell.BATCH_SIZE, "10");

    final MockHBaseClientService hBaseClient = new MockHBaseClientService();
    runner.addControllerService("hbaseClient", hBaseClient);
    runner.enableControllerService(hBaseClient);
    runner.setProperty(PutHBaseCell.HBASE_CLIENT_SERVICE, "hbaseClient");

    getHBaseClientService(runner);

    // this one will go to failure
    final String content = "some content";
    runner.enqueue(content.getBytes("UTF-8"), new HashMap<String, String>());

    // this will go to success
    final String content2 = "some content2";
    final Map<String, String> attributes = getAttributeMapWithEL("table", "row", "cf", "cq");
    runner.enqueue(content2.getBytes("UTF-8"), attributes);

    runner.run();
    runner.assertTransferCount(PutHBaseCell.REL_SUCCESS, 1);
    runner.assertTransferCount(PutHBaseCell.REL_FAILURE, 1);

    assertEquals(1, runner.getProvenanceEvents().size());
}
 
Example 17
Source File: TestPutSolrContentStream.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpsUrlShouldRequireSSLContext() throws InitializationException {
    final TestRunner runner = TestRunners.newTestRunner(PutSolrContentStream.class);
    runner.setProperty(PutSolrContentStream.SOLR_TYPE, PutSolrContentStream.SOLR_TYPE_STANDARD.getValue());
    runner.setProperty(PutSolrContentStream.SOLR_LOCATION, "https://localhost:8443/solr");
    runner.assertNotValid();

    final SSLContextService sslContextService = new MockSSLContextService();
    runner.addControllerService("ssl-context", sslContextService);
    runner.enableControllerService(sslContextService);

    runner.setProperty(PutSolrContentStream.SSL_CONTEXT_SERVICE, "ssl-context");
    runner.assertValid();
}
 
Example 18
Source File: TestPutHive3QL.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testInvalidStatement() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(PutHive3QL.class);
    final File tempDir = folder.getRoot();
    final File dbDir = new File(tempDir, "db");
    final DBCPService service = new MockDBCPService(dbDir.getAbsolutePath());
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            stmt.executeUpdate(createPersons);
        }
    }

    runner.setProperty(PutHive3QL.HIVE_DBCP_SERVICE, "dbcp");

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

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

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

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

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

    // should fail because of the table is invalid
    runner.assertAllFlowFilesTransferred(PutHive3QL.REL_FAILURE, 1);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            final ResultSet rs = stmt.executeQuery("SELECT * FROM PERSONS");
            assertTrue(rs.next());
        }
    }
}
 
Example 19
Source File: TestPutHive_1_1QL.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFailInMiddleWithBadNumberFormat() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(PutHive_1_1QL.class);
    final File tempDir = folder.getRoot();
    final File dbDir = new File(tempDir, "db");
    final DBCPService service = new MockDBCPService(dbDir.getAbsolutePath());
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            stmt.executeUpdate(createPersonsAutoId);
        }
    }

    runner.setProperty(PutHive_1_1QL.HIVE_DBCP_SERVICE, "dbcp");

    final Map<String, String> goodAttributes = new HashMap<>();
    goodAttributes.put("hiveql.args.1.type", String.valueOf(Types.INTEGER));
    goodAttributes.put("hiveql.args.1.value", "84");

    final Map<String, String> badAttributes = new HashMap<>();
    badAttributes.put("hiveql.args.1.type", String.valueOf(Types.INTEGER));
    badAttributes.put("hiveql.args.1.value", "NOT_NUMBER");

    final byte[] data = "INSERT INTO PERSONS (NAME, CODE) VALUES ('Mark', ?)".getBytes();
    runner.enqueue(data, goodAttributes);
    runner.enqueue(data, badAttributes);
    runner.enqueue(data, goodAttributes);
    runner.enqueue(data, goodAttributes);
    runner.run();

    runner.assertTransferCount(PutHive_1_1QL.REL_SUCCESS, 3);
    runner.assertTransferCount(PutHive_1_1QL.REL_FAILURE, 1);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            final ResultSet rs = stmt.executeQuery("SELECT * FROM PERSONS");
            assertTrue(rs.next());
            assertEquals(1, rs.getInt(1));
            assertEquals("Mark", rs.getString(2));
            assertEquals(84, rs.getInt(3));
            assertTrue(rs.next());
            assertTrue(rs.next());
            assertFalse(rs.next());
        }
    }
}
 
Example 20
Source File: ITFetchS3Object.java    From localization_nifi with Apache License 2.0 3 votes vote down vote up
@Test
public void testFetchS3ObjectUsingCredentialsProviderService() throws Throwable {
    putTestFile("test-file", getFileFromResourceName(SAMPLE_FILE_RESOURCE_NAME));

    final TestRunner runner = TestRunners.newTestRunner(new FetchS3Object());

    final AWSCredentialsProviderControllerService serviceImpl = new AWSCredentialsProviderControllerService();

    runner.addControllerService("awsCredentialsProvider", serviceImpl);

    runner.setProperty(serviceImpl, AbstractAWSProcessor.CREDENTIALS_FILE, System.getProperty("user.home") + "/aws-credentials.properties");
    runner.enableControllerService(serviceImpl);
    runner.assertValid(serviceImpl);

    runner.setProperty(FetchS3Object.AWS_CREDENTIALS_PROVIDER_SERVICE, "awsCredentialsProvider");
    runner.setProperty(FetchS3Object.REGION, REGION);
    runner.setProperty(FetchS3Object.BUCKET, BUCKET_NAME);

    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "test-file");
    runner.enqueue(new byte[0], attrs);

    runner.run(1);

    runner.assertAllFlowFilesTransferred(FetchS3Object.REL_SUCCESS, 1);

}