@Test public void testUpdate() { final List<TechnicalQualification> insertedTechnicals = new ArrayList<>(); final List<TechnicalQualification> updatedTechnicals = new ArrayList<>(); final List<Long> deletedTechnicalIds = new ArrayList<>(); final List<Long> deletedDocumentsReferenceIds = new ArrayList<>(); when(technicalQualificationRepository.findByEmployeeId(100L, "1")).thenReturn(getTechnicalsInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedTechnicalIds.addAll((List<Long>) args[0]); return null; } }).when(technicalQualificationRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedTechnicals.add((TechnicalQualification) args[0]); return null; } }).when(technicalQualificationRepository).insert(Matchers.any(TechnicalQualification.class), Matchers.anyLong()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); updatedTechnicals.add((TechnicalQualification) args[0]); return null; } }).when(technicalQualificationRepository).update(Matchers.any(TechnicalQualification.class)); 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/TechnicalQualificationService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } technicalQualificationService.update(employee); List<TechnicalQualification> expectedInsertedTechnicals = new ArrayList<>(); List<TechnicalQualification> expectedUpdatedTechnicals = new ArrayList<>(); List<Long> expectedDeletedTechnicalIds = new ArrayList<>(); List<Long> expectedDeletedDocumentsReferenceIds = new ArrayList<>(); expectedInsertedTechnicals.add(employee.getTechnical().get(0)); expectedUpdatedTechnicals.add(employee.getTechnical().get(1)); expectedDeletedTechnicalIds.add(10L); expectedDeletedDocumentsReferenceIds.add(10L); assertTrue(expectedInsertedTechnicals.containsAll(insertedTechnicals)); assertEquals(expectedInsertedTechnicals.size(), insertedTechnicals.size()); assertTrue(expectedUpdatedTechnicals.containsAll(updatedTechnicals)); assertEquals(expectedUpdatedTechnicals.size(), updatedTechnicals.size()); assertTrue(expectedDeletedTechnicalIds.containsAll(deletedTechnicalIds)); assertEquals(expectedDeletedTechnicalIds.size(), deletedTechnicalIds.size()); assertTrue(expectedDeletedDocumentsReferenceIds.containsAll(deletedDocumentsReferenceIds)); assertEquals(expectedDeletedDocumentsReferenceIds.size(), deletedDocumentsReferenceIds.size()); }
public void update(Employee employee) { if (isEmpty(employee.getTechnical())) return; List<TechnicalQualification> technicals = technicalQualificationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getTechnical().forEach((technical) -> { if (needsInsert(technical, technicals)) { technicalQualificationRepository.insert(technical, employee.getId()); } else if (needsUpdate(technical, technicals)) { technical.setTenantId(employee.getTenantId()); technicalQualificationRepository.update(technical); } }); deleteTechnicalsInDBThatAreNotInInput(employee.getTechnical(), technicals, employee.getId(), employee.getTenantId()); }
TechnicalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getTechnical())) return; List<TechnicalQualification> technicals = technicalQualificationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getTechnical().forEach((technical) -> { if (needsInsert(technical, technicals)) { technicalQualificationRepository.insert(technical, employee.getId()); } else if (needsUpdate(technical, technicals)) { technical.setTenantId(employee.getTenantId()); technicalQualificationRepository.update(technical); } }); deleteTechnicalsInDBThatAreNotInInput(employee.getTechnical(), technicals, employee.getId(), employee.getTenantId()); } }
TechnicalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getTechnical())) return; List<TechnicalQualification> technicals = technicalQualificationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getTechnical().forEach((technical) -> { if (needsInsert(technical, technicals)) { technicalQualificationRepository.insert(technical, employee.getId()); } else if (needsUpdate(technical, technicals)) { technical.setTenantId(employee.getTenantId()); technicalQualificationRepository.update(technical); } }); deleteTechnicalsInDBThatAreNotInInput(employee.getTechnical(), technicals, employee.getId(), employee.getTenantId()); }  }
TechnicalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getTechnical())) return; List<TechnicalQualification> technicals = technicalQualificationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getTechnical().forEach((technical) -> { if (needsInsert(technical, technicals)) { technicalQualificationRepository.insert(technical, employee.getId()); } else if (needsUpdate(technical, technicals)) { technical.setTenantId(employee.getTenantId()); technicalQualificationRepository.update(technical); } }); deleteTechnicalsInDBThatAreNotInInput(employee.getTechnical(), technicals, employee.getId(), employee.getTenantId()); }  void update(Employee employee); }
TechnicalQualificationService { public void update(Employee employee) { if (isEmpty(employee.getTechnical())) return; List<TechnicalQualification> technicals = technicalQualificationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getTechnical().forEach((technical) -> { if (needsInsert(technical, technicals)) { technicalQualificationRepository.insert(technical, employee.getId()); } else if (needsUpdate(technical, technicals)) { technical.setTenantId(employee.getTenantId()); technicalQualificationRepository.update(technical); } }); deleteTechnicalsInDBThatAreNotInInput(employee.getTechnical(), technicals, employee.getId(), employee.getTenantId()); }  void update(Employee employee);  }