package Ġdk . cache t . car p . st udies . domain . users Ċ Ċ import Ġdk . cache t . car p . common . application . EmailAddress Ċ import Ġdk . cache t . car p . common . application . UUID Ċ import Ġdk . cache t . car p . common . application . users . Email Account Identity Ċ import Ġdk . cache t . car p . deployments . application . Study Deployment Status Ċ import Ġdk . cache t . car p . deployments . application . users . Study Invitation Ċ import Ġdk . cache t . car p . protocols . infrastructure . test . create Empty Protocol Ċ import Ġdk . cache t . car p . protocols . infrastructure . test . create Single Master Device Protocol Ċ import Ġkotlinx . datetime . Clock Ċ import Ġkotlin . test .* ĊĊ Ċ /** Ċ Ġ* ĠTests Ġfor Ġ[ Re cruit ment ]. Ċ Ġ*/ Ċ class ĠRe cruit ment Test Ċ { ĊĠĠĠ Ġprivate Ġval Ġstudy Id Ġ= ĠUUID . randomUUID () ĊĠĠĠ Ġprivate Ġval Ġparticipant Email Ġ= ĠEmail Address ( Ġ" test @ test . com " Ġ) ĊĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcreating _ re cruit ment _ from Snapshot _ obt ained _ by _ get Snapshot _ is _ the _ same () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġprotocol Ġ= Ġcreate Empty Protocol () ĊĠĠĠĠĠĠĠ Ġval Ġinv itation Ġ= ĠStudy Invitation ( Ġ" Test ", Ġ" A Ġstudy " Ġ) ĊĠĠĠĠĠĠĠ Ġre cruit ment . lock In Study ( Ġprotocol . get Snapshot (), Ġinv itation Ġ) ĊĠĠĠĠĠĠĠ Ġre cruit ment . add Participant Group ( Ġset Of ( Ġparticipant . id Ġ) Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsnapshot Ġ= Ġre cruit ment . get Snapshot () ĊĠĠĠĠĠĠĠ Ġval Ġfrom Snapshot Ġ= ĠRe cruit ment . from Snapshot ( Ġsnapshot Ġ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġre cruit ment . study Id , Ġfrom Snapshot . study Id Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġre cruit ment . getStatus (), Ġfrom Snapshot . getStatus () Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġre cruit ment . participants , Ġfrom Snapshot . participants Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġre cruit ment . participant Groups , Ġfrom Snapshot . participant Groups Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġadd Participant _ suc ceeds () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġparticipant Events Ġ= Ġre cruit ment . consume Events (). filter Is Instance < Re cruit ment . Event . Participant Added >() ĊĠĠĠĠĠĠĠ Ġval Ġretrieved Participant Ġ= Ġre cruit ment . participants ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( ĠEmail Account Identity ( Ġparticipant Email Ġ), Ġparticipant . account Identity Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġparticipant , Ġretrieved Participant . single () Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġparticipant , Ġparticipant Events . single (). participant Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suppress ( Ġ" Replace Assert Boolean With Assert Equality " Ġ) ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġadd Participant _ tw ice _ returns _ same _ participant () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġp 1 Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġp 2 Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġparticipant Events Ġ= Ġre cruit ment . consume Events (). filter Is Instance < Re cruit ment . Event . Participant Added >() ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( Ġp 1 Ġ== Ġp 2 Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġ 1 , Ġparticipant Events . size Ġ) Ġ// ĠEvent Ġshould Ġonly Ġbe Ġpublished Ġfor Ġfirst Ġparticipant . ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġlock In Study _ suc ceeds () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ ĠassertTrue ( Ġre cruit ment . getStatus () Ġis ĠRe cruit ment Status . Await ing Study To Go Live Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġprotocol Ġ= Ġcreate Single Master Device Protocol (). get Snapshot () ĊĠĠĠĠĠĠĠ Ġval Ġinv itation Ġ= ĠStudy Invitation ( Ġ" Study ", Ġ" This Ġstudy Ġis Ġabout Ġ..." Ġ) ĊĠĠĠĠĠĠĠ Ġre cruit ment . lock In Study ( Ġprotocol , Ġinv itation Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġstatus After Ġ= Ġre cruit ment . getStatus () ĊĠĠĠĠĠĠĠ ĠassertTrue ( Ġstatus After Ġis ĠRe cruit ment Status . Ready For Deployment Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġprotocol , Ġstatus After . study Protocol Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġinv itation , Ġstatus After . inv itation Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġlock In Study _ only _ allowed _ once () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġprotocol Ġ= Ġcreate Single Master Device Protocol (). get Snapshot () ĊĠĠĠĠĠĠĠ Ġval Ġinv itation Ġ= ĠStudy Invitation ( Ġ" Some Ġstudy " Ġ) ĊĠĠĠĠĠĠĠ Ġre cruit ment . lock In Study ( Ġprotocol , Ġinv itation Ġ) ĊĊĠĠĠĠĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ Ġre cruit ment . lock In Study ( Ġprotocol , Ġinv itation Ġ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġadd Participant Group _ suc ceeds () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġprotocol Ġ= Ġcreate Empty Protocol () ĊĠĠĠĠĠĠĠ Ġre cruit ment . lock In Study ( Ġprotocol . get Snapshot (), ĠStudy Invitation ( Ġ" Some Ġstudy " Ġ) Ġ) ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( Ġre cruit ment . getStatus () Ġis ĠRe cruit ment Status . Ready For Deployment Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ids Ġ= Ġset Of ( Ġparticipant . id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġgroup Ġ= Ġre cruit ment . add Participant Group ( Ġparticipant Ids Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( ĠRe cruit ment . Event . Participant Group Added ( Ġparticipant Ids Ġ), Ġre cruit ment . consume Events (). last () Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġparticipant . id , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġre cruit ment . participant Groups [ Ġgroup . id Ġ] ?. participant Ids ?. single OrNull () ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġadd Participant Group _ fails _ when _ study _ protocol _ not _ locked _ in () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( Ġre cruit ment . getStatus () Ġis ĠRe cruit ment Status . Ready For Deployment Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ids Ġ= Ġset Of ( Ġparticipant . id Ġ) ĊĠĠĠĠĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ Ġre cruit ment . add Participant Group ( Ġparticipant Ids Ġ) Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġparticip ation Events Ġ= Ġre cruit ment . consume Events (). filter Is Instance < Re cruit ment . Event . Participant Group Added >() ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġ 0 , Ġparticip ation Events . count () Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Participant Group Status _ suc ceeds () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġparticipant Ġ= Ġre cruit ment . add Participant ( Ġparticipant Email Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġprotocol Ġ= Ġcreate Empty Protocol () ĊĠĠĠĠĠĠĠ Ġre cruit ment . lock In Study ( Ġprotocol . get Snapshot (), ĠStudy Invitation ( Ġ" Some Ġstudy " Ġ) Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġgroup Ġ= Ġre cruit ment . add Participant Group ( Ġset Of ( Ġparticipant . id Ġ) Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġstub Deployment Status Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠStudy Deployment Status . Deploy ing Devices ( ĠClock . System . now (), Ġgroup . id , Ġempty List (), Ġnull Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġgroup Status Ġ= Ġre cruit ment . get Participant Group Status ( Ġstub Deployment Status Ġ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġgroup . id , Ġgroup Status . id Ġ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Ġset Of ( Ġparticipant Ġ), Ġgroup Status . participants Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Participant Group Status _ fails _ for _ unknown _ study Deployment Id () ĊĠĠĠ Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġre cruit ment Ġ= ĠRe cruit ment ( Ġstudy Id Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġunknown Id Ġ= ĠUUID . randomUUID () ĊĠĠĠĠĠĠĠ Ġval Ġstub Deployment Status Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠStudy Deployment Status . Deploy ing Devices ( ĠClock . System . now (), Ġunknown Id , Ġempty List (), Ġnull Ġ) ĊĠĠĠĠĠĠĠ Ġassert Fails With < IllegalArgumentException > Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġre cruit ment . get Participant Group Status ( Ġstub Deployment Status Ġ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