-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathNotesServerSyncTaskTest.java
More file actions
105 lines (84 loc) · 4.71 KB
/
NotesServerSyncTaskTest.java
File metadata and controls
105 lines (84 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.persistence;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_EDITED;
import static it.niedermann.owncloud.notes.shared.model.DBStatus.VOID;
import android.content.Context;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import com.nextcloud.android.sso.api.ParsedResponse;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.io.IOException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import io.reactivex.Observable;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.sync.NotesAPI;
import it.niedermann.owncloud.notes.shared.model.SyncResultStatus;
@SuppressWarnings("CallToThreadRun")
@RunWith(RobolectricTestRunner.class)
public class NotesServerSyncTaskTest {
@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
private NotesServerSyncTask task;
private final Account account = mock(Account.class);
private final NotesRepository repo = mock(NotesRepository.class);
private final NotesAPI notesAPI = mock(NotesAPI.class);
private final ApiProvider apiProvider = mock(ApiProvider.class);
@Before
public void setup() throws NextcloudFilesAppAccountNotFoundException, IOException {
when(apiProvider.getNotesAPI(any(), any(), any())).thenReturn(notesAPI);
NotesTestingUtil.mockSingleSignOn(new SingleSignOnAccount(account.getAccountName(), account.getUserName(), "", account.getUrl(), ""));
this.task = new NotesServerSyncTask(mock(Context.class), repo, account, false, apiProvider) {
@Override
void onPreExecute() {
}
@Override
void onPostExecute(SyncResultStatus status) {
}
};
}
@Test
public void testPushLocalChanges() {
when(repo.getLocalModifiedNotes(anyLong())).thenReturn(Arrays.asList(
new Note(1, null, Calendar.getInstance(), "Does not has a remoteId yet, therefore", "This note should be created on the server", "", false, "1", LOCAL_EDITED, 0, "", 0),
new Note(1, 2L, Calendar.getInstance(), "Has already a remoteId, therefore", "This note should be updated on the server", "", false, "1", LOCAL_EDITED, 0, "", 0)
));
this.task.run();
verify(notesAPI).createNote(argThat(argument -> "This note should be created on the server".equals(argument.getContent())));
verify(notesAPI).editNote(argThat(argument -> "This note should be updated on the server".equals(argument.getContent())));
}
@Test
public void testPullRemoteChanges() {
when(account.getModified()).thenReturn(Calendar.getInstance());
when(repo.getAccountById(anyLong())).thenReturn(account);
when(repo.getIdMap(anyLong())).thenReturn(Map.of(1000L, 1L, 2000L, 2L));
when(repo.updateIfNotModifiedLocallyAndAnyRemoteColumnHasChanged(anyLong(), anyLong(), anyString(), anyBoolean(), anyString(), anyString(), anyString(), anyString())).thenReturn(1);
when(notesAPI.getNotes(any(), any())).thenReturn(Observable.just(ParsedResponse.of(Arrays.asList(
new Note(0, 1000L, Calendar.getInstance(), "RemoteId is in the idMap, therefore", "This note should be updated locally", "", false, "1", VOID, 0, "", 0),
new Note(0, 3000L, Calendar.getInstance(), "Is a new RemoteId, therefore", "This note should be created locally", "", false, "1", VOID, 0, "", 0)
))));
this.task.run();
verify(repo).addNote(anyLong(), argThat(argument -> "This note should be created locally".equals(argument.getContent())));
verify(repo).updateIfNotModifiedLocallyAndAnyRemoteColumnHasChanged(anyLong(), anyLong(), anyString(), anyBoolean(), anyString(), anyString(), argThat("This note should be updated locally"::equals), anyString());
}
}