package Ġorg . be ck n . one . sandbox . b ap . message . services Ċ Ċ import Ġcom . mongodb . Mongo Exception Ċ import Ġio . kot est . assertions . arrow . either . shouldBe Left Ċ import Ġio . kot est . assertions . arrow . either . shouldBe Right Ċ import Ġio . kot est . core . spec . style . Describe Spec Ċ import Ġio . kot est . matchers . ints . shouldBe Exactly Ċ import Ġorg . be ck n . one . sandbox . b ap . errors . database . Database Error Ċ import Ġorg . be ck n . one . sandbox . b ap . message . entities . On Search Dao Ċ import Ġorg . be ck n . one . sandbox . b ap . message . entities . On Search Message Dao Ċ import Ġorg . be ck n . one . sandbox . b ap . message . factories . Protocol Catalog Factory Ċ import Ġorg . be ck n . one . sandbox . b ap . message . factories . Protocol Context Factory Ċ import Ġorg . be ck n . one . sandbox . b ap . message . map pers . On Search Response Mapper Ċ import Ġorg . be ck n . one . sandbox . b ap . message . repositories . Be ck n Response Repository Ċ import Ġorg . be ck n . protocol . schemas . Protocol On Search Ċ import Ġorg . be ck n . protocol . schemas . Protocol On Search Message Ċ import Ġorg . mockito . kotlin . mock Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . test . context . Active Profiles Ċ import Ġorg . springframework . test . context . Test Property Source Ċ Ċ @ SpringBootTest Ċ @ Active Profiles ( value Ġ= Ġ[" test "]) Ċ @ Test Property Source ( locations Ġ= Ġ["/ application - test . yml "]) Ċ internal Ġclass ĠResponse Storage Service Spec Ġ@ Autowired Ġconstructor ( ĊĠĠĠ Ġval Ġsearch Response Mapper : ĠOn Search Response Mapper , ĊĠĠĠ Ġval Ġsearch Response Storage Service : ĠResponse Storage Service < Protocol On Search >, ĊĠĠĠ Ġval Ġsearch Response Repo : ĠBe ck n Response Repository < On Search Dao > Ċ ) Ġ: ĠDescribe Spec () Ġ{ ĊĊĊĠ Ġprivate Ġval Ġcontext Ġ= ĠProtocol Context Factory . fixed ĊĊĠ Ġval Ġschema Search Response Ġ= ĠProtocol On Search ( ĊĠĠĠ Ġcontext Ġ= ĠProtocol Context Factory . fixed , ĊĠĠĠ Ġmessage Ġ= ĠProtocol On Search Message ( Protocol Catalog Factory . create ( 2 )) ĊĠ Ġ) ĊĊĠ Ġinit Ġ{ ĊĠĠĠ Ġdescribe (" Search Response Store ") Ġ{ ĊĠĠĠĠĠ Ġval Ġsearch Response Ġ= ĠOn Search Dao ( ĊĠĠĠĠĠĠĠ Ġcontext Ġ= ĠProtocol Context Factory . fixed As Entity ( context ), ĊĠĠĠĠĠĠĠ Ġmessage Ġ= ĠOn Search Message Dao ( ĊĠĠĠĠĠĠĠĠĠ Ġcatalog Ġ= ĠProtocol Catalog Factory . create As Entity ( schema Search Response . message ?. catalog ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠ Ġcontext (" when Ġsave Ġis Ġcalled Ġwith Ġsearch Ġresponse ") Ġ{ ĊĠĠĠĠĠĠĠ Ġsearch Response Repo . clear () ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġsearch Response Storage Service . save ( schema Search Response ) ĊĊĠĠĠĠĠĠĠ Ġit (" should Ġsave Ġresponse Ġto Ġstore ") Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġsearch Response Repo . all (). size ĠshouldBe Exactly Ġ 1 ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġit (" should Ġrespond Ġwith Ġeither . right Ġto Ġindicate Ġsuccess ") Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġresponse . shouldBe Right () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġcontext (" when ĠfindBy Message ID Ġis Ġcalled Ġwith Ġid ") Ġ{ ĊĠĠĠĠĠĠĠ Ġsearch Response Repo . clear () ĊĠĠĠĠĠĠĠ Ġsearch Response Storage Service . save ( schema Search Response ) ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġsearch Response Storage Service . findBy MessageId ( context . message Id ) ĊĊĠĠĠĠĠĠĠ Ġit (" should Ġrespond Ġwith Ġeither . right Ġcontaining Ġthe Ġsearch Ġresults ") Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġresponse . shouldBe Right ( listOf ( schema Search Response )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġcontext (" when Ġerror Ġis Ġencountered Ġwhile Ġsaving ") Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmock Repo Ġ= Ġmock < Be ck n Response Repository < On Search Dao >> Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġon Generic Ġ{ Ġinsert One ( search Response ) Ġ}. then Throw ( Mongo Exception (" Write Ġerror ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġfailure Search Response Service Ġ= ĠResponse Storage ServiceImpl ( mock Repo , Ġsearch Response Mapper ) ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġfailure Search Response Service . save ( schema Search Response ) ĊĊĠĠĠĠĠĠĠ Ġit (" should Ġreturn Ġa Ġleft Ġwith Ġwrite Ġerror ") Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġresponse . shouldBe Left ( Database Error . On Write ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġcontext (" when Ġerror Ġis Ġencountered Ġwhile Ġfetching Ġmessage Ġby Ġid ") Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmock Repo Ġ= Ġmock < Be ck n Response Repository < On Search Dao >> Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġon Generic Ġ{ ĠfindBy MessageId ( context . message Id ) Ġ}. then Throw ( Mongo Exception (" Write Ġerror ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġfailure Search Response Service Ġ= ĠResponse Storage ServiceImpl ( mock Repo , Ġsearch Response Mapper ) ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġfailure Search Response Service . findBy MessageId ( context . message Id ) ĊĊĠĠĠĠĠĠĠ Ġit (" should Ġreturn Ġa Ġleft Ġwith Ġwrite Ġerror ") Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġresponse . shouldBe Left ( Database Error . On Read ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ }