ProfileSaverWorker implements Runnable, Closeable { @Override public void run() { try { log.debug("Starting saving user db."); final long now = System.currentTimeMillis(); ArrayList<User> users = saveModified(); dbManager.saveUsers(users); if (dbManager.dbIsNotEnabled() && users.size() > 0) { archiveUser(now); } lastStart = now; log.debug("Saving user db finished. Modified {} users.", users.size()); } catch (Throwable t) { log.error("Error saving users.", t); } } ProfileSaverWorker(UserDao userDao, FileManager fileManager, DBManager dbManager); @Override void run(); @Override void close();  }
@Test public void testCorrectProfilesAreSaved() throws IOException { ProfileSaverWorker profileSaverWorker = new ProfileSaverWorker(userDao, fileManager, new DBManager(blockingIOProcessor, true)); User user1 = new User("1", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); User user2 = new User("2", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); User user3 = new User("3", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); User user4 = new User("4", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); ConcurrentMap<UserKey, User> userMap = new ConcurrentHashMap<>(); userMap.put(new UserKey(user1), user1); userMap.put(new UserKey(user2), user2); userMap.put(new UserKey(user3), user3); userMap.put(new UserKey(user4), user4); when(userDao.getUsers()).thenReturn(userMap); profileSaverWorker.run(); verify(fileManager, times(4)).overrideUserFile(any()); verify(fileManager).overrideUserFile(user1); verify(fileManager).overrideUserFile(user2); verify(fileManager).overrideUserFile(user3); verify(fileManager).overrideUserFile(user4); }
@Test public void testNoProfileChanges() throws Exception { User user1 = new User("1", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); User user2 = new User("2", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); User user3 = new User("3", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); User user4 = new User("4", "", AppNameUtil.BLYNK, "local", "127.0.0.1", false, false); Map<UserKey, User> userMap = new HashMap<>(); userMap.put(new UserKey("1", AppNameUtil.BLYNK), user1); userMap.put(new UserKey("2", AppNameUtil.BLYNK), user2); userMap.put(new UserKey("3", AppNameUtil.BLYNK), user3); userMap.put(new UserKey("4", AppNameUtil.BLYNK), user4); Thread.sleep(1); ProfileSaverWorker profileSaverWorker = new ProfileSaverWorker(userDao, fileManager, new DBManager(blockingIOProcessor, true)); when(userDao.getUsers()).thenReturn(userMap); profileSaverWorker.run(); verifyNoMoreInteractions(fileManager); }