HarvestAgentManagerImpl implements HarvestAgentManager { @Override public void abort(TargetInstance aTargetInstance) { aTargetInstance.setState(TargetInstance.STATE_ABORTED); targetInstanceDao.save(aTargetInstance); HarvestAgentStatusDTO status = getHarvestAgentStatusFor(aTargetInstance.getJobName()); if (status == null) { log.warn("ABORT Failed. Failed to find the Harvest Agent for the Job {}.", aTargetInstance.getJobName()); } else { HarvestAgent agent = harvestAgentFactory.getHarvestAgent(status.getHost(), status.getPort(), status.getService()); try { agent.abort(aTargetInstance.getJobName()); } catch (RuntimeException e) { log.warn("ABORT Failed. Failed Abort the Job {} on the Harvest Agent {}.", aTargetInstance.getJobName(), agent.getName()); } } }  @Override void heartbeat(HarvestAgentStatusDTO aStatus); @Override void updateProfileOverrides(TargetInstance aTargetInstance, String profileString); @Override void pause(TargetInstance aTargetInstance); @Override void resume(TargetInstance aTargetInstance); @Override void abort(TargetInstance aTargetInstance); @Override void stop(TargetInstance aTargetInstance); @Override void pauseAll(); @Override void resumeAll(); @Override LogReader getLogReader(TargetInstance aTargetInstance); @Override void pauseAgent(String agentName); @Override void resumeAgent(String agentName); @Override boolean runningOrPaused(TargetInstance aTargetInstance); @Override void restrictBandwidthFor(TargetInstance targetInstance); @Override List<HarvestAgentStatusDTO> getHarvestersForAgency(String agencyName); @Override void markDead(HarvestAgentStatusDTO agent); @Override void initiateHarvest(HarvestAgentStatusDTO aHarvestAgent, TargetInstance aTargetInstance, String profile,
			String seedsString); @Override void recoverHarvests(String haHost, int haPort, String haService, List<String> activeJobs); @Override boolean lock(Long tiOid); @Override void unLock(Long tiOid); @Override HashMap<String, HarvestAgentStatusDTO> getHarvestAgents(); void setTargetInstanceManager(TargetInstanceManager targetInstanceManager); void setHarvestAgentFactory(HarvestAgentFactory harvestAgentFactory); void setTargetInstanceDao(TargetInstanceDAO targetInstanceDao); @Override void purgeAbortedTargetInstances(String[] tiNames); @Override List<HarvestAgentStatusDTO> getAvailableHarvesters(String agencyName); @Override HarvestAgentStatusDTO getHarvester(String agencyName, String harvesterType);  }
@Test public void testAbortTargetInstance() { Long tOid = 123L; TargetInstance mockTargetInstance = mock(TargetInstance.class); HarvestAgentStatusDTO agentStatusDTO2 = setupHarvestAgentWithHarvestState(tOid, "irrelevant", mockTargetInstance); underTest.harvestAgents.put("test1", agentStatusDTO2); HarvestAgent mockHarvestAgent = mock(HarvestAgent.class); when(mockHarvestAgentFactory.getHarvestAgent(anyString(), anyInt(), anyString())).thenReturn(mockHarvestAgent); underTest.abort(mockTargetInstance); verify(mockTargetInstance).setState(TargetInstance.STATE_ABORTED); verify(mockTargetInstanceDAO).save(mockTargetInstance); verify(mockHarvestAgent).abort(mockTargetInstance.getJobName()); }
@Test public void testAbortTargetInstanceRuntimeException() { Long tOid = 123L; TargetInstance mockTargetInstance = mock(TargetInstance.class); HarvestAgentStatusDTO agentStatusDTO2 = setupHarvestAgentWithHarvestState(tOid, "irrelevant", mockTargetInstance); underTest.harvestAgents.put("test1", agentStatusDTO2); HarvestAgent mockHarvestAgent = mock(HarvestAgent.class); when(mockHarvestAgentFactory.getHarvestAgent(anyString(), anyInt(), anyString())).thenReturn(mockHarvestAgent); doThrow(new RuntimeException()).when(mockHarvestAgent).abort(anyString()); underTest.abort(mockTargetInstance); verify(mockTargetInstance).setState(TargetInstance.STATE_ABORTED); verify(mockTargetInstanceDAO).save(mockTargetInstance); }
@Test public void testAbortTargetInstanceNoStatus() { TargetInstance mockTargetInstance = mock(TargetInstance.class); HarvestAgent mockHarvestAgent = mock(HarvestAgent.class); when(mockHarvestAgentFactory.getHarvestAgent(anyString(), anyInt(), anyString())).thenReturn(mockHarvestAgent); underTest.abort(mockTargetInstance); verify(mockTargetInstance).setState(TargetInstance.STATE_ABORTED); verify(mockTargetInstanceDAO).save(mockTargetInstance); verifyNoMoreInteractions(mockHarvestAgent); }