Search in sources :

Example 1 with MockConfigurationContext

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

the class TestServerAndClient method testClientTermination.

@Test
public void testClientTermination() throws InitializationException, IOException, InterruptedException {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final MockControllerServiceInitializationContext serverInitContext = new MockControllerServiceInitializationContext(server, "server");
    server.initialize(serverInitContext);
    final Map<PropertyDescriptor, String> serverProperties = new HashMap<>();
    final MockConfigurationContext serverContext = new MockConfigurationContext(serverProperties, serverInitContext.getControllerServiceLookup());
    server.startServer(serverContext);
    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();
    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals(null, original);
    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals("test", originalAfterPut);
    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);
    client = null;
    clientInitContext = null;
    clientContext = null;
    Thread.sleep(2000);
    System.gc();
    server.shutdownServer();
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 2 with MockConfigurationContext

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

the class TestServerAndClient method testOptimisticLock.

@Test
public void testOptimisticLock() throws Exception {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client1 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext1 = new MockControllerServiceInitializationContext(client1, "client1");
    client1.initialize(clientInitContext1);
    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(client2, "client2");
    client1.initialize(clientInitContext2);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext1 = new MockConfigurationContext(clientProperties, clientInitContext1.getControllerServiceLookup());
    client1.cacheConfig(clientContext1);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties, clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    final Serializer<String> stringSerializer = new StringSerializer();
    final Deserializer<String> stringDeserializer = new StringDeserializer();
    final String key = "test-optimistic-lock";
    // Ensure there's no existing key
    assertFalse(client1.containsKey(key, stringSerializer));
    assertNull(client1.fetch(key, stringSerializer, stringDeserializer));
    // Client 1 inserts the key.
    client1.put(key, "valueC1-0", stringSerializer, stringSerializer);
    // Client 1 and 2 fetch the key
    AtomicCacheEntry<String, String, Long> c1 = client1.fetch(key, stringSerializer, stringDeserializer);
    AtomicCacheEntry<String, String, Long> c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals(new Long(0), c1.getRevision().orElse(0L));
    assertEquals("valueC1-0", c1.getValue());
    assertEquals(new Long(0), c2.getRevision().orElse(0L));
    assertEquals("valueC1-0", c2.getValue());
    // Client 1 replace
    c1.setValue("valueC1-1");
    boolean c1Result = client1.replace(c1, stringSerializer, stringSerializer);
    assertTrue("C1 should be able to replace the key", c1Result);
    // Client 2 replace with the old revision
    c2.setValue("valueC2-1");
    boolean c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertFalse("C2 shouldn't be able to replace the key", c2Result);
    // Client 2 fetch the key again
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC1-1", c2.getValue());
    assertEquals(new Long(1), c2.getRevision().orElse(0L));
    // Now, Client 2 knows the correct revision so it can replace the key
    c2.setValue("valueC2-2");
    c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertTrue("C2 should be able to replace the key", c2Result);
    // Assert the cache
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC2-2", c2.getValue());
    assertEquals(new Long(2), c2.getRevision().orElse(0L));
    client1.close();
    client2.close();
    server.shutdownServer();
}
Also used : Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 3 with MockConfigurationContext

use of org.apache.nifi.util.MockConfigurationContext 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 4 with MockConfigurationContext

use of org.apache.nifi.util.MockConfigurationContext 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)

Example 5 with MockConfigurationContext

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

the class ITReportLineageToAtlas method test.

private void test(TestConfiguration tc) throws InitializationException, IOException {
    final ReportLineageToAtlas reportingTask = new ReportLineageToAtlas();
    final MockComponentLog logger = new MockComponentLog("reporting-task-id", reportingTask);
    final ReportingInitializationContext initializationContext = mock(ReportingInitializationContext.class);
    when(initializationContext.getLogger()).thenReturn(logger);
    final ConfigurationContext configurationContext = new MockConfigurationContext(tc.properties, null);
    final ValidationContext validationContext = mock(ValidationContext.class);
    when(validationContext.getProperty(any())).then(invocation -> new MockPropertyValue(tc.properties.get(invocation.getArguments()[0])));
    final ReportingContext reportingContext = mock(ReportingContext.class);
    final MockStateManager stateManager = new MockStateManager(reportingTask);
    final EventAccess eventAccess = mock(EventAccess.class);
    when(reportingContext.getProperties()).thenReturn(tc.properties);
    when(reportingContext.getProperty(any())).then(invocation -> new MockPropertyValue(tc.properties.get(invocation.getArguments()[0])));
    when(reportingContext.getStateManager()).thenReturn(stateManager);
    when(reportingContext.getEventAccess()).thenReturn(eventAccess);
    when(eventAccess.getGroupStatus(eq("root"))).thenReturn(tc.rootPgStatus);
    final ProvenanceRepository provenanceRepository = mock(ProvenanceRepository.class);
    when(eventAccess.getControllerStatus()).thenReturn(tc.rootPgStatus);
    when(eventAccess.getProvenanceRepository()).thenReturn(provenanceRepository);
    when(eventAccess.getProvenanceEvents(eq(-1L), anyInt())).thenReturn(tc.provenanceRecords);
    when(provenanceRepository.getMaxEventId()).thenReturn((long) tc.provenanceRecords.size() - 1);
    when(provenanceRepository.getEvent(anyLong())).then(invocation -> tc.provenanceRecords.get(((Long) invocation.getArguments()[0]).intValue()));
    // To mock this async method invocations, keep the requested event ids in a stack.
    final ComputeLineageSubmission lineageComputationSubmission = mock(ComputeLineageSubmission.class);
    when(provenanceRepository.submitLineageComputation(anyLong(), any())).thenAnswer(invocation -> {
        requestedLineageComputationIds.push((Long) invocation.getArguments()[0]);
        return lineageComputationSubmission;
    });
    when(lineageComputationSubmission.getResult()).then(invocation -> tc.lineageResults.get(requestedLineageComputationIds.pop()));
    final ComputeLineageSubmission expandParentsSubmission = mock(ComputeLineageSubmission.class);
    when(provenanceRepository.submitExpandParents(anyLong(), any())).thenAnswer(invocation -> {
        requestedExpandParentsIds.push(((Long) invocation.getArguments()[0]));
        return expandParentsSubmission;
    });
    when(expandParentsSubmission.getResult()).then(invocation -> tc.parentLineageResults.get(requestedExpandParentsIds.pop()));
    tc.properties.put(ATLAS_NIFI_URL, "http://localhost:8080/nifi");
    tc.properties.put(ATLAS_URLS, TARGET_ATLAS_URL);
    tc.properties.put(ATLAS_USER, "admin");
    tc.properties.put(ATLAS_PASSWORD, "admin");
    tc.properties.put(new PropertyDescriptor.Builder().name("hostnamePattern.example").dynamic(true).build(), ".*");
    reportingTask.initialize(initializationContext);
    reportingTask.validate(validationContext);
    reportingTask.setup(configurationContext);
    reportingTask.onTrigger(reportingContext);
    reportingTask.onUnscheduled();
    reportingTask.onStopped();
}
Also used : EventAccess(org.apache.nifi.reporting.EventAccess) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) MockComponentLog(org.apache.nifi.util.MockComponentLog) ComputeLineageSubmission(org.apache.nifi.provenance.lineage.ComputeLineageSubmission) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ValidationContext(org.apache.nifi.components.ValidationContext) ReportingContext(org.apache.nifi.reporting.ReportingContext) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) MockStateManager(org.apache.nifi.state.MockStateManager) Matchers.anyLong(org.mockito.Matchers.anyLong) ProvenanceRepository(org.apache.nifi.provenance.ProvenanceRepository)

Aggregations

MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)12 HashMap (java.util.HashMap)11 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)10 Test (org.junit.Test)8 MockControllerServiceInitializationContext (org.apache.nifi.util.MockControllerServiceInitializationContext)6 DistributedMapCacheClientService (org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService)5 ConfigurationContext (org.apache.nifi.controller.ConfigurationContext)4 DistributedMapCacheServer (org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer)4 Processor (org.apache.nifi.processor.Processor)3 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)3 TestRunner (org.apache.nifi.util.TestRunner)3 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)2 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)2 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)2 IOException (java.io.IOException)2 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)2 MockStateManager (org.apache.nifi.state.MockStateManager)2 MockComponentLog (org.apache.nifi.util.MockComponentLog)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1