@Test public void testUpdate() { List<Long> insertedLanguages = new ArrayList<>(); List<Long> deletedLanguages = new ArrayList<>(); when(employeeLanguageRepository.findByEmployeeId(100L, "1")).thenReturn(getLanguagesInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedLanguages.add((Long) args[0]); return null; } }).when(employeeLanguageRepository).insert(Matchers.anyLong(), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedLanguages.addAll((List<Long>) args[0]); return null; } }).when(employeeLanguageRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); Employee employee = null; try { employee = getEmployee("org/egov/eis/service/ServiceHistoryService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } employeeLanguageService.update(employee); List<Long> expectedInsertedLanguages = new ArrayList<>(); List<Long> expectedDeletedLanguages = new ArrayList<>(); expectedInsertedLanguages.add(employee.getLanguagesKnown().get(1)); expectedDeletedLanguages.add(2L); assertTrue(expectedInsertedLanguages.containsAll(insertedLanguages)); assertEquals(expectedInsertedLanguages.size(), insertedLanguages.size()); assertTrue(expectedDeletedLanguages.containsAll(deletedLanguages)); assertEquals(expectedDeletedLanguages.size(), deletedLanguages.size()); }
public void update(Employee employee) { if (isEmpty(employee.getLanguagesKnown())) return; List<Long> languagesFromDb = employeeLanguageRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getLanguagesKnown().forEach((language) -> { if (needsInsert(language, languagesFromDb)) employeeLanguageRepository.insert(language, employee.getId(), employee.getTenantId()); }); deleteLanguagesInDbThatAreNotInInput(employee.getLanguagesKnown(), languagesFromDb, employee.getId(), employee.getTenantId()); }
EmployeeLanguageService { public void update(Employee employee) { if (isEmpty(employee.getLanguagesKnown())) return; List<Long> languagesFromDb = employeeLanguageRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getLanguagesKnown().forEach((language) -> { if (needsInsert(language, languagesFromDb)) employeeLanguageRepository.insert(language, employee.getId(), employee.getTenantId()); }); deleteLanguagesInDbThatAreNotInInput(employee.getLanguagesKnown(), languagesFromDb, employee.getId(), employee.getTenantId()); } }
EmployeeLanguageService { public void update(Employee employee) { if (isEmpty(employee.getLanguagesKnown())) return; List<Long> languagesFromDb = employeeLanguageRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getLanguagesKnown().forEach((language) -> { if (needsInsert(language, languagesFromDb)) employeeLanguageRepository.insert(language, employee.getId(), employee.getTenantId()); }); deleteLanguagesInDbThatAreNotInInput(employee.getLanguagesKnown(), languagesFromDb, employee.getId(), employee.getTenantId()); }  }
EmployeeLanguageService { public void update(Employee employee) { if (isEmpty(employee.getLanguagesKnown())) return; List<Long> languagesFromDb = employeeLanguageRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getLanguagesKnown().forEach((language) -> { if (needsInsert(language, languagesFromDb)) employeeLanguageRepository.insert(language, employee.getId(), employee.getTenantId()); }); deleteLanguagesInDbThatAreNotInInput(employee.getLanguagesKnown(), languagesFromDb, employee.getId(), employee.getTenantId()); }  void update(Employee employee); }
EmployeeLanguageService { public void update(Employee employee) { if (isEmpty(employee.getLanguagesKnown())) return; List<Long> languagesFromDb = employeeLanguageRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getLanguagesKnown().forEach((language) -> { if (needsInsert(language, languagesFromDb)) employeeLanguageRepository.insert(language, employee.getId(), employee.getTenantId()); }); deleteLanguagesInDbThatAreNotInInput(employee.getLanguagesKnown(), languagesFromDb, employee.getId(), employee.getTenantId()); }  void update(Employee employee);  }