JobAssignerUtilities { public Agent assignAgentToJob(Job job, List<Agent> agents) { Agent result = null; if (job.getStatus() == JobStatus.ASSIGNED) { Agent assignedAgent = agents.stream().filter(a -> a.getId().equals(job.getAssignedAgentId())).findFirst().orElse(null); result = assignedAgent; boolean isEligible = this.isAssignedAgentEligibleForJob(job, assignedAgent); if (!isEligible) { job.setStatus(JobStatus.UNASSIGNED); if (assignedAgent != null) { assignedAgent.setAssigned(false); } LOGGER.info(String.format("Job %s unassigned from Agent", job.getJobDefinitionName())); } } if (job.getStatus() == JobStatus.UNASSIGNED) { List<Agent> eligibleAgents = this.getEligibleAgentsForJob(job, agents); Agent agentForJob = this.pickMostSuitableAgent(eligibleAgents); if (agentForJob != null) { job.setAssignedAgentId(agentForJob.getId()); job.setStatus(JobStatus.ASSIGNED); agentForJob.setAssigned(true); result = agentForJob; LOGGER.info(String.format("Job %s assigned to Agent %s", job.getJobDefinitionName(), agentForJob.getName())); } } return result; }  Agent assignAgentToJob(Job job, List<Agent> agents); List<Agent> getEligibleAgentsForJob(Job job, List<Agent> agents); Agent pickMostSuitableAgent(List<Agent> agents); boolean isAgentEligibleForJob(Job job, Agent agent); boolean hasAssignableAgent(Job job, List<Agent> agents); boolean isAssignedAgentEligibleForJob(Job job, Agent agent);  }
@Test public void assignAgentToJob_awaitingJobViableAgent_assigned() { Job job = this.getJobWithResources(1); List<Agent> agents = this.getViableAgentsWithResources(1, 1); Agent actualResult = this.jobAssignerUtilities.assignAgentToJob(job, agents); Assert.assertTrue(actualResult.isAssigned()); Assert.assertEquals(JobStatus.ASSIGNED, job.getStatus()); Assert.assertEquals(actualResult.getId(), job.getAssignedAgentId()); }