Search in sources :

Example 1 with ApiVersion

use of it.niedermann.owncloud.notes.shared.model.ApiVersion in project nextcloud-notes by stefan-niedermann.

the class ApiVersionUtilTest method testGetPreferredApiVersion.

@SuppressWarnings("ConstantConditions")
@Test
public void testGetPreferredApiVersion() {
    assertNull(ApiVersionUtil.getPreferredApiVersion(null));
    assertNull(ApiVersionUtil.getPreferredApiVersion(""));
    assertNull(ApiVersionUtil.getPreferredApiVersion("[]"));
    assertNull(ApiVersionUtil.getPreferredApiVersion("foo"));
    ApiVersion result;
    result = ApiVersionUtil.getPreferredApiVersion("[0.2]");
    assertEquals(0, result.getMajor());
    assertEquals(2, result.getMinor());
    result = ApiVersionUtil.getPreferredApiVersion("[1.1]");
    assertEquals(1, result.getMajor());
    assertEquals(1, result.getMinor());
    result = ApiVersionUtil.getPreferredApiVersion("[0.2,1.1]");
    assertEquals(1, result.getMajor());
    assertEquals(1, result.getMinor());
    result = ApiVersionUtil.getPreferredApiVersion("[1.1,0.2]");
    assertEquals(1, result.getMajor());
    assertEquals(1, result.getMinor());
    result = ApiVersionUtil.getPreferredApiVersion("[10.0,1.1,1.0,0.2]");
    assertEquals(1, result.getMajor());
    assertEquals(1, result.getMinor());
    result = ApiVersionUtil.getPreferredApiVersion("[1.1,1.5,1.0]");
    assertEquals(1, result.getMajor());
    assertEquals(5, result.getMinor());
    result = ApiVersionUtil.getPreferredApiVersion("[1.1,,foo,1.0]");
    assertEquals(1, result.getMajor());
    assertEquals(1, result.getMinor());
}
Also used : ApiVersion(it.niedermann.owncloud.notes.shared.model.ApiVersion) Test(org.junit.Test)

Example 2 with ApiVersion

use of it.niedermann.owncloud.notes.shared.model.ApiVersion in project nextcloud-notes by stefan-niedermann.

the class ApiVersionUtilTest method testParse_valid_many.

