Program: 105804
Project: 105804
Buggy source:
@Before public void setup() throws Exception {
  MockitoAnnotations.initMocks(this);
  _svc._volsDao=_volumeDao;
  _svc._accountMgr=_accountMgr;
  _svc._userVmDao=_userVmDao;
  _svc._storagePoolDao=_storagePoolDao;
  _svc._vmSnapshotDao=_vmSnapshotDao;
  _svc._vmInstanceDao=_vmInstanceDao;
  _svc._jobMgr=_jobMgr;
  _svc.volFactory=_volFactory;
  AccountVO account=new AccountVO("String_Node_Str",1L,"String_Node_Str",Account.ACCOUNT_TYPE_NORMAL,"String_Node_Str");
  UserVO user=new UserVO(1,"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str",UUID.randomUUID().toString());
  CallContext.register(user,account);
  AsyncJobExecutionContext context=new AsyncJobExecutionContext();
  AsyncJobExecutionContext.init(_svc._jobMgr,_joinMapDao);
  AsyncJobVO job=new AsyncJobVO();
  context.setJob(job);
  AsyncJobExecutionContext.setCurrentExecutionContext(context);
  TransactionLegacy txn=TransactionLegacy.open("String_Node_Str");
  try {
    VolumeVO volumeOfRunningVm=new VolumeVO("String_Node_Str",1L,1L,1L,1L,1L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    when(_svc._volsDao.findById(1L)).thenReturn(volumeOfRunningVm);
    UserVmVO runningVm=new UserVmVO(1L,"String_Node_Str","String_Node_Str",1,HypervisorType.XenServer,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    runningVm.setState(State.Running);
    runningVm.setDataCenterId(1L);
    when(_svc._userVmDao.findById(1L)).thenReturn(runningVm);
    VolumeVO volumeOfStoppedVm=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    volumeOfStoppedVm.setPoolId(1L);
    when(_svc._volsDao.findById(2L)).thenReturn(volumeOfStoppedVm);
    UserVmVO stoppedVm=new UserVmVO(2L,"String_Node_Str","String_Node_Str",1,HypervisorType.XenServer,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    stoppedVm.setState(State.Stopped);
    stoppedVm.setDataCenterId(1L);
    when(_svc._userVmDao.findById(2L)).thenReturn(stoppedVm);
    UserVmVO hyperVVm=new UserVmVO(3L,"String_Node_Str","String_Node_Str",1,HypervisorType.Hyperv,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    hyperVVm.setState(State.Stopped);
    hyperVVm.setDataCenterId(1L);
    when(_svc._userVmDao.findById(3L)).thenReturn(hyperVVm);
    VolumeVO volumeOfStoppeHyperVVm=new VolumeVO("String_Node_Str",1L,1L,1L,1L,3L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    volumeOfStoppeHyperVVm.setPoolId(1L);
    when(_svc._volsDao.findById(3L)).thenReturn(volumeOfStoppeHyperVVm);
    StoragePoolVO unmanagedPool=new StoragePoolVO();
    when(_svc._storagePoolDao.findById(1L)).thenReturn(unmanagedPool);
    StoragePoolVO managedPool=new StoragePoolVO();
    managedPool.setManaged(true);
    when(_svc._storagePoolDao.findById(2L)).thenReturn(managedPool);
    VolumeVO managedPoolVolume=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    managedPoolVolume.setPoolId(2L);
    when(_svc._volsDao.findById(4L)).thenReturn(managedPoolVolume);
    VolumeInfo volumeWithIncorrectVolumeType=Mockito.mock(VolumeInfo.class);
    when(volumeWithIncorrectVolumeType.getId()).thenReturn(5L);
    when(volumeWithIncorrectVolumeType.getVolumeType()).thenReturn(Volume.Type.ISO);
    when(_svc.volFactory.getVolume(5L)).thenReturn(volumeWithIncorrectVolumeType);
    VolumeInfo correctRootVolume=Mockito.mock(VolumeInfo.class);
    when(correctRootVolume.getId()).thenReturn(6L);
    when(correctRootVolume.getDataCenterId()).thenReturn(1L);
    when(correctRootVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
    when(correctRootVolume.getInstanceId()).thenReturn(null);
    when(_svc.volFactory.getVolume(6L)).thenReturn(correctRootVolume);
    VolumeVO correctRootVolumeVO=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    when(_svc._volsDao.findById(6L)).thenReturn(correctRootVolumeVO);
    VolumeInfo managedVolume=Mockito.mock(VolumeInfo.class);
    when(managedVolume.getId()).thenReturn(7L);
    when(managedVolume.getDataCenterId()).thenReturn(1L);
    when(managedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
    when(managedVolume.getInstanceId()).thenReturn(null);
    when(managedVolume.getPoolId()).thenReturn(2L);
    when(_svc.volFactory.getVolume(7L)).thenReturn(managedVolume);
    VolumeVO managedVolume1=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    managedVolume1.setPoolId(2L);
    managedVolume1.setDataCenterId(1L);
    when(_svc._volsDao.findById(7L)).thenReturn(managedVolume1);
    UserVmVO vmHavingRootVolume=new UserVmVO(4L,"String_Node_Str","String_Node_Str",1,HypervisorType.XenServer,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    vmHavingRootVolume.setState(State.Stopped);
    vmHavingRootVolume.setDataCenterId(1L);
    when(_svc._userVmDao.findById(4L)).thenReturn(vmHavingRootVolume);
    List<VolumeVO> vols=new ArrayList<VolumeVO>();
    vols.add(new VolumeVO());
    when(_svc._volsDao.findByInstanceAndDeviceId(4L,0L)).thenReturn(vols);
    VolumeInfo uploadedVolume=Mockito.mock(VolumeInfo.class);
    when(uploadedVolume.getId()).thenReturn(8L);
    when(uploadedVolume.getDataCenterId()).thenReturn(1L);
    when(uploadedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
    when(uploadedVolume.getInstanceId()).thenReturn(null);
    when(uploadedVolume.getPoolId()).thenReturn(1L);
    when(uploadedVolume.getState()).thenReturn(Volume.State.Uploaded);
    when(_svc.volFactory.getVolume(8L)).thenReturn(uploadedVolume);
    VolumeVO upVolume=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    upVolume.setPoolId(1L);
    upVolume.setDataCenterId(1L);
    upVolume.setState(Volume.State.Uploaded);
    when(_svc._volsDao.findById(8L)).thenReturn(upVolume);
    when(_svc._vmSnapshotDao.findByVm(any(Long.class))).thenReturn(new ArrayList<VMSnapshotVO>());
    when(_svc._vmInstanceDao.findById(any(Long.class))).thenReturn(stoppedVm);
  }
  finally {
    txn.close("String_Node_Str");
  }
  doNothing().when(_svc._accountMgr).checkAccess(any(Account.class),any(AccessType.class),any(Boolean.class),any(ControlledEntity.class));
  doNothing().when(_svc._jobMgr).updateAsyncJobAttachment(any(Long.class),any(String.class),any(Long.class));
  when(_svc._jobMgr.submitAsyncJob(any(AsyncJobVO.class),any(String.class),any(Long.class))).thenReturn(1L);
}

Fixed version (ground truth):
@Before public void setup() throws Exception {
  MockitoAnnotations.initMocks(this);
  _svc._volsDao=_volumeDao;
  _svc._accountMgr=_accountMgr;
  _svc._userVmDao=_userVmDao;
  _svc._storagePoolDao=_storagePoolDao;
  _svc._vmSnapshotDao=_vmSnapshotDao;
  _svc._vmInstanceDao=_vmInstanceDao;
  _svc._jobMgr=_jobMgr;
  _svc.volFactory=_volFactory;
  AccountVO account=new AccountVO("String_Node_Str",1L,"String_Node_Str",Account.ACCOUNT_TYPE_NORMAL,"String_Node_Str");
  UserVO user=new UserVO(1,"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str",UUID.randomUUID().toString());
  CallContext.register(user,account);
  AsyncJobExecutionContext context=new AsyncJobExecutionContext();
  AsyncJobExecutionContext.init(_svc._jobMgr,_joinMapDao);
  AsyncJobVO job=new AsyncJobVO();
  context.setJob(job);
  AsyncJobExecutionContext.setCurrentExecutionContext(context);
  TransactionLegacy txn=TransactionLegacy.open("String_Node_Str");
  try {
    VolumeVO volumeOfRunningVm=new VolumeVO("String_Node_Str",1L,1L,1L,1L,1L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    when(_svc._volsDao.findById(1L)).thenReturn(volumeOfRunningVm);
    UserVmVO runningVm=new UserVmVO(1L,"String_Node_Str","String_Node_Str",1,HypervisorType.XenServer,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    runningVm.setState(State.Running);
    runningVm.setDataCenterId(1L);
    when(_svc._userVmDao.findById(1L)).thenReturn(runningVm);
    VolumeVO volumeOfStoppedVm=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    volumeOfStoppedVm.setPoolId(1L);
    when(_svc._volsDao.findById(2L)).thenReturn(volumeOfStoppedVm);
    UserVmVO stoppedVm=new UserVmVO(2L,"String_Node_Str","String_Node_Str",1,HypervisorType.XenServer,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    stoppedVm.setState(State.Stopped);
    stoppedVm.setDataCenterId(1L);
    when(_svc._userVmDao.findById(2L)).thenReturn(stoppedVm);
    UserVmVO hyperVVm=new UserVmVO(3L,"String_Node_Str","String_Node_Str",1,HypervisorType.Hyperv,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    hyperVVm.setState(State.Stopped);
    hyperVVm.setDataCenterId(1L);
    when(_svc._userVmDao.findById(3L)).thenReturn(hyperVVm);
    VolumeVO volumeOfStoppeHyperVVm=new VolumeVO("String_Node_Str",1L,1L,1L,1L,3L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    volumeOfStoppeHyperVVm.setPoolId(1L);
    when(_svc._volsDao.findById(3L)).thenReturn(volumeOfStoppeHyperVVm);
    StoragePoolVO unmanagedPool=new StoragePoolVO();
    when(_svc._storagePoolDao.findById(1L)).thenReturn(unmanagedPool);
    StoragePoolVO managedPool=new StoragePoolVO();
    managedPool.setManaged(true);
    when(_svc._storagePoolDao.findById(2L)).thenReturn(managedPool);
    VolumeVO managedPoolVolume=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    managedPoolVolume.setPoolId(2L);
    when(_svc._volsDao.findById(4L)).thenReturn(managedPoolVolume);
    VolumeInfo volumeWithIncorrectVolumeType=Mockito.mock(VolumeInfo.class);
    when(volumeWithIncorrectVolumeType.getId()).thenReturn(5L);
    when(volumeWithIncorrectVolumeType.getVolumeType()).thenReturn(Volume.Type.ISO);
    when(_svc.volFactory.getVolume(5L)).thenReturn(volumeWithIncorrectVolumeType);
    VolumeInfo correctRootVolume=Mockito.mock(VolumeInfo.class);
    when(correctRootVolume.getId()).thenReturn(6L);
    when(correctRootVolume.getDataCenterId()).thenReturn(1L);
    when(correctRootVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
    when(correctRootVolume.getInstanceId()).thenReturn(null);
    when(_svc.volFactory.getVolume(6L)).thenReturn(correctRootVolume);
    VolumeVO correctRootVolumeVO=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    when(_svc._volsDao.findById(6L)).thenReturn(correctRootVolumeVO);
    VolumeInfo managedVolume=Mockito.mock(VolumeInfo.class);
    when(managedVolume.getId()).thenReturn(7L);
    when(managedVolume.getDataCenterId()).thenReturn(1L);
    when(managedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
    when(managedVolume.getInstanceId()).thenReturn(null);
    when(managedVolume.getPoolId()).thenReturn(2L);
    when(_svc.volFactory.getVolume(7L)).thenReturn(managedVolume);
    VolumeVO managedVolume1=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    managedVolume1.setPoolId(2L);
    managedVolume1.setDataCenterId(1L);
    when(_svc._volsDao.findById(7L)).thenReturn(managedVolume1);
    UserVmVO vmHavingRootVolume=new UserVmVO(4L,"String_Node_Str","String_Node_Str",1,HypervisorType.XenServer,1L,false,false,1L,1L,1L,null,"String_Node_Str",null);
    vmHavingRootVolume.setState(State.Stopped);
    vmHavingRootVolume.setDataCenterId(1L);
    when(_svc._userVmDao.findById(4L)).thenReturn(vmHavingRootVolume);
    List<VolumeVO> vols=new ArrayList<VolumeVO>();
    vols.add(new VolumeVO());
    when(_svc._volsDao.findByInstanceAndDeviceId(4L,0L)).thenReturn(vols);
    VolumeInfo uploadedVolume=Mockito.mock(VolumeInfo.class);
    when(uploadedVolume.getId()).thenReturn(8L);
    when(uploadedVolume.getDataCenterId()).thenReturn(1L);
    when(uploadedVolume.getVolumeType()).thenReturn(Volume.Type.ROOT);
    when(uploadedVolume.getInstanceId()).thenReturn(null);
    when(uploadedVolume.getPoolId()).thenReturn(1L);
    when(uploadedVolume.getState()).thenReturn(Volume.State.Uploaded);
    when(_svc.volFactory.getVolume(8L)).thenReturn(uploadedVolume);
    VolumeVO upVolume=new VolumeVO("String_Node_Str",1L,1L,1L,1L,2L,"String_Node_Str","String_Node_Str",Storage.ProvisioningType.THIN,1,null,null,"String_Node_Str",Volume.Type.ROOT);
    upVolume.setPoolId(1L);
    upVolume.setDataCenterId(1L);
    upVolume.setState(Volume.State.Uploaded);
    when(_svc._volsDao.findById(8L)).thenReturn(upVolume);
    when(_svc._vmSnapshotDao.findByVm(any(Long.class))).thenReturn(new ArrayList<VMSnapshotVO>());
    when(_svc._vmInstanceDao.findById(any(Long.class))).thenReturn(stoppedVm);
  }
  finally {
    txn.close("String_Node_Str");
  }
  doNothing().when(_svc._accountMgr).checkAccess(any(Account.class),any(AccessType.class),any(ControlledEntity.class));
  doNothing().when(_svc._jobMgr).updateAsyncJobAttachment(any(Long.class),any(String.class),any(Long.class));
  when(_svc._jobMgr.submitAsyncJob(any(AsyncJobVO.class),any(String.class),any(Long.class))).thenReturn(1L);
}