Skip to content

Commit 5cddfa6

Browse files
tobiasKaminskyAlvaroBrey
authored andcommitted
all to Kotlin
more tests Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
1 parent fae71d6 commit 5cddfa6

12 files changed

Lines changed: 558 additions & 341 deletions

library/src/androidTest/java/com/owncloud/android/AbstractIT.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public abstract class AbstractIT {
8383
public static OwnCloudClient client;
8484
public static OwnCloudClient client2;
8585
protected static NextcloudClient nextcloudClient;
86+
protected static NextcloudClient nextcloudClientAdmin;
8687
protected static Context context;
8788
protected static Uri url;
8889

@@ -122,6 +123,12 @@ public static void beforeAll() throws InterruptedException,
122123
String credentials = Credentials.basic(loginName, password);
123124
nextcloudClient = new NextcloudClient(url, userId, credentials, context);
124125

126+
nextcloudClientAdmin = new NextcloudClient(
127+
url,
128+
"admin",
129+
Credentials.basic("admin", "admin"),
130+
context);
131+
125132
waitForServer(client, url);
126133
testConnection();
127134
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2022 Tobias Kaminsky
5+
* Copyright (C) 2022 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.owncloud.android.lib.resources.notifications
29+
30+
import com.owncloud.android.AbstractIT
31+
import org.junit.Assert.assertTrue
32+
import org.junit.Test
33+
34+
class DeleteAllNotificationsRemoteOperationIT : AbstractIT() {
35+
@Test
36+
fun testDeleteAllNotification() {
37+
// create one notification
38+
assertTrue(
39+
nextcloudClientAdmin.execute(
40+
CreateNotificationRemoteOperation(
41+
nextcloudClient.userId,
42+
"test"
43+
)
44+
).isSuccess
45+
)
46+
47+
var result = nextcloudClient.execute(GetNotificationsRemoteOperation())
48+
assertTrue(result.isSuccess)
49+
assertTrue(result.resultData.isNotEmpty())
50+
51+
assertTrue(nextcloudClient.execute(DeleteAllNotificationsRemoteOperation()).isSuccess)
52+
53+
result = nextcloudClient.execute(GetNotificationsRemoteOperation())
54+
assertTrue(result.isSuccess)
55+
assertTrue(result.resultData.isEmpty())
56+
}
57+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2022 Tobias Kaminsky
5+
* Copyright (C) 2022 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.owncloud.android.lib.resources.notifications
29+
30+
import com.owncloud.android.AbstractIT
31+
import org.junit.Assert.assertFalse
32+
import org.junit.Assert.assertTrue
33+
import org.junit.Test
34+
35+
class DeleteNotificationRemoteOperationIT : AbstractIT() {
36+
@Test
37+
fun deleteOneNotification() {
38+
// create one notification
39+
assertTrue(
40+
nextcloudClientAdmin.execute(
41+
CreateNotificationRemoteOperation(
42+
nextcloudClient.userId,
43+
"test"
44+
)
45+
).isSuccess
46+
)
47+
48+
val result = nextcloudClient.execute(GetNotificationsRemoteOperation())
49+
assertTrue(result.isSuccess)
50+
assertTrue(result.resultData.isNotEmpty())
51+
52+
val firstNotificationId = result.resultData.first().notificationId
53+
54+
assertTrue(DeleteNotificationRemoteOperation(firstNotificationId).execute(nextcloudClient).isSuccess)
55+
56+
val getResult = GetNotificationRemoteOperation(firstNotificationId).execute(nextcloudClient)
57+
assertFalse(getResult.isSuccess)
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2022 Tobias Kaminsky
5+
* Copyright (C) 2022 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.owncloud.android.lib.resources.notifications
29+
30+
import com.owncloud.android.AbstractIT
31+
import org.junit.Assert.assertEquals
32+
import org.junit.Assert.assertFalse
33+
import org.junit.Assert.assertNotNull
34+
import org.junit.Assert.assertTrue
35+
import org.junit.Test
36+
37+
class GetNotificationRemoteOperationIT : AbstractIT() {
38+
@Test
39+
fun testSingleNotification() {
40+
// get all notifications
41+
val resultAll = nextcloudClient.execute(GetNotificationsRemoteOperation())
42+
assertTrue(resultAll.isSuccess)
43+
44+
val allNotifications = resultAll.resultData
45+
val firstNotificationId = allNotifications.first().notificationId
46+
47+
// check one specific
48+
val result = nextcloudClient.execute(GetNotificationRemoteOperation(firstNotificationId))
49+
assertTrue(result.isSuccess)
50+
51+
val notification = result.resultData
52+
assertNotNull(notification)
53+
assertEquals(firstNotificationId, notification.notificationId)
54+
}
55+
56+
@Test
57+
fun testNonExisting() {
58+
assertFalse(GetNotificationRemoteOperation(-1).execute(nextcloudClient).isSuccess)
59+
}
60+
}

library/src/androidTest/java/com/owncloud/android/lib/resources/notifications/GetNotificationsRemoteOperationIT.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,24 @@ package com.owncloud.android.lib.resources.notifications
2323

2424
import com.owncloud.android.AbstractIT
2525
import junit.framework.Assert.assertTrue
26+
import org.junit.Assert
2627
import org.junit.Test
2728

2829
class GetNotificationsRemoteOperationIT : AbstractIT() {
2930
@Test
3031
fun testNotifications() {
32+
// create one notification
33+
Assert.assertTrue(
34+
nextcloudClientAdmin.execute(
35+
CreateNotificationRemoteOperation(
36+
nextcloudClient.userId,
37+
"test"
38+
)
39+
).isSuccess
40+
)
41+
3142
val result = nextcloudClient.execute(GetNotificationsRemoteOperation())
3243
assertTrue(result.isSuccess)
33-
34-
val notifications = result.resultData
35-
36-
assertTrue(notifications.isNotEmpty())
44+
assertTrue(result.resultData.isNotEmpty())
3745
}
3846
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2022 Tobias Kaminsky
5+
* Copyright (C) 2022 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.owncloud.android.lib.resources.notifications
29+
30+
import com.nextcloud.common.NextcloudClient
31+
import com.nextcloud.operations.PostMethod
32+
import com.owncloud.android.lib.common.operations.RemoteOperation
33+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
34+
import com.owncloud.android.lib.common.utils.Log_OC
35+
import com.owncloud.android.lib.resources.notifications.models.Notification
36+
import okhttp3.FormBody
37+
import org.apache.commons.httpclient.HttpStatus
38+
39+
/**
40+
* Provides the remote notifications from the server handling the following data structure accessible via the
41+
* notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at {@link
42+
* "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}.
43+
*/
44+
class CreateNotificationRemoteOperation(private val userId: String, private val message: String) :
45+
RemoteOperation<Notification>() {
46+
override fun run(client: NextcloudClient): RemoteOperationResult<Notification> {
47+
var result: RemoteOperationResult<Notification>
48+
val status: Int
49+
var post: PostMethod? = null
50+
val url =
51+
client.baseUri.toString() + "/ocs/v2.php/apps/notifications/api/v2/admin_notifications/"
52+
53+
try {
54+
val bodyRequest = FormBody
55+
.Builder()
56+
.add("shortMessage", message)
57+
.build()
58+
59+
post = PostMethod(url + userId, true, bodyRequest)
60+
status = client.execute(post)
61+
val response = post.getResponseBodyAsString()
62+
if (isSuccess(status)) {
63+
result = RemoteOperationResult(true, post)
64+
Log_OC.d(TAG, "Successful response: $response")
65+
} else {
66+
result = RemoteOperationResult(false, post)
67+
Log_OC.e(TAG, "Failed response while getting user notifications ")
68+
Log_OC.e(TAG, "*** status code: $status ; response message: $response")
69+
}
70+
} catch (e: Exception) {
71+
result = RemoteOperationResult(e)
72+
Log_OC.e(TAG, "Exception while getting remote notifications", e)
73+
} finally {
74+
post?.releaseConnection()
75+
}
76+
return result
77+
}
78+
79+
private fun isSuccess(status: Int): Boolean {
80+
return status == HttpStatus.SC_OK
81+
}
82+
83+
companion object {
84+
private val TAG = CreateNotificationRemoteOperation::class.java.simpleName
85+
}
86+
}

0 commit comments

Comments
 (0)