// ĠCopyright Ġ 2 0 2 0 ĠThe ĠCross - Media ĠMeasurement ĠAuthors Ċ // Ċ // Ġ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 Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . writ ers Ċ Ċ import Ġcom . google . cloud . Timestamp Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġcom . google . common . truth . extensions . proto . Proto Truth . assertThat Ċ import Ġjava . time . Instant Ċ import Ġkotlinx . coroutines . run Blocking Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . junit . runners . JUnit 4 Ċ import Ġorg . wf an et . measurement . common . identity . External Id Ċ import Ġorg . wf an et . measurement . common . identity . Internal Id Ċ import Ġorg . wf an et . measurement . common . identity . testing . Fixed Id Generator Ċ import Ġorg . wf an et . measurement . common . to Instant Ċ import Ġorg . wf an et . measurement . common . toJson Ċ import Ġorg . wf an et . measurement . common . to Proto Time Ċ import Ġorg . wf an et . measurement . internal . king dom . Re quisition Ċ import Ġorg . wf an et . measurement . internal . king dom . Re quisition . Re quisition State Ċ import Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . testing . King dom Database TestBase Ċ import Ġorg . wf an et . measurement . king dom . deploy . g cloud . spanner . testing . build Re quisition Details Ċ Ċ private Ġconst Ġval ĠDATA _ PROVIDER _ ID Ġ= Ġ 1 L Ċ private Ġconst Ġval ĠEXTER NAL _ DATA _ PROVIDER _ ID Ġ= Ġ 2 L Ċ private Ġconst Ġval ĠC AMP A IGN _ ID Ġ= Ġ 3 L Ċ private Ġconst Ġval ĠEXTER NAL _ CA MP A IGN _ ID Ġ= Ġ 4 L Ċ Ċ private Ġconst Ġval ĠREQ UIS ITION _ ID Ġ= Ġ 5 L Ċ private Ġconst Ġval ĠEXTER NAL _ REQ UIS ITION _ ID Ġ= Ġ 6 L Ċ private Ġconst Ġval ĠNEW _ REQ UIS ITION _ ID Ġ= Ġ 7 L Ċ private Ġconst Ġval ĠNEW _ EXTERNAL _ REQ UIS ITION _ ID Ġ= Ġ 8 L Ċ Ċ private Ġconst Ġval ĠAD VERT IS ER _ ID Ġ= Ġ 9 L Ċ Ċ private Ġval ĠWINDO W _ START _ TIME : ĠInstant Ġ= ĠInstant . of Epoch Second ( 1 2 3 ) Ċ private Ġval ĠWINDO W _ END _ TIME : ĠInstant Ġ= ĠInstant . of Epoch Second ( 4 5 6 ) Ċ Ċ private Ġval ĠREQ UIS ITION _ DETAILS Ġ= Ġbuild Re quisition Details ( 1 0 1 0 1 L ) Ċ private Ġval ĠNEW _ REQ UIS ITION _ DETAILS Ġ= Ġbuild Re quisition Details ( 2 0 2 0 2 L ) Ċ Ċ private Ġconst Ġval ĠCOMB IN ED _ PUBLIC _ KEY _ RESOURCE _ ID Ġ= Ġ" combined - public - key - 1 " Ċ private Ġconst Ġval ĠNEW _ COMB IN ED _ PUBLIC _ KEY _ RESOURCE _ ID Ġ= Ġ" combined - public - key - 2 " Ċ Ċ private Ġval ĠREQ UIS ITION : ĠRe quisition Ġ= ĊĠ ĠRe quisition . newBuilder () ĊĠĠĠ Ġ. apply Ġ{ ĊĠĠĠĠĠ Ġexternal DataProvider Id Ġ= ĠEXTER NAL _ DATA _ PROVIDER _ ID ĊĠĠĠĠĠ Ġexternal Campaign Id Ġ= ĠEXTER NAL _ CA MP A IGN _ ID ĊĠĠĠĠĠ Ġexternal Re quisition Id Ġ= ĠEXTER NAL _ REQ UIS ITION _ ID ĊĠĠĠĠĠ Ġcombined PublicKey ResourceId Ġ= ĠCOMB IN ED _ PUBLIC _ KEY _ RESOURCE _ ID ĊĠĠĠĠĠ Ġwindow StartTime Ġ= ĠWINDO W _ START _ TIME . to Proto Time () ĊĠĠĠĠĠ Ġwindow EndTime Ġ= ĠWINDO W _ END _ TIME . to Proto Time () ĊĠĠĠĠĠ Ġstate Ġ= ĠRe quisition State . UN FUL FIL LED ĊĠĠĠĠĠ Ġre quisition Details Ġ= ĠREQ UIS ITION _ DETAILS ĊĠĠĠĠĠ Ġre quisition Details Json Ġ= ĠREQ UIS ITION _ DETAILS . toJson () ĊĠĠĠ Ġ} ĊĠĠĠ Ġ. build () Ċ Ċ private Ġval ĠINPUT _ REQ UIS ITION : ĠRe quisition Ġ= ĊĠ ĠREQ UIS ITION . to Builder (). clear External Re quisition Id (). build () Ċ Ċ private Ġval ĠNEW _ TIMESTAMP : ĠTimestamp Ġ= ĠTimestamp . of Time Seconds And Nan os ( 9 9 9 , Ġ 0 ) Ċ Ċ @ RunWith ( JUnit 4 :: class ) Ċ class ĠCreate Re quisition Test Ġ: ĠKing dom Database TestBase () Ġ{ ĊĠ Ġprivate Ġval Ġid Generator Ġ= ĊĠĠĠ ĠFixed Id Generator ( Internal Id ( NEW _ REQ UIS ITION _ ID ), ĠExternal Id ( NEW _ EXTERNAL _ REQ UIS ITION _ ID )) ĊĊĠ Ġprivate Ġfun Ġcreate Re quisition ( requis ition : ĠRe quisition ): ĠRe quisition Ġ= Ġrun Blocking Ġ{ ĊĠĠĠ ĠCreate Re quisition ( requis ition ). execute ( database Client , Ġid Generator ) ĊĠ Ġ} ĊĊĠ Ġ@ Before ĊĠ Ġfun Ġpopulate Database () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠ Ġinsert DataProvider ( DATA _ PROVIDER _ ID , ĠEXTER NAL _ DATA _ PROVIDER _ ID ) ĊĠĠĠ Ġinsert Campaign ( DATA _ PROVIDER _ ID , ĠC AMP A IGN _ ID , ĠEXTER NAL _ CA MP A IGN _ ID , ĠAD VERT IS ER _ ID ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġsuspend Ġfun Ġinsert The Re quisition () Ġ{ ĊĠĠĠ Ġinsert Re quisition ( ĊĠĠĠĠĠ Ġdata Provider Id Ġ= ĠDATA _ PROVIDER _ ID , ĊĠĠĠĠĠ Ġcampaign Id Ġ= ĠC AMP A IGN _ ID , ĊĠĠĠĠĠ Ġre quisition Id Ġ= ĠREQ UIS ITION _ ID , ĊĠĠĠĠĠ Ġexternal Re quisition Id Ġ= ĠEXTER NAL _ REQ UIS ITION _ ID , ĊĠĠĠĠĠ Ġcombined PublicKey ResourceId Ġ= ĠCOMB IN ED _ PUBLIC _ KEY _ RESOURCE _ ID , ĊĠĠĠĠĠ Ġwindow StartTime Ġ= ĠWINDO W _ START _ TIME , ĊĠĠĠĠĠ Ġwindow EndTime Ġ= ĠWINDO W _ END _ TIME , ĊĠĠĠĠĠ Ġstate Ġ= ĠRe quisition State . UN FUL FIL LED , ĊĠĠĠĠĠ Ġre quisition Details Ġ= ĠREQ UIS ITION _ DETAILS ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` no Ġrequis itions Ġexist Ġyet `() Ġ{ ĊĠĠĠ Ġval Ġtimestamp Before Ġ= Ġcurrent Sp anner Timestamp ĊĠĠĠ Ġval Ġre quisition Ġ= Ġcreate Re quisition ( INPUT _ REQ UIS ITION ) ĊĠĠĠ Ġval Ġtimestamp After Ġ= Ġcurrent Sp anner Timestamp ĊĊĠĠĠ Ġval Ġexpected Re quisition Ġ= ĊĠĠĠĠĠ ĠREQ UIS ITION ĊĠĠĠĠĠĠĠ Ġ. to Builder () ĊĠĠĠĠĠĠĠ Ġ. set External Re quisition Id ( NEW _ EXTERNAL _ REQ UIS ITION _ ID ) ĊĠĠĠĠĠĠĠ Ġ. set CreateTime ( requis ition . create Time ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ ĠassertThat ( requis ition ). comp aring Expected Fields Only (). isEqualTo ( expected Re quisition ) ĊĊĠĠĠ Ġval ĠcreateTime Ġ= Ġre quisition . create Time . to Instant () ĊĠĠĠ ĠassertThat ( create Time ). is GreaterThan ( timestamp Before ) ĊĠĠĠ ĠassertThat ( create Time ). is LessThan ( timestamp After ) ĊĊĠĠĠ ĠassertThat ( read All Re quis itions In Sp anner ()) ĊĠĠĠĠĠ Ġ. comp aring Expected Fields Only () ĊĠĠĠĠĠ Ġ. contains Exactly ( expected Re quisition ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` requis ition Ġalready Ġexists `() Ġ= ĊĠĠĠ Ġrun Blocking < Unit > Ġ{ ĊĠĠĠĠĠ Ġinsert The Re quisition () ĊĠĠĠĠĠ Ġval Ġre quisition Ġ= Ġcreate Re quisition ( INPUT _ REQ UIS ITION ) ĊĊĠĠĠĠĠ ĠassertThat ( requis ition ). comp aring Expected Fields Only (). isEqualTo ( REQ UIS ITION ) ĊĠĠĠĠĠ ĠassertThat ( read All Re quis itions In Sp anner ()) ĊĠĠĠĠĠĠĠ Ġ. comp aring Expected Fields Only () ĊĠĠĠĠĠĠĠ Ġ. contains Exactly ( REQ UIS ITION ) ĊĠĠĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` start Ġtime Ġused Ġin Ġide mpot ency `() Ġ= ĊĠĠĠ Ġrun Blocking < Unit > Ġ{ ĊĠĠĠĠĠ Ġinsert The Re quisition () ĊĊĠĠĠĠĠ Ġval Ġnew Re quisition Ġ= ĊĠĠĠĠĠĠĠ ĠINPUT _ REQ UIS ITION ĊĠĠĠĠĠĠĠĠĠ Ġ. to Builder () ĊĠĠĠĠĠĠĠĠĠ Ġ. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexternal Re quisition Id Ġ= ĠNEW _ EXTERNAL _ REQ UIS ITION _ ID ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwindow StartTime Ġ= ĠNEW _ TIMESTAMP . to Proto () ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠ Ġval Ġnew Re quisition Without Id Ġ= Ġnew Re quisition . to Builder (). clear External Re quisition Id (). build () ĊĊĠĠĠĠĠ Ġval Ġre quisition Ġ= Ġcreate Re quisition ( new Re quisition Without Id ) ĊĊĠĠĠĠĠ ĠassertThat ( requis ition ). comp aring Expected Fields Only (). isEqualTo ( new Re quisition ) ĊĊĠĠĠĠĠ ĠassertThat ( read All Re quis itions In Sp anner ()) ĊĠĠĠĠĠĠĠ Ġ. comp aring Expected Fields Only () ĊĠĠĠĠĠĠĠ Ġ. contains Exactly ( REQ UIS ITION , Ġnew Re quisition ) ĊĠĠĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` end Ġtime Ġused Ġin Ġide mpot ency `() Ġ= ĊĠĠĠ Ġrun Blocking < Unit > Ġ{ ĊĠĠĠĠĠ Ġinsert The Re quisition () ĊĊĠĠĠĠĠ Ġval Ġnew Re quisition Ġ= ĊĠĠĠĠĠĠĠ ĠINPUT _ REQ UIS ITION ĊĠĠĠĠĠĠĠĠĠ Ġ. to Builder () ĊĠĠĠĠĠĠĠĠĠ Ġ. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexternal Re quisition Id Ġ= ĠNEW _ EXTERNAL _ REQ UIS ITION _ ID ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwindow EndTime Ġ= ĠNEW _ TIMESTAMP . to Proto () ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠ Ġval Ġnew Re quisition Without Id Ġ= Ġnew Re quisition . to Builder (). clear External Re quisition Id (). build () ĊĊĠĠĠĠĠ Ġval Ġre quisition Ġ= Ġcreate Re quisition ( new Re quisition Without Id ) ĊĊĠĠĠĠĠ ĠassertThat ( requis ition ). comp aring Expected Fields Only (). isEqualTo ( new Re quisition ) ĊĊĠĠĠĠĠ ĠassertThat ( read All Re quis itions In Sp anner ()) ĊĠĠĠĠĠĠĠ Ġ. comp aring Expected Fields Only () ĊĠĠĠĠĠĠĠ Ġ. contains Exactly ( REQ UIS ITION , Ġnew Re quisition ) ĊĠĠĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` details Ġused Ġin Ġide mpot ency `() Ġ= ĊĠĠĠ Ġrun Blocking < Unit > Ġ{ ĊĠĠĠĠĠ Ġinsert The Re quisition () ĊĊĠĠĠĠĠ Ġval Ġnew Re quisition Ġ= ĊĠĠĠĠĠĠĠ ĠINPUT _ REQ UIS ITION ĊĠĠĠĠĠĠĠĠĠ Ġ. to Builder () ĊĠĠĠĠĠĠĠĠĠ Ġ. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexternal Re quisition Id Ġ= ĠNEW _ EXTERNAL _ REQ UIS ITION _ ID ĊĠĠĠĠĠĠĠĠĠĠĠ Ġre quisition Details Ġ= ĠNEW _ REQ UIS ITION _ DETAILS ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠ Ġval Ġnew Re quisition Without Id Ġ= Ġnew Re quisition . to Builder (). clear External Re quisition Id (). build () ĊĊĠĠĠĠĠ Ġval Ġre quisition Ġ= Ġcreate Re quisition ( new Re quisition Without Id ) ĊĊĠĠĠĠĠ ĠassertThat ( requis ition ). comp aring Expected Fields Only (). isEqualTo ( new Re quisition ) ĊĊĠĠĠĠĠ ĠassertThat ( read All Re quis itions In Sp anner ()) ĊĠĠĠĠĠĠĠ Ġ. comp aring Expected Fields Only () ĊĠĠĠĠĠĠĠ Ġ. contains Exactly ( REQ UIS ITION , Ġnew Re quisition ) ĊĠĠĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` combined Ġpublic Ġkey ĠID Ġused Ġin Ġide mpot ency `() Ġ= ĊĠĠĠ Ġrun Blocking < Unit > Ġ{ ĊĠĠĠĠĠ Ġinsert The Re quisition () ĊĊĠĠĠĠĠ Ġval Ġnew Re quisition Ġ= ĊĠĠĠĠĠĠĠ ĠINPUT _ REQ UIS ITION ĊĠĠĠĠĠĠĠĠĠ Ġ. to Builder () ĊĠĠĠĠĠĠĠĠĠ Ġ. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexternal Re quisition Id Ġ= ĠNEW _ EXTERNAL _ REQ UIS ITION _ ID ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcombined PublicKey ResourceId Ġ= ĠNEW _ COMB IN ED _ PUBLIC _ KEY _ RESOURCE _ ID ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠ Ġval Ġnew Re quisition Without Id Ġ= Ġnew Re quisition . to Builder (). clear External Re quisition Id (). build () ĊĊĠĠĠĠĠ Ġval Ġre quisition Ġ= Ġcreate Re quisition ( new Re quisition Without Id ) ĊĊĠĠĠĠĠ ĠassertThat ( requis ition ). comp aring Expected Fields Only (). isEqualTo ( new Re quisition ) ĊĊĠĠĠĠĠ ĠassertThat ( read All Re quis itions In Sp anner ()) ĊĠĠĠĠĠĠĠ Ġ. comp aring Expected Fields Only () ĊĠĠĠĠĠĠĠ Ġ. contains Exactly ( REQ UIS ITION , Ġnew Re quisition ) ĊĠĠĠ Ġ} Ċ } Ċ