Ejemplo n.º 1
0
  @Override
  public void enableControllerService(final ControllerService service) {
    final ControllerServiceConfiguration configuration =
        context.getConfiguration(service.getIdentifier());
    if (configuration == null) {
      throw new IllegalArgumentException("Controller Service " + service + " is not known");
    }

    if (configuration.isEnabled()) {
      throw new IllegalStateException(
          "Cannot enable Controller Service " + service + " because it is not disabled");
    }

    try {
      final ConfigurationContext configContext =
          new MockConfigurationContext(service, configuration.getProperties(), context);
      ReflectionUtils.invokeMethodsWithAnnotation(OnEnabled.class, service, configContext);
    } catch (final InvocationTargetException ite) {
      ite.getCause().printStackTrace();
      Assert.fail("Failed to enable Controller Service " + service + " due to " + ite.getCause());
    } catch (final Exception e) {
      e.printStackTrace();
      Assert.fail("Failed to enable Controller Service " + service + " due to " + e);
    }

    configuration.setEnabled(true);
  }
Ejemplo n.º 2
0
  StandardProcessorTestRunner(final Processor processor) {
    this.processor = processor;
    this.idGenerator = new AtomicLong(0L);
    this.sharedState = new SharedSessionState(processor, idGenerator);
    this.flowFileQueue = sharedState.getFlowFileQueue();
    this.sessionFactory = new MockSessionFactory(sharedState, processor);
    this.processorStateManager = new MockStateManager(processor);
    this.context = new MockProcessContext(processor, processorStateManager);

    detectDeprecatedAnnotations(processor);

    final MockProcessorInitializationContext mockInitContext =
        new MockProcessorInitializationContext(processor, context);
    processor.initialize(mockInitContext);
    logger = mockInitContext.getLogger();

    try {
      ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor);
    } catch (final Exception e) {
      Assert.fail("Could not invoke methods annotated with @OnAdded annotation due to: " + e);
    }

    triggerSerially = null != processor.getClass().getAnnotation(TriggerSerially.class);

    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, processor);
  }
Ejemplo n.º 3
0
 @Override
 public void shutdown() {
   try {
     ReflectionUtils.invokeMethodsWithAnnotation(OnShutdown.class, processor);
   } catch (final Exception e) {
     Assert.fail("Could not invoke methods annotated with @OnShutdown annotation due to: " + e);
   }
 }
Ejemplo n.º 4
0
  @Override
  public void removeControllerService(final ControllerService service) {
    disableControllerService(service);

    try {
      ReflectionUtils.invokeMethodsWithAnnotation(OnRemoved.class, service);
    } catch (final Exception e) {
      e.printStackTrace();
      Assert.fail("Failed to remove Controller Service " + service + " due to " + e);
    }

    context.removeControllerService(service);
  }
Ejemplo n.º 5
0
  @Override
  public void disableControllerService(final ControllerService service) {
    final ControllerServiceConfiguration configuration =
        context.getConfiguration(service.getIdentifier());
    if (configuration == null) {
      throw new IllegalArgumentException("Controller Service " + service + " is not known");
    }

    if (!configuration.isEnabled()) {
      throw new IllegalStateException(
          "Controller service " + service + " cannot be disabled because it is not enabled");
    }

    try {
      ReflectionUtils.invokeMethodsWithAnnotation(OnDisabled.class, service);
    } catch (final Exception e) {
      e.printStackTrace();
      Assert.fail("Failed to disable Controller Service " + service + " due to " + e);
    }

    configuration.setEnabled(false);
  }
Ejemplo n.º 6
0
  @Override
  public void addControllerService(
      final String identifier,
      final ControllerService service,
      final Map<String, String> properties)
      throws InitializationException {
    // hold off on failing due to deprecated annotation for now... will introduce later.
    // for ( final Method method : service.getClass().getMethods() ) {
    // if ( method.isAnnotationPresent(org.apache.nifi.controller.annotation.OnConfigured.class) ) {
    // Assert.fail("Controller Service " + service + " is using deprecated Annotation " +
    // org.apache.nifi.controller.annotation.OnConfigured.class + " for method " + method);
    // }
    // }

    final MockComponentLog logger = new MockComponentLog(identifier, service);
    controllerServiceLoggers.put(identifier, logger);
    final MockStateManager serviceStateManager = new MockStateManager(service);
    final MockControllerServiceInitializationContext initContext =
        new MockControllerServiceInitializationContext(
            requireNonNull(service), requireNonNull(identifier), logger, serviceStateManager);
    controllerServiceStateManagers.put(identifier, serviceStateManager);
    initContext.addControllerServices(context);
    service.initialize(initContext);

    final Map<PropertyDescriptor, String> resolvedProps = new HashMap<>();
    for (final Map.Entry<String, String> entry : properties.entrySet()) {
      resolvedProps.put(service.getPropertyDescriptor(entry.getKey()), entry.getValue());
    }

    try {
      ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, service);
    } catch (final InvocationTargetException
        | IllegalAccessException
        | IllegalArgumentException e) {
      throw new InitializationException(e);
    }

    context.addControllerService(identifier, service, resolvedProps, null);
  }
Ejemplo n.º 7
0
  @Override
  public void run(
      final int iterations,
      final boolean stopOnFinish,
      final boolean initialize,
      final long runWait) {
    if (iterations < 1) {
      throw new IllegalArgumentException();
    }

    context.assertValid();
    context.enableExpressionValidation();
    try {
      if (initialize) {
        try {
          ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, processor, context);
        } catch (final Exception e) {
          e.printStackTrace();
          Assert.fail(
              "Could not invoke methods annotated with @OnScheduled annotation due to: " + e);
        }
      }

      final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
      @SuppressWarnings("unchecked")
      final Future<Throwable>[] futures = new Future[iterations];
      for (int i = 0; i < iterations; i++) {
        final Future<Throwable> future = executorService.submit(new RunProcessor());
        futures[i] = future;
      }

      executorService.shutdown();
      try {
        executorService.awaitTermination(runWait, TimeUnit.MILLISECONDS);
      } catch (final InterruptedException e1) {
      }

      int finishedCount = 0;
      boolean unscheduledRun = false;
      for (final Future<Throwable> future : futures) {
        try {
          final Throwable thrown = future.get(); // wait for the result
          if (thrown != null) {
            throw new AssertionError(thrown);
          }

          if (++finishedCount == 1) {
            unscheduledRun = true;
            try {
              ReflectionUtils.invokeMethodsWithAnnotation(OnUnscheduled.class, processor, context);
            } catch (final Exception e) {
              Assert.fail(
                  "Could not invoke methods annotated with @OnUnscheduled annotation due to: " + e);
            }
          }
        } catch (final Exception e) {
        }
      }

      if (!unscheduledRun) {
        try {
          ReflectionUtils.invokeMethodsWithAnnotation(OnUnscheduled.class, processor, context);
        } catch (final Exception e) {
          Assert.fail(
              "Could not invoke methods annotated with @OnUnscheduled annotation due to: " + e);
        }
      }

      if (stopOnFinish) {
        try {
          ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, processor, context);
        } catch (final Exception e) {
          Assert.fail("Could not invoke methods annotated with @OnStopped annotation due to: " + e);
        }
      }
    } finally {
      context.disableExpressionValidation();
    }
  }