@Test public void testUpdate() { final List<Probation> insertedProbations = new ArrayList<>(); final List<Probation> updatedProbations = new ArrayList<>(); final List<Long> deletedProbationIds = new ArrayList<>(); final List<Long> deletedDocumentsReferenceIds = new ArrayList<>(); when(probationRepository.findByEmployeeId(100L, "1")).thenReturn(getProbationsInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedProbationIds.addAll((List<Long>) args[0]); return null; } }).when(probationRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedProbations.add((Probation) args[0]); return null; } }).when(probationRepository).insert(Matchers.any(Probation.class), Matchers.anyLong()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); updatedProbations.add((Probation) args[0]); return null; } }).when(probationRepository).update(Matchers.any(Probation.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/ProbationService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } probationService.update(employee); List<Probation> expectedInsertedProbations = new ArrayList<>(); List<Probation> expectedUpdatedProbations = new ArrayList<>(); List<Long> expectedDeletedProbationIds = new ArrayList<>(); List<Long> expectedDeletedDocumentsReferenceIds = new ArrayList<>(); expectedInsertedProbations.add(employee.getProbation().get(1)); expectedUpdatedProbations.add(employee.getProbation().get(0)); expectedDeletedProbationIds.add(10L); expectedDeletedDocumentsReferenceIds.add(10L); assertTrue(expectedInsertedProbations.containsAll(insertedProbations)); assertEquals(expectedInsertedProbations.size(), insertedProbations.size()); assertTrue(expectedUpdatedProbations.containsAll(updatedProbations)); assertEquals(expectedUpdatedProbations.size(), updatedProbations.size()); assertTrue(expectedDeletedProbationIds.containsAll(deletedProbationIds)); assertEquals(expectedDeletedProbationIds.size(), deletedProbationIds.size()); assertTrue(expectedDeletedDocumentsReferenceIds.containsAll(deletedDocumentsReferenceIds)); assertEquals(expectedDeletedDocumentsReferenceIds.size(), deletedDocumentsReferenceIds.size()); }
public void update(Employee employee) { if (isEmpty(employee.getProbation())) return; List<Probation> probations = probationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getProbation().forEach((probation) -> { if (needsInsert(probation, probations)) { probationRepository.insert(probation, employee.getId()); } else if (needsUpdate(probation, probations)) { probation.setTenantId(employee.getTenantId()); probationRepository.update(probation); } }); deleteProbationsInDBThatAreNotInInput(employee.getProbation(), probations, employee.getId(), employee.getTenantId()); }
ProbationService { public void update(Employee employee) { if (isEmpty(employee.getProbation())) return; List<Probation> probations = probationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getProbation().forEach((probation) -> { if (needsInsert(probation, probations)) { probationRepository.insert(probation, employee.getId()); } else if (needsUpdate(probation, probations)) { probation.setTenantId(employee.getTenantId()); probationRepository.update(probation); } }); deleteProbationsInDBThatAreNotInInput(employee.getProbation(), probations, employee.getId(), employee.getTenantId()); } }
ProbationService { public void update(Employee employee) { if (isEmpty(employee.getProbation())) return; List<Probation> probations = probationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getProbation().forEach((probation) -> { if (needsInsert(probation, probations)) { probationRepository.insert(probation, employee.getId()); } else if (needsUpdate(probation, probations)) { probation.setTenantId(employee.getTenantId()); probationRepository.update(probation); } }); deleteProbationsInDBThatAreNotInInput(employee.getProbation(), probations, employee.getId(), employee.getTenantId()); }  }
ProbationService { public void update(Employee employee) { if (isEmpty(employee.getProbation())) return; List<Probation> probations = probationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getProbation().forEach((probation) -> { if (needsInsert(probation, probations)) { probationRepository.insert(probation, employee.getId()); } else if (needsUpdate(probation, probations)) { probation.setTenantId(employee.getTenantId()); probationRepository.update(probation); } }); deleteProbationsInDBThatAreNotInInput(employee.getProbation(), probations, employee.getId(), employee.getTenantId()); }  void update(Employee employee); }
ProbationService { public void update(Employee employee) { if (isEmpty(employee.getProbation())) return; List<Probation> probations = probationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getProbation().forEach((probation) -> { if (needsInsert(probation, probations)) { probationRepository.insert(probation, employee.getId()); } else if (needsUpdate(probation, probations)) { probation.setTenantId(employee.getTenantId()); probationRepository.update(probation); } }); deleteProbationsInDBThatAreNotInInput(employee.getProbation(), probations, employee.getId(), employee.getTenantId()); }  void update(Employee employee);  }