@Test
public void testUpdateResources_CancelRequest() throws Exception {

  Resource.Type type = Resource.Type.Request;
  AmbariManagementController managementController = createMock(AmbariManagementController.class);
  ActionManager actionManager = createNiceMock(ActionManager.class);
  HostRoleCommand hostRoleCommand = createNiceMock(HostRoleCommand.class);
  Stage stage = createNiceMock(Stage.class);
  Clusters clusters = createNiceMock(Clusters.class);
  List<HostRoleCommand> hostRoleCommands = new LinkedList<HostRoleCommand>();
  hostRoleCommands.add(hostRoleCommand);
  Collection<Stage> stages = new HashSet<Stage>();
  stages.add(stage);
  org.apache.ambari.server.actionmanager.Request requestMock =
    createNiceMock(org.apache.ambari.server.actionmanager.Request.class);
  expect(requestMock.getCommands()).andReturn(hostRoleCommands).anyTimes();
  expect(requestMock.getStages()).andReturn(stages).anyTimes();
  expect(stage.getOrderedHostRoleCommands()).andReturn(hostRoleCommands).anyTimes();
  Capture<Collection<Long>> requestIdsCapture = newCapture();
  // set expectations
  expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
  expect(actionManager.getRequests(capture(requestIdsCapture))).
    andReturn(Collections.singletonList(requestMock)).anyTimes();
  expect(hostRoleCommand.getStatus()).andReturn(HostRoleStatus.IN_PROGRESS).anyTimes();
  RequestStatusResponse response = createNiceMock(RequestStatusResponse.class);
  // replay
  replay(managementController, actionManager, hostRoleCommand, clusters, requestMock, response, stage);
  ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
    type,
    PropertyHelper.getPropertyIds(type),
    PropertyHelper.getKeyPropertyIds(type),
    managementController);
  // TEST CASE: Check update request validation (abort reason not specified)
  // add the property map to a set for the request.
  Map<String, Object> properties = new LinkedHashMap<String, Object>();
  // create the request
  Request request = PropertyHelper.getUpdateRequest(properties, null);
  Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).
    equals("100").toPredicate();
  try {
    provider.updateResources(request, predicate);
    Assert.fail("Expected an java.lang.IllegalArgumentException: Abort reason can not be empty.");
  } catch (IllegalArgumentException e) {
    // expected
  }
  // Add abort reason to previous request
  properties.put(RequestResourceProvider.REQUEST_ABORT_REASON_PROPERTY_ID, "Some reason");
  // TEST CASE: Check update request validation (new state is not specified)
  request = PropertyHelper.getUpdateRequest(properties, null);
  try {
    provider.updateResources(request, predicate);
    Assert.fail("Expected an java.lang.IllegalArgumentException: null is wrong value.");
  } catch (IllegalArgumentException e) {
    // expected
  }
  // TEST CASE: Check update request validation (new state is wrong)
  properties.put(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID, "COMPLETED");
  request = PropertyHelper.getUpdateRequest(properties, null);
  try {
    provider.updateResources(request, predicate);
    Assert.fail("Expected an java.lang.IllegalArgumentException: COMPLETED is wrong value. " +
      "The only allowed value for updating request status is ABORTED");
  } catch (IllegalArgumentException e) {
    // expected
  }
  // TEST CASE: Check update request validation (request is in wrong state)
  // Put valid request status
  properties.put(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID, "ABORTED");
  for (HostRoleStatus status : HostRoleStatus.values()) {
    reset(hostRoleCommand);
    expect(hostRoleCommand.getStatus()).andReturn(status).anyTimes();
    replay(hostRoleCommand);
    request = PropertyHelper.getUpdateRequest(properties, null);
    if (status == HostRoleStatus.IN_PROGRESS ||
      status == HostRoleStatus.PENDING ||
      status == HostRoleStatus.HOLDING ||
      status == HostRoleStatus.HOLDING_FAILED ||
      status == HostRoleStatus.HOLDING_TIMEDOUT ||
      status == HostRoleStatus.COMPLETED ||
      status == HostRoleStatus.ABORTED ||
      status == HostRoleStatus.FAILED ||
      status == HostRoleStatus.TIMEDOUT ||
      status == HostRoleStatus.QUEUED ||
      status == HostRoleStatus.SKIPPED_FAILED) { // the only valid cases
      provider.updateResources(request, predicate);
    } else {  // In other cases, should error out
      try {
        provider.updateResources(request, predicate);
        Assert.fail("Expected an java.lang.IllegalArgumentException");
      } catch (IllegalArgumentException e) {
        // expected
      }
    }
  }
  // verify
  verify(managementController, response, stage);
}