diff --git a/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetActivitiesRemoteOperationTest.kt b/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetActivitiesRemoteOperationTest.kt new file mode 100644 index 000000000..133c38798 --- /dev/null +++ b/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetActivitiesRemoteOperationTest.kt @@ -0,0 +1,44 @@ +/* + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2019 Tobias Kaminsky + * Copyright (C) 2019 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package com.nextcloud.lib.resources.users + +import com.owncloud.android.AbstractIT +import com.owncloud.android.lib.resources.activities.GetActivitiesRemoteOperation +import com.owncloud.android.lib.resources.activities.model.Activity +import org.junit.Assert.assertTrue +import org.junit.Test +import java.util.* + +class GetActivitiesRemoteOperationTest : AbstractIT() { + @Test + fun getActivities() { + val result = GetActivitiesRemoteOperation().execute(nextcloudClient) + assertTrue(result.isSuccess) + + val activities = result.data[0] as ArrayList + val lastGiven = result.data[1] as Integer; + + assertTrue(activities.isNotEmpty()) + assertTrue(lastGiven > 0) + } +} diff --git a/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetUserInfoRemoteOperationTest.kt b/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetUserInfoRemoteOperationTest.kt index 74e58b32e..6032c523d 100644 --- a/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetUserInfoRemoteOperationTest.kt +++ b/src/androidTest/kotlin/com/nextcloud/lib/resources/users/GetUserInfoRemoteOperationTest.kt @@ -29,9 +29,9 @@ package com.nextcloud.lib.resources.users import androidx.test.platform.app.InstrumentationRegistry import com.owncloud.android.AbstractIT +import com.owncloud.android.lib.common.OwnCloudBasicCredentials import com.owncloud.android.lib.common.UserInfo import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation -import okhttp3.Credentials import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -40,27 +40,27 @@ import org.junit.Test class GetUserInfoRemoteOperationTest : AbstractIT() { @Test fun testGetUserNoQuota() { - nextcloudClient.credentials = Credentials.basic("user1", "user1") - val userInfoResult = GetUserInfoRemoteOperation().execute(nextcloudClient) + client.credentials = OwnCloudBasicCredentials("user1", "user1") + val userInfoResult = GetUserInfoRemoteOperation().execute(client) assertTrue(userInfoResult.isSuccess) - val userInfo = userInfoResult.data[0] as UserInfo - assertEquals("User One", userInfo.displayName) - assertEquals("user1", userInfo.id) + + assertEquals("User One", userInfo.getDisplayName()) + assertEquals("user1", userInfo.getId()) assertEquals(GetUserInfoRemoteOperation.SPACE_UNLIMITED, - userInfo.quota?.quota) + userInfo.getQuota().getQuota()) } @Test fun testGetUser1GbQuota() { - nextcloudClient.credentials = Credentials.basic("user2", "user2") - val userInfoResult = GetUserInfoRemoteOperation().execute(nextcloudClient) + client.credentials = OwnCloudBasicCredentials("user2", "user2") + val userInfoResult = GetUserInfoRemoteOperation().execute(client) assertTrue(userInfoResult.isSuccess) - val userInfo = userInfoResult.data[0] as UserInfo - assertEquals("User Two", userInfo.displayName) - assertEquals("user2", userInfo.id) - assertEquals(1073741824L, userInfo.quota?.quota) + + assertEquals("User Two", userInfo.getDisplayName()) + assertEquals("user2", userInfo.getId()) + assertEquals(1073741824, userInfo.getQuota().getQuota()) } @After @@ -70,6 +70,6 @@ class GetUserInfoRemoteOperationTest : AbstractIT() { val loginName = arguments.getString("TEST_SERVER_USERNAME") val password = arguments.getString("TEST_SERVER_PASSWORD") - nextcloudClient.credentials = Credentials.basic(loginName, password) + client.credentials = OwnCloudBasicCredentials(loginName, password) } } diff --git a/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java b/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java index fc60dfecc..abd58d830 100644 --- a/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java +++ b/src/main/java/com/owncloud/android/lib/resources/activities/GetActivitiesRemoteOperation.java @@ -34,6 +34,8 @@ import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -48,11 +50,11 @@ import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; -import org.apache.commons.httpclient.methods.GetMethod; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -94,12 +96,80 @@ public GetActivitiesRemoteOperation(int lastGiven) { } @Override - protected RemoteOperationResult run(OwnCloudClient client) { + protected RemoteOperationResult run(NextcloudClient client) { RemoteOperationResult result; int status; GetMethod get = null; ArrayList activities; String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP; + + // add filter for fileId, if available + if (!fileId.isEmpty()) { + url = url + "/filter"; + } + + Log_OC.d(TAG, "URL: " + url); + + try { + get = new GetMethod(url, true); + + HashMap parameters = new HashMap<>(); + parameters.put("format", "json"); + parameters.put("previews", "true"); + + if (lastGiven != -1) { + parameters.put("since", String.valueOf(lastGiven)); + } + + if (!fileId.isEmpty()) { + parameters.put("sort", "desc"); + parameters.put("object_type", "files"); + parameters.put("object_id", fileId); + } + + get.setQueryString(parameters); + + status = client.execute(get); + String response = get.getResponseBodyAsString(); + + String nextPageHeader = get.response.header("X-Activity-Last-Given"); + if (nextPageHeader != null) { + lastGiven = Integer.parseInt(nextPageHeader); + } else { + lastGiven = -1; + } + + if (isSuccess(status)) { + Log_OC.d(TAG, "Successful response: " + response); + result = new RemoteOperationResult(true, get); + // Parse the response + activities = parseResult(response); + + ArrayList data = new ArrayList<>(); + data.add(activities); + data.add(lastGiven); + result.setData(data); + } else { + result = new RemoteOperationResult(false, get); + Log_OC.e(TAG, "Failed response while getting user activities"); + Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); + } + } finally { + if (get != null) { + get.releaseConnection(); + } + } + + return result; + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result; + int status; + org.apache.commons.httpclient.methods.GetMethod get = null; + ArrayList activities; + String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP; // add filter for fileId, if available if (!fileId.isEmpty()) { @@ -109,7 +179,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { Log_OC.d(TAG, "URL: " + url); try { - get = new GetMethod(url); + get = new org.apache.commons.httpclient.methods.GetMethod(url); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); ArrayList parameters = new ArrayList<>(); diff --git a/src/main/java/com/owncloud/android/lib/resources/users/GetUserInfoRemoteOperation.java b/src/main/java/com/owncloud/android/lib/resources/users/GetUserInfoRemoteOperation.java index 07a9db573..3d2c81091 100644 --- a/src/main/java/com/owncloud/android/lib/resources/users/GetUserInfoRemoteOperation.java +++ b/src/main/java/com/owncloud/android/lib/resources/users/GetUserInfoRemoteOperation.java @@ -28,8 +28,7 @@ package com.owncloud.android.lib.resources.users; import com.google.gson.reflect.TypeToken; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.Quota; import com.owncloud.android.lib.common.UserInfo; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -38,9 +37,10 @@ import com.owncloud.android.lib.resources.OCSRemoteOperation; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.GetMethod; import java.util.ArrayList; -import java.util.HashMap; /** * Gets information (id, display name, and e-mail address and many other things) about the user logged in. @@ -54,7 +54,7 @@ public class GetUserInfoRemoteOperation extends OCSRemoteOperation { private static final String TAG = GetUserInfoRemoteOperation.class.getSimpleName(); // OCS Route - private static final String OCS_ROUTE_SELF = "/ocs/v2.php/cloud/user"; + private static final String OCS_ROUTE_SELF = "/ocs/v1.php/cloud/user"; /** * Quota return value for a not computed space value. @@ -77,7 +77,7 @@ public class GetUserInfoRemoteOperation extends OCSRemoteOperation { public static final long QUOTA_LIMIT_INFO_NOT_AVAILABLE = Long.MIN_VALUE; @Override - protected RemoteOperationResult run(NextcloudClient client) { + protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result; int status; GetMethod get = null; @@ -87,17 +87,14 @@ protected RemoteOperationResult run(NextcloudClient client) { // get the user try { - get = new GetMethod(url, true); - HashMap map = new HashMap<>(); - map.put("format", "json"); - - get.setQueryString(map); - status = client.execute(get); + get = new GetMethod(url); + get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + get.setQueryString(new NameValuePair[]{new NameValuePair("format", "json")}); + status = client.executeMethod(get); if (isSuccess(status)) { - ServerResponse ocsResponse = getServerResponse(get, - new TypeToken>() { - }); + ServerResponse ocsResponse = getServerResponse(get, new TypeToken>() { + }); UserInfo userInfo = ocsResponse.getOcs().getData(); @@ -118,11 +115,11 @@ protected RemoteOperationResult run(NextcloudClient client) { } else { result = new RemoteOperationResult(false, get); String response = get.getResponseBodyAsString(); - Log_OC.e(TAG, "Failed response while getting user information"); - if (response.isEmpty()) { - Log_OC.e(TAG, "*** status code: " + status); + Log_OC.e(TAG, "Failed response while getting user information "); + if (response != null) { + Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); } else { - Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response); + Log_OC.e(TAG, "*** status code: " + status); } } } catch (Exception e) {