@Test public void testUpdate() { final List<Assignment> insertedAssignments = new ArrayList<>(); final List<Assignment> updatedAssignments = new ArrayList<>(); final List<Long> deletedAssignmentIds = new ArrayList<>(); final List<Assignment> insertedHodDepartmentAlongWithAssignment = new ArrayList<>(); final List<String> insertedHodDepartmentIdsDuringUpdate = new ArrayList<>(); final List<String> deletedHodDepartmentIds = new ArrayList<>(); final List<Long> deletedDocumentsReferenceIds = new ArrayList<>(); when(assignmentRepository.findByEmployeeId(100L, "1")).thenReturn(getAssignmentsInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedAssignmentIds.addAll((List<Long>) args[0]); return null; } }).when(assignmentRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedAssignments.add((Assignment) args[0]); return null; } }).when(assignmentRepository).insert(Matchers.any(Assignment.class), Matchers.anyLong()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); updatedAssignments.add((Assignment) args[0]); return null; } }).when(assignmentRepository).update(Matchers.any(Assignment.class)); when(hodDepartmentRepository.findByAssignmentId(10L, "1")).thenReturn(getHodsInDbForAssignment()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedHodDepartmentIds.addAll((List<String>) args[0]); return null; } }).when(hodDepartmentRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedHodDepartmentIdsDuringUpdate.add(args[0].toString()); return null; } }).when(hodDepartmentRepository).insert(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedHodDepartmentAlongWithAssignment.add((Assignment) args[0]); return null; } }).when(hodDepartmentRepository).save(Matchers.any(Assignment.class), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedDocumentsReferenceIds.addAll((List<Long>) args[2]); return null; } }).when(employeeDocumentsRepository).deleteForReferenceIds(Matchers.anyLong(), Matchers.any(EntityType.class), Matchers.anyListOf(Long.class), Matchers.anyString()); Employee employee = null; try { employee = getEmployee("org/egov/eis/service/AssignmentService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } assignmentService.update(employee); List<Assignment> expectedInsertedAssignments = new ArrayList<>(); List<Assignment> expectedUpdatedAssignments = new ArrayList<>(); List<Long> expectedDeletedAssignmentIds = new ArrayList<>(); List<Assignment> expectedInsertedHodAlongWithAssignment = new ArrayList<>(); List<String> expectedInsertedHodDepartmentIdsDuringUpdate = new ArrayList<>(); List<String> expectedDeletedHodDepartmentIds = new ArrayList<>(); List<String> expectedDeletedDocumentsReferenceIds = new ArrayList<>(); expectedInsertedAssignments.add(employee.getAssignments().get(2)); expectedInsertedHodAlongWithAssignment.add(employee.getAssignments().get(2)); expectedInsertedHodDepartmentIdsDuringUpdate .add(employee.getAssignments().get(0).getHod().get(0).getDepartment()); expectedUpdatedAssignments.add(employee.getAssignments().get(0)); expectedUpdatedAssignments.add(employee.getAssignments().get(1)); expectedDeletedAssignmentIds.add(10L); expectedDeletedHodDepartmentIds.add("10"); expectedDeletedDocumentsReferenceIds.add("10"); assertTrue(expectedInsertedAssignments.containsAll(insertedAssignments)); assertEquals(expectedInsertedAssignments.size(), insertedAssignments.size()); assertTrue(expectedUpdatedAssignments.containsAll(updatedAssignments)); assertEquals(expectedUpdatedAssignments.size(), updatedAssignments.size()); assertTrue(expectedDeletedAssignmentIds.containsAll(deletedAssignmentIds)); assertEquals(expectedDeletedAssignmentIds.size(), deletedAssignmentIds.size()); assertTrue(expectedInsertedHodAlongWithAssignment.containsAll(insertedHodDepartmentAlongWithAssignment)); assertEquals(expectedInsertedHodAlongWithAssignment.size(), insertedHodDepartmentAlongWithAssignment.size()); assertTrue(expectedInsertedHodDepartmentIdsDuringUpdate.containsAll(insertedHodDepartmentIdsDuringUpdate)); assertEquals(expectedInsertedHodDepartmentIdsDuringUpdate.size(), insertedHodDepartmentIdsDuringUpdate.size()); assertEquals(expectedDeletedHodDepartmentIds.size(), deletedHodDepartmentIds.size()); assertEquals(expectedDeletedDocumentsReferenceIds.size(), deletedDocumentsReferenceIds.size()); }
public void update(Employee employee) { List<Assignment> assignments = assignmentRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getAssignments().forEach((assignment) -> { if (needsInsert(assignment, assignments)) { assignmentRepository.insert(assignment, employee.getId()); if (assignment.getHod() != null) { hodDepartmentRepository.save(assignment, employee.getTenantId()); } } else if (needsUpdate(assignment, assignments)) { assignment.setTenantId(employee.getTenantId()); assignmentRepository.update(assignment); if (!isEmpty(assignment.getHod())) { updateHod(assignment, employee.getTenantId()); } } }); deleteAssignmentsInDBThatAreNotInInput(employee.getAssignments(), assignments, employee.getId(), employee.getTenantId()); }
AssignmentService { public void update(Employee employee) { List<Assignment> assignments = assignmentRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getAssignments().forEach((assignment) -> { if (needsInsert(assignment, assignments)) { assignmentRepository.insert(assignment, employee.getId()); if (assignment.getHod() != null) { hodDepartmentRepository.save(assignment, employee.getTenantId()); } } else if (needsUpdate(assignment, assignments)) { assignment.setTenantId(employee.getTenantId()); assignmentRepository.update(assignment); if (!isEmpty(assignment.getHod())) { updateHod(assignment, employee.getTenantId()); } } }); deleteAssignmentsInDBThatAreNotInInput(employee.getAssignments(), assignments, employee.getId(), employee.getTenantId()); } }
AssignmentService { public void update(Employee employee) { List<Assignment> assignments = assignmentRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getAssignments().forEach((assignment) -> { if (needsInsert(assignment, assignments)) { assignmentRepository.insert(assignment, employee.getId()); if (assignment.getHod() != null) { hodDepartmentRepository.save(assignment, employee.getTenantId()); } } else if (needsUpdate(assignment, assignments)) { assignment.setTenantId(employee.getTenantId()); assignmentRepository.update(assignment); if (!isEmpty(assignment.getHod())) { updateHod(assignment, employee.getTenantId()); } } }); deleteAssignmentsInDBThatAreNotInInput(employee.getAssignments(), assignments, employee.getId(), employee.getTenantId()); }  }
AssignmentService { public void update(Employee employee) { List<Assignment> assignments = assignmentRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getAssignments().forEach((assignment) -> { if (needsInsert(assignment, assignments)) { assignmentRepository.insert(assignment, employee.getId()); if (assignment.getHod() != null) { hodDepartmentRepository.save(assignment, employee.getTenantId()); } } else if (needsUpdate(assignment, assignments)) { assignment.setTenantId(employee.getTenantId()); assignmentRepository.update(assignment); if (!isEmpty(assignment.getHod())) { updateHod(assignment, employee.getTenantId()); } } }); deleteAssignmentsInDBThatAreNotInInput(employee.getAssignments(), assignments, employee.getId(), employee.getTenantId()); }  List<Assignment> getAssignments(Long employeeId, AssignmentGetRequest assignmentGetRequest); void update(Employee employee); }
AssignmentService { public void update(Employee employee) { List<Assignment> assignments = assignmentRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getAssignments().forEach((assignment) -> { if (needsInsert(assignment, assignments)) { assignmentRepository.insert(assignment, employee.getId()); if (assignment.getHod() != null) { hodDepartmentRepository.save(assignment, employee.getTenantId()); } } else if (needsUpdate(assignment, assignments)) { assignment.setTenantId(employee.getTenantId()); assignmentRepository.update(assignment); if (!isEmpty(assignment.getHod())) { updateHod(assignment, employee.getTenantId()); } } }); deleteAssignmentsInDBThatAreNotInInput(employee.getAssignments(), assignments, employee.getId(), employee.getTenantId()); }  List<Assignment> getAssignments(Long employeeId, AssignmentGetRequest assignmentGetRequest); void update(Employee employee);  }