@@ -23,6 +23,8 @@ import dev.mokkery.answering.throws
2323import dev.mokkery.every
2424import dev.mokkery.everySuspend
2525import dev.mokkery.matcher.any
26+ import dev.mokkery.matcher.capture.Capture
27+ import dev.mokkery.matcher.capture.capture
2628import dev.mokkery.mock
2729import dev.mokkery.resetAnswers
2830import dev.mokkery.resetCalls
@@ -90,7 +92,7 @@ class RemoteConfigClientTests {
9092 mockEventsDao = mock(MockMode .autoUnit)
9193 mockClientExceptionHandler = mock(MockMode .autoUnit)
9294 mockSdkLogger = mock(MockMode .autofill)
93- everySuspend { mockSdkLogger.error(any< String > (), any<Throwable >()) } returns Unit
95+ everySuspend { mockSdkLogger.error(any(), any<Throwable >()) } returns Unit
9496 }
9597
9698 @AfterTest
@@ -222,7 +224,12 @@ class RemoteConfigClientTests {
222224 )
223225 } returns Result .success(configSignatureResponse)
224226 everySuspend { mockCrypto.verify(any(), any()) } returns false
225-
227+ val eventSlot = Capture .slot<SdkEvent .Internal .Sdk .Answer .Response <Response >>()
228+ everySuspend {
229+ mockSdkEventManager.emitEvent(
230+ capture(eventSlot)
231+ )
232+ } returns Unit
226233
227234 val appCodeBasedRemoteConfigEvent = SdkEvent .Internal .Sdk .ApplyAppCodeBasedRemoteConfig ()
228235
@@ -234,51 +241,82 @@ class RemoteConfigClientTests {
234241
235242 onlineSdkEvents.await() shouldBe listOf (appCodeBasedRemoteConfigEvent)
236243 verifySuspend(VerifyMode .exactly(0 )) { mockRemoteConfigResponseHandler.handle(any()) }
237- verifySuspend { mockEventsDao.removeEvent(appCodeBasedRemoteConfigEvent) }
244+ verifySuspend {
245+ appCodeBasedRemoteConfigEvent.ack(mockEventsDao, mockSdkLogger)
246+ }
247+ verifySuspend(VerifyMode .exactly(1 )) {
248+ mockSdkEventManager.emitEvent(any())
249+ }
250+ eventSlot.values.size shouldBe 1
251+ eventSlot.values.first().let {
252+ it.originId shouldBe appCodeBasedRemoteConfigEvent.id
253+ it.result.isFailure shouldBe true
254+ it.result.exceptionOrNull()?.message shouldBe " Remote config signature verification failed"
255+ }
238256 }
239257
240258 @Test
241- fun testConsumer_shouldNotCall_responseHandler_butAckEvent_whenConfigIsNotFound () = runTest {
242- createClient(backgroundScope).register()
243-
244- val configResponse =
245- Response (configRequest, HttpStatusCode .NotFound , Headers .Empty , CONFIG_RESULT )
246- val configSignatureResponse = Response (
247- configSignatureRequest,
248- HttpStatusCode .OK ,
249- Headers .Empty ,
250- CONFIG_SIGNATURE_RESULT
251- )
259+ fun testConsumer_shouldNotCall_responseHandler_butAckEvent_andNotReemitEvent_whenConfigIsNotFound () =
260+ runTest {
261+ createClient(backgroundScope).register()
252262
253- every { mockUrlFactory.create(ECUrlType .RemoteConfig ) } returns configUrl
254- every {
255- mockUrlFactory.create(
256- ECUrlType .RemoteConfigSignature
263+ val configResponse =
264+ Response (configRequest, HttpStatusCode .NotFound , Headers .Empty , CONFIG_RESULT )
265+ val configSignatureResponse = Response (
266+ configSignatureRequest,
267+ HttpStatusCode .OK ,
268+ Headers .Empty ,
269+ CONFIG_SIGNATURE_RESULT
257270 )
258- } returns configSignatureUrl
259- everySuspend { mockNetworkClient.send(configRequest) } returns Result .failure(
260- SdkException .FailedRequestException (
271+
272+ every { mockUrlFactory.create(ECUrlType .RemoteConfig ) } returns configUrl
273+ every {
274+ mockUrlFactory.create(
275+ ECUrlType .RemoteConfigSignature
276+ )
277+ } returns configSignatureUrl
278+ val exception = SdkException .FailedRequestException (
261279 configResponse
262280 )
263- )
264- everySuspend {
265- mockNetworkClient.send(
266- configSignatureRequest
281+ everySuspend { mockNetworkClient.send(configRequest) } returns Result .failure(
282+ exception
267283 )
268- } returns Result .success(configSignatureResponse)
284+ everySuspend {
285+ mockNetworkClient.send(
286+ configSignatureRequest
287+ )
288+ } returns Result .success(configSignatureResponse)
269289
270- val appCodeBasedRemoteConfigEvent = SdkEvent .Internal .Sdk .ApplyAppCodeBasedRemoteConfig ()
290+ val appCodeBasedRemoteConfigEvent =
291+ SdkEvent .Internal .Sdk .ApplyAppCodeBasedRemoteConfig ()
271292
272- val onlineSdkEvents = backgroundScope.async {
273- onlineEvents.take(1 ).toList()
274- }
293+ val onlineSdkEvents = backgroundScope.async {
294+ onlineEvents.take(1 ).toList()
295+ }
275296
276- onlineEvents.emit(appCodeBasedRemoteConfigEvent)
297+ onlineEvents.emit(appCodeBasedRemoteConfigEvent)
277298
278- onlineSdkEvents.await() shouldBe listOf (appCodeBasedRemoteConfigEvent)
279- verifySuspend(VerifyMode .exactly(0 )) { mockRemoteConfigResponseHandler.handle(any()) }
280- verifySuspend { mockEventsDao.removeEvent(appCodeBasedRemoteConfigEvent) }
281- }
299+ onlineSdkEvents.await() shouldBe listOf (appCodeBasedRemoteConfigEvent)
300+ verifySuspend(VerifyMode .exactly(0 )) { mockRemoteConfigResponseHandler.handle(any()) }
301+ verifySuspend {
302+ mockClientExceptionHandler.handleException(
303+ exception,
304+ any(),
305+ appCodeBasedRemoteConfigEvent
306+ )
307+ }
308+ verifySuspend {
309+ mockSdkEventManager.emitEvent(
310+ SdkEvent .Internal .Sdk .Answer .Response (
311+ appCodeBasedRemoteConfigEvent.id,
312+ Result .success(Unit )
313+ )
314+ )
315+ }
316+ verifySuspend(VerifyMode .exactly(0 )) {
317+ mockSdkEventManager.emitEvent(appCodeBasedRemoteConfigEvent)
318+ }
319+ }
282320
283321 @Test
284322 fun testConsumer_shouldNotCall_responseHandler_butAckEvent_whenSignatureIsNotFound () = runTest {
@@ -300,11 +338,12 @@ class RemoteConfigClientTests {
300338 )
301339 } returns configSignatureUrl
302340 everySuspend { mockNetworkClient.send(configRequest) } returns Result .success(configResponse)
341+ val exception = SdkException .FailedRequestException (configSignatureResponse)
303342 everySuspend {
304343 mockNetworkClient.send(
305344 configSignatureRequest
306345 )
307- } returns Result .failure(SdkException . FailedRequestException (configSignatureResponse) )
346+ } returns Result .failure(exception )
308347
309348 val appCodeBasedRemoteConfigEvent = SdkEvent .Internal .Sdk .ApplyAppCodeBasedRemoteConfig ()
310349
@@ -316,7 +355,13 @@ class RemoteConfigClientTests {
316355
317356 onlineSdkEvents.await() shouldBe listOf (appCodeBasedRemoteConfigEvent)
318357 verifySuspend(VerifyMode .exactly(0 )) { mockRemoteConfigResponseHandler.handle(any()) }
319- verifySuspend { mockEventsDao.removeEvent(appCodeBasedRemoteConfigEvent) }
358+ verifySuspend {
359+ mockClientExceptionHandler.handleException(
360+ exception,
361+ any(),
362+ appCodeBasedRemoteConfigEvent
363+ )
364+ }
320365 }
321366
322367 @Test
@@ -331,7 +376,9 @@ class RemoteConfigClientTests {
331376 every {
332377 mockUrlFactory.create(ECUrlType .RemoteConfig )
333378 } returns configUrl
334- everySuspend { mockNetworkClient.send(configRequest) } returns Result .success(configResponse)
379+ everySuspend { mockNetworkClient.send(configRequest) } returns Result .success(
380+ configResponse
381+ )
335382 every {
336383 mockUrlFactory.create(ECUrlType .RemoteConfigSignature )
337384 } returns configSignatureUrl
0 commit comments