Search in sources :

Example 1 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi by apache.

the class TestStandardProcessScheduler method setup.

@Before
public void setup() throws InitializationException {
    final Map<String, String> overrideProperties = new HashMap<>();
    overrideProperties.put(NiFiProperties.ADMINISTRATIVE_YIELD_DURATION, "2 millis");
    overrideProperties.put(NiFiProperties.PROCESSOR_SCHEDULING_TIMEOUT, "10 millis");
    this.nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, overrideProperties);
    // load the system bundle
    systemBundle = SystemBundle.create(nifiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    scheduler = new StandardProcessScheduler(new FlowEngine(1, "Unit Test", true), Mockito.mock(FlowController.class), null, stateMgrProvider, nifiProperties);
    scheduler.setSchedulingAgent(SchedulingStrategy.TIMER_DRIVEN, Mockito.mock(SchedulingAgent.class));
    reportingTask = new TestReportingTask();
    final ReportingInitializationContext config = new StandardReportingInitializationContext(UUID.randomUUID().toString(), "Test", SchedulingStrategy.TIMER_DRIVEN, "5 secs", Mockito.mock(ComponentLog.class), null, nifiProperties, null);
    reportingTask.initialize(config);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(null, variableRegistry);
    final TerminationAwareLogger logger = Mockito.mock(TerminationAwareLogger.class);
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
    final LoggableComponent<ReportingTask> loggableComponent = new LoggableComponent<>(reportingTask, systemBundle.getBundleDetails().getCoordinate(), logger);
    taskNode = new StandardReportingTaskNode(loggableComponent, UUID.randomUUID().toString(), null, scheduler, validationContextFactory, new StandardComponentVariableRegistry(variableRegistry), reloadComponent);
    controller = Mockito.mock(FlowController.class);
    final ConcurrentMap<String, ProcessorNode> processorMap = new ConcurrentHashMap<>();
    Mockito.doAnswer(new Answer<ProcessorNode>() {

        @Override
        public ProcessorNode answer(InvocationOnMock invocation) throws Throwable {
            final String id = invocation.getArgumentAt(0, String.class);
            return processorMap.get(id);
        }
    }).when(controller).getProcessorNode(Mockito.anyString());
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final ProcessorNode procNode = invocation.getArgumentAt(0, ProcessorNode.class);
            processorMap.putIfAbsent(procNode.getIdentifier(), procNode);
            return null;
        }
    }).when(controller).onProcessorAdded(Mockito.any(ProcessorNode.class));
    rootGroup = new MockProcessGroup(controller);
    Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(rootGroup);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) ValidationContextFactory(org.apache.nifi.controller.ValidationContextFactory) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) MockProcessGroup(org.apache.nifi.controller.service.mock.MockProcessGroup) StandardProcessorNode(org.apache.nifi.controller.StandardProcessorNode) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) FlowController(org.apache.nifi.controller.FlowController) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractReportingTask(org.apache.nifi.reporting.AbstractReportingTask) ReportingTask(org.apache.nifi.reporting.ReportingTask) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) FlowEngine(org.apache.nifi.engine.FlowEngine) ReloadComponent(org.apache.nifi.controller.ReloadComponent) ComponentLog(org.apache.nifi.logging.ComponentLog) LoggableComponent(org.apache.nifi.controller.LoggableComponent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TerminationAwareLogger(org.apache.nifi.controller.TerminationAwareLogger) Before(org.junit.Before)

Example 2 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi by apache.

the class ReportingTaskingInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ReportingTask reportingTask = (ReportingTask) component;
    ReportingInitializationContext context = new MockReportingInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        reportingTask.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MockReportingInitializationContext(org.apache.nifi.mock.MockReportingInitializationContext) MockReportingInitializationContext(org.apache.nifi.mock.MockReportingInitializationContext) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 3 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi by apache.

the class TestAmbariReportingTask method testOnTrigger.

@Test
public void testOnTrigger() throws InitializationException, IOException {
    final String metricsUrl = "http://myambari:6188/ws/v1/timeline/metrics";
    final String applicationId = "NIFI";
    final String hostName = "localhost";
    // create the jersey client mocks for handling the post
    final Client client = Mockito.mock(Client.class);
    final WebTarget target = Mockito.mock(WebTarget.class);
    final Invocation.Builder builder = Mockito.mock(Invocation.Builder.class);
    final Response response = Mockito.mock(Response.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(client.target(metricsUrl)).thenReturn(target);
    Mockito.when(target.request()).thenReturn(builder);
    Mockito.when(builder.post(Matchers.any(Entity.class))).thenReturn(response);
    // mock the ReportingInitializationContext for initialize(...)
    final ComponentLog logger = Mockito.mock(ComponentLog.class);
    final ReportingInitializationContext initContext = Mockito.mock(ReportingInitializationContext.class);
    Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(initContext.getLogger()).thenReturn(logger);
    // mock the ConfigurationContext for setup(...)
    final ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
    // mock the ReportingContext for onTrigger(...)
    final ReportingContext context = Mockito.mock(ReportingContext.class);
    Mockito.when(context.getProperty(AmbariReportingTask.METRICS_COLLECTOR_URL)).thenReturn(new MockPropertyValue(metricsUrl));
    Mockito.when(context.getProperty(AmbariReportingTask.APPLICATION_ID)).thenReturn(new MockPropertyValue(applicationId));
    Mockito.when(context.getProperty(AmbariReportingTask.HOSTNAME)).thenReturn(new MockPropertyValue(hostName));
    Mockito.when(context.getProperty(AmbariReportingTask.PROCESS_GROUP_ID)).thenReturn(new MockPropertyValue("1234"));
    final EventAccess eventAccess = Mockito.mock(EventAccess.class);
    Mockito.when(context.getEventAccess()).thenReturn(eventAccess);
    Mockito.when(eventAccess.getControllerStatus()).thenReturn(status);
    // create a testable instance of the reporting task
    final AmbariReportingTask task = new TestableAmbariReportingTask(client);
    task.initialize(initContext);
    task.setup(configurationContext);
    task.onTrigger(context);
}
Also used : EventAccess(org.apache.nifi.reporting.EventAccess) Entity(javax.ws.rs.client.Entity) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) Invocation(javax.ws.rs.client.Invocation) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) ReportingContext(org.apache.nifi.reporting.ReportingContext) Response(javax.ws.rs.core.Response) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 4 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi-minifi by apache.

