// ĠCopyright Ġ 2 0 2 1 ĠGoogle ĠLLC Ċ // Ċ // ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ // Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ // ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ // Ċ // ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ // Ċ // ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ // Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ // ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ // ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ // Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ċ package Ġcom . google . android . libraries . car . trust agent Ċ Ċ import Ġandroidx . test . ext . junit . runners . Android JUnit 4 Ċ import Ġcom . google . android . comp anion protos . Operation Proto . Operation Type Ċ import Ġcom . google . android . libraries . car . trust agent . ble message stream . Bluetooth Gatt Manager Ċ import Ġcom . google . android . libraries . car . trust agent . ble message stream . Message Stream Ċ import Ġcom . google . android . libraries . car . trust agent . ble message stream . Stream Message Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ 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 . in Order Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . never Ċ 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 Ġjava . util . UUID Ċ import Ġjava . util . concurrent . Semaphore Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġjavax . crypto . SecretKey Ċ import Ġkotlin . test . assert Fails With Ċ import Ġkotlinx . coroutines . test . Un conf ined Test Dispatcher Ċ import Ġorg . junit . Assert . fail Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ @ Opt In ( kotlin x . coroutines . Experimental Cor outines Api :: class ) Ċ class ĠCar Test Ġ{ ĊĠ Ġprivate Ġval Ġcar Id Ġ= ĠUUID . fromString (" 3 e 6 a 5 c 9 4 - e 1 d 4 - 4 d 1 0 - 9 9 e 1 - 7 5 e 8 af 4 ee 4 6 3 ") ĊĠ Ġprivate Ġval Ġcar Name Ġ= Ġ" test Car Name " ĊĠ Ġprivate Ġval Ġtest Dispatcher Ġ= ĠUn conf ined Test Dispatcher () ĊĊĠ Ġprivate Ġlateinit Ġvar Ġcar : ĠCar ĊĠ Ġprivate Ġlateinit Ġvar Ġbluetooth Gatt Manager : ĠBluetooth Gatt Manager ĊĠ Ġprivate Ġlateinit Ġvar Ġble Message Stream : ĠMessage Stream ĊĠ Ġprivate Ġlateinit Ġvar Ġidentification Key : ĠSecret Key ĊĊĠ Ġ@ Before ĊĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġbluetooth Gatt Manager Ġ= Ġmock () ĊĠĠĠ Ġble Message Stream Ġ= Ġmock () ĊĠĠĠ Ġidentification Key Ġ= Ġmock () ĊĊĠĠĠ Ġcar Ġ= ĊĠĠĠĠĠ ĠCar ( bluetooth Gatt Manager , Ġble Message Stream , Ġidentification Key , Ġcar Id , Ġcar Name , Ġtest Dispatcher ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Set Callback _ for Same Recipient _ throws Error () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġcar . set Callback ( mock (), Ġrecipient ) ĊĊĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ Ġcar . set Callback ( mock (), Ġrecipient ) Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Set Callback _ same Recipient _ same Callback _ does Not Throw Error () Ġ{ ĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Set Callback _ for Different Recipient _ does Not Throw Error () Ġ{ ĊĠĠĠ Ġval Ġrecipient 1 Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġrecipient 2 Ġ= ĠUUID . fromString (" 1 2 c 9 3 2 4 1 - c 2 0 0 - 4 9 d 7 - ad 2 6 - ad 4 0 4 2 3 6 9 eb 3 ") ĊĊĠĠĠ Ġcar . set Callback ( mock (), Ġrecipient 1 ) ĊĠĠĠ Ġcar . set Callback ( mock (), Ġrecipient 2 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Clear Callback () Ġ{ ĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĠĠĠ Ġcar . clear Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġ// ĠThis Ġset Ġshould Ġnot Ġthrow Ġan Ġerror Ġsince Ġthe Ġcallback Ġwas Ġcleared Ġnow . ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Clear Callback _ for Different Callback _ does Not Clear () Ġ{ ĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġval Ġother Callback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġcar . clear Callback ( other Callback , Ġrecipient ) ĊĊĠĠĠ Ġ// ĠThe Ġclear Ġwas Ġcalled Ġfor Ġa Ġdifferent Ġcallback , Ġso Ġit Ġwon 't Ġsucceed Ġand Ġan Ġerror Ġshould ĊĠĠĠ Ġ// Ġstill Ġbe Ġthrown . ĊĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ Ġcar . set Callback ( other Callback , Ġrecipient ) Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Message Received _ not ifies Callback () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġval Ġmessage Ġ= Ġ" message ". toByteArray () ĊĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġmessage , ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . CLIENT _ MESSAGE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĊĠĠĠ Ġverify ( callback ). on Message Received ( message ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Un claimed Messages _ deliver ed When Callback Registered () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġmessage Ġ= Ġ" message ". toByteArray () ĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġmessage , ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . CLIENT _ MESSAGE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĊĠĠĠ Ġval Ġcallback Ġ= Ġmock < Car . Callback >() ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġverify ( callback ). on Message Received ( message ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Un claimed Messages _ deliver ed In Fifo Order () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġmessage 1 Ġ= Ġ" message 1 ". toByteArray () ĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġmessage 1 , ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . CLIENT _ MESSAGE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġval Ġmessage 2 Ġ= Ġ" message 2 ". toByteArray () ĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġmessage 2 , ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . CLIENT _ MESSAGE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġcallback Ġ= Ġmock < Car . Callback >() ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġin Order ( callback ). apply Ġ{ ĊĠĠĠĠĠ Ġverify ( callback ). on Message Received ( message 1 ) ĊĠĠĠĠĠ Ġverify ( callback ). on Message Received ( message 2 ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Send Query _ writes To Message Stream () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġparameters Ġ= Ġnull ) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĠĠĠ Ġcar . query Id . set ( query Id ) ĊĊĠĠĠ Ġcar . send Query ( query , Ġrecipient , Ġon Response Ġ= Ġ{}) ĊĊĠĠĠ Ġval Ġcapt or Ġ= Ġargument Captor < Stream Message >() ĊĠĠĠ Ġverify ( ble Message Stream ). sendMessage ( capt or . capture ()) ĊĊĠĠĠ Ġval Ġexpected Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery . to Proto ByteArray ( query Id Ġ= Ġquery Id , Ġrecipient Ġ= Ġrecipient ), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġtrue , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ ĠassertThat ( capt or . first Value ). isEqualTo ( expected Message ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Send Multiple Queries _ in crements Query Id () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġparameters Ġ= Ġnull ) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĠĠĠ Ġcar . query Id . set ( query Id ) ĊĊĠĠĠ Ġcar . send Query ( query , Ġrecipient , Ġon Response Ġ= Ġ{}) ĊĠĠĠ Ġcar . send Query ( query , Ġrecipient , Ġon Response Ġ= Ġ{}) ĊĊĠĠĠ Ġval Ġcapt or Ġ= Ġargument Captor < Stream Message >() ĊĠĠĠ Ġverify ( ble Message Stream , Ġtimes ( 2 )). sendMessage ( capt or . capture ()) ĊĊĠĠĠ Ġvar Ġexpected Query Ġ= Ġquery . to Proto ByteArray ( query Id Ġ= Ġquery Id , Ġrecipient Ġ= Ġrecipient ) ĊĠĠĠ ĠassertThat ( capt or . first Value . payload ). isEqualTo ( expected Query ) ĊĊĠĠĠ Ġ// ĠSecond Ġquery Ġshould Ġhave Ġan Ġincrement ed Ġid . ĊĠĠĠ Ġexpected Query Ġ= Ġquery . to Proto ByteArray ( query Id Ġ= Ġquery Id Ġ+ Ġ 1 , Ġrecipient Ġ= Ġrecipient ) ĊĠĠĠ ĠassertThat ( capt or . second Value . payload ). isEqualTo ( expected Query ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Response Received _ not ifies Correct Handler () Ġ{ ĊĠĠĠ Ġval Ġsemaphore Ġ= ĠSem aphore ( 0 ) ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġparameters Ġ= Ġnull ) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĠĠĠ Ġcar . query Id . set ( query Id ) ĊĊĠĠĠ Ġval Ġexpected Query Response Ġ= ĊĠĠĠĠĠ ĠQuery Response ( query Id , Ġis Successful Ġ= Ġtrue , Ġ" response ". toByteArray ()) ĊĊĠĠĠ Ġcar . send Query ( query , Ġrecipient ) Ġ{ Ġquery Response Ġ-> ĊĠĠĠĠĠ Ġsemaphore . release () ĊĠĠĠĠĠ ĠassertThat ( query Response ). isEqualTo ( expected Query Response ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġexpected Query Response . to Proto ByteArray (), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY _ RESPONSE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĊĠĠĠ Ġ// ĠIf Ġthe Ġcallback Ġis Ġnot Ġcalled , Ġthen Ġthe Ġsemaphore Ġwill Ġnever Ġbe Ġacquired Ġand Ġwill Ġfail Ġthe ĊĠĠĠ Ġ// Ġtest . ĠWait Ġfor Ġ 1 0 0 ms , Ġsince Ġit Ġshould Ġbe Ġinvoked Ġimmediately . ĊĠĠĠ ĠassertThat ( semaphore . try Acquire ( 1 0 0 , ĠTimeUnit . MILLISECONDS )). isTrue () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Response Received _ wrong Operation Type _ does Not Notify Handler () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġparameters Ġ= Ġnull ) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĠĠĠ Ġcar . query Id . set ( query Id ) ĊĊĠĠĠ Ġval Ġquery Response Ġ= ĠQuery Response ( query Id , Ġis Successful Ġ= Ġtrue , Ġ" response ". toByteArray ()) ĊĊĠĠĠ Ġcar . send Query ( query , Ġrecipient ) Ġ{ Ġ_ Ġ-> Ġfail (" Handler Ġcalled ") Ġ} ĊĊĠĠĠ Ġ// ĠStream Message Ġwith Ġthe Ġwrong ĠOperation Type . ĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery Response . to Proto ByteArray (), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Response Received _ wrong Query Id _ does Notify Handler () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġparameters Ġ= Ġnull ) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĠĠĠ Ġval Ġother Query Id Ġ= Ġ 2 6 ĊĠĠĠ Ġcar . query Id . set ( query Id ) ĊĊĠĠĠ Ġval Ġquery Response Ġ= ĠQuery Response ( other Query Id , Ġis Successful Ġ= Ġtrue , Ġ" response ". toByteArray ()) ĊĊĠĠĠ Ġcar . send Query ( query , Ġrecipient ) Ġ{ Ġ_ Ġ-> Ġfail (" Handler Ġinvoked ") Ġ} ĊĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery Response . to Proto ByteArray (), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY _ RESPONSE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Rec ieved _ inv okes Callback () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġsender Ġ= ĠUUID . fromString (" 6 4 ed 6 5 2 b - b 7 0 6 - 4 6 8 4 - a 8 bc - 0 dbc 1 7 ee 4 5 2 d ") ĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġ" parameters ". toByteArray ()) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery . to Proto ByteArray ( query Id , Ġsender ), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĊĠĠĠ Ġverify ( callback ). on Query Received ( query Id , Ġsender , Ġquery ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Rec ieved _ with Wrong Operation Callback _ does Not Invoke Callback () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġ" parameters ". toByteArray ()) ĊĊĠĠĠ Ġ// ĠMessage Ġwith Ġthe Ġwrong Ġoperation Ġtype . ĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery . to Proto ByteArray ( query Id Ġ= Ġ 1 3 , Ġrecipient ), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY _ RESPONSE , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĊĠĠĠ Ġverify ( callback , Ġnever ()). on Query Received ( any (), Ġany (), Ġany ()) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Received _ deliver ed When Callback Registered () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĠĠĠ Ġval Ġsender Ġ= ĠUUID . fromString (" 6 4 ed 6 5 2 b - b 7 0 6 - 4 6 8 4 - a 8 bc - 0 dbc 1 7 ee 4 5 2 d ") ĊĠĠĠ Ġval Ġquery Ġ= ĠQuery (" request ". toByteArray (), Ġ" parameters ". toByteArray ()) ĊĠĠĠ Ġval Ġquery Id Ġ= Ġ 1 3 ĊĠĠĠ Ġval Ġstream Message Ġ= ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery . to Proto ByteArray ( query Id , Ġsender ), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( stream Message ) ĊĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġverify ( callback ). on Query Received ( query Id , Ġsender , Ġquery ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest Query Received _ deliver ed In Fifo Order () Ġ{ ĊĠĠĠ Ġval Ġrecipient Ġ= ĠUUID . fromString (" e 2 8 4 f 4 5 d - 6 6 6 f - 4 7 9 f - bd 4 8 - b 8 be 0 2 8 3 9 7 7 e ") ĊĊĠĠĠ Ġval Ġsender Ġ= ĠUUID . fromString (" 8 1 a 0 7 6 3 d - f 7 f 5 - 4 b 7 d - ab 0 1 - 0 9 a 4 e 4 5 2 5 7 c 3 ") ĊĠĠĠ Ġval Ġquery 1 Ġ= ĠQuery (" request 1 ". toByteArray (), Ġ" parameters ". toByteArray ()) ĊĠĠĠ Ġval Ġquery Id 1 Ġ= Ġ 1 3 ĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery 1 . to Proto ByteArray ( query Id 1 , Ġsender ), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġsender 2 Ġ= ĠUUID . fromString (" 2 0 a 4 af 9 7 - 5 6 4 7 - 4 1 7 6 - 9 e 4 f - dfc 0 dc 5 b 9 3 a 9 ") ĊĠĠĠ Ġval Ġquery 2 Ġ= ĠQuery (" request 2 ". toByteArray (), Ġ" parameters ". toByteArray ()) ĊĠĠĠ Ġval Ġquery Id 2 Ġ= Ġ 1 4 ĊĠĠĠ Ġcar . stream Forwarding Message Callback . on Message Received ( ĊĠĠĠĠĠ ĠStream Message ( ĊĠĠĠĠĠĠĠ Ġpayload Ġ= Ġquery 2 . to Proto ByteArray ( query Id 2 , Ġsender 2 ), ĊĠĠĠĠĠĠĠ Ġoperation Ġ= ĠOperation Type . QUERY , ĊĠĠĠĠĠĠĠ Ġis Payload Encrypted Ġ= Ġfalse , ĊĠĠĠĠĠĠĠ Ġoriginal Message Size Ġ= Ġ 0 , ĊĠĠĠĠĠĠĠ Ġrecipient Ġ= Ġrecipient ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġcallback : ĠCar . Callback Ġ= Ġmock () ĊĠĠĠ Ġcar . set Callback ( callback , Ġrecipient ) ĊĊĠĠĠ Ġin Order ( callback ). apply Ġ{ ĊĠĠĠĠĠ Ġverify ( callback ). on Query Received ( query Id 1 , Ġsender , Ġquery 1 ) ĊĠĠĠĠĠ Ġverify ( callback ). on Query Received ( query Id 2 , Ġsender 2 , Ġquery 2 ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