|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.owncloud.android.providers |
| 9 | + |
| 10 | +import android.content.ContentValues |
| 11 | +import android.content.ContentUris |
| 12 | +import android.net.Uri |
| 13 | +import androidx.sqlite.db.SimpleSQLiteQuery |
| 14 | +import androidx.sqlite.db.SupportSQLiteDatabase |
| 15 | +import com.owncloud.android.db.ProviderMeta |
| 16 | +import io.mockk.Runs |
| 17 | +import io.mockk.every |
| 18 | +import io.mockk.just |
| 19 | +import io.mockk.mockk |
| 20 | +import kotlinx.coroutines.async |
| 21 | +import kotlinx.coroutines.awaitAll |
| 22 | +import kotlinx.coroutines.coroutineScope |
| 23 | +import kotlinx.coroutines.runBlocking |
| 24 | +import org.junit.Assert.assertEquals |
| 25 | +import org.junit.Assert.assertTrue |
| 26 | +import org.junit.Before |
| 27 | +import org.junit.Test |
| 28 | + |
| 29 | +@Suppress("MagicNumber") |
| 30 | +class FileContentProviderTests { |
| 31 | + |
| 32 | + private lateinit var provider: FileContentProvider |
| 33 | + private lateinit var db: SupportSQLiteDatabase |
| 34 | + |
| 35 | + @Before |
| 36 | + fun setup() { |
| 37 | + provider = FileContentProvider() |
| 38 | + db = mockk() |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun insertNewFileShouldReturnNewId() { |
| 43 | + val values = ContentValues().apply { |
| 44 | + put(ProviderMeta.ProviderTableMeta.FILE_PATH, "/path/to/file.txt") |
| 45 | + put( ProviderMeta. ProviderTableMeta. FILE_ACCOUNT_OWNER, "[email protected]") |
| 46 | + } |
| 47 | + |
| 48 | + // Mock insert to return new ID |
| 49 | + every { db.insert(any(), any(), any()) } returns 42L |
| 50 | + |
| 51 | + val result: Uri = provider.upsertSingleFile( |
| 52 | + db, |
| 53 | + ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, |
| 54 | + values |
| 55 | + ) |
| 56 | + |
| 57 | + assertEquals( |
| 58 | + ContentUris.withAppendedId(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, 42), |
| 59 | + result |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun updateExistingFileShouldReturnSameId() { |
| 65 | + val values = ContentValues().apply { |
| 66 | + put(ProviderMeta.ProviderTableMeta.FILE_PATH, "/path/to/file.txt") |
| 67 | + put( ProviderMeta. ProviderTableMeta. FILE_ACCOUNT_OWNER, "[email protected]") |
| 68 | + } |
| 69 | + |
| 70 | + // Simulate insert conflict |
| 71 | + every { db.insert(ProviderMeta.ProviderTableMeta.FILE_TABLE_NAME, any(), values) } returns -1L |
| 72 | + |
| 73 | + // Simulate update returning 1 row affected |
| 74 | + every { |
| 75 | + db.update( |
| 76 | + ProviderMeta.ProviderTableMeta.FILE_TABLE_NAME, |
| 77 | + any(), |
| 78 | + values, |
| 79 | + any(), |
| 80 | + any() |
| 81 | + ) |
| 82 | + } returns 1 |
| 83 | + |
| 84 | + // Mock cursor to return ID 99 |
| 85 | + val cursor = mockk<android.database.Cursor>() |
| 86 | + every { cursor.moveToFirst() } returns true |
| 87 | + every { cursor.getLong(0) } returns 99L |
| 88 | + every { cursor.close() } just Runs |
| 89 | + |
| 90 | + every { db.query(any<SimpleSQLiteQuery>()) } returns cursor |
| 91 | + |
| 92 | + val result: Uri = provider.upsertSingleFile( |
| 93 | + db, |
| 94 | + ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, |
| 95 | + values |
| 96 | + ) |
| 97 | + |
| 98 | + assertEquals(ContentUris.withAppendedId(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, 99), result) |
| 99 | + |
| 100 | + cursor.close() |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun testConcurrentUpserts() = runBlocking { |
| 105 | + val values = ContentValues().apply { |
| 106 | + put(ProviderMeta.ProviderTableMeta.FILE_PATH, "/path/to/file.txt") |
| 107 | + put( ProviderMeta. ProviderTableMeta. FILE_ACCOUNT_OWNER, "[email protected]") |
| 108 | + } |
| 109 | + |
| 110 | + // shared state to simulate race |
| 111 | + val inserted = mutableListOf<Long>() |
| 112 | + |
| 113 | + // mock insert: fail first call, succeed second |
| 114 | + every { db.insert(any(), any(), any()) } answers { |
| 115 | + synchronized(inserted) { |
| 116 | + if (inserted.isEmpty()) { |
| 117 | + // first thread "fails" insert which means already existing file id will be returned |
| 118 | + inserted.add(-1L) |
| 119 | + -1L |
| 120 | + } else { |
| 121 | + // second thread "succeeds" it will update existing one |
| 122 | + inserted.add(42L) |
| 123 | + 42L |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // mock update only one row should be affected |
| 129 | + every { db.update(any(), any(), any(), any(), any()) } returns 1 |
| 130 | + |
| 131 | + // mock query existing file id will return 99 |
| 132 | + val cursor = mockk<android.database.Cursor>() |
| 133 | + every { cursor.moveToFirst() } returns true |
| 134 | + every { cursor.getLong(0) } returns 99L |
| 135 | + every { cursor.close() } just Runs |
| 136 | + every { db.query(any<SimpleSQLiteQuery>()) } returns cursor |
| 137 | + |
| 138 | + // launch two coroutines simulating concurrent threads |
| 139 | + val results = mutableListOf<Uri>() |
| 140 | + coroutineScope { |
| 141 | + val job1 = |
| 142 | + async { |
| 143 | + results.add(provider.upsertSingleFile(db, ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, values)) |
| 144 | + } |
| 145 | + val job2 = |
| 146 | + async { |
| 147 | + results.add(provider.upsertSingleFile(db, ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, values)) |
| 148 | + } |
| 149 | + awaitAll(job1, job2) |
| 150 | + } |
| 151 | + |
| 152 | + // both URIs should be correct (one updated, one inserted) |
| 153 | + assertTrue(results.contains(ContentUris.withAppendedId(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, 99))) |
| 154 | + assertTrue(results.contains(ContentUris.withAppendedId(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILE, 42))) |
| 155 | + |
| 156 | + cursor.close() |
| 157 | + } |
| 158 | +} |
0 commit comments