@Test public void testUpdate() { final List<EducationalQualification> insertedEducationalQualifications = new ArrayList<>(); final List<EducationalQualification> updatedEducationalQualifications = new ArrayList<>(); final List<Long> deletedEducationalQualificationIds = new ArrayList<>(); final List<Long> deletedDocumentsReferenceIds = new ArrayList<>(); when(educationalQualificationRepository.findByEmployeeId(100L, "1")) .thenReturn(getEducationalQualificationsInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedEducationalQualificationIds.addAll((List<Long>) args[0]); return null; } }).when(educationalQualificationRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedEducationalQualifications.add((EducationalQualification) args[0]); return null; } }).when(educationalQualificationRepository).insert(Matchers.any(EducationalQualification.class), Matchers.anyLong()); 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()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); updatedEducationalQualifications.add((EducationalQualification) args[0]); return null; } }).when(educationalQualificationRepository).update(Matchers.any(EducationalQualification.class)); Employee employee = null; try { employee = getEmployee("org/egov/eis/service/EducationalQualificationService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } educationalQualificationService.update(employee); List<EducationalQualification> expectedInsertedEducationalQualifications = new ArrayList<>(); List<EducationalQualification> expectedUpdatedEducationalQualifications = new ArrayList<>(); List<Long> expecteddeletedEducationalQualificationIds = new ArrayList<>(); List<Long> expectedDeletedEducationalQualificationReferenceIds = new ArrayList<>(); expectedUpdatedEducationalQualifications.add(employee.getEducation().get(0)); expectedInsertedEducationalQualifications.add(employee.getEducation().get(1)); expectedInsertedEducationalQualifications.add(employee.getEducation().get(2)); expectedInsertedEducationalQualifications.add(employee.getEducation().get(3)); expecteddeletedEducationalQualificationIds.add(26L); expectedDeletedEducationalQualificationReferenceIds.add(26L); assertTrue(expectedInsertedEducationalQualifications.containsAll(insertedEducationalQualifications)); assertEquals(expectedInsertedEducationalQualifications.size(), insertedEducationalQualifications.size()); assertTrue(expectedUpdatedEducationalQualifications.containsAll(updatedEducationalQualifications)); assertEquals(expectedUpdatedEducationalQualifications.size(), updatedEducationalQualifications.size()); assertTrue(expecteddeletedEducationalQualificationIds.containsAll(deletedEducationalQualificationIds)); assertEquals(expecteddeletedEducationalQualificationIds.size(), deletedEducationalQualificationIds.size()); assertTrue(expectedDeletedEducationalQualificationReferenceIds.containsAll(deletedDocumentsReferenceIds)); assertEquals(expectedDeletedEducationalQualificationReferenceIds.size(), deletedDocumentsReferenceIds.size()); }
public void update(Employee employee) { if (isEmpty(employee.getEducation())) return; List<EducationalQualification> educations = educationalQualificationRepository .findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getEducation().forEach((education) -> { if (needsInsert(education, educations)) { educationalQualificationRepository.insert(education, employee.getId()); } else if (needsUpdate(education, educations)) { education.setTenantId(employee.getTenantId()); educationalQualificationRepository.update(education); } }); deleteEducationsInDBThatAreNotInInput(employee.getEducation(), educations, employee.getId(), employee.getTenantId()); }
EducationalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getEducation())) return; List<EducationalQualification> educations = educationalQualificationRepository .findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getEducation().forEach((education) -> { if (needsInsert(education, educations)) { educationalQualificationRepository.insert(education, employee.getId()); } else if (needsUpdate(education, educations)) { education.setTenantId(employee.getTenantId()); educationalQualificationRepository.update(education); } }); deleteEducationsInDBThatAreNotInInput(employee.getEducation(), educations, employee.getId(), employee.getTenantId()); } }
EducationalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getEducation())) return; List<EducationalQualification> educations = educationalQualificationRepository .findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getEducation().forEach((education) -> { if (needsInsert(education, educations)) { educationalQualificationRepository.insert(education, employee.getId()); } else if (needsUpdate(education, educations)) { education.setTenantId(employee.getTenantId()); educationalQualificationRepository.update(education); } }); deleteEducationsInDBThatAreNotInInput(employee.getEducation(), educations, employee.getId(), employee.getTenantId()); }  }
EducationalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getEducation())) return; List<EducationalQualification> educations = educationalQualificationRepository .findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getEducation().forEach((education) -> { if (needsInsert(education, educations)) { educationalQualificationRepository.insert(education, employee.getId()); } else if (needsUpdate(education, educations)) { education.setTenantId(employee.getTenantId()); educationalQualificationRepository.update(education); } }); deleteEducationsInDBThatAreNotInInput(employee.getEducation(), educations, employee.getId(), employee.getTenantId()); }  void update(Employee employee); }
EducationalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getEducation())) return; List<EducationalQualification> educations = educationalQualificationRepository .findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getEducation().forEach((education) -> { if (needsInsert(education, educations)) { educationalQualificationRepository.insert(education, employee.getId()); } else if (needsUpdate(education, educations)) { education.setTenantId(employee.getTenantId()); educationalQualificationRepository.update(education); } }); deleteEducationsInDBThatAreNotInInput(employee.getEducation(), educations, employee.getId(), employee.getTenantId()); }  void update(Employee employee);  }