@Test
public void testDeleteResourcesStoppedHostComponentState() throws Exception {

  AmbariManagementController managementController = createMock(AmbariManagementController.class);
  Clusters clusters = createNiceMock(Clusters.class);
  Cluster cluster = createNiceMock(Cluster.class);
  Service service = createNiceMock(Service.class);
  //
  // Data structure for holding ServiceComponent information
  //
  class TestComponent {
    public String Name;
    public ServiceComponent Component;
    public State DesiredState;
    public TestComponent(String name, ServiceComponent component, State desiredState) {
      Name = name;
      Component = component;
      DesiredState = desiredState;
    }
  }
  //
  // Set up three components in STARTED state.
  //
  TestComponent component1 = new TestComponent("Component100", createNiceMock(ServiceComponent.class), State.STARTED);
  TestComponent component2 = new TestComponent("Component101", createNiceMock(ServiceComponent.class), State.STARTED);
  TestComponent component3 = new TestComponent("Component102", createNiceMock(ServiceComponent.class), State.STARTED);
  Map<String, ServiceComponent> scMap = new HashMap<String, ServiceComponent>();
  scMap.put(component1.Name, component1.Component);
  scMap.put(component2.Name, component2.Component);
  scMap.put(component3.Name, component3.Component);
  Map<String, ServiceComponentHost> schMap1 = new HashMap<>();
  ServiceComponentHost sch1 = createNiceMock(ServiceComponentHost.class);
  schMap1.put("Host1", sch1);
  Map<String, ServiceComponentHost> schMap2 = new HashMap<>();
  ServiceComponentHost sch2 = createNiceMock(ServiceComponentHost.class);
  schMap2.put("Host2", sch2);
  Map<String, ServiceComponentHost> schMap3 = new HashMap<>();
  ServiceComponentHost sch3 = createNiceMock(ServiceComponentHost.class);
  schMap3.put("Host3", sch3);
  String clusterName = "Cluster100";
  String serviceName = "Service100";
  // set expectations
  expect(managementController.getClusters()).andReturn(clusters).anyTimes();
  expect(clusters.getCluster(clusterName)).andReturn(cluster).anyTimes();
  expect(cluster.getService(serviceName)).andReturn(service).anyTimes();
  expect(service.getDesiredState()).andReturn(State.STARTED).anyTimes();  // Service is in a non-removable state
  expect(service.getName()).andReturn(serviceName).anyTimes();
  expect(service.getServiceComponents()).andReturn(scMap).anyTimes();
  expect(component1.Component.getDesiredState()).andReturn(component1.DesiredState).anyTimes();
  expect(component2.Component.getDesiredState()).andReturn(component2.DesiredState).anyTimes();
  expect(component3.Component.getDesiredState()).andReturn(component3.DesiredState).anyTimes();
  expect(component1.Component.canBeRemoved()).andReturn(component1.DesiredState.isRemovableState()).anyTimes();
  expect(component2.Component.canBeRemoved()).andReturn(component2.DesiredState.isRemovableState()).anyTimes();
  expect(component3.Component.canBeRemoved()).andReturn(component3.DesiredState.isRemovableState()).anyTimes();
  expect(component1.Component.getServiceComponentHosts()).andReturn(schMap1).anyTimes();
  expect(component2.Component.getServiceComponentHosts()).andReturn(schMap2).anyTimes();
  expect(component3.Component.getServiceComponentHosts()).andReturn(schMap3).anyTimes();
  // Put the SCH in INSTALLED state so that the service can be deleted,
  // no matter what state the service component is in.
  State sch1State = State.INSTALLED;
  expect(sch1.getDesiredState()).andReturn(sch1State).anyTimes();
  expect(sch1.canBeRemoved()).andReturn(sch1State.isRemovableState()).anyTimes();
  State sch2State = State.INSTALLED;
  expect(sch2.getDesiredState()).andReturn(sch2State).anyTimes();
  expect(sch2.canBeRemoved()).andReturn(sch2State.isRemovableState()).anyTimes();
  State sch3State = State.INSTALLED;
  expect(sch3.getDesiredState()).andReturn(sch3State).anyTimes();
  expect(sch3.canBeRemoved()).andReturn(sch3State.isRemovableState()).anyTimes();
  expect(service.getCluster()).andReturn(cluster);
  cluster.deleteService(serviceName);
  // replay
  replay(managementController, clusters, cluster, service,
          component1.Component, component2.Component, component3.Component, sch1, sch2, sch3);
  SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator());
  ResourceProvider provider = getServiceProvider(managementController);
  AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver();
  ((ObservableResourceProvider)provider).addObserver(observer);
  // delete the service named Service100
  Predicate  predicate = new PredicateBuilder().property(ServiceResourceProvider.SERVICE_CLUSTER_NAME_PROPERTY_ID).equals(clusterName).and()
          .property(ServiceResourceProvider.SERVICE_SERVICE_NAME_PROPERTY_ID).equals(serviceName).toPredicate();
  provider.deleteResources(new RequestImpl(null, null, null, null), predicate);
  ResourceProviderEvent lastEvent = observer.getLastEvent();
  Assert.assertNotNull(lastEvent);
  Assert.assertEquals(Resource.Type.Service, lastEvent.getResourceType());
  Assert.assertEquals(ResourceProviderEvent.Type.Delete, lastEvent.getType());
  Assert.assertEquals(predicate, lastEvent.getPredicate());
  Assert.assertNull(lastEvent.getRequest());
  // verify
  verify(managementController, clusters, cluster, service,
          component1.Component, component2.Component, component3.Component, sch1, sch2, sch3);
}