@Test public void testUpdate() { List<Long> insertedJurisdictions = new ArrayList<>(); List<Long> deletedJurisdictions = new ArrayList<>(); when(employeeJurisdictionRepository.findByEmployeeId(100L, "1")).thenReturn(getJurisdictionsInDBForEmployee()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); insertedJurisdictions.add((Long) args[0]); return null; } }).when(employeeJurisdictionRepository).insert(Matchers.anyLong(), Matchers.anyLong(), Matchers.anyString()); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); deletedJurisdictions.addAll((List<Long>) args[0]); return null; } }).when(employeeJurisdictionRepository).delete(Matchers.anyListOf(Long.class), Matchers.anyLong(), Matchers.anyString()); Employee employee = null; try { employee = getEmployee("org/egov/eis/service/EmployeeJurisdictionService.employees1.json"); } catch (Exception e) { e.printStackTrace(); fail(); } employeeJurisdictionService.update(employee); List<Long> expectedInsertedJurisdictions = new ArrayList<>(); List<Long> expectedDeletedJurisdictions = new ArrayList<>(); expectedInsertedJurisdictions.add(employee.getJurisdictions().get(0)); expectedDeletedJurisdictions.add(101L); expectedDeletedJurisdictions.add(102L); assertTrue(expectedInsertedJurisdictions.containsAll(insertedJurisdictions)); assertEquals(expectedInsertedJurisdictions.size(), insertedJurisdictions.size()); assertTrue(expectedDeletedJurisdictions.containsAll(deletedJurisdictions)); assertEquals(expectedDeletedJurisdictions.size(), deletedJurisdictions.size()); }
public void update(Employee employee) { List<Long> jurisdictionsFromDb = employeeJurisdictionRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getJurisdictions().forEach((jurisdiction) -> { if (needsInsert(jurisdiction, jurisdictionsFromDb)) employeeJurisdictionRepository.insert(jurisdiction, employee.getId(), employee.getTenantId()); }); deleteJurisdictionsInDbThatAreNotInInput(employee.getJurisdictions(), jurisdictionsFromDb, employee.getId(), employee.getTenantId()); }
EmployeeJurisdictionService { public void update(Employee employee) { List<Long> jurisdictionsFromDb = employeeJurisdictionRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getJurisdictions().forEach((jurisdiction) -> { if (needsInsert(jurisdiction, jurisdictionsFromDb)) employeeJurisdictionRepository.insert(jurisdiction, employee.getId(), employee.getTenantId()); }); deleteJurisdictionsInDbThatAreNotInInput(employee.getJurisdictions(), jurisdictionsFromDb, employee.getId(), employee.getTenantId()); } }
EmployeeJurisdictionService { public void update(Employee employee) { List<Long> jurisdictionsFromDb = employeeJurisdictionRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getJurisdictions().forEach((jurisdiction) -> { if (needsInsert(jurisdiction, jurisdictionsFromDb)) employeeJurisdictionRepository.insert(jurisdiction, employee.getId(), employee.getTenantId()); }); deleteJurisdictionsInDbThatAreNotInInput(employee.getJurisdictions(), jurisdictionsFromDb, employee.getId(), employee.getTenantId()); }  }
EmployeeJurisdictionService { public void update(Employee employee) { List<Long> jurisdictionsFromDb = employeeJurisdictionRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getJurisdictions().forEach((jurisdiction) -> { if (needsInsert(jurisdiction, jurisdictionsFromDb)) employeeJurisdictionRepository.insert(jurisdiction, employee.getId(), employee.getTenantId()); }); deleteJurisdictionsInDbThatAreNotInInput(employee.getJurisdictions(), jurisdictionsFromDb, employee.getId(), employee.getTenantId()); }  void update(Employee employee); }
EmployeeJurisdictionService { public void update(Employee employee) { List<Long> jurisdictionsFromDb = employeeJurisdictionRepository.findByEmployeeId(employee.getId(), employee.getTenantId()); employee.getJurisdictions().forEach((jurisdiction) -> { if (needsInsert(jurisdiction, jurisdictionsFromDb)) employeeJurisdictionRepository.insert(jurisdiction, employee.getId(), employee.getTenantId()); }); deleteJurisdictionsInDbThatAreNotInInput(employee.getJurisdictions(), jurisdictionsFromDb, employee.getId(), employee.getTenantId()); }  void update(Employee employee);  }