package Ġcom . vis able . chat . repositories Ċ Ċ import Ġcom . vis able . chat . entities . Message Ċ import Ġcom . vis able . chat . util . Docker Compose Ċ import Ġcom . vis able . chat . util . Singleton DB And Queue Ċ import Ġorg . junit . Class Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . data . domain . Sort Ċ import Ġorg . springframework . test . context . junit 4 . Spring Runner Ċ import Ġorg . assertj . core . api . Assertions For Class Types . assertThat Ċ import Ġorg . junit . After Ċ Ċ @ SpringBootTest Ċ @ RunWith ( Spring Runner :: class ) Ċ class ĠMessage Repository Integration Tests : ĠSingleton DB And Queue () Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġprivate Ġval ĠMessage 1 Ġ= ĠMessage ( 1 , 2 ," Hello ") ĊĠĠĠĠĠĠĠ Ġprivate Ġval ĠMessage 2 Ġ= ĠMessage ( 2 , 3 ," World ") ĊĠĠĠĠĠĠĠ Ġprivate Ġval ĠMessage 3 Ġ= ĠMessage ( 3 , 2 ," M ach o ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġmessage Repository : ĠMessage Repository ĊĊĠĠĠ Ġ@ After ĊĠĠĠ Ġfun Ġclean Up DB (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . delete All () ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven Message Entity _ when Insert With Save _ Then Message Is Pers isted () Ġ{ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save ( Message 1 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( Message 1 . getId ()). isNotNull () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven Message Entity _ when Insert With Save And Flush _ Then Message Is Pers isted () Ġ{ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save And Flush ( Message 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( Message 2 . getId ()). isNotNull () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Re ci ep ient ByID _ IN _ ASC _ ORDER (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save All ( mutable ListOf < Message >( Message ( 1 , 2 ," Hello "), Message ( 3 , 2 ," M ach o "))) ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Recipient ( 2 , ĠSort . by (" createdAt "). ascending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġdif Ġ= Ġap [ 0 ]. createdAt !!. time Ġ- Ġap [ 1 ]. createdAt !!. time ĊĠĠĠĠĠĠĠ Ġassert ( dif Ġ< Ġ 0 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Re ci ep ient ByID _ IN _ DESC _ ORDER (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save All ( mutable ListOf < Message >( Message 1 , Message 3 )) ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Recipient ( 2 , ĠSort . by (" createdAt "). desc ending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġdif Ġ= Ġap [ 0 ]. createdAt !!. time Ġ- Ġap [ 1 ]. createdAt !!. time ĊĠĠĠĠĠĠĠ Ġassert ( dif Ġ> Ġ 0 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Non Ex istent Re ci ep ient ByID (){ ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Recipient ( 0 , ĠSort . by (" createdAt "). ascending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 0 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Sender ByID _ IN _ ASC _ ORDER (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save All ( mutable ListOf < Message >( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 1 , 2 ," Hello ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ, Message ( 1 , 3 ," Talk "))) ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Sender ( 1 , ĠSort . by (" createdAt "). ascending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġdif Ġ= Ġap [ 0 ]. createdAt !!. time Ġ- Ġap [ 1 ]. createdAt !!. time ĊĠĠĠĠĠĠĠ Ġassert ( dif Ġ< Ġ 0 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Sender ByID _ IN _ DESC _ ORDER (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save All ( mutable ListOf < Message >( Message 1 , Message 2 , Message ( 1 , 3 ," Talk "))) ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Sender ( 1 , ĠSort . by (" createdAt "). desc ending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ Ġval Ġdif Ġ= Ġap [ 0 ]. createdAt !!. time Ġ- Ġap [ 1 ]. createdAt !!. time ĊĠĠĠĠĠĠĠ Ġassert ( dif Ġ> Ġ 0 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Non Ex istent Sender ByID (){ ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Sender ( 0 , ĠSort . by (" createdAt "). ascending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 0 ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Sender And Recipient ByID _ IN _ ASC _ ORDER (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save All ( mutable ListOf < Message >( Message 1 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 2 , 1 ," I "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 2 , 1 ," Heart "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 2 , 1 ," Radio "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 1 , 2 ," Talk "))) ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Recipient And Sender ( 1 , Ġ 2 , Sort . by (" createdAt "). ascending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 3 ) ĊĠĠĠĠĠĠĠ Ġfor ( x Ġin Ġ 1 .. ( ap . size - 1 )){ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġvar Ġdif Ġ= Ġap [ x - 1 ]. createdAt !!. time Ġ- Ġap [ x ]. createdAt !!. time ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert ( dif Ġ< Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfind Sender And Recipient ByID _ IN _ DESC _ ORDER (){ ĊĠĠĠĠĠĠĠ Ġmessage Repository . save All ( mutable ListOf < Message >( Message 1 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 2 , 1 ," I "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 2 , 1 ," Heart "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 2 , 1 ," Radio "), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMessage ( 1 , 2 ," Talk "))) ĊĠĠĠĠĠĠĠ Ġval Ġap : ĠList < Message > Ġ= Ġ Ġmessage Repository . findBy Recipient And Sender ( 1 , Ġ 2 , Sort . by (" createdAt "). desc ending ()) ĊĠĠĠĠĠĠĠ ĠassertThat ( ap . size ). isEqualTo ( 3 ) ĊĠĠĠĠĠĠĠ Ġfor ( x Ġin Ġ 1 .. ( ap . size - 1 )){ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġvar Ġdif Ġ= Ġap [ x - 1 ]. createdAt !!. time Ġ- Ġap [ x ]. createdAt !!. time ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassert ( dif Ġ> Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ } Ċ