TaskService extends AbstractAdoService<Task> { public User getTaskAssignee(Contact contact) throws TaskCreationException { User assignee = null; if (contact.getContactOfficer() != null) { assignee = contact.getContactOfficer(); } else { List<User> officers = new ArrayList<>(); if (contact.getDistrict() != null) { officers = userService.getAllByDistrict(contact.getDistrict(), false, UserRole.CONTACT_OFFICER); } if (officers.isEmpty() && contact.getPerson().getAddress().getDistrict() != null) { officers = userService.getAllByDistrict(contact.getPerson().getAddress().getDistrict(), false, UserRole.CONTACT_OFFICER); } if (officers.isEmpty() && contact.getCaze() != null && contact.getCaze().getDistrict() != null) { officers = userService.getAllByDistrict(contact.getCaze().getDistrict(), false, UserRole.CONTACT_OFFICER); } if (!officers.isEmpty()) { Random rand = new Random(); assignee = officers.get(rand.nextInt(officers.size())); } } if (assignee == null) { List<User> supervisors = new ArrayList<>(); if (contact.getRegion() != null) { supervisors = userService.getAllByRegionAndUserRoles(contact.getRegion(), UserRole.CONTACT_SUPERVISOR); } if (supervisors.isEmpty() && contact.getPerson().getAddress().getRegion() != null) { supervisors = userService.getAllByRegionAndUserRoles(contact.getPerson().getAddress().getRegion(), UserRole.CONTACT_SUPERVISOR); } if (supervisors.isEmpty() && contact.getCaze() != null && contact.getCaze().getDistrict() != null) { supervisors = userService.getAllByRegionAndUserRoles(contact.getCaze().getRegion(), UserRole.CONTACT_SUPERVISOR); } if (!supervisors.isEmpty()) { Random rand = new Random(); assignee = supervisors.get(rand.nextInt(supervisors.size())); } else { throw new TaskCreationException("Contact has not contact officer and no region - can't create follow-up task: " + contact.getUuid()); } } return assignee; } TaskService(); List<Task> getAllActiveTasksAfter(Date date, User user); List<String> getAllActiveUuids(User user); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Task> taskPath); Predicate createAssigneeFilter(CriteriaBuilder cb, Join<?, User> assigneeUserJoin); long getCount(TaskCriteria taskCriteria); List<Task> findBy(TaskCriteria taskCriteria, boolean ignoreUserFilter); Predicate buildCriteriaFilter(TaskCriteria taskCriteria, CriteriaBuilder cb, Root<Task> from); Task buildTask(User creatorUser); User getTaskAssignee(Contact contact);  }
@Test public void testGetTaskAssigneeFromContactWithContactOfficer() throws TaskCreationException { User contactOfficer = new User(); contactOfficer.setId(1L); Contact contact = new Contact(); contact.setContactOfficer(contactOfficer); User actualAssignee = taskService.getTaskAssignee(contact); assertEquals(actualAssignee.getId(), contactOfficer.getId()); }
@Test public void testGetTaskAssigneeFromDistrictOfficers() throws TaskCreationException { User contactOfficer = new User(); contactOfficer.setId(1L); District district = new District(); Contact contact = new Contact(); contact.setDistrict(district); Mockito.when(userService.getAllByDistrict(any(District.class), anyBoolean(), anyObject())) .thenReturn(Collections.singletonList(contactOfficer)); User actualAssignee = taskService.getTaskAssignee(contact); assertEquals(actualAssignee.getId(), contactOfficer.getId()); }
@Test public void testGetTaskAssigneeFromRegionSupervisors() throws TaskCreationException { User contactOfficer = new User(); contactOfficer.setId(1L); Contact contact = new Contact(); Location location = new Location(); Person person = new Person(); person.setAddress(location); contact.setPerson(person); Region region = new Region(); contact.setRegion(region); Mockito.when(userService.getAllByRegionAndUserRoles(any(Region.class), anyObject())).thenReturn(Collections.singletonList(contactOfficer)); User actualAssignee = taskService.getTaskAssignee(contact); assertEquals(actualAssignee.getId(), contactOfficer.getId()); }
@Test(expected = TaskCreationException.class) public void testGetTaskAssigneeException() throws TaskCreationException { Contact contact = new Contact(); Location location = new Location(); Person person = new Person(); person.setAddress(location); contact.setPerson(person); Case caze = new Case(); contact.setCaze(caze); Mockito.when(userService.getAllByRegionAndUserRoles(any(Region.class), anyObject())).thenReturn(Collections.emptyList()); taskService.getTaskAssignee(contact); }