use of org.apache.nifi.components.ValidationContext in project nifi by apache.
the class TestStandardValidators method testNonEmptyELValidator.
@Test
public void testNonEmptyELValidator() {
Validator val = StandardValidators.NON_EMPTY_EL_VALIDATOR;
ValidationContext vc = mock(ValidationContext.class);
Mockito.when(vc.isExpressionLanguageSupported("foo")).thenReturn(true);
ValidationResult vr = val.validate("foo", "", vc);
assertFalse(vr.isValid());
vr = val.validate("foo", " h", vc);
assertTrue(vr.isValid());
Mockito.when(vc.isExpressionLanguagePresent("${test}")).thenReturn(true);
vr = val.validate("foo", "${test}", vc);
assertTrue(vr.isValid());
vr = val.validate("foo", "${test", vc);
assertTrue(vr.isValid());
}
use of org.apache.nifi.components.ValidationContext in project nifi by apache.
the class TestStandardValidators method testTimePeriodValidator.
@Test
public void testTimePeriodValidator() {
Validator val = StandardValidators.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
ValidationResult vr;
final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
vr = val.validate("TimePeriodTest", "0 sense made", validationContext);
assertFalse(vr.isValid());
vr = val.validate("TimePeriodTest", null, validationContext);
assertFalse(vr.isValid());
vr = val.validate("TimePeriodTest", "0 secs", validationContext);
assertFalse(vr.isValid());
vr = val.validate("TimePeriodTest", "999 millis", validationContext);
assertFalse(vr.isValid());
vr = val.validate("TimePeriodTest", "999999999 nanos", validationContext);
assertFalse(vr.isValid());
vr = val.validate("TimePeriodTest", "1 sec", validationContext);
assertTrue(vr.isValid());
}
use of org.apache.nifi.components.ValidationContext in project nifi by apache.
the class NotificationServiceManager method notify.
public void notify(final NotificationType type, final String subject, final String message) {
final List<ConfiguredNotificationService> configs = servicesByNotificationType.get(type);
if (configs == null || configs.isEmpty()) {
return;
}
for (final ConfiguredNotificationService config : configs) {
final NotificationService service = config.getService();
final AtomicInteger attemptCount = new AtomicInteger(0);
notificationExecutor.submit(new Runnable() {
@Override
public void run() {
// Check if the service is valid; if not, warn now so that users know this before they fail to receive notifications
final ValidationContext validationContext = new NotificationValidationContext(buildNotificationContext(config), variableRegistry);
final Collection<ValidationResult> validationResults = service.validate(validationContext);
final List<String> invalidReasons = new ArrayList<>();
for (final ValidationResult result : validationResults) {
if (!result.isValid()) {
invalidReasons.add(result.toString());
}
}
// If the service is valid, attempt to send the notification
boolean failure = false;
if (invalidReasons.isEmpty()) {
final NotificationContext context = buildNotificationContext(config);
try {
service.notify(context, type, subject, message);
logger.info("Successfully sent notification of type {} to {}", type, service);
} catch (final Throwable t) {
// keep running even if a Throwable is caught because we need to ensure that we are able to restart NiFi
logger.error("Failed to send notification of type {} to {} with Subject {} due to {}. Will ", type, service == null ? "Unknown Notification Service" : service.toString(), subject, t.toString());
logger.error("", t);
failure = true;
}
} else {
logger.warn("Notification Service {} is not valid for the following reasons: {}", service, invalidReasons);
failure = true;
}
final int attempts = attemptCount.incrementAndGet();
if (failure) {
if (attempts < maxAttempts) {
logger.info("After failing to send notification to {} {} times, will attempt again in 1 minute", service, attempts);
notificationExecutor.schedule(this, 1, TimeUnit.MINUTES);
} else {
logger.info("After failing to send notification of type {} to {} {} times, will no longer attempt to send notification", type, service, attempts);
}
}
}
});
if (NotificationType.NIFI_STOPPED.equals(type)) {
// we don't want to return before the notifier has had a chance to perform its task.
while (attemptCount.get() == 0) {
try {
Thread.sleep(1000L);
} catch (final InterruptedException ie) {
}
}
}
}
}
use of org.apache.nifi.components.ValidationContext in project nifi by apache.
the class StandardProcessorNode method isValid.
@Override
public boolean isValid() {
try {
final ValidationContext validationContext = getValidationContext();
final Collection<ValidationResult> validationResults = super.validate(validationContext);
for (final ValidationResult result : validationResults) {
if (!result.isValid()) {
return false;
}
}
for (final Relationship undef : getUndefinedRelationships()) {
if (!isAutoTerminated(undef)) {
return false;
}
}
switch(getInputRequirement()) {
case INPUT_ALLOWED:
break;
case INPUT_FORBIDDEN:
{
if (!getIncomingNonLoopConnections().isEmpty()) {
return false;
}
break;
}
case INPUT_REQUIRED:
{
if (getIncomingNonLoopConnections().isEmpty()) {
return false;
}
break;
}
}
} catch (final Throwable t) {
LOG.warn("Failed during validation", t);
return false;
}
return true;
}
use of org.apache.nifi.components.ValidationContext in project nifi by apache.
the class StandardProcessorNode method getValidationErrors.
@Override
public Collection<ValidationResult> getValidationErrors() {
final List<ValidationResult> results = new ArrayList<>();
try {
// we are willing to make in order to save on validation costs that would be unnecessary most of the time.
if (getScheduledState() == ScheduledState.STOPPED) {
final ValidationContext validationContext = getValidationContext();
final Collection<ValidationResult> validationResults = super.validate(validationContext);
for (final ValidationResult result : validationResults) {
if (!result.isValid()) {
results.add(result);
}
}
for (final Relationship relationship : getUndefinedRelationships()) {
if (!isAutoTerminated(relationship)) {
final ValidationResult error = new ValidationResult.Builder().explanation("Relationship '" + relationship.getName() + "' is not connected to any component and is not auto-terminated").subject("Relationship " + relationship.getName()).valid(false).build();
results.add(error);
}
}
switch(getInputRequirement()) {
case INPUT_ALLOWED:
break;
case INPUT_FORBIDDEN:
{
final int incomingConnCount = getIncomingNonLoopConnections().size();
if (incomingConnCount != 0) {
results.add(new ValidationResult.Builder().explanation("Processor does not allow upstream connections but currently has " + incomingConnCount).subject("Upstream Connections").valid(false).build());
}
break;
}
case INPUT_REQUIRED:
{
if (getIncomingNonLoopConnections().isEmpty()) {
results.add(new ValidationResult.Builder().explanation("Processor requires an upstream connection but currently has none").subject("Upstream Connections").valid(false).build());
}
break;
}
}
}
} catch (final Throwable t) {
results.add(new ValidationResult.Builder().explanation("Failed to run validation due to " + t.toString()).valid(false).build());
}
return results;
}
Aggregations