@Test
public void testDeleteResourcesBadServiceState() throws Exception {

  AmbariManagementController managementController = createMock(AmbariManagementController.class);
  Clusters clusters = createNiceMock(Clusters.class);
  Cluster cluster = createNiceMock(Cluster.class);
  Service service = createNiceMock(Service.class);
  String serviceName = "Service100";
  // set expectations
  expect(managementController.getClusters()).andReturn(clusters).anyTimes();
  expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
  expect(cluster.getClusterId()).andReturn(2L).anyTimes();
  expect(cluster.getService(serviceName)).andReturn(service).anyTimes();
  expect(service.getDesiredState()).andReturn(State.STARTED).anyTimes();
  expect(service.getName()).andReturn(serviceName).anyTimes();
  expect(service.getServiceComponents()).andReturn(new HashMap<String, ServiceComponent>());
  expect(service.getCluster()).andReturn(cluster);
  cluster.deleteService(serviceName);
  // replay
  replay(managementController, clusters, cluster, service);
  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("Cluster100").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);
}