@@ -29,140 +29,94 @@ import com.squareup.moshi.JsonAdapter
29
29
import com.squareup.moshi.Moshi
30
30
import com.squareup.moshi.Types
31
31
import org.junit.Assert.assertEquals
32
- import org.junit.Assert.assertTrue
33
32
import org.junit.Assert.assertNotEquals
33
+ import org.junit.Assert.assertTrue
34
34
import org.junit.Before
35
35
import org.junit.Test
36
+ import java.io.File
36
37
import java.lang.reflect.Type
37
38
38
39
class ShareeResponseTest {
39
40
40
- var response: CommonOcsResponse <ShareeOcsResponse >? = null
41
+ lateinit var response: CommonOcsResponse <ShareeOcsResponse >
42
+
43
+ private fun loadResponses (fileName : String , adapter : JsonAdapter <CommonOcsResponse <ShareeOcsResponse >>) =
44
+ adapter.fromJson(File (fileName).readText())
41
45
42
46
@Before
43
47
fun prepare () {
44
48
val moshi = Moshi .Builder ().build()
45
49
val type: Type = Types .newParameterizedType(CommonOcsResponse ::class .java, ShareeOcsResponse ::class .java)
46
50
val adapter: JsonAdapter <CommonOcsResponse <ShareeOcsResponse >> = moshi.adapter(type)
47
- response = adapter.fromJson( EXAMPLE_RESPONSE )
51
+ response = loadResponses( EXAMPLE_RESPONSE_JSON , adapter) !!
48
52
}
49
53
50
54
@Test
51
55
fun `check structure - ok - contains meta` () {
52
- assertEquals(" OK" , response? .ocs? .meta? .message!! )
53
- assertEquals(200 , response? .ocs? .meta? .statusCode!! )
54
- assertEquals(" ok" , response? .ocs? .meta? .status!! )
55
- assertTrue(response? .ocs? .meta? .itemsPerPage?.isEmpty()!! )
56
- assertTrue(response? .ocs? .meta? .totalItems?.isEmpty()!! )
56
+ assertEquals(" OK" , response.ocs.meta.message!! )
57
+ assertEquals(200 , response.ocs.meta.statusCode!! )
58
+ assertEquals(" ok" , response.ocs.meta.status!! )
59
+ assertTrue(response.ocs.meta.itemsPerPage?.isEmpty()!! )
60
+ assertTrue(response.ocs.meta.totalItems?.isEmpty()!! )
57
61
}
58
62
59
63
@Test
60
64
fun `check structure - ok - contains exact` () {
61
- assertNotEquals(null , response? .ocs? .data? .exact)
65
+ assertNotEquals(null , response.ocs.data.exact)
62
66
}
63
67
64
68
@Test
65
69
fun `check structure - ok - contains groups` () {
66
- assertNotEquals(null , response? .ocs? .data? .groups)
70
+ assertNotEquals(null , response.ocs.data.groups)
67
71
}
68
72
69
73
@Test
70
74
fun `check structure - ok - contains remotes` () {
71
- assertNotEquals(null , response? .ocs? .data? .remotes)
75
+ assertNotEquals(null , response.ocs.data.remotes)
72
76
}
73
77
74
78
@Test
75
79
fun `check structure - ok - contains users` () {
76
- assertNotEquals(null , response? .ocs? .data? .users)
80
+ assertNotEquals(null , response.ocs.data.users)
77
81
}
78
82
79
83
@Test
80
84
fun `check structure - ok - groups contains two items` () {
81
- assertEquals(2 , response? .ocs? .data? .groups? .size)
85
+ assertEquals(2 , response.ocs.data.groups.size)
82
86
}
83
87
84
88
@Test
85
89
fun `check structure - ok - users contains two items` () {
86
- assertEquals(2 , response? .ocs? .data? .users? .size)
90
+ assertEquals(2 , response.ocs.data.users.size)
87
91
}
88
92
89
93
@Test
90
94
fun `check structure - ok - exact_users contains one item` () {
91
- assertEquals(1 , response? .ocs? .data? .exact?.users?.size)
95
+ assertEquals(1 , response.ocs.data.exact?.users?.size)
92
96
}
93
97
94
98
@Test
95
99
fun `check structure - ok - user1 contains additional data` () {
96
- assertEquals(
" [email protected] " , response
? .ocs
? .data
? .users
? .get(
0 )
? .value
? .additionalInfo)
100
+ assertEquals(
" [email protected] " , response.ocs.data.users.get(
0 ).value.additionalInfo)
97
101
}
98
102
99
103
@Test
100
104
fun `check structure - ok - user2 does not contain additional data` () {
101
- assertEquals(null , response!! .ocs.data.users!! [1 ].value!! .additionalInfo)
105
+ assertEquals(null , response.ocs.data.users[1 ].value.additionalInfo)
102
106
}
103
107
104
- companion object {
105
- val EXAMPLE_RESPONSE = """
106
- {
107
- "ocs": {
108
- "data": {
109
- "exact": {
110
- "groups": [],
111
- "remotes": [],
112
- "users": [
113
- {
114
- "label": "admin",
115
- "value": {
116
- "shareType": 0,
117
- "shareWith": "admin"
118
- }
119
- }
120
- ]
121
- },
122
- "groups": [
123
- {
124
- "label": "group1",
125
- "value": {
126
- "shareType": 1,
127
- "shareWith": "group1"
128
- }
129
- },
130
- {
131
- "label": "group2",
132
- "value": {
133
- "shareType": 1,
134
- "shareWith": "group2"
135
- }
136
- }
137
- ],
138
- "remotes": [],
139
- "users": [
140
- {
141
- "label": "user1",
142
- "value": {
143
- "shareType": 0,
144
- "shareWith": "user1",
145
- "shareWithAdditionalInfo": "[email protected] "
146
- }
147
- },
148
- {
149
- "label": "user2",
150
- "value": {
151
- "shareType": 0,
152
- "shareWith": "user2"
153
- }
154
- }
155
- ]
156
- },
157
- "meta": {
158
- "itemsperpage": "",
159
- "message": "OK",
160
- "status": "ok",
161
- "statuscode": 200,
162
- "totalitems": ""
163
- }
108
+ @Test
109
+ fun `check empty response - ok - parsing ok` () {
110
+ val moshi = Moshi .Builder ().build()
111
+ val type: Type = Types .newParameterizedType(CommonOcsResponse ::class .java, ShareeOcsResponse ::class .java)
112
+ val adapter: JsonAdapter <CommonOcsResponse <ShareeOcsResponse >> = moshi.adapter(type)
113
+ loadResponses(EMPTY_RESPONSE_JSON , adapter)!!
164
114
}
165
- }
166
- """
115
+
116
+ companion object {
117
+ val RESOURCES_PATH =
118
+ " /home/schabi/Projects/owncloud-android/owncloud-android-library/owncloudComLibrary/src/test/resources/com.owncloud.android.lib.resources.sharees.responses"
119
+ val EXAMPLE_RESPONSE_JSON = " $RESOURCES_PATH /example_sharee_response.json"
120
+ val EMPTY_RESPONSE_JSON = " $RESOURCES_PATH /empty_sharee_response.json"
167
121
}
168
122
}
0 commit comments