SyncAccountJob extends Worker { boolean syncAccounts(OpacClient app, AccountDataSource data, SharedPreferences sp, ReminderHelper helper) { boolean failed = false; List<Account> accounts = data.getAccountsWithPassword(); if (!sp.contains("update_151_clear_cache")) { data.invalidateCachedData(); sp.edit().putBoolean("update_151_clear_cache", true).apply(); } for (Account account : accounts) { if (BuildConfig.DEBUG) { Log.i(TAG, "Loading data for Account " + account.toString()); } AccountData res; try { Library library = app.getLibrary(account.getLibrary()); if (!library.isAccountSupported()) { data.deleteAccountData(account); continue; } OpacApi api = app.getNewApi(library); res = api.account(account); if (res == null) { failed = true; continue; } } catch (JSONException | IOException | OpacApi.OpacErrorException e) { e.printStackTrace(); failed = true; continue; } catch (OpacClient.LibraryRemovedException e) { continue; } account.setPasswordKnownValid(true); try { data.update(account); data.storeCachedAccountData(account, res); } finally { helper.generateAlarms(); } } return failed; } SyncAccountJob(@NonNull Context context,
            @NonNull WorkerParameters workerParams); static void scheduleJob(Context ctx); static void scheduleRetryJob(Context ctx); static void runImmediately(Context ctx); @NonNull @Override Result doWork(); static final String PREF_SYNC_SERVICE; }
@Test public void shouldDoNothingWithoutAccounts() { when(data.getAccountsWithPassword()).thenReturn(new ArrayList<Account>()); assertFalse(service.syncAccounts(app, data, sp, helper)); verify(data).getAccountsWithPassword(); verifyNoMoreInteractions(data); }