From af99117c75fafa1cd26a2d4d94cb906a257a6e34 Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Mon, 17 May 2021 18:38:09 +0200 Subject: [PATCH] Add Unit test for fetching Capabilities --- .../importaccount/ImportAccountActivity.java | 6 +- .../owncloud/notes/main/MainActivity.java | 4 +- .../owncloud/notes/main/MainViewModel.java | 3 +- .../notes/persistence/CapabilitiesClient.java | 8 +-- .../notes/persistence/CapabilitiesWorker.java | 6 +- .../persistence/CapabilitiesClientTest.java | 65 +++++++++++++++++++ 6 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 app/src/test/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClientTest.java diff --git a/app/src/main/java/it/niedermann/owncloud/notes/importaccount/ImportAccountActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/importaccount/ImportAccountActivity.java index 994c652c..00b41298 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/importaccount/ImportAccountActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/importaccount/ImportAccountActivity.java @@ -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() { /** diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java index ef4ae2fc..5e813e04 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java @@ -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() { @Override public void onSuccess(Account result) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java index 4ea49173..9c0296a1 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java @@ -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()); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClient.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClient.java index 3f27b820..33e42382 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClient.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClient.java @@ -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> 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> userResponse = ocsAPI.getUser(ssoAccount.userId).execute(); if (userResponse.isSuccessful()) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesWorker.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesWorker.java index 32edbed7..4f8852e7 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesWorker.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/CapabilitiesWorker.java @@ -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(); } diff --git a/app/src/test/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClientTest.java b/app/src/test/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClientTest.java new file mode 100644 index 00000000..61fc6388 --- /dev/null +++ b/app/src/test/java/it/niedermann/owncloud/notes/persistence/CapabilitiesClientTest.java @@ -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> responseMock = mock(ParsedResponse.class); + + final OcsResponse 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()); + } +} \ No newline at end of file