@Test public void testUpdate() { final List<Regularisation> insertedRegularisations = new ArrayList<>(); final List<Regularisation> updatedRegularisations = new ArrayList<>(); final List<Long> deletedRegularisationIds = new ArrayList<>(); final List<Long> deletedDocumentsReferenceIds = new ArrayList<>(); when(regularisationRepository.findByEmployeeId(100L, "1")).thenReturn(getRegularisationsInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedRegularisationIds.addAll((List<Long>) args[0]); return null; } }).when(regularisationRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedRegularisations.add((Regularisation) args[0]); return null; } }).when(regularisationRepository).insert(Matchers.any(Regularisation.class), Matchers.anyLong()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); updatedRegularisations.add((Regularisation) args[0]); return null; } }).when(regularisationRepository).update(Matchers.any(Regularisation.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/RegularisationService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } regularisationService.update(employee); List<Regularisation> expectedInsertedRegularisations = new ArrayList<>(); List<Regularisation> expectedUpdatedRegularisations = new ArrayList<>(); List<Long> expectedDeletedRegularisationIds = new ArrayList<>(); List<Long> expectedDeletedDocumentsReferenceIds = new ArrayList<>(); expectedInsertedRegularisations.add(employee.getRegularisation().get(0)); expectedInsertedRegularisations.add(employee.getRegularisation().get(1)); expectedUpdatedRegularisations.add(employee.getRegularisation().get(2)); expectedDeletedRegularisationIds.add(105L); expectedDeletedDocumentsReferenceIds.add(105L); assertTrue(expectedInsertedRegularisations.containsAll(insertedRegularisations)); assertEquals(expectedInsertedRegularisations.size(), insertedRegularisations.size()); assertTrue(expectedUpdatedRegularisations.containsAll(updatedRegularisations)); assertEquals(expectedUpdatedRegularisations.size(), updatedRegularisations.size()); assertTrue(expectedDeletedRegularisationIds.containsAll(deletedRegularisationIds)); assertEquals(expectedDeletedRegularisationIds.size(), deletedRegularisationIds.size()); assertTrue(expectedDeletedDocumentsReferenceIds.containsAll(deletedDocumentsReferenceIds)); assertEquals(expectedDeletedDocumentsReferenceIds.size(), deletedDocumentsReferenceIds.size()); }
public void update(Employee employee) { List<Regularisation> regularisations = regularisationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getRegularisation())) { employee.getRegularisation().forEach((regularisation) -> { if (needsInsert(regularisation, regularisations)) { regularisationRepository.insert(regularisation, employee.getId()); } else if (needsUpdate(regularisation, regularisations)) { regularisation.setTenantId(employee.getTenantId()); regularisationRepository.update(regularisation); } }); } deleteRegularisationsInDBThatAreNotInInput(employee.getRegularisation(), regularisations, employee.getId(),employee.getTenantId()); }
RegularisationService { public void update(Employee employee) { List<Regularisation> regularisations = regularisationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getRegularisation())) { employee.getRegularisation().forEach((regularisation) -> { if (needsInsert(regularisation, regularisations)) { regularisationRepository.insert(regularisation, employee.getId()); } else if (needsUpdate(regularisation, regularisations)) { regularisation.setTenantId(employee.getTenantId()); regularisationRepository.update(regularisation); } }); } deleteRegularisationsInDBThatAreNotInInput(employee.getRegularisation(), regularisations, employee.getId(),employee.getTenantId()); } }
RegularisationService { public void update(Employee employee) { List<Regularisation> regularisations = regularisationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getRegularisation())) { employee.getRegularisation().forEach((regularisation) -> { if (needsInsert(regularisation, regularisations)) { regularisationRepository.insert(regularisation, employee.getId()); } else if (needsUpdate(regularisation, regularisations)) { regularisation.setTenantId(employee.getTenantId()); regularisationRepository.update(regularisation); } }); } deleteRegularisationsInDBThatAreNotInInput(employee.getRegularisation(), regularisations, employee.getId(),employee.getTenantId()); }  }
RegularisationService { public void update(Employee employee) { List<Regularisation> regularisations = regularisationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getRegularisation())) { employee.getRegularisation().forEach((regularisation) -> { if (needsInsert(regularisation, regularisations)) { regularisationRepository.insert(regularisation, employee.getId()); } else if (needsUpdate(regularisation, regularisations)) { regularisation.setTenantId(employee.getTenantId()); regularisationRepository.update(regularisation); } }); } deleteRegularisationsInDBThatAreNotInInput(employee.getRegularisation(), regularisations, employee.getId(),employee.getTenantId()); }  void update(Employee employee); }
RegularisationService { public void update(Employee employee) { List<Regularisation> regularisations = regularisationRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); if (!isEmpty(employee.getRegularisation())) { employee.getRegularisation().forEach((regularisation) -> { if (needsInsert(regularisation, regularisations)) { regularisationRepository.insert(regularisation, employee.getId()); } else if (needsUpdate(regularisation, regularisations)) { regularisation.setTenantId(employee.getTenantId()); regularisationRepository.update(regularisation); } }); } deleteRegularisationsInDBThatAreNotInInput(employee.getRegularisation(), regularisations, employee.getId(),employee.getTenantId()); }  void update(Employee employee);  }