Search in sources :

Example 1 with MockVariableRegistry

use of org.apache.nifi.util.MockVariableRegistry in project nifi by apache.

the class HiveConnectionPoolTest method testExpressionLanguageSupport.

@Test
public void testExpressionLanguageSupport() throws Exception {
    final String URL = "jdbc:hive2://localhost:10000/default";
    final String USER = "user";
    final String PASS = "pass";
    final int MAX_CONN = 7;
    // 10000 milliseconds
    final String MAX_WAIT = "10 sec";
    final String CONF = "/path/to/hive-site.xml";
    hiveConnectionPool = new HiveConnectionPool();
    Map<PropertyDescriptor, String> props = new HashMap<PropertyDescriptor, String>() {

        {
            put(HiveConnectionPool.DATABASE_URL, "${url}");
            put(HiveConnectionPool.DB_USER, "${username}");
            put(HiveConnectionPool.DB_PASSWORD, "${password}");
            put(HiveConnectionPool.MAX_TOTAL_CONNECTIONS, "${maxconn}");
            put(HiveConnectionPool.MAX_WAIT_TIME, "${maxwait}");
            put(HiveConnectionPool.HIVE_CONFIGURATION_RESOURCES, "${hiveconf}");
        }
    };
    MockVariableRegistry registry = new MockVariableRegistry();
    registry.setVariable(new VariableDescriptor("url"), URL);
    registry.setVariable(new VariableDescriptor("username"), USER);
    registry.setVariable(new VariableDescriptor("password"), PASS);
    registry.setVariable(new VariableDescriptor("maxconn"), Integer.toString(MAX_CONN));
    registry.setVariable(new VariableDescriptor("maxwait"), MAX_WAIT);
    registry.setVariable(new VariableDescriptor("hiveconf"), CONF);
    MockConfigurationContext context = new MockConfigurationContext(props, null, registry);
    hiveConnectionPool.onConfigured(context);
    Field dataSourceField = HiveConnectionPool.class.getDeclaredField("dataSource");
    dataSourceField.setAccessible(true);
    basicDataSource = (BasicDataSource) dataSourceField.get(hiveConnectionPool);
    assertEquals(URL, basicDataSource.getUrl());
    assertEquals(USER, basicDataSource.getUsername());
    assertEquals(PASS, basicDataSource.getPassword());
    assertEquals(MAX_CONN, basicDataSource.getMaxActive());
    assertEquals(10000L, basicDataSource.getMaxWait());
    assertEquals(URL, hiveConnectionPool.getConnectionURL());
}
Also used : Field(java.lang.reflect.Field) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) HashMap(java.util.HashMap) MockVariableRegistry(org.apache.nifi.util.MockVariableRegistry) VariableDescriptor(org.apache.nifi.registry.VariableDescriptor) Test(org.junit.Test)

Example 2 with MockVariableRegistry

use of org.apache.nifi.util.MockVariableRegistry in project nifi by apache.

the class MetricsReportingTaskTest method setUp.

/**
 * Set up the test environment and mock behaviour. This includes registering {@link #reporterServiceStub} in the
 * different contexts, overriding {@link MetricsReportingTask#currentStatusReference} and instantiating the test
 * subject.
 */
@Before
public void setUp() throws Exception {
    Map<String, ControllerService> services = new HashMap<>();
    services.put(REPORTER_SERVICE_IDENTIFIER, reporterServiceStub);
    testedReportingTask = new MetricsReportingTask();
    reportingContextStub = new MockReportingContext(services, new MockStateManager(testedReportingTask), new MockVariableRegistry());
    rootGroupStatus = new ProcessGroupStatus();
    innerGroupStatus = new ProcessGroupStatus();
    when(reporterServiceStub.createReporter(any())).thenReturn(reporterMock);
    when(reporterServiceStub.getIdentifier()).thenReturn(REPORTER_SERVICE_IDENTIFIER);
    reportingContextStub.setProperty(MetricsReportingTask.REPORTER_SERVICE.getName(), REPORTER_SERVICE_IDENTIFIER);
    reportingContextStub.addControllerService(reporterServiceStub, REPORTER_SERVICE_IDENTIFIER);
    configurationContextStub = new MockConfigurationContext(reportingContextStub.getProperties(), reportingContextStub.getControllerServiceLookup());
    reportingInitContextStub = new MockReportingInitializationContext(TEST_INIT_CONTEXT_ID, TEST_INIT_CONTEXT_NAME, new MockComponentLog(TEST_TASK_ID, testedReportingTask));
}
Also used : ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) HashMap(java.util.HashMap) MockStateManager(org.apache.nifi.state.MockStateManager) MockComponentLog(org.apache.nifi.util.MockComponentLog) MockVariableRegistry(org.apache.nifi.util.MockVariableRegistry) MockReportingInitializationContext(org.apache.nifi.util.MockReportingInitializationContext) ControllerService(org.apache.nifi.controller.ControllerService) MockReportingContext(org.apache.nifi.util.MockReportingContext) Before(org.junit.Before)

Aggregations

HashMap (java.util.HashMap)2 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)2 MockVariableRegistry (org.apache.nifi.util.MockVariableRegistry)2 Field (java.lang.reflect.Field)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 ControllerService (org.apache.nifi.controller.ControllerService)1 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)1 VariableDescriptor (org.apache.nifi.registry.VariableDescriptor)1 MockStateManager (org.apache.nifi.state.MockStateManager)1 MockComponentLog (org.apache.nifi.util.MockComponentLog)1 MockReportingContext (org.apache.nifi.util.MockReportingContext)1 MockReportingInitializationContext (org.apache.nifi.util.MockReportingInitializationContext)1 Before (org.junit.Before)1 Test (org.junit.Test)1