mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-11-22 21:06:09 +03:00
Add Unit test for fetching Capabilities
This commit is contained in:
parent
5fda7fb408
commit
af99117c75
6 changed files with 79 additions and 13 deletions
|
@ -27,8 +27,8 @@ import it.niedermann.owncloud.notes.branding.BrandingUtil;
|
|||
import it.niedermann.owncloud.notes.databinding.ActivityImportAccountBinding;
|
||||
import it.niedermann.owncloud.notes.exception.ExceptionDialogFragment;
|
||||
import it.niedermann.owncloud.notes.exception.ExceptionHandler;
|
||||
import it.niedermann.owncloud.notes.persistence.CapabilitiesClient;
|
||||
import it.niedermann.owncloud.notes.persistence.ApiProvider;
|
||||
import it.niedermann.owncloud.notes.persistence.CapabilitiesClient;
|
||||
import it.niedermann.owncloud.notes.persistence.SyncWorker;
|
||||
import it.niedermann.owncloud.notes.persistence.entity.Account;
|
||||
import it.niedermann.owncloud.notes.shared.model.Capabilities;
|
||||
|
@ -90,8 +90,8 @@ public class ImportAccountActivity extends AppCompatActivity {
|
|||
Log.i(TAG, "Added account: " + "name:" + ssoAccount.name + ", " + ssoAccount.url + ", userId" + ssoAccount.userId);
|
||||
try {
|
||||
Log.i(TAG, "Loading capabilities for " + ssoAccount.name);
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null);
|
||||
final String displayName = CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount);
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null, ApiProvider.getInstance());
|
||||
final String displayName = CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount, ApiProvider.getInstance());
|
||||
importAccountViewModel.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities, displayName, new IResponseCallback<Account>() {
|
||||
|
||||
/**
|
||||
|
|
|
@ -656,8 +656,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A
|
|||
Log.i(TAG, "Added account: " + "name:" + ssoAccount.name + ", " + ssoAccount.url + ", userId" + ssoAccount.userId);
|
||||
try {
|
||||
Log.i(TAG, "Refreshing capabilities for " + ssoAccount.name);
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null);
|
||||
final String displayName = CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount);
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null, ApiProvider.getInstance());
|
||||
final String displayName = CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount, ApiProvider.getInstance());
|
||||
mainViewModel.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities, displayName, new IResponseCallback<Account>() {
|
||||
@Override
|
||||
public void onSuccess(Account result) {
|
||||
|
|
|
@ -33,6 +33,7 @@ import it.niedermann.owncloud.notes.branding.BrandingUtil;
|
|||
import it.niedermann.owncloud.notes.exception.IntendedOfflineException;
|
||||
import it.niedermann.owncloud.notes.main.navigation.NavigationAdapter;
|
||||
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
|
||||
import it.niedermann.owncloud.notes.persistence.ApiProvider;
|
||||
import it.niedermann.owncloud.notes.persistence.CapabilitiesClient;
|
||||
import it.niedermann.owncloud.notes.persistence.NotesRepository;
|
||||
import it.niedermann.owncloud.notes.persistence.entity.Account;
|
||||
|
@ -397,7 +398,7 @@ public class MainViewModel extends AndroidViewModel {
|
|||
try {
|
||||
final SingleSignOnAccount ssoAccount = AccountImporter.getSingleSignOnAccount(getApplication(), localAccount.getAccountName());
|
||||
try {
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplication(), ssoAccount, localAccount.getCapabilitiesETag());
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplication(), ssoAccount, localAccount.getCapabilitiesETag(), ApiProvider.getInstance());
|
||||
repo.updateCapabilitiesETag(localAccount.getId(), capabilities.getETag());
|
||||
repo.updateBrand(localAccount.getId(), capabilities.getColor(), capabilities.getTextColor());
|
||||
localAccount.setColor(capabilities.getColor());
|
||||
|
|
|
@ -26,8 +26,8 @@ public class CapabilitiesClient {
|
|||
private static final String HEADER_KEY_ETAG = "ETag";
|
||||
|
||||
@WorkerThread
|
||||
public static Capabilities getCapabilities(@NonNull Context context, @NonNull SingleSignOnAccount ssoAccount, @Nullable String lastETag) throws Throwable {
|
||||
final OcsAPI ocsAPI = ApiProvider.getInstance().getOcsAPI(context, ssoAccount);
|
||||
public static Capabilities getCapabilities(@NonNull Context context, @NonNull SingleSignOnAccount ssoAccount, @Nullable String lastETag, @NonNull ApiProvider apiProvider) throws Throwable {
|
||||
final OcsAPI ocsAPI = apiProvider.getOcsAPI(context, ssoAccount);
|
||||
try {
|
||||
final ParsedResponse<OcsResponse<Capabilities>> response = ocsAPI.getCapabilities(lastETag).blockingSingle();
|
||||
final Capabilities capabilities = response.getResponse().ocs.data;
|
||||
|
@ -50,8 +50,8 @@ public class CapabilitiesClient {
|
|||
|
||||
@WorkerThread
|
||||
@Nullable
|
||||
public static String getDisplayName(@NonNull Context context, @NonNull SingleSignOnAccount ssoAccount) {
|
||||
final OcsAPI ocsAPI = ApiProvider.getInstance().getOcsAPI(context, ssoAccount);
|
||||
public static String getDisplayName(@NonNull Context context, @NonNull SingleSignOnAccount ssoAccount, @NonNull ApiProvider apiProvider) {
|
||||
final OcsAPI ocsAPI = apiProvider.getOcsAPI(context, ssoAccount);
|
||||
try {
|
||||
final Response<OcsResponse<OcsUser>> userResponse = ocsAPI.getUser(ssoAccount.userId).execute();
|
||||
if (userResponse.isSuccessful()) {
|
||||
|
|
|
@ -47,18 +47,18 @@ public class CapabilitiesWorker extends Worker {
|
|||
try {
|
||||
final SingleSignOnAccount ssoAccount = AccountImporter.getSingleSignOnAccount(getApplicationContext(), account.getAccountName());
|
||||
Log.i(TAG, "Refreshing capabilities for " + ssoAccount.name);
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, account.getCapabilitiesETag());
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, account.getCapabilitiesETag(), ApiProvider.getInstance());
|
||||
repo.updateCapabilitiesETag(account.getId(), capabilities.getETag());
|
||||
repo.updateBrand(account.getId(), capabilities.getColor(), capabilities.getTextColor());
|
||||
repo.updateApiVersion(account.getId(), capabilities.getApiVersion());
|
||||
Log.i(TAG, capabilities.toString());
|
||||
repo.updateDisplayName(account.getId(), CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount));
|
||||
repo.updateDisplayName(account.getId(), CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount, ApiProvider.getInstance()));
|
||||
} catch (Throwable e) {
|
||||
if (e instanceof NextcloudHttpRequestFailedException) {
|
||||
if (((NextcloudHttpRequestFailedException) e).getStatusCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
||||
Log.i(TAG, "Capabilities not modified.");
|
||||
return Result.success();
|
||||
} else if(((NextcloudHttpRequestFailedException) e).getStatusCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
|
||||
} else if (((NextcloudHttpRequestFailedException) e).getStatusCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
|
||||
Log.i(TAG, "Server is in maintenance mode.");
|
||||
return Result.success();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package it.niedermann.owncloud.notes.persistence;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.nextcloud.android.sso.api.ParsedResponse;
|
||||
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 org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import it.niedermann.owncloud.notes.persistence.sync.OcsAPI;
|
||||
import it.niedermann.owncloud.notes.shared.model.Capabilities;
|
||||
import it.niedermann.owncloud.notes.shared.model.OcsResponse;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = {Build.VERSION_CODES.P})
|
||||
public class CapabilitiesClientTest {
|
||||
|
||||
@Rule
|
||||
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
|
||||
|
||||
private final SingleSignOnAccount ssoAccount = mock(SingleSignOnAccount.class);
|
||||
private final ApiProvider apiProvider = mock(ApiProvider.class);
|
||||
private final OcsAPI ocsAPI = mock(OcsAPI.class);
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
when(apiProvider.getOcsAPI(any(), any())).thenReturn(ocsAPI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCapabilities() throws Throwable {
|
||||
//noinspection unchecked
|
||||
final ParsedResponse<OcsResponse<Capabilities>> responseMock = mock(ParsedResponse.class);
|
||||
|
||||
final OcsResponse<Capabilities> mockOcs = new OcsResponse<>();
|
||||
mockOcs.ocs = new OcsResponse.OcsWrapper<>();
|
||||
mockOcs.ocs.data = new Capabilities();
|
||||
mockOcs.ocs.data.setApiVersion("[1.0]");
|
||||
|
||||
when(responseMock.getResponse()).thenReturn(mockOcs);
|
||||
when(responseMock.getHeaders()).thenReturn(Map.of("ETag", "1234"));
|
||||
when(ocsAPI.getCapabilities(any())).thenReturn(Observable.just(responseMock));
|
||||
|
||||
final Capabilities capabilities = CapabilitiesClient.getCapabilities(ApplicationProvider.getApplicationContext(), ssoAccount, null, apiProvider);
|
||||
|
||||
assertEquals("[1.0]", capabilities.getApiVersion());
|
||||
assertEquals("ETag should be read correctly from response but wasn't.", "1234", capabilities.getETag());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue