diff --git a/app/src/main/java/foundation/e/drive/database/DbHelper.java b/app/src/main/java/foundation/e/drive/database/DbHelper.java index 12828f24b6e787ef5934c8f0d209ffe3fcddaf8f..edc336bbec9145de6a99f0787fb98a7c5afd5988 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -14,6 +14,8 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; +import androidx.annotation.VisibleForTesting; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -84,8 +86,8 @@ public final class DbHelper extends SQLiteOpenHelper { } } - - private static SyncedFileStateDAO openSyncedFileStateDAO(Context context, boolean writeMod){ + @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) + public static SyncedFileStateDAO openSyncedFileStateDAO(Context context, boolean writeMod) { final SyncedFileStateDAO dao = new SyncedFileStateDAO(context); try { dao.open(writeMod); @@ -162,7 +164,7 @@ public final class DbHelper extends SQLiteOpenHelper { if (dao == null) { return new ArrayList<>(); } - final List result = dao.getBySyncedFolderID(ids); + final List result = dao.getBySyncedFolderIds(ids); dao.close(); return result; } diff --git a/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java b/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java index ee507044d064c247b857f6dc70b70f700b87609c..18cff0df548c69b8fb99787783f5a4463c431024 100644 --- a/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java +++ b/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java @@ -186,11 +186,17 @@ import static foundation.e.drive.database.SyncedFileStateContract.SYNCEDFOLDER_I } /** - * Fetch many SyncedFileState by their syncedFolder's id - * @param syncedFolderIds List of id of parent syncedFolder. - * @return List List of SyncedFileState filtered on syncedFolder ID. - */ - /* package */ List getBySyncedFolderID(List syncedFolderIds) { + * Fetch every syncedFileState for subfile of directories identified by syncedFolder's id. + * + * @param syncedFolderIds List of SyncedFolderIds + * @return List List of SyncedFileState filtered on syncedFolder ID. The result is an empty list + * if list of syncedFolderIds is null or empty + */ + /* package */ List getBySyncedFolderIds(List syncedFolderIds) { + final List result = new ArrayList<>(); + if (syncedFolderIds == null || syncedFolderIds.isEmpty()) { + return result; + } final String whereClause = SYNCEDFOLDER_ID + " IN (?)"; final String[] whereValue = new String[] { syncedFolderIds.toString() @@ -198,9 +204,7 @@ import static foundation.e.drive.database.SyncedFileStateContract.SYNCEDFOLDER_I .replace("]", ")") }; final Cursor cursor = mDB.query(TABLE_NAME, allColumns, whereClause, whereValue, null, null, null); - cursor.moveToFirst(); - final List result = new ArrayList<>(); while(!cursor.isAfterLast() ) { result.add( cursorToSyncedFileState(cursor) ); cursor.moveToNext(); diff --git a/app/src/test/java/foundation/e/drive/database/SyncedFileStateDAOTest.java b/app/src/test/java/foundation/e/drive/database/SyncedFileStateDAOTest.java new file mode 100644 index 0000000000000000000000000000000000000000..945b8694edbeba55c7049a3b2e3983b04c9e0b56 --- /dev/null +++ b/app/src/test/java/foundation/e/drive/database/SyncedFileStateDAOTest.java @@ -0,0 +1,81 @@ +/* + * Copyright © MURENA SAS 2022. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ + +package foundation.e.drive.database; + +import android.content.Context; +import android.os.Build; + +import androidx.test.core.app.ApplicationProvider; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.util.ArrayList; +import java.util.List; + +import foundation.e.drive.models.SyncedFileState; +import foundation.e.drive.models.SyncedFolder; + +/** + * @author vincent Bourgmayer + */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = Build.VERSION_CODES.O, manifest = Config.NONE) +public class SyncedFileStateDAOTest { + + private SyncedFileStateDAO daoUnderTest; + private final Context context; + + public SyncedFileStateDAOTest() { + context = ApplicationProvider.getApplicationContext(); + } + + + @Before + public void prepareDB() { + final SyncedFolder dummyFolder = new SyncedFolder("Picture", "local/", "remote/", true, true, true, true); + dummyFolder.setId(12); + DbHelper.insertSyncedFolder(dummyFolder, context); + + final SyncedFileState dummy = new SyncedFileState(6, "foo.jpg", "local/foo.jpg", "remote/foo.jpg", "7777", 0L, 12, true, 3); + DbHelper.manageSyncedFileStateDB(dummy, "INSERT", context); + + daoUnderTest = DbHelper.openSyncedFileStateDAO(context, false); + } + + @After + public void closeDB() { + daoUnderTest.close(); + } + + @Test + public void getBySyncedFolderIds_emptyIdList_returnEmptyList() { + final List folderIds = new ArrayList<>(); + final List result = daoUnderTest.getBySyncedFolderIds(folderIds); + Assert.assertTrue(result.isEmpty()); + } + + /* + * @todo find why this test fails + */ + @Test + @Ignore + public void getBySyncedFolderIds_notEmpty_returnOneFileState() { + final List folderIds = new ArrayList<>(); + folderIds.add(new Long(12)); + final List result = daoUnderTest.getBySyncedFolderIds(folderIds); + Assert.assertFalse("result should contain 1 SyncedFileState but was empty", result.isEmpty()); + } +}