From 1c08a942d116c3ada6c41019f9b212c6a6c5ef80 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 8 Oct 2020 15:57:03 +0200 Subject: [PATCH] test model --- .../lib/resources/CommonOcsResponse.kt | 10 +- .../shares/responses/ShareeResponse.kt | 22 --- .../android/lib/ShareeResponseTest.kt | 134 ++++++++++++++++++ 3 files changed, 142 insertions(+), 24 deletions(-) create mode 100644 owncloudComLibrary/src/test/java/com/owncloud/android/lib/ShareeResponseTest.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/CommonOcsResponse.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/CommonOcsResponse.kt index 2cfbe2ed..7a37dc82 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/CommonOcsResponse.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/CommonOcsResponse.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.resources +import com.squareup.moshi.Json import com.squareup.moshi.JsonClass // Response retrieved by OCS Rest API, used to obtain capabilities, shares and user info among others. @@ -41,6 +42,11 @@ data class OCSResponse( @JsonClass(generateAdapter = true) data class MetaData( val status: String, - val statuscode: Int, - val message: String? + @Json(name = "statuscode") + val statusCode: Int, + val message: String?, + @Json(name = "itemsperpage") + val itemsPerPage: String?, + @Json(name = "totalitems") + val totalItems: String? ) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponse.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponse.kt index 19f19728..fe3ed67c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponse.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareeResponse.kt @@ -32,14 +32,6 @@ import com.squareup.moshi.JsonClass */ @JsonClass(generateAdapter = true) data class ShareeOcsResponse( - @Json(name = "data") - val data: ShareeResponseData?, - @Json(name = "meta") - val meta: ShareeMeta? -) - -@JsonClass(generateAdapter = true) -data class ShareeResponseData( @Json(name = "exact") val exact: ExactSharees?, @Json(name = "groups") @@ -75,17 +67,3 @@ data class ShareeValue( @Json(name = "shareWith") val shareWith: String? ) - -@JsonClass(generateAdapter = true) -data class ShareeMeta( - @Json(name = "itemsperpage") - val itemsPerPage: Int?, - @Json(name = "message") - val message: String?, - @Json(name = "status") - val status: String?, - @Json(name = "statuscode") - val statusCode: Int?, - @Json(name = "totalitems") - val totalItems: Int? -) diff --git a/owncloudComLibrary/src/test/java/com/owncloud/android/lib/ShareeResponseTest.kt b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/ShareeResponseTest.kt new file mode 100644 index 00000000..4248018a --- /dev/null +++ b/owncloudComLibrary/src/test/java/com/owncloud/android/lib/ShareeResponseTest.kt @@ -0,0 +1,134 @@ +package com.owncloud.android.lib + +import com.owncloud.android.lib.resources.CommonOcsResponse +import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types +import junit.framework.Assert.assertEquals +import junit.framework.Assert.assertTrue +import org.junit.Assert.assertNotEquals +import org.junit.Before +import org.junit.Test +import java.lang.reflect.Type + +class ShareeResponseTest { + + var response: CommonOcsResponse? = null + + @Before + fun prepare() { + val moshi = Moshi.Builder().build() + val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareeOcsResponse::class.java) + val adapter: JsonAdapter> = moshi.adapter(type) + response = adapter.fromJson(EXAMPLE_RESPONSE) + } + + @Test + fun `check structure - ok - contains meta`() { + assertEquals("OK", response?.ocs?.meta?.message!!) + assertEquals(200, response?.ocs?.meta?.statusCode!!) + assertEquals("ok", response?.ocs?.meta?.status!!) + assertTrue(response?.ocs?.meta?.itemsPerPage?.isEmpty()!!) + assertTrue(response?.ocs?.meta?.totalItems?.isEmpty()!!) + } + + @Test + fun `check structure - ok - contains exact`() { + assertNotEquals(null, response?.ocs?.data?.exact) + } + + @Test + fun `check structure - ok - contains groups`() { + assertNotEquals(null, response?.ocs?.data?.groups) + } + + @Test + fun `check structure - ok - contains remotes`() { + assertNotEquals(null, response?.ocs?.data?.remotes) + } + + @Test + fun `check structure - ok - contains users`() { + assertNotEquals(null, response?.ocs?.data?.users) + } + + @Test + fun `check structure - ok - groups contains two items`() { + assertEquals(2, response?.ocs?.data?.groups?.size) + } + + @Test + fun `check structure - ok - users contains two items`() { + assertEquals(2, response?.ocs?.data?.users?.size) + } + + @Test + fun `check structure - ok - exact_users contains one item`() { + assertEquals(1, response?.ocs?.data?.exact?.users?.size) + } + + companion object { + val EXAMPLE_RESPONSE = """ +{ + "ocs": { + "data": { + "exact": { + "groups": [], + "remotes": [], + "users": [ + { + "label": "admin", + "value": { + "shareType": 0, + "shareWith": "admin" + } + } + ] + }, + "groups": [ + { + "label": "group1", + "value": { + "shareType": 1, + "shareWith": "group1" + } + }, + { + "label": "group2", + "value": { + "shareType": 1, + "shareWith": "group2" + } + } + ], + "remotes": [], + "users": [ + { + "label": "user1", + "value": { + "shareType": 0, + "shareWith": "user1" + } + }, + { + "label": "user2", + "value": { + "shareType": 0, + "shareWith": "user2" + } + } + ] + }, + "meta": { + "itemsperpage": "", + "message": "OK", + "status": "ok", + "statuscode": 200, + "totalitems": "" + } + } +} + """ + } +} \ No newline at end of file