// ĠCopyright Ġ 2 0 1 7 - 2 0 2 0 ĠFit bit , ĠInc Ċ // ĠSPDX - License - Identifier : ĠApache - 2 . 0 Ċ Ċ package Ġcom . fit bit . gold eng ate . bindings . co ap . block Ċ Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . Co ap Endpoint Exception Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . Co ap Endpoint Response Exception Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Extended Error Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Extended Error Decoder Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Incoming Response Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Options Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Outgoing Request Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Raw Response Message Ċ import Ġcom . fit bit . gold eng ate . bindings . co ap . data . Response Code Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . argument Captor Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . do Return Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . times Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify No More Interactions Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġio . reactivex . Observer Ċ import Ġio . reactivex . Single Emitter Ċ import Ġorg . junit . Assert . assertEquals Ċ import Ġorg . junit . Test Ċ import Ġjava . util . Arrays Ċ import Ġkotlin . test . assertTrue Ċ Ċ class ĠBlock wise Co ap Response Listener Test Ġ{ ĊĠĠĠ Ġprivate Ġval Ġmock Progress Observer Ġ= Ġmock < Observer < Int >>() ĊĠĠĠ Ġprivate Ġval Ġmock Request Ġ= Ġmock < Outgoing Request > Ġ{ ĊĠĠĠĠĠĠĠ Ġon Ġ{ Ġprogress Observer Ġ} Ġdo Return Ġmock Progress Observer ĊĠĠĠĠĠĠĠ Ġon Ġ{ Ġexpect Success Ġ} Ġdo Return Ġ Ġtrue ĊĠĠĠ Ġ} ĊĠĠĠ Ġprivate Ġval Ġmock Single Emitter Ġ= Ġmock < Single Emitter < Incoming Response >>() ĊĠĠĠ Ġprivate Ġval Ġmock Decoder Ġ= Ġmock < Extended Error Decoder >() ĊĊĠĠĠ Ġprivate Ġval Ġlistener Ġ= ĠBlock wise Co ap Response Listener ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmock Request , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmock Single Emitter , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmock Decoder ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġprivate Ġval Ġtest Code Ġ= ĠResponse Code . created ĊĠĠĠ Ġprivate Ġval ĠtestData Ġ= Ġ" Hello , ". toByteArray () ĊĠĠĠ Ġprivate Ġval Ġtest Options Ġ= ĠOptions () ĊĠĠĠ Ġprivate Ġval Ġtest Message Ġ= ĠRaw Response Message ( test Code , Ġtest Options , ĠtestData ) ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Not Signal For Ack Callback () Ġ{ ĊĠĠĠĠĠĠĠ Ġlistener . on Ack () ĊĊĠĠĠĠĠĠĠ Ġverify No More Interactions ( mock Single Emitter ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Signal Error For Error Callback () Ġ{ ĊĠĠĠĠĠĠĠ Ġlistener . onError ( 1 , Ġ" error Ġmessage ") ĊĊĠĠĠĠĠĠĠ Ġverify ( mock Single Emitter ). onError ( any < Co ap Endpoint Exception >()) ĊĠĠĠĠĠĠĠ Ġverify ( mock Progress Observer ). onError ( any < Co ap Endpoint Exception >()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Only Signal Response Listener And Not Body On First On Next Callback () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message ) ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġverify Success Signal On Response Emitter () ĊĠĠĠĠĠĠĠ ĠassertEquals ( test Code , Ġresponse . response Code ) ĊĊĠĠĠĠĠĠĠ Ġverify ( mock Single Emitter ). on Success ( any ()) ĊĠĠĠĠĠĠĠ Ġresponse . body . as Data (). test () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. assertNot Complete () ĊĠĠĠĠĠĠĠ Ġverify ( mock Progress Observer , times ( 0 )). onComplete () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Signal Response Listener And Body On Next And Complete Callback () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message ) ĊĠĠĠĠĠĠĠ Ġlistener . onComplete () ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġverify Success Signal On Response Emitter () ĊĠĠĠĠĠĠĠ ĠassertEquals ( test Code , Ġresponse . response Code ) ĊĊĠĠĠĠĠĠĠ Ġverify ( mock Single Emitter ). on Success ( any ()) ĊĠĠĠĠĠĠĠ Ġresponse . body . as Data (). test () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Value Ġ{ ĠArrays . equals ( testData , Ġit ) Ġ} ĊĠĠĠĠĠĠĠ Ġverify ( mock Progress Observer ). onComplete () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Not Signal Response Listener On Only On Complete Callback () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġlistener . onComplete () ĊĊĠĠĠĠĠĠĠ Ġverify No More Interactions ( mock Single Emitter ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Signal Body With Aggregate Block Data From On Next Callback () Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠtestData 2 Ġ= Ġ" ĠWorld ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġtest Message 2 Ġ= ĠRaw Response Message ( test Code , Ġtest Options , ĠtestData 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġexpected Body Ġ= Ġ" Hello , ĠWorld ". toByteArray () ĊĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message ) ĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message 2 ) ĊĠĠĠĠĠĠĠ Ġlistener . onComplete () ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġverify Success Signal On Response Emitter () ĊĊĠĠĠĠĠĠĠ Ġresponse . body . as Data (). test () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Value Ġ{ ĠArrays . equals ( expected Body , Ġit ) Ġ} ĊĠĠĠĠĠĠĠ Ġverify ( mock Progress Observer ). onComplete () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Send Partial Cached Data If Failure Before Re ceiving All Blocks () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġthis Ġis Ġthe Ġpartial Ġdata Ġreceived Ġby t Ġthe Ġsuccessful Ġ[ onNext ] Ġcalls ĊĠĠĠĠĠĠĠ Ġval Ġexpected Partial Data Ġ= ĠtestData ĊĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message ) ĊĠĠĠĠĠĠĠ Ġlistener . onError ( 1 , Ġ" error Ġmessage ") ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġverify Success Signal On Response Emitter () ĊĊĠĠĠĠĠĠĠ Ġresponse . body . as Data (). test () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Error Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġit Ġis ĠCo ap Endpoint Exception Ġ&& ĠArrays . equals ( expected Partial Data , Ġit . partial Data ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Send Error If Failure Re ceiving First Blocks () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġexpected Empty Partial Data Ġ= ĠByteArray ( 0 ) ĊĊĠĠĠĠĠĠĠ Ġ// Ġfirst Ġcallback Ġis Ġerror Ġwithout Ġany Ġdata Ġreceived ĊĠĠĠĠĠĠĠ Ġlistener . onError ( 1 , Ġ" error Ġmessage ") ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġverify Failure Signal On Response Emitter () ĊĊĠĠĠĠĠĠĠ Ġ// Ġexception Ġhas Ġempty Ġdata Ġas Ġfirst Ġblock Ġfailed ĊĠĠĠĠĠĠĠ ĠassertTrue ( Arrays . equals ( expected Empty Partial Data , Ġresponse . partial Data )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Signal Body WithError If Received ErrorMessage () Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠtestData 2 Ġ= Ġ" ĠWorld ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġtest Message 2 Ġ= ĠRaw Response Message ( Response Code . internal ServerError , Ġtest Options , ĠtestData 2 ) ĊĊĠĠĠĠĠĠĠ Ġval Ġmock Extended Error Ġ= Ġmock < Extended Error >() ĊĠĠĠĠĠĠĠ Ġwhenever ( Ġmock Decoder . decode ( test Message 2 )). do Return ( mock Extended Error ) ĊĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message ) ĊĠĠĠĠĠĠĠ Ġlistener . onNext ( test Message 2 ) ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġverify Success Signal On Response Emitter () ĊĊĠĠĠĠĠĠĠ Ġresponse . body . as Data (). test () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. assert Error Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit Ġis ĠCo ap Endpoint Response Exception Ġ&& Ġit . response Code Ġ== ĠResponse Code . internal ServerError Ġ&& Ġit . extended Error Ġ== Ġmock Extended Error ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġverify ( mock Progress Observer ). onError ( any ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġverify Success Signal On Response Emitter (): ĠIncoming Response Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresponse Captor Ġ= Ġargument Captor < Incoming Response >() ĊĠĠĠĠĠĠĠ Ġverify ( mock Single Emitter ). on Success ( response Captor . capture ()) ĊĠĠĠĠĠĠĠ Ġreturn Ġresponse Captor . first Value ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġverify Failure Signal On Response Emitter (): ĠCo ap Endpoint Exception Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresponse Captor Ġ= Ġargument Captor < Co ap Endpoint Exception >() ĊĠĠĠĠĠĠĠ Ġverify ( mock Single Emitter ). onError ( response Captor . capture ()) ĊĠĠĠĠĠĠĠ Ġreturn Ġresponse Captor . first Value ĊĠĠĠ Ġ} Ċ } Ċ