@SuppressWarnings("unchecked")
@Test
public void testHandleContainerStatusInvalidCompletions() throws Exception {

  rm = new MockRM(new YarnConfiguration());
  rm.start();
  EventHandler handler =
      spy(rm.getRMContext().getDispatcher().getEventHandler());
  // Case 1: Unmanaged AM
  RMApp app = rm.submitApp(1024, true);
  // Case 1.1: AppAttemptId is null
  ContainerStatus status = ContainerStatus.newInstance(
      ContainerId.newInstance(ApplicationAttemptId.newInstance(
          app.getApplicationId(), 2), 1),
      ContainerState.COMPLETE, "Dummy Completed", 0);
  rm.getResourceTrackerService().handleContainerStatus(status);
  verify(handler, never()).handle((Event) any());
  // Case 1.2: Master container is null
  RMAppAttemptImpl currentAttempt =
      (RMAppAttemptImpl) app.getCurrentAppAttempt();
  currentAttempt.setMasterContainer(null);
  status = ContainerStatus.newInstance(
      ContainerId.newInstance(currentAttempt.getAppAttemptId(), 0),
      ContainerState.COMPLETE, "Dummy Completed", 0);
  rm.getResourceTrackerService().handleContainerStatus(status);
  verify(handler, never()).handle((Event)any());
  // Case 2: Managed AM
  app = rm.submitApp(1024);
  // Case 2.1: AppAttemptId is null
  status = ContainerStatus.newInstance(
      ContainerId.newInstance(ApplicationAttemptId.newInstance(
          app.getApplicationId(), 2), 1),
      ContainerState.COMPLETE, "Dummy Completed", 0);
  try {
    rm.getResourceTrackerService().handleContainerStatus(status);
  } catch (Exception e) {
    // expected - ignore
  }
  verify(handler, never()).handle((Event)any());
  // Case 2.2: Master container is null
  currentAttempt =
      (RMAppAttemptImpl) app.getCurrentAppAttempt();
  currentAttempt.setMasterContainer(null);
  status = ContainerStatus.newInstance(
      ContainerId.newInstance(currentAttempt.getAppAttemptId(), 0),
      ContainerState.COMPLETE, "Dummy Completed", 0);
  try {
    rm.getResourceTrackerService().handleContainerStatus(status);
  } catch (Exception e) {
    // expected - ignore
  }
  verify(handler, never()).handle((Event)any());
}