Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Niedermann <info@niedermann.it>2021-05-03 18:36:24 +0300
committerStefan Niedermann <info@niedermann.it>2021-05-03 19:22:38 +0300
commitbbf569b0e9a1a93e4fff8074cc180274fc1f2d24 (patch)
treee43c3237b7a36d8d3f4881129cd977e93690f9db /app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java
parente3c4c1cb40ba229f50dd534c9edef42cb807ed18 (diff)
parent1cc44ede8e6c605345d794452c099a07a49d2f49 (diff)
Merge branch 'master' into 916-settings
# Conflicts: # app/src/main/java/it/niedermann/owncloud/notes/manageaccounts/ManageAccountAdapter.java # app/src/main/java/it/niedermann/owncloud/notes/manageaccounts/ManageAccountsActivity.java # app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java # app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java # app/src/main/java/it/niedermann/owncloud/notes/shared/model/ApiVersion.java # app/src/main/res/values/strings.xml
Diffstat (limited to 'app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java')
-rw-r--r--app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java187
1 files changed, 187 insertions, 0 deletions
diff --git a/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java b/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java
new file mode 100644
index 00000000..11c58dde
--- /dev/null
+++ b/app/src/test/java/it/niedermann/owncloud/notes/persistence/NotesRepositoryTest.java
@@ -0,0 +1,187 @@
+package it.niedermann.owncloud.notes.persistence;
+
+import android.content.Context;
+import android.os.Build;
+
+import androidx.annotation.NonNull;
+import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
+import androidx.room.Room;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.google.common.util.concurrent.MoreExecutors;
+import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Map;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+
+import it.niedermann.owncloud.notes.persistence.entity.Account;
+import it.niedermann.owncloud.notes.persistence.entity.Note;
+import it.niedermann.owncloud.notes.shared.model.Capabilities;
+import it.niedermann.owncloud.notes.shared.model.IResponseCallback;
+
+import static it.niedermann.owncloud.notes.persistence.NotesDatabaseTestUtil.getOrAwaitValue;
+import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_DELETED;
+import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_EDITED;
+import static it.niedermann.owncloud.notes.shared.model.DBStatus.VOID;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.fail;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(sdk = {Build.VERSION_CODES.P})
+public class NotesRepositoryTest {
+
+ @Rule
+ public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
+
+ private NotesRepository repo = null;
+ private Account account = null;
+ private Account secondAccount = null;
+ private NotesDatabase db;
+
+ @Before
+ public void setupDB() throws NextcloudHttpRequestFailedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
+ final Context context = ApplicationProvider.getApplicationContext();
+ db = Room
+ .inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), NotesDatabase.class)
+ .allowMainThreadQueries()
+ .build();
+
+ final Constructor<NotesRepository> constructor = NotesRepository.class.getDeclaredConstructor(Context.class, NotesDatabase.class, ExecutorService.class);
+ constructor.setAccessible(true);
+ repo = constructor.newInstance(context, db, MoreExecutors.newDirectExecutorService());
+
+ repo.addAccount("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", new Capabilities("{\"ocs\":{\"meta\":{\"status\":\"ok\",\"statuscode\":200,\"message\":\"OK\"},\"data\":{\"version\":{\"major\":18,\"minor\":0,\"micro\":4,\"string\":\"18.0.4\",\"edition\":\"\",\"extendedSupport\":false},\"capabilities\":{\"core\":{\"pollinterval\":60,\"webdav-root\":\"remote.php\\/webdav\"},\"bruteforce\":{\"delay\":0},\"files\":{\"bigfilechunking\":true,\"blacklisted_files\":[\".htaccess\"],\"directEditing\":{\"url\":\"https:\\/\\/efss.qloud.my\\/ocs\\/v2.php\\/apps\\/files\\/api\\/v1\\/directEditing\",\"etag\":\"ed2b141af2a39b0e42666952ba60988d\"},\"versioning\":true,\"undelete\":true},\"activity\":{\"apiv2\":[\"filters\",\"filters-api\",\"previews\",\"rich-strings\"]},\"ocm\":{\"enabled\":true,\"apiVersion\":\"1.0-proposal1\",\"endPoint\":\"https:\\/\\/efss.qloud.my\\/index.php\\/ocm\",\"resourceTypes\":[{\"name\":\"file\",\"shareTypes\":[\"user\",\"group\"],\"protocols\":{\"webdav\":\"\\/public.php\\/webdav\\/\"}}]},\"deck\":{\"version\":\"0.8.2\"},\"richdocuments\":{\"mimetypes\":[\"application\\/vnd.oasis.opendocument.text\",\"application\\/vnd.oasis.opendocument.spreadsheet\",\"application\\/vnd.oasis.opendocument.graphics\",\"application\\/vnd.oasis.opendocument.presentation\",\"application\\/vnd.lotus-wordpro\",\"application\\/vnd.visio\",\"application\\/vnd.wordperfect\",\"application\\/msonenote\",\"application\\/msword\",\"application\\/rtf\",\"text\\/rtf\",\"application\\/vnd.openxmlformats-officedocument.wordprocessingml.document\",\"application\\/vnd.openxmlformats-officedocument.wordprocessingml.template\",\"application\\/vnd.ms-word.document.macroEnabled.12\",\"application\\/vnd.ms-word.template.macroEnabled.12\",\"application\\/vnd.ms-excel\",\"application\\/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\"application\\/vnd.openxmlformats-officedocument.spreadsheetml.template\",\"application\\/vnd.ms-excel.sheet.macroEnabled.12\",\"application\\/vnd.ms-excel.template.macroEnabled.12\",\"application\\/vnd.ms-excel.addin.macroEnabled.12\",\"application\\/vnd.ms-excel.sheet.binary.macroEnabled.12\",\"application\\/vnd.ms-powerpoint\",\"application\\/vnd.openxmlformats-officedocument.presentationml.presentation\",\"application\\/vnd.openxmlformats-officedocument.presentationml.template\",\"application\\/vnd.openxmlformats-officedocument.presentationml.slideshow\",\"application\\/vnd.ms-powerpoint.addin.macroEnabled.12\",\"application\\/vnd.ms-powerpoint.presentation.macroEnabled.12\",\"application\\/vnd.ms-powerpoint.template.macroEnabled.12\",\"application\\/vnd.ms-powerpoint.slideshow.macroEnabled.12\",\"text\\/csv\"],\"mimetypesNoDefaultOpen\":[\"image\\/svg+xml\",\"application\\/pdf\",\"text\\/plain\",\"text\\/spreadsheet\"],\"collabora\":[],\"direct_editing\":false,\"templates\":false,\"productName\":\"\\u5728\\u7ebf\\u534f\\u4f5c\"},\"dav\":{\"chunking\":\"1.0\"},\"files_sharing\":{\"api_enabled\":true,\"public\":{\"enabled\":true,\"password\":{\"enforced\":true,\"askForOptionalPassword\":false},\"expire_date\":{\"enabled\":true,\"days\":\"7\",\"enforced\":false},\"multiple_links\":true,\"expire_date_internal\":{\"enabled\":false},\"send_mail\":false,\"upload\":true,\"upload_files_drop\":true},\"resharing\":true,\"user\":{\"send_mail\":false,\"expire_date\":{\"enabled\":true}},\"group_sharing\":true,\"group\":{\"enabled\":true,\"expire_date\":{\"enabled\":true}},\"default_permissions\":31,\"federation\":{\"outgoing\":false,\"incoming\":false,\"expire_date\":{\"enabled\":true}},\"sharee\":{\"query_lookup_default\":false},\"sharebymail\":{\"enabled\":true,\"upload_files_drop\":{\"enabled\":true},\"password\":{\"enabled\":true},\"expire_date\":{\"enabled\":true}}},\"external\":{\"v1\":[\"sites\",\"device\",\"groups\",\"redirect\"]},\"notifications\":{\"ocs-endpoints\":[\"list\",\"get\",\"delete\",\"delete-all\",\"icons\",\"rich-strings\",\"action-web\"],\"push\":[\"devices\",\"object-data\",\"delete\"],\"admin-notifications\":[\"ocs\",\"cli\"]},\"password_policy\":{\"minLength\":8,\"enforceNonCommonPassword\":true,\"enforceNumericCharacters\":false,\"enforceSpecialCharacters\":false,\"enforceUpperLowerCase\":false,\"api\":{\"generate\":\"https:\\/\\/efss.qloud.my\\/ocs\\/v2.php\\/apps\\/password_policy\\/api\\/v1\\/generate\",\"validate\":\"https:\\/\\/efss.qloud.my\\/ocs\\/v2.php\\/apps\\/password_policy\\/api\\/v1\\/validate\"}},\"theming\":{\"name\":\"QloudData\",\"url\":\"https:\\/\\/www.qloud.my\\/qloud-data\\/\",\"slogan\":\"Powered by NextCloud\",\"color\":\"#1E4164\",\"color-text\":\"#ffffff\",\"color-element\":\"#1E4164\",\"logo\":\"https:\\/\\/efss.qloud.my\\/index.php\\/apps\\/theming\\/image\\/logo?useSvg=1&v=47\",\"background\":\"https:\\/\\/efss.qloud.my\\/core\\/img\\/background.png?v=47\",\"background-plain\":false,\"background-default\":true,\"logoheader\":\"https:\\/\\/efss.qloud.my\\/index.php\\/apps\\/theming\\/image\\/logo?useSvg=1&v=47\",\"favicon\":\"https:\\/\\/efss.qloud.my\\/index.php\\/apps\\/theming\\/image\\/logo?useSvg=1&v=47\"},\"registration\":{\"enabled\":true,\"apiRoot\":\"\\/ocs\\/v2.php\\/apps\\/registration\\/api\\/v1\\/\",\"apiLevel\":\"v1\"}}}}}", null), new IResponseCallback<Account>() {
+ @Override
+ public void onSuccess(Account result) {
+
+ }
+
+ @Override
+ public void onError(@NonNull Throwable t) {
+ fail();
+ }
+ });
+ account = repo.getAccountByName("彼得@äöüß.example.com");
+
+ repo.addAccount("https://example.org", "test", "test@example.org", new Capabilities("{ocs: {}}", null), new IResponseCallback<Account>() {
+ @Override
+ public void onSuccess(Account result) {
+
+ }
+
+ @Override
+ public void onError(@NonNull Throwable t) {
+ fail();
+ }
+ });
+ secondAccount = repo.getAccountByName("test@example.org");
+
+ Arrays.stream(new Note[]{
+ new Note(1, 1001L, Calendar.getInstance(), "美好的一天", "C", "Movies", false, null, VOID, account.getId(), "", 0),
+ new Note(2, null, Calendar.getInstance(), "T", "C", "Movies", false, null, LOCAL_EDITED, account.getId(), "", 0),
+ new Note(3, 1003L, Calendar.getInstance(), "美好的一天", "C", "Movies", false, null, LOCAL_EDITED, account.getId(), "", 0),
+ new Note(4, null, Calendar.getInstance(), "T", "C", "Music", false, null, VOID, account.getId(), "", 0),
+ new Note(5, 1005L, Calendar.getInstance(), "美好的一天", "C", " 兄弟,这真是美好的一天。", false, null, LOCAL_EDITED, account.getId(), "", 0),
+ new Note(6, 1006L, Calendar.getInstance(), "美好的一天", "C", " 兄弟,这真是美好的一天。", false, null, LOCAL_DELETED, account.getId(), "", 0),
+ new Note(7, null, Calendar.getInstance(), "T", "C", "Music", true, null, LOCAL_EDITED, secondAccount.getId(), "", 0),
+ new Note(8, 1008L, Calendar.getInstance(), "美好的一天", "C", "ToDo", true, null, LOCAL_EDITED, secondAccount.getId(), "", 0),
+ new Note(9, 1009L, Calendar.getInstance(), "美好的一天", "C", "ToDo", true, null, LOCAL_DELETED, secondAccount.getId(), "", 0)
+ }).forEach(note -> db.getNoteDao().addNote(note));
+ }
+
+ @After
+ public void closeDb() {
+ db.close();
+ }
+
+ @Test
+ public void testGetIdMap() {
+ final Map<Long, Long> idMapOfFirstAccount = repo.getIdMap(account.getId());
+ assertEquals(3, idMapOfFirstAccount.size());
+ assertEquals(Long.valueOf(1L), idMapOfFirstAccount.get(1001L));
+ assertEquals(Long.valueOf(3L), idMapOfFirstAccount.get(1003L));
+ assertEquals(Long.valueOf(5L), idMapOfFirstAccount.get(1005L));
+
+ final Map<Long, Long> idMapOfSecondAccount = repo.getIdMap(secondAccount.getId());
+ assertEquals(1, idMapOfSecondAccount.size());
+ assertEquals(Long.valueOf(8L), idMapOfSecondAccount.get(1008L));
+ }
+
+ @Test
+ public void testAddAccount() throws NextcloudHttpRequestFailedException, InterruptedException {
+ repo.addAccount("https://äöüß.example.com", "彼得", "彼得@äöüß.example.com", new Capabilities("{ocs: {}}", null), new IResponseCallback<Account>() {
+ @Override
+ public void onSuccess(Account createdAccount) {
+ assertEquals("https://äöüß.example.com", createdAccount.getUrl());
+ assertEquals("彼得", createdAccount.getUserName());
+ assertEquals("彼得@äöüß.example.com", createdAccount.getAccountName());
+ }
+
+ @Override
+ public void onError(@NonNull Throwable t) {
+ fail();
+ }
+ });
+ }
+
+ @Test
+ public void testAddNote() {
+ final Note localNote = new Note(null, Calendar.getInstance(), "Fancy Title", "MyContent", "Samples", false, "123");
+ localNote.setId(99);
+ final Note createdNoteFromLocal = repo.addNote(account.getId(), localNote);
+ assertEquals(LOCAL_EDITED, createdNoteFromLocal.getStatus());
+ assertEquals("MyContent", createdNoteFromLocal.getExcerpt());
+
+ final Note createdNoteFromRemote = repo.addNote(account.getId(), new Note(null, Calendar.getInstance(), "Fancy Title", "MyContent", "Samples", false, "123"));
+ assertEquals(VOID, createdNoteFromRemote.getStatus());
+ assertEquals("MyContent", createdNoteFromRemote.getExcerpt());
+ }
+
+ @Test
+ public void updateApiVersion() {
+ assertThrows(IllegalArgumentException.class, () -> repo.updateApiVersion(account.getId(), ""));
+ assertThrows(IllegalArgumentException.class, () -> repo.updateApiVersion(account.getId(), "asdf"));
+ assertThrows(IllegalArgumentException.class, () -> repo.updateApiVersion(account.getId(), "{}"));
+
+ repo.updateApiVersion(account.getId(), null);
+ assertNull(repo.getAccountById(account.getId()).getApiVersion());
+ repo.updateApiVersion(account.getId(), "[]");
+ assertNull(repo.getAccountById(account.getId()).getApiVersion());
+
+ repo.updateApiVersion(account.getId(), "[1.0]");
+ assertEquals("[1.0]", repo.getAccountById(account.getId()).getApiVersion());
+ repo.updateApiVersion(account.getId(), "[0.2, 1.0]");
+ assertEquals("[0.2, 1.0]", repo.getAccountById(account.getId()).getApiVersion());
+
+ // TODO is this really indented?
+ repo.updateApiVersion(account.getId(), "[0.2, abc]");
+ assertEquals("[0.2, abc]", repo.getAccountById(account.getId()).getApiVersion());
+ }
+
+ @Test
+ @Ignore("Need to find a way to stub deleteAndSync method")
+ public void moveNoteToAnotherAccount() throws InterruptedException {
+ final Note noteToMove = repo.getNoteById(1);
+ assertEquals(3, repo.getLocalModifiedNotes(secondAccount.getId()).size());
+ final Note movedNote = getOrAwaitValue(repo.moveNoteToAnotherAccount(secondAccount, noteToMove));
+ assertEquals(4, repo.getLocalModifiedNotes(secondAccount.getId()).size());
+ assertEquals(LOCAL_EDITED, movedNote.getStatus());
+ // TODO assert deleteAndSync has been called
+ }
+} \ No newline at end of file