diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java b/src/com/owncloud/android/lib/resources/users/RemoteGetUserQuotaOperation.java similarity index 62% rename from src/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java rename to src/com/owncloud/android/lib/resources/users/RemoteGetUserQuotaOperation.java index 3d394fd5..e059cf41 100644 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.java +++ b/src/com/owncloud/android/lib/resources/users/RemoteGetUserQuotaOperation.java @@ -1,33 +1,68 @@ -package com.owncloud.android.lib.resources.users; +/* ownCloud Android Library is available under MIT license + * + * Copyright (C) 2015 ownCloud Inc. + * Copyright (C) 2015 Bartosz Przybylski + * Copyright (C) 2014 Marcello Steiner + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ -import android.util.Base64; -import android.util.Log; +package com.owncloud.android.lib.resources.users; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.OwnCloudCredentials; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.http.HttpStatus; import org.json.JSONObject; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; /** - * Created by marcello on 11/11/14. + * @author marcello */ -public class GetRemoteUserQuotaOperation extends RemoteOperation { +public class RemoteGetUserQuotaOperation extends RemoteOperation { - private static final String TAG = GetRemoteUserNameOperation.class.getSimpleName(); + static public class Quota { + long mFree, mUsed, mTotal; + double mRelative; + public Quota(long free, long used, long total, double relative) { + mFree = free; + mUsed = used; + mTotal = total; + mRelative = relative; + } + + public long getFree() { return mFree; } + public long getUsed() { return mUsed; } + public long getTotal() { return mTotal; } + public double getRelative() { return mRelative; } + } + + private static final String TAG = RemoteGetUserQuotaOperation.class.getSimpleName(); private static final String NODE_OCS = "ocs"; private static final String NODE_DATA = "data"; @@ -43,7 +78,7 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation { @Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; - int status = -1; + int status; GetMethod get = null; @@ -53,7 +88,6 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation { String url = client.getBaseUri() + OCS_ROUTE + credentials.getUsername(); get = new GetMethod(url); - get.addRequestHeader(createAuthenticationHeader(credentials)); get.setQueryString(new NameValuePair[]{new NameValuePair("format","json")}); status = client.executeMethod(get); @@ -75,10 +109,7 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation { result = new RemoteOperationResult(true, status, get.getResponseHeaders()); //Quota data in data collection ArrayList data = new ArrayList(); - data.add(quotaFree); - data.add(quotaUsed); - data.add(quotaTotal); - data.add(quotaRelative); + data.add(new Quota(quotaFree, quotaUsed, quotaTotal, quotaRelative)); result.setData(data); } else { @@ -106,14 +137,5 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation { return (status == HttpStatus.SC_OK); } - private Header createAuthenticationHeader(OwnCloudCredentials credentials){ - Header h = new Header(); - h.setName("Authorization"); - String authString = credentials.getUsername()+":"+credentials.getAuthToken(); - String encodedString = Base64.encodeToString(authString.getBytes(), Base64.NO_WRAP); - h.setValue("Basic "+encodedString); - return h; - } - } diff --git a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java index 18770120..7405af59 100644 --- a/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java +++ b/test_client/src/com/owncloud/android/lib/test_project/TestActivity.java @@ -44,6 +44,7 @@ import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.network.NetworkUtils; +import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation; import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation; @@ -57,6 +58,7 @@ import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation; import com.owncloud.android.lib.resources.shares.GetRemoteSharesOperation; import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation; import com.owncloud.android.lib.resources.shares.ShareType; +import com.owncloud.android.lib.resources.users.RemoteGetUserQuotaOperation; /** * Activity to test OC framework @@ -330,6 +332,11 @@ public class TestActivity extends Activity { return result; } + + public RemoteOperationResult getQuota() { + RemoteGetUserQuotaOperation getUserQuotaOperation = new RemoteGetUserQuotaOperation(); + return getUserQuotaOperation.execute(mClient); + } /** diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetUserQuotaTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetUserQuotaTest.java new file mode 100644 index 00000000..26ebd196 --- /dev/null +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/GetUserQuotaTest.java @@ -0,0 +1,81 @@ +/* ownCloud Android Library is available under MIT license + * + * Copyright (C) 2015 ownCloud Inc. + * Copyright (C) 2015 Bartosz Przybylski + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.test_project.test; + +import java.io.File; +import java.util.*; + +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.resources.users.RemoteGetUserQuotaOperation.Quota; +import com.owncloud.android.lib.test_project.TestActivity; +import com.owncloud.android.lib.resources.files.*; + + +/** + * Class to test Get User Quota + * + * @author Bartosz Przybylski + * + */ +public class GetUserQuotaTest extends RemoteTest { + + + private static final String LOG_TAG = GetUserQuotaTest.class.getCanonicalName(); + + /* Files to download. These files must exist on the account */ + private static final String IMAGE_PATH = "/fileToDownload.png"; + private static final String IMAGE_PATH_WITH_SPECIAL_CHARS = "/@file@download.png"; + private static final String IMAGE_NOT_FOUND = "/fileNotFound.png"; + + private String mFullPath2Image; + private String mFullPath2ImageWitSpecialChars; + private String mFullPath2ImageNotFound; + private String mDownloadedFilePath; + private TestActivity mActivity; + + + @Override + protected void setUp() throws Exception { + super.setUp(); + setActivityInitialTouchMode(false); + mActivity = getActivity(); + } + + /** + * Test GetUserQuota + */ + public void testGetUserQuota() { + RemoteOperationResult result = mActivity.getQuota(); + assertTrue(result.isSuccess()); + Quota quota = (Quota)((ArrayList)result.getData()).get(0); + assertTrue(quota.getFree() >= 0); + assertTrue(quota.getUsed() >= 0); + assertTrue(quota.getTotal() > 0); + } + +}