@Override public ValidationContext getControllerServiceValidationContext(final ControllerService controllerService) { final MockProcessContext serviceProcessContext = new MockProcessContext(controllerService, context, stateManager, variableRegistry); return new MockValidationContext(serviceProcessContext, stateManager, variableRegistry); }
@Override public ValidationResult setProperty(final ControllerService service, final PropertyDescriptor property, final String value) { final MockStateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier()); if (serviceStateManager == null) { throw new IllegalStateException("Controller service " + service + " has not been added to this TestRunner via the #addControllerService method"); } final ControllerServiceConfiguration configuration = getConfigToUpdate(service); final Map<PropertyDescriptor, String> curProps = configuration.getProperties(); final Map<PropertyDescriptor, String> updatedProps = new HashMap<>(curProps); final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service); final ValidationResult validationResult = property.validate(value, validationContext); final String oldValue = updatedProps.get(property); updatedProps.put(property, value); configuration.setProperties(updatedProps); if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) { service.onPropertyModified(property, oldValue, value); } return validationResult; }
/** * Updates the value of the property with the given PropertyDescriptor to * the specified value IF and ONLY IF the value is valid according to the * descriptor's validator. Otherwise, the property value is not updated. In * either case, the ValidationResult is returned, indicating whether or not * the property is valid * * @param descriptor of property to modify * @param value new value * @return result */ public ValidationResult setProperty(final PropertyDescriptor descriptor, final String value) { requireNonNull(descriptor); requireNonNull(value, "Cannot set property to null value; if the intent is to remove the property, call removeProperty instead"); final PropertyDescriptor fullyPopulatedDescriptor = component.getPropertyDescriptor(descriptor.getName()); final ValidationResult result = fullyPopulatedDescriptor.validate(value, new MockValidationContext(this, stateManager, variableRegistry)); String oldValue = properties.put(fullyPopulatedDescriptor, value); if (oldValue == null) { oldValue = fullyPopulatedDescriptor.getDefaultValue(); } if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) { component.onPropertyModified(fullyPopulatedDescriptor, oldValue, value); } return result; }
/** * Validates the current properties, returning ValidationResults for any * invalid properties. All processor defined properties will be validated. * If they are not included in the in the purposed configuration, the * default value will be used. * * @return Collection of validation result objects for any invalid findings * only. If the collection is empty then the processor is valid. Guaranteed * non-null */ public Collection<ValidationResult> validate() { final List<ValidationResult> results = new ArrayList<>(); final ValidationContext validationContext = new MockValidationContext(this, stateManager, variableRegistry); final Collection<ValidationResult> componentResults = component.validate(validationContext); results.addAll(componentResults); final Collection<ValidationResult> serviceResults = validateReferencedControllerServices(validationContext); results.addAll(serviceResults); // verify all controller services are enabled for (Map.Entry<String, ControllerServiceConfiguration> service : getControllerServices().entrySet()) { if (!service.getValue().isEnabled()) { results.add(new ValidationResult.Builder() .explanation("Controller service " + service.getKey() + " for " + this.getName() + " is not enabled") .valid(false) .build()); } } return results; }
@Override public void assertNotValid(final ControllerService service) { final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier()); if (serviceStateManager == null) { throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method"); } final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service); final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext); for (final ValidationResult result : results) { if (!result.isValid()) { return; } } Assert.fail("Expected Controller Service " + service + " to be invalid but it is valid"); }
@Override public void assertValid(final ControllerService service) { final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier()); if (serviceStateManager == null) { throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method"); } final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service); final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext); for (final ValidationResult result : results) { if (!result.isValid()) { Assert.fail("Expected Controller Service to be valid but it is invalid due to: " + result.toString()); } } }
@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"); } // ensure controller service is valid before enabling final ValidationContext validationContext = new MockValidationContext(context).getControllerServiceValidationContext(service); final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext); for (final ValidationResult result : results) { if (!result.isValid()) { throw new IllegalStateException("Cannot enable Controller Service " + service + " because it is in an invalid state: " + result.toString()); } } try { final ConfigurationContext configContext = new MockConfigurationContext(service, configuration.getProperties(), context,variableRegistry); 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); }
@Override public ValidationContext getControllerServiceValidationContext(final ControllerService controllerService) { final MockProcessContext serviceProcessContext = new MockProcessContext(controllerService, context, stateManager, variableRegistry); return new MockValidationContext(serviceProcessContext, stateManager, variableRegistry); }
@Override public ValidationResult setProperty(final ControllerService service, final PropertyDescriptor property, final String value) { final MockStateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier()); if (serviceStateManager == null) { throw new IllegalStateException("Controller service " + service + " has not been added to this TestRunner via the #addControllerService method"); } final ControllerServiceConfiguration configuration = getConfigToUpdate(service); final Map<PropertyDescriptor, String> curProps = configuration.getProperties(); final Map<PropertyDescriptor, String> updatedProps = new HashMap<>(curProps); final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service); final ValidationResult validationResult = property.validate(value, validationContext); final String oldValue = updatedProps.get(property); updatedProps.put(property, value); configuration.setProperties(updatedProps); if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) { service.onPropertyModified(property, oldValue, value); } return validationResult; }
/** * Updates the value of the property with the given PropertyDescriptor to * the specified value IF and ONLY IF the value is valid according to the * descriptor's validator. Otherwise, the property value is not updated. In * either case, the ValidationResult is returned, indicating whether or not * the property is valid * * @param descriptor of property to modify * @param value new value * @return result */ public ValidationResult setProperty(final PropertyDescriptor descriptor, final String value) { requireNonNull(descriptor); requireNonNull(value, "Cannot set property to null value; if the intent is to remove the property, call removeProperty instead"); final PropertyDescriptor fullyPopulatedDescriptor = component.getPropertyDescriptor(descriptor.getName()); final ValidationResult result = fullyPopulatedDescriptor.validate(value, new MockValidationContext(this, stateManager, variableRegistry)); String oldValue = properties.put(fullyPopulatedDescriptor, value); if (oldValue == null) { oldValue = fullyPopulatedDescriptor.getDefaultValue(); } if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) { component.onPropertyModified(fullyPopulatedDescriptor, oldValue, value); } return result; }
/** * Validates the current properties, returning ValidationResults for any * invalid properties. All processor defined properties will be validated. * If they are not included in the in the purposed configuration, the * default value will be used. * * @return Collection of validation result objects for any invalid findings * only. If the collection is empty then the processor is valid. Guaranteed * non-null */ public Collection<ValidationResult> validate() { final List<ValidationResult> results = new ArrayList<>(); final ValidationContext validationContext = new MockValidationContext(this, stateManager, variableRegistry); final Collection<ValidationResult> componentResults = component.validate(validationContext); results.addAll(componentResults); final Collection<ValidationResult> serviceResults = validateReferencedControllerServices(validationContext); results.addAll(serviceResults); // verify all controller services are enabled for (Map.Entry<String, ControllerServiceConfiguration> service : getControllerServices().entrySet()) { if (!service.getValue().isEnabled()) { results.add(new ValidationResult.Builder() .explanation("Controller service " + service.getKey() + " for " + this.getName() + " is not enabled") .valid(false) .build()); } } return results; }
@Override public void assertNotValid(final ControllerService service) { final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier()); if (serviceStateManager == null) { throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method"); } final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service); final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext); for (final ValidationResult result : results) { if (!result.isValid()) { return; } } Assert.fail("Expected Controller Service " + service + " to be invalid but it is valid"); }
@Override public void assertValid(final ControllerService service) { final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier()); if (serviceStateManager == null) { throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method"); } final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service); final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext); for (final ValidationResult result : results) { if (!result.isValid()) { Assert.fail("Expected Controller Service to be valid but it is invalid due to: " + result.toString()); } } }
@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"); } // ensure controller service is valid before enabling final ValidationContext validationContext = new MockValidationContext(context).getControllerServiceValidationContext(service); final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext); for (final ValidationResult result : results) { if (!result.isValid()) { throw new IllegalStateException("Cannot enable Controller Service " + service + " because it is in an invalid state: " + result.toString()); } } try { final ConfigurationContext configContext = new MockConfigurationContext(service, configuration.getProperties(), context,variableRegistry); 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); }