diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt new file mode 100644 index 000000000..beb585c8c --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/NotificationIT.kt @@ -0,0 +1,50 @@ +/* Nextcloud Android Library is available under MIT license + * + * @author Tobias Kaminsky + * Copyright (C) 2023 Tobias Kaminsky + * Copyright (C) 2023 Nextcloud GmbH + * + * 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.resources.notifications + +import com.owncloud.android.AbstractIT +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertTrue +import org.junit.Test + +class NotificationIT : AbstractIT() { + @Test + fun getNotification() { + // get all + val all = GetNotificationsRemoteOperation().execute(client) + assertTrue(all.isSuccess) + + // get one + val firstNotification = all.resultData[0] + val first = GetNotificationRemoteOperation(firstNotification.notificationId).execute( + client + ) + assertTrue(first.isSuccess) + assertEquals(firstNotification.message, first.resultData.message) + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index d017b9014..23ad7a298 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/library/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -35,8 +35,6 @@ import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.files.CreateLocalFileException; -import com.owncloud.android.lib.resources.notifications.models.Notification; -import com.owncloud.android.lib.resources.notifications.models.PushResponse; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.Header; @@ -58,7 +56,6 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; -import java.util.List; import java.util.Locale; import javax.net.ssl.SSLException; @@ -156,8 +153,6 @@ public enum ResultCode { private ArrayList mData; private T resultData; - private List mNotificationData; - private PushResponse mPushResponse; /** * Public constructor from result code. @@ -533,41 +528,6 @@ public Object getSingleData() { return mData.get(0); } - /** - * @deprecated use getResultData() instead - */ - public void setNotificationData(List notifications) { - mNotificationData = notifications; - } - - /** - * @deprecated use getResultData() instead - */ - public PushResponse getPushResponseData() { - if (!mSuccess) { - throw new RuntimeException("Accessing result data after operation failed!"); - } - return mPushResponse; - } - - /** - * @deprecated use getResultData() instead - */ - public void setPushResponseData(PushResponse pushResponseData) { - mPushResponse = pushResponseData; - } - - /** - * @deprecated use getResultData() instead - */ - public List getNotificationData() { - if (!mSuccess) { - throw new RuntimeException("Accessing result data after operation failed!"); - } - return mNotificationData; - } - - public boolean isSuccess() { return mSuccess; } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java index 8aa6d5e6a..2a6ddd3f9 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationRemoteOperation.java @@ -39,11 +39,8 @@ import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; -import org.json.JSONException; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -52,7 +49,7 @@ * accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. */ -public class GetNotificationRemoteOperation extends RemoteOperation { +public class GetNotificationRemoteOperation extends RemoteOperation { // OCS Route private static final String OCS_ROUTE_LIST_V12_AND_UP = @@ -70,14 +67,13 @@ public GetNotificationRemoteOperation(int id) { @SuppressFBWarnings("HTTP_PARAMETER_POLLUTION") @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result; int status; GetMethod get = null; - List notifications = new ArrayList<>(); String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP + id + JSON_FORMAT; - // get the notifications + // get the notification try { get = new GetMethod(url); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); @@ -86,14 +82,14 @@ protected RemoteOperationResult run(OwnCloudClient client) { String response = get.getResponseBodyAsString(); if (isSuccess(status)) { - result = new RemoteOperationResult(true, status, get.getResponseHeaders()); + result = new RemoteOperationResult<>(true, status, get.getResponseHeaders()); Log_OC.d(this, "Successful response: " + response); // Parse the response - notifications.add(parseResult(response)); - result.setNotificationData(notifications); + Notification notification = parseResult(response); + result.setResultData(notification); } else { - result = new RemoteOperationResult(false, status, get.getResponseHeaders()); + result = new RemoteOperationResult<>(false, status, get.getResponseHeaders()); Log_OC.e(this, "Failed response while getting user notifications "); if (response != null) { Log_OC.e(this, "*** status code: " + status + " ; response message: " + response); @@ -102,7 +98,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { } } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(this, "Exception while getting remote notifications", e); } finally { if (get != null) { @@ -113,9 +109,8 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } - private Notification parseResult(String response) throws JSONException { - JsonParser jsonParser = new JsonParser(); - JsonObject jo = (JsonObject) jsonParser.parse(response); + private Notification parseResult(String response) { + JsonObject jo = (JsonObject) JsonParser.parseString(response); JsonObject jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA); Gson gson = new Gson(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java index f84aef890..4990931bd 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperation.java @@ -42,7 +42,6 @@ import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; -import org.json.JSONException; import java.lang.reflect.Type; import java.util.List; @@ -52,7 +51,7 @@ * accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}. */ -public class GetNotificationsRemoteOperation extends RemoteOperation { +public class GetNotificationsRemoteOperation extends RemoteOperation> { // OCS Route private static final String OCS_ROUTE_LIST_V12_AND_UP = @@ -66,9 +65,9 @@ public class GetNotificationsRemoteOperation extends RemoteOperation { private static final String NODE_DATA = "data"; @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; - int status = -1; + protected RemoteOperationResult> run(OwnCloudClient client) { + RemoteOperationResult> result; + int status; GetMethod get = null; List notifications; String url = client.getBaseUri() + OCS_ROUTE_LIST_V12_AND_UP; @@ -82,14 +81,14 @@ protected RemoteOperationResult run(OwnCloudClient client) { String response = get.getResponseBodyAsString(); if (isSuccess(status)) { - result = new RemoteOperationResult(true, status, get.getResponseHeaders()); + result = new RemoteOperationResult<>(true, status, get.getResponseHeaders()); Log_OC.d(TAG, "Successful response: " + response); // Parse the response notifications = parseResult(response); - result.setNotificationData(notifications); + result.setResultData(notifications); } else { - result = new RemoteOperationResult(false, status, get.getResponseHeaders()); + result = new RemoteOperationResult<>(false, status, get.getResponseHeaders()); Log_OC.e(TAG, "Failed response while getting user notifications "); if (response != null) { Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); @@ -98,7 +97,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { } } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Exception while getting remote notifications", e); } finally { if (get != null) { @@ -109,13 +108,13 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } - private List parseResult(String response) throws JSONException { - JsonParser jsonParser = new JsonParser(); - JsonObject jo = (JsonObject)jsonParser.parse(response); + private List parseResult(String response) { + JsonObject jo = (JsonObject) JsonParser.parseString(response); JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA); Gson gson = new Gson(); - Type listType = new TypeToken>(){}.getType(); + Type listType = new TypeToken>() { + }.getType(); return gson.fromJson(jsonDataArray, listType); } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/notifications/RegisterAccountDeviceForNotificationsOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/notifications/RegisterAccountDeviceForNotificationsOperation.java index bb28902fd..bb0be4ae0 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/notifications/RegisterAccountDeviceForNotificationsOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/notifications/RegisterAccountDeviceForNotificationsOperation.java @@ -39,11 +39,10 @@ import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.Utf8PostMethod; -import org.json.JSONException; import java.lang.reflect.Type; -public class RegisterAccountDeviceForNotificationsOperation extends RemoteOperation { +public class RegisterAccountDeviceForNotificationsOperation extends RemoteOperation { // OCS Route private static final String OCS_ROUTE = "/ocs/v2.php/apps/notifications/api/v2/push" + JSON_FORMAT; @@ -60,9 +59,9 @@ public class RegisterAccountDeviceForNotificationsOperation extends RemoteOperat private static final String PROXY_SERVER = "proxyServer"; private static final String INVALID_SESSION_TOKEN = "INVALID_SESSION_TOKEN"; - private String pushTokenHash; - private String devicePublicKey; - private String proxyServer; + private final String pushTokenHash; + private final String devicePublicKey; + private final String proxyServer; public RegisterAccountDeviceForNotificationsOperation(String pushTokenHash, String devicePublicKey, @@ -73,10 +72,9 @@ public RegisterAccountDeviceForNotificationsOperation(String pushTokenHash, } @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result; int status; - PushResponse pushResponse; Utf8PostMethod post = null; try { @@ -85,28 +83,28 @@ protected RemoteOperationResult run(OwnCloudClient client) { post.setParameter(PUSH_TOKEN_HASH, pushTokenHash); post.setParameter(DEVICE_PUBLIC_KEY, devicePublicKey); post.setParameter(PROXY_SERVER, proxyServer); - + post.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); status = client.executeMethod(post); String response = post.getResponseBodyAsString(); if (isSuccess(status)) { - result = new RemoteOperationResult(true, status, post.getResponseHeaders()); + result = new RemoteOperationResult<>(true, status, post.getResponseHeaders()); Log_OC.d(TAG, "Successful response: " + response); // Parse the response - pushResponse = parseResult(response); - result.setPushResponseData(pushResponse); + PushResponse pushResponse = parseResult(response); + result.setResultData(pushResponse); } else { if (isInvalidSessionToken(response)) { - result = new RemoteOperationResult(RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD); + result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD); } else { - result = new RemoteOperationResult(false, status, post.getResponseHeaders()); + result = new RemoteOperationResult<>(false, status, post.getResponseHeaders()); } } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Exception while registering device for notifications", e); } finally { if (post != null) { @@ -116,20 +114,19 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } - private PushResponse parseResult(String response) throws JSONException { - JsonParser jsonParser = new JsonParser(); - JsonObject jo = (JsonObject)jsonParser.parse(response); + private PushResponse parseResult(String response) { + JsonObject jo = (JsonObject) JsonParser.parseString(response); JsonObject jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA); Gson gson = new Gson(); - Type pushResponseType = new TypeToken(){}.getType(); + Type pushResponseType = new TypeToken() { + }.getType(); return gson.fromJson(jsonDataObject, pushResponseType); } private boolean isInvalidSessionToken(String response) { - JsonParser jsonParser = new JsonParser(); - JsonObject jsonObject = (JsonObject)jsonParser.parse(response); + JsonObject jsonObject = (JsonObject) JsonParser.parseString(response); String message = jsonObject.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA).get(MESSAGE).getAsString(); return INVALID_SESSION_TOKEN.equals(message); diff --git a/scripts/analysis/findbugs-results.txt b/scripts/analysis/findbugs-results.txt index dee261df4..405e2afe8 100644 --- a/scripts/analysis/findbugs-results.txt +++ b/scripts/analysis/findbugs-results.txt @@ -1 +1 @@ -140 +134