@Test
public void testParse_valid_many() {
    Collection<ApiVersion> result;
    ApiVersion current;
    Iterator<ApiVersion> iterator;
    result = ApiVersionUtil.parse("[0.2, 1.0]");
    assertEquals(2, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("[\"0.2\", \"1.0\"]");
    assertEquals(2, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("[0.2,1.0]");
    assertEquals(2, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("[\"0.2\",\"1.0\"]");
    assertEquals(2, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("[0.2, \"1.0\"]");
    assertEquals(2, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("[0.2,\"1.0\"]");
    assertEquals(2, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
}
Also used : ApiVersion(it.niedermann.owncloud.notes.shared.model.ApiVersion) Test(org.junit.Test)

Example 3 with ApiVersion

use of it.niedermann.owncloud.notes.shared.model.ApiVersion in project nextcloud-notes by stefan-niedermann.

the class ApiVersionUtilTest method testParse_valid_one.

@Test
public void testParse_valid_one() {
    Collection<ApiVersion> result;
    ApiVersion current;
    result = ApiVersionUtil.parse("[0.2]");
    assertEquals(1, result.size());
    current = result.iterator().next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    result = ApiVersionUtil.parse("[1.0]");
    assertEquals(1, result.size());
    current = result.iterator().next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("[\"0.2\"]");
    assertEquals(1, result.size());
    current = result.iterator().next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    result = ApiVersionUtil.parse("[\"1.0\"]");
    assertEquals(1, result.size());
    current = result.iterator().next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
    result = ApiVersionUtil.parse("1.0");
    assertEquals(1, result.size());
    current = result.iterator().next();
    assertEquals(1, current.getMajor());
    assertEquals(0, current.getMinor());
}
Also used : ApiVersion(it.niedermann.owncloud.notes.shared.model.ApiVersion) Test(org.junit.Test)

Example 4 with ApiVersion

use of it.niedermann.owncloud.notes.shared.model.ApiVersion in project nextcloud-notes by stefan-niedermann.

the class ApiVersionUtilTest method testParse_invalid_many.

@Test
public void testParse_invalid_many() {
    Collection<ApiVersion> result;
    ApiVersion current;
    Iterator<ApiVersion> iterator;
    result = ApiVersionUtil.parse("[0.2, foo]");
    assertEquals(1, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(0, current.getMajor());
    assertEquals(2, current.getMinor());
    result = ApiVersionUtil.parse("[foo, 1.1]");
    assertEquals(1, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(1, current.getMinor());
    assertEquals(0, ApiVersionUtil.parse("[foo, bar]").size());
    result = ApiVersionUtil.parse("[, 1.1]");
    assertEquals(1, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(1, current.getMinor());
    result = ApiVersionUtil.parse("[1.1, ?]");
    assertEquals(1, result.size());
    iterator = result.iterator();
    current = iterator.next();
    assertEquals(1, current.getMajor());
    assertEquals(1, current.getMinor());
}
Also used : ApiVersion(it.niedermann.owncloud.notes.shared.model.ApiVersion) Test(org.junit.Test)

Example 5 with ApiVersion

use of it.niedermann.owncloud.notes.shared.model.ApiVersion in project nextcloud-notes by stefan-niedermann.

the class NotesRepository method updateNoteAndSync.

/**
 * Updates a single Note with a new content.
 * The title is derived from the new content automatically, and modified date as well as DBStatus are updated, too -- if the content differs to the state in the database.
 *
 * @param oldNote    Note to be changed
 * @param newContent New content. If this is <code>null</code>, then <code>oldNote</code> is saved again (useful for undoing changes).
 * @param newTitle   New title. If this is <code>null</code>, then either the old title is reused (in case the note has been synced before) or a title is generated (in case it is a new note)
 * @param callback   When the synchronization is finished, this callback will be invoked (optional).
 * @return changed {@link Note} if differs from database, otherwise the old {@link Note}.
 */
@WorkerThread
public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) {
    final Note newNote;
    // Re-read the up to date remoteId from the database because the UI might not have the state after synchronization yet
    // https://github.com/stefan-niedermann/nextcloud-notes/issues/1198
    @Nullable final Long remoteId = db.getNoteDao().getRemoteId(oldNote.getId());
    if (newContent == null) {
        newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY());
    } else {
        final String title;
        if (newTitle != null) {
            title = newTitle;
        } else {
            final ApiVersion preferredApiVersion = ApiVersionUtil.getPreferredApiVersion(localAccount.getApiVersion());
            if ((remoteId == null || preferredApiVersion == null || preferredApiVersion.compareTo(ApiVersion.API_VERSION_1_0) < 0) && (defaultNonEmptyTitle.equals(oldNote.getTitle()))) {
                title = NoteUtil.generateNonEmptyNoteTitle(newContent, context);
            } else {
                title = oldNote.getTitle();
            }
        }
        newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY());
    }
    int rows = db.getNoteDao().updateNote(newNote);
    // if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
    if (rows > 0) {
        notifyWidgets();
        if (callback != null) {
            addCallbackPush(localAccount, callback);
        }
        scheduleSync(localAccount, true);
        return newNote;
    } else {
        if (callback != null) {
            callback.onFinish();
        }
        return oldNote;
    }
}
Also used : ApiVersion(it.niedermann.owncloud.notes.shared.model.ApiVersion) Note(it.niedermann.owncloud.notes.persistence.entity.Note) Nullable(androidx.annotation.Nullable) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

ApiVersion (it.niedermann.owncloud.notes.shared.model.ApiVersion)6 Test (org.junit.Test)4 Nullable (androidx.annotation.Nullable)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 TextUtils (android.text.TextUtils)1 NonNull (androidx.annotation.NonNull)1 WorkerThread (androidx.annotation.WorkerThread)1 OnConflictStrategy (androidx.room.OnConflictStrategy)1 Migration (androidx.room.migration.Migration)1 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)1 ApiProvider (it.niedermann.owncloud.notes.persistence.ApiProvider)1 Account (it.niedermann.owncloud.notes.persistence.entity.Account)1 Note (it.niedermann.owncloud.notes.persistence.entity.Note)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1