HarvestNowController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aResp, Object aCmd, BindException aErrs) throws Exception { TargetInstanceCommand cmd = (TargetInstanceCommand) aCmd; if (!cmd.getCmd().equals(TargetInstanceCommand.ACTION_HARVEST)) { aReq.getSession().removeAttribute(TargetInstanceCommand.SESSION_TI); return new ModelAndView("redirect:/" + Constants.CNTRL_TI_QUEUE); } HashMap<String, HarvestAgentStatusDTO> agents = harvestCoordinator.getHarvestAgents(); TargetInstance ti = targetInstanceManager.getTargetInstance(cmd.getTargetInstanceId()); String instanceAgency = ti.getOwner().getAgency().getName(); String key = ""; HarvestAgentStatusDTO agent = null; HashMap<String, HarvestAgentStatusDTO> allowedAgents = new HashMap<String, HarvestAgentStatusDTO>(); Iterator<String> it = agents.keySet().iterator(); while (it.hasNext()) { key = (String) it.next(); agent = agents.get(key); if (agent.getAllowedAgencies().contains(instanceAgency) || agent.getAllowedAgencies().isEmpty()) { allowedAgents.put(key, agent); } } if (aErrs.hasErrors()) { ModelAndView mav = new ModelAndView(); mav.addObject(Constants.GBL_CMD_DATA, aCmd); mav.addObject(TargetInstanceCommand.MDL_AGENTS, allowedAgents); mav.addObject(Constants.GBL_ERRORS, aErrs); mav.addObject(TargetInstanceCommand.MDL_INSTANCE, ti); mav.setViewName(Constants.VIEW_HARVEST_NOW); return mav; } ModelAndView mav = new ModelAndView("redirect:/" + Constants.CNTRL_TI_QUEUE); HarvestAgentStatusDTO has = (HarvestAgentStatusDTO) harvestCoordinator.getHarvestAgents().get(cmd.getAgent()); if(harvestCoordinator.isQueuePaused()) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.instance.queue.paused", new Object[] { ti.getOid() }, Locale.getDefault())); } else if(!has.isAcceptTasks()) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.instance.agent.paused", new Object[] { ti.getOid(), has.getName() }, Locale.getDefault())); mav.addObject(Constants.GBL_CMD_DATA, aCmd); mav.addObject(TargetInstanceCommand.MDL_AGENTS, allowedAgents); mav.addObject(Constants.GBL_ERRORS, aErrs); mav.addObject(TargetInstanceCommand.MDL_INSTANCE, ti); mav.setViewName(Constants.VIEW_HARVEST_NOW); } else if(has.getMemoryWarning()) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.instance.agent.notaccept", new Object[] { ti.getOid(), has.getName() }, Locale.getDefault())); } else { try { harvestCoordinator.harvest(ti, has); } catch (HibernateOptimisticLockingFailureException e) { ti = targetInstanceManager.getTargetInstance(ti.getOid()); if (ti.getState().equals(TargetInstance.STATE_RUNNING) || ti.getState().equals(TargetInstance.STATE_STOPPING)) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.instance.run.by.other", new Object[] { ti.getOid() }, Locale.getDefault())); } } } return mav; } HarvestNowController(); void setHarvestCoordinator(HarvestCoordinator harvestCoordinator); void setTargetInstanceManager(TargetInstanceManager targetInstanceManager); void setMessageSource(MessageSource messageSource);  }
@Test public final void testProcessFormSubmission() throws Exception { TargetInstance ti = tidao.load(5001L); ti.setState(TargetInstance.STATE_SCHEDULED); HashMap<String, HarvesterStatusDTO> aHarvesterStatus = new HashMap<String, HarvesterStatusDTO>(); aHarvesterStatus.put("Target-5002", getStatusDTO("Running")); HarvestAgentStatusDTO aStatus = new HarvestAgentStatusDTO(); aStatus.setName("Test Agent"); aStatus.setHarvesterStatus(aHarvesterStatus); aStatus.setMaxHarvests(2); hc.heartbeat(aStatus); HttpServletRequest aReq = new MockHttpServletRequest(); HttpServletResponse aResp = new MockHttpServletResponse(); TargetInstanceCommand aCmd = new TargetInstanceCommand(); aCmd.setCmd(TargetInstanceCommand.ACTION_HARVEST); aCmd.setAgent("Test Agent"); aCmd.setTargetInstanceId(5001L); BindException aErrors = new BindException(aCmd, TargetInstanceCommand.ACTION_HARVEST); ModelAndView mav = testInstance.processFormSubmission(aReq, aResp, aCmd, aErrors); assertTrue(mav != null); assertTrue(ti.getState().equals(TargetInstance.STATE_RUNNING)); }
@Test public final void testProcessFormSubmissionPaused() { TargetInstance ti = tidao.load(5001L); ti.setState(TargetInstance.STATE_SCHEDULED); hc.pauseQueue(); HashMap<String, HarvesterStatusDTO> aHarvesterStatus = new HashMap<String, HarvesterStatusDTO>(); aHarvesterStatus.put("Target-5002", getStatusDTO("Running")); HarvestAgentStatusDTO aStatus = new HarvestAgentStatusDTO(); aStatus.setName("Test Agent"); aStatus.setHarvesterStatus(aHarvesterStatus); aStatus.setMaxHarvests(2); hc.heartbeat(aStatus); HttpServletRequest aReq = new MockHttpServletRequest(); HttpServletResponse aResp = new MockHttpServletResponse(); TargetInstanceCommand aCmd = new TargetInstanceCommand(); aCmd.setCmd(TargetInstanceCommand.ACTION_HARVEST); aCmd.setAgent("Test Agent"); aCmd.setTargetInstanceId(5001L); BindException aErrors = new BindException(aCmd, TargetInstanceCommand.ACTION_HARVEST); try { ModelAndView mav = testInstance.processFormSubmission(aReq, aResp, aCmd, aErrors); assertTrue(mav != null); assertTrue(ti.getState().equals(TargetInstance.STATE_SCHEDULED)); } catch(Exception e) { fail(e.getMessage()); } }
@Test public final void testProcessFormSubmissionAgentPaused() throws Exception { TargetInstance ti = tidao.load(5001L); ti.setState(TargetInstance.STATE_SCHEDULED); HashMap<String, HarvesterStatusDTO> aHarvesterStatus = new HashMap<String, HarvesterStatusDTO>(); aHarvesterStatus.put("Target-5002", getStatusDTO("Running")); HarvestAgentStatusDTO aStatus = new HarvestAgentStatusDTO(); aStatus.setName("Test Agent"); aStatus.setHarvesterStatus(aHarvesterStatus); aStatus.setMaxHarvests(2); aStatus.setAcceptTasks(false); hc.heartbeat(aStatus); HttpServletRequest aReq = new MockHttpServletRequest(); HttpServletResponse aResp = new MockHttpServletResponse(); TargetInstanceCommand aCmd = new TargetInstanceCommand(); aCmd.setCmd(TargetInstanceCommand.ACTION_HARVEST); aCmd.setAgent("Test Agent"); aCmd.setTargetInstanceId(5001L); BindException aErrors = new BindException(aCmd, TargetInstanceCommand.ACTION_HARVEST); ModelAndView mav = testInstance.processFormSubmission(aReq, aResp, aCmd, aErrors); assertTrue(mav != null); assertTrue(ti.getState().equals(TargetInstance.STATE_SCHEDULED)); }