the class ReportingTaskingInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ReportingTask reportingTask = (ReportingTask) component;
    ReportingInitializationContext context = new MockReportingInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        reportingTask.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MockReportingInitializationContext(org.apache.nifi.mock.MockReportingInitializationContext) MockReportingInitializationContext(org.apache.nifi.mock.MockReportingInitializationContext) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 5 with ReportingInitializationContext

use of org.apache.nifi.reporting.ReportingInitializationContext in project nifi by apache.

the class StandardFlowSynchronizer method getOrCreateReportingTask.

private ReportingTaskNode getOrCreateReportingTask(final FlowController controller, final ReportingTaskDTO dto, final boolean controllerInitialized, final boolean existingFlowEmpty) throws ReportingTaskInstantiationException {
    // create a new reporting task node when the controller is not initialized or the flow is empty
    if (!controllerInitialized || existingFlowEmpty) {
        BundleCoordinate coordinate;
        try {
            coordinate = BundleUtils.getCompatibleBundle(dto.getType(), dto.getBundle());
        } catch (final IllegalStateException e) {
            final BundleDTO bundleDTO = dto.getBundle();
            if (bundleDTO == null) {
                coordinate = BundleCoordinate.UNKNOWN_COORDINATE;
            } else {
                coordinate = new BundleCoordinate(bundleDTO.getGroup(), bundleDTO.getArtifact(), bundleDTO.getVersion());
            }
        }
        final ReportingTaskNode reportingTask = controller.createReportingTask(dto.getType(), dto.getId(), coordinate, false);
        reportingTask.setName(dto.getName());
        reportingTask.setComments(dto.getComments());
        reportingTask.setSchedulingPeriod(dto.getSchedulingPeriod());
        reportingTask.setSchedulingStrategy(SchedulingStrategy.valueOf(dto.getSchedulingStrategy()));
        reportingTask.setAnnotationData(dto.getAnnotationData());
        reportingTask.setProperties(dto.getProperties());
        final ComponentLog componentLog = new SimpleProcessLogger(dto.getId(), reportingTask.getReportingTask());
        final ReportingInitializationContext config = new StandardReportingInitializationContext(dto.getId(), dto.getName(), SchedulingStrategy.valueOf(dto.getSchedulingStrategy()), dto.getSchedulingPeriod(), componentLog, controller, nifiProperties, controller);
        try {
            reportingTask.getReportingTask().initialize(config);
        } catch (final InitializationException ie) {
            throw new ReportingTaskInstantiationException("Failed to initialize reporting task of type " + dto.getType(), ie);
        }
        return reportingTask;
    } else {
        // otherwise return the existing reporting task node
        return controller.getReportingTaskNode(dto.getId());
    }
}
Also used : ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) BundleDTO(org.apache.nifi.web.api.dto.BundleDTO) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) InitializationException(org.apache.nifi.reporting.InitializationException) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ComponentLog(org.apache.nifi.logging.ComponentLog)

Aggregations

ReportingInitializationContext (org.apache.nifi.reporting.ReportingInitializationContext)7 ReportingTask (org.apache.nifi.reporting.ReportingTask)4 StandardReportingInitializationContext (org.apache.nifi.controller.reporting.StandardReportingInitializationContext)3 ComponentLog (org.apache.nifi.logging.ComponentLog)3 NarCloseable (org.apache.nifi.nar.NarCloseable)3 ConfigurationContext (org.apache.nifi.controller.ConfigurationContext)2 ReportingTaskInstantiationException (org.apache.nifi.controller.reporting.ReportingTaskInstantiationException)2 StandardReportingTaskNode (org.apache.nifi.controller.reporting.StandardReportingTaskNode)2 MockReportingInitializationContext (org.apache.nifi.mock.MockReportingInitializationContext)2 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 EventAccess (org.apache.nifi.reporting.EventAccess)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 ReportingContext (org.apache.nifi.reporting.ReportingContext)2 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Client (javax.ws.rs.client.Client)1 Entity (javax.ws.rs.client.Entity)1