@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)
            .getControllerServiceValidationContext(service);
    final ValidationResult validationResult = property.validate(value, validationContext);

    updatedProps.put(property, value);
    configuration.setProperties(updatedProps);

    return validationResult;
  }
  @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);
  }
  @Override
  public boolean isControllerServiceEnabled(final ControllerService service) {
    final ControllerServiceConfiguration configuration =
        context.getConfiguration(service.getIdentifier());
    if (configuration == null) {
      throw new IllegalArgumentException("Controller Service " + service + " is not known");
    }

    return configuration.isEnabled();
  }
  private ControllerServiceConfiguration getConfigToUpdate(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 modified because it is not disabled");
    }

    return configuration;
  }
  @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)
            .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 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);
  }
 /**
  * Returns the State Manager for the given Controller Service.
  *
  * @param controllerService the Controller Service whose State Manager should be returned
  * @return the State Manager for the given Controller Service
  */
 @Override
 public MockStateManager getStateManager(final ControllerService controllerService) {
   return controllerServiceStateManagers.get(controllerService.getIdentifier());
 }