@Test public void testUpdate() { final List<ServiceHistory> insertedServiceHistories = new ArrayList<>(); final List<ServiceHistory> updatedServiceHistories = new ArrayList<>(); final List<Long> deletedServiceHistoryIds = new ArrayList<>(); final List<Long> deletedDocumentsReferenceIds = new ArrayList<>(); when(serviceHistoryRepository.findByEmployeeId(100L, "1")).thenReturn(getServiceHistoriesInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedServiceHistoryIds.addAll((List<Long>) args[0]); return null; } }).when(serviceHistoryRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedServiceHistories.add((ServiceHistory) args[0]); return null; } }).when(serviceHistoryRepository).insert(Matchers.any(ServiceHistory.class), Matchers.anyLong()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); updatedServiceHistories.add((ServiceHistory) args[0]); return null; } }).when(serviceHistoryRepository).update(Matchers.any(ServiceHistory.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/ServiceHistoryService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } serviceHistoryService.update(employee); List<ServiceHistory> expectedInsertedServiceHistories = new ArrayList<>(); List<ServiceHistory> expectedUpdatedServiceHistories = new ArrayList<>(); List<Long> expectedDeletedServiceHistoryIds = new ArrayList<>(); List<Long> expectedDeletedDocumentsReferenceIds = new ArrayList<>(); expectedInsertedServiceHistories.add(employee.getServiceHistory().get(1)); expectedUpdatedServiceHistories.add(employee.getServiceHistory().get(0)); expectedDeletedServiceHistoryIds.add(10L); expectedDeletedDocumentsReferenceIds.add(10L); assertTrue(expectedInsertedServiceHistories.containsAll(insertedServiceHistories)); assertEquals(expectedInsertedServiceHistories.size(), insertedServiceHistories.size()); assertTrue(expectedUpdatedServiceHistories.containsAll(updatedServiceHistories)); assertEquals(expectedUpdatedServiceHistories.size(), updatedServiceHistories.size()); assertTrue(expectedDeletedServiceHistoryIds.containsAll(deletedServiceHistoryIds)); assertEquals(expectedDeletedServiceHistoryIds.size(), deletedServiceHistoryIds.size()); assertTrue(expectedDeletedDocumentsReferenceIds.containsAll(deletedDocumentsReferenceIds)); assertEquals(expectedDeletedDocumentsReferenceIds.size(), deletedDocumentsReferenceIds.size()); }
public void update(Employee employee) { List<ServiceHistory> services = serviceHistoryRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getServiceHistory())) employee.getServiceHistory().forEach((service) -> { if (needsInsert(service, services)) { serviceHistoryRepository.insert(service, employee.getId()); } else if (needsUpdate(service, services)) { service.setTenantId(employee.getTenantId()); serviceHistoryRepository.update(service); } }); deleteInDBThatAreNotInInput(employee.getServiceHistory(), services, employee.getId(), employee.getTenantId()); }
ServiceHistoryService { public void update(Employee employee) { List<ServiceHistory> services = serviceHistoryRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getServiceHistory())) employee.getServiceHistory().forEach((service) -> { if (needsInsert(service, services)) { serviceHistoryRepository.insert(service, employee.getId()); } else if (needsUpdate(service, services)) { service.setTenantId(employee.getTenantId()); serviceHistoryRepository.update(service); } }); deleteInDBThatAreNotInInput(employee.getServiceHistory(), services, employee.getId(), employee.getTenantId()); } }
ServiceHistoryService { public void update(Employee employee) { List<ServiceHistory> services = serviceHistoryRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getServiceHistory())) employee.getServiceHistory().forEach((service) -> { if (needsInsert(service, services)) { serviceHistoryRepository.insert(service, employee.getId()); } else if (needsUpdate(service, services)) { service.setTenantId(employee.getTenantId()); serviceHistoryRepository.update(service); } }); deleteInDBThatAreNotInInput(employee.getServiceHistory(), services, employee.getId(), employee.getTenantId()); }  }
ServiceHistoryService { public void update(Employee employee) { List<ServiceHistory> services = serviceHistoryRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getServiceHistory())) employee.getServiceHistory().forEach((service) -> { if (needsInsert(service, services)) { serviceHistoryRepository.insert(service, employee.getId()); } else if (needsUpdate(service, services)) { service.setTenantId(employee.getTenantId()); serviceHistoryRepository.update(service); } }); deleteInDBThatAreNotInInput(employee.getServiceHistory(), services, employee.getId(), employee.getTenantId()); }  void update(Employee employee); }
ServiceHistoryService { public void update(Employee employee) { List<ServiceHistory> services = serviceHistoryRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getServiceHistory())) employee.getServiceHistory().forEach((service) -> { if (needsInsert(service, services)) { serviceHistoryRepository.insert(service, employee.getId()); } else if (needsUpdate(service, services)) { service.setTenantId(employee.getTenantId()); serviceHistoryRepository.update(service); } }); deleteInDBThatAreNotInInput(employee.getServiceHistory(), services, employee.getId(), employee.getTenantId()); }  void update(Employee employee);  }