-
Notifications
You must be signed in to change notification settings - Fork 9
/
CustomerIOTest.kt
308 lines (246 loc) · 9.89 KB
/
CustomerIOTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package io.customer.sdk
import android.net.Uri
import io.customer.base.data.ErrorResult
import io.customer.base.error.ErrorDetail
import io.customer.base.error.StatusCode
import io.customer.base.utils.ActionUtils.Companion.getEmptyAction
import io.customer.base.utils.ActionUtils.Companion.getErrorAction
import io.customer.sdk.MockCustomerIOBuilder.Companion.defaultConfig
import io.customer.sdk.data.communication.CustomerIOUrlHandler
import io.customer.sdk.data.model.Region
import io.customer.sdk.data.request.MetricEvent
import io.customer.sdk.utils.verifyError
import io.customer.sdk.utils.verifySuccess
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBeEqualTo
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.`when`
import org.mockito.kotlin.any
import org.mockito.kotlin.verify
internal class CustomerIOTest : BaseTest() {
private lateinit var customerIO: CustomerIO
lateinit var mockCustomerIO: MockCustomerIOBuilder
@Before
fun setUp() {
mockCustomerIO = MockCustomerIOBuilder()
customerIO = mockCustomerIO.build()
`when`(mockCustomerIO.store.deviceStore).thenReturn(deviceStore)
}
@Test
fun `verify SDK default configuration is correct`() {
customerIO.config.siteId shouldBeEqualTo MockCustomerIOBuilder.siteId
customerIO.config.apiKey shouldBeEqualTo MockCustomerIOBuilder.apiKey
customerIO.config.timeout shouldBeEqualTo MockCustomerIOBuilder.timeout.toLong()
customerIO.config.region shouldBeEqualTo MockCustomerIOBuilder.region
customerIO.config.urlHandler shouldBeEqualTo MockCustomerIOBuilder.urlHandler
customerIO.config.autoTrackScreenViews shouldBeEqualTo MockCustomerIOBuilder.shouldAutoRecordScreenViews
customerIO.config.autoTrackDeviceAttributes shouldBeEqualTo MockCustomerIOBuilder.autoTrackDeviceAttributes
}
@Test
fun verify_onUpdatingBuilderConfigurations_expectCustomerIOOConfigToBeUpdated() {
val mockCustomerIOBuilder =
MockCustomerIOBuilder(
defaultConfig.copy(
autoTrackDeviceAttributes = false,
autoTrackScreenViews = true,
siteId = "new-id",
apiKey = "new-key",
region = Region.EU,
urlHandler = object : CustomerIOUrlHandler {
override fun handleCustomerIOUrl(uri: Uri): Boolean {
return true
}
},
timeout = 9000
)
)
customerIO = mockCustomerIOBuilder.build()
customerIO.config.siteId shouldNotBeEqualTo defaultConfig.siteId
customerIO.config.apiKey shouldNotBeEqualTo defaultConfig.apiKey
customerIO.config.timeout shouldNotBeEqualTo defaultConfig.timeout
customerIO.config.region shouldNotBeEqualTo defaultConfig.region
customerIO.config.urlHandler shouldNotBeEqualTo defaultConfig.urlHandler
customerIO.config.autoTrackScreenViews shouldNotBeEqualTo defaultConfig.autoTrackScreenViews
customerIO.config.autoTrackDeviceAttributes shouldNotBeEqualTo defaultConfig.autoTrackDeviceAttributes
}
@Test
fun `verify SDK returns success when customer is identified`() {
`when`(
mockCustomerIO.api.identify(
identifier = any(),
attributes = any()
)
).thenReturn(getEmptyAction())
val response = customerIO.identify("test-identifier").execute()
verifySuccess(response, Unit)
}
@Test
fun `verify SDK returns error when customer identify request fails`() {
`when`(
mockCustomerIO.api.identify(
identifier = any(),
attributes = any()
)
).thenReturn(
getErrorAction(
errorResult = ErrorResult(
error =
ErrorDetail(statusCode = StatusCode.InternalServerError)
)
)
)
val response = customerIO.identify("test-identifier").execute()
verifyError(response, StatusCode.InternalServerError)
}
@Test
fun `verify SDK returns success when event is tracked`() {
`when`(
mockCustomerIO.api.track(
name = any(),
attributes = any()
)
).thenReturn(getEmptyAction())
val response = customerIO.track("event_name", mapOf("key" to "value")).execute()
verifySuccess(response, Unit)
}
@Test
fun `verify SDK returns success when screen is tracked`() {
`when`(
mockCustomerIO.api.screen(
name = any(),
attributes = any()
)
).thenReturn(getEmptyAction())
val response = customerIO.screen("Login", mapOf("key" to "value")).execute()
verifySuccess(response, Unit)
}
@Test
fun `verify SDK returns error when event tracking request fails`() {
`when`(
mockCustomerIO.api.track(
name = any(),
attributes = any()
)
).thenReturn(
getErrorAction(
errorResult = ErrorResult(
error =
ErrorDetail(statusCode = StatusCode.InternalServerError)
)
)
)
val response = customerIO.track("event_name", mapOf("key" to "value")).execute()
verifyError(response, StatusCode.InternalServerError)
}
@Test
fun `verify SDK returns error when screen tracking request fails`() {
`when`(
mockCustomerIO.api.screen(
name = any(),
attributes = any()
)
).thenReturn(
getErrorAction(
errorResult = ErrorResult(
error =
ErrorDetail(statusCode = StatusCode.BadRequest)
)
)
)
val response = customerIO.screen("Login", emptyMap()).execute()
verifyError(response, StatusCode.BadRequest)
}
@Test
fun `verify SDK returns success when device is added`() {
`when`(
mockCustomerIO.api.registerDeviceToken(
any(),
any(),
)
).thenReturn(getEmptyAction())
val response = customerIO.registerDeviceToken("event_name").execute()
verifySuccess(response, Unit)
}
@Test
fun verify_bothDefaultAndCustomAttributesGetsAdded_withRegisterToken() {
`when`(
mockCustomerIO.api.registerDeviceToken(
any(),
any(),
)
).thenReturn(getEmptyAction())
customerIO.deviceAttributes.putAll(mapOf("test" to "value"))
val expectedToken = "token"
val expectedAttributes = customerIO.deviceAttributes + deviceStore.buildDeviceAttributes()
val response = customerIO.registerDeviceToken(expectedToken).execute()
verify(mockCustomerIO.api).registerDeviceToken(expectedToken, expectedAttributes)
verifySuccess(response, Unit)
}
@Test
fun verify_defaultAttributesGetsSkipped_onBasisOfConfig() {
val mockCustomerIO =
MockCustomerIOBuilder(MockCustomerIOBuilder.defaultConfig.copy(autoTrackDeviceAttributes = false))
customerIO = mockCustomerIO.build()
`when`(mockCustomerIO.store.deviceStore).thenReturn(deviceStore)
`when`(
mockCustomerIO.api.registerDeviceToken(
any(),
any(),
)
).thenReturn(getEmptyAction())
customerIO.deviceAttributes.putAll(mapOf("test" to "value"))
val expectedToken = "token"
val expectedAttributes = mapOf("test" to "value")
customerIO.registerDeviceToken(expectedToken).execute()
verify(mockCustomerIO.api).registerDeviceToken(expectedToken, expectedAttributes)
}
@Test
fun `verify SDK returns error when adding device request fails`() {
`when`(
mockCustomerIO.api.registerDeviceToken(
any(),
any(),
)
)
.thenReturn(getErrorAction(errorResult = ErrorResult(error = ErrorDetail(statusCode = StatusCode.BadRequest))))
val response = customerIO.registerDeviceToken("event_name").execute()
verifyError(response, StatusCode.BadRequest)
}
@Test
fun `verify SDK returns error when device is removed`() {
`when`(
mockCustomerIO.api.deleteDeviceToken()
).thenReturn(getEmptyAction())
val response = customerIO.deleteDeviceToken().execute()
verifySuccess(response, Unit)
}
@Test
fun `verify SDK returns error when removing device request fails`() {
`when`(
mockCustomerIO.api.deleteDeviceToken()
)
.thenReturn(getErrorAction(errorResult = ErrorResult(error = ErrorDetail(statusCode = StatusCode.BadRequest))))
val response = customerIO.deleteDeviceToken().execute()
verifyError(response, StatusCode.BadRequest)
}
@Test
fun `verify SDK returns success when push event metric is tracked`() {
`when`(
mockCustomerIO.api.trackMetric(any(), any(), any())
).thenReturn(getEmptyAction())
val response =
customerIO.trackMetric("delivery_id", MetricEvent.delivered, "token").execute()
verifySuccess(response, Unit)
}
@Test
fun `verify SDK returns error when push event metric request fails`() {
`when`(
mockCustomerIO.api.trackMetric(any(), any(), any())
)
.thenReturn(getErrorAction(errorResult = ErrorResult(error = ErrorDetail(statusCode = StatusCode.BadRequest))))
val response =
customerIO.trackMetric("delivery_id", MetricEvent.delivered, "token").execute()
verifyError(response, StatusCode.BadRequest)
}
}