package Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . controller Ċ Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any OrNull Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . argument Captor Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . eq Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . assert Throws Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . security . oauth 2 . server . resource . authentication . Jwt Authentication Token Ċ import Ġorg . springframework . web . server . ResponseStatus Exception Ċ import Ġorg . springframework . web . server . Server Web Input Exception Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . controller . map pers . Jwt Auth User Mapper Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Auth User DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Cancellation Re asons DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Create Ref erral Request DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Ref erral Assignment DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . jpa . entity . Auth User Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . jpa . entity . Cancellation Reason Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . service . H MP PS Auth Service Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . service . Ref erral Service Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . service . Service Category Service Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . util . Auth User Factory Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . util . Jwt Token Factory Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . util . Ref erral Factory Ċ import Ġjava . util . UUID Ċ import Ġjavax . persistence . Entity NotFoundException Ċ Ċ internal Ġclass ĠRef erral Controller Test Ġ{ ĊĠ Ġprivate Ġval Ġref erral Service Ġ= Ġmock < Ref erral Service >() ĊĠ Ġprivate Ġval Ġservice Category Service Ġ= Ġmock < Service Category Service >() ĊĠ Ġprivate Ġval Ġh mp ps Auth Service Ġ= Ġmock < H MP PS Auth Service >() ĊĠ Ġprivate Ġval Ġjwt Auth User Mapper Ġ= Ġmock < Jwt Auth User Mapper >() ĊĠ Ġprivate Ġval Ġref erral Controller Ġ= ĠRef erral Controller ( ref erral Service , Ġservice Category Service , Ġh mp ps Auth Service , Ġjwt Auth User Mapper ) ĊĠ Ġprivate Ġval Ġtoken Factory Ġ= ĠJwt Token Factory () ĊĠ Ġprivate Ġval Ġref erral Factory Ġ= ĠRef erral Factory () ĊĠ Ġprivate Ġval Ġuser Factory Ġ= ĠAuth User Factory () ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Draft Ref erral Ġhandles ĠEntity NotFound Ġexceptions Ġfrom ĠInter ventions Service `() Ġ{ ĊĠĠĠ Ġwhenever ( ref erral Service . create Draft Ref erral ( any (), Ġany (), Ġany (), Ġany OrNull (), Ġany OrNull (), Ġany OrNull ())). then Throw ( Entity NotFoundException :: class . java ) ĊĠĠĠ ĠassertThrows < Server Web Input Exception > Ġ{ ĊĠĠĠĠĠ Ġref erral Controller . create Draft Ref erral ( Create Ref erral Request DTO (" CR N 2 0 ", ĠUUID . randomUUID ()), Ġtoken Factory . create ()) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` get Sent Ref err als Ġfilters Ġby Ġservice Ġprovider Ġorganization Ġfrom Ġquery Ġparam `() Ġ{ ĊĠĠĠ Ġref erral Controller . get Sent Ref err als ( sent To Ġ= Ġ" H AR MON Y _ LI V ING ") ĊĠĠĠ Ġverify ( ref erral Service ). get Sent Ref err als For ServiceProvider ID (" H AR MON Y _ LI V ING ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` get Sent Ref err als Ġfilters Ġby ĠPP Ġfrom Ġquery Ġparam `() Ġ{ ĊĠĠĠ Ġref erral Controller . get Sent Ref err als ( sent By Ġ= Ġ" bern ard . be aks ") ĊĠĠĠ Ġverify ( ref erral Service ). get Sent Ref err als Sent By (" bern ard . be aks ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` get Sent Ref err als Ġfilters Ġby Ġassign ee Ġfrom Ġquery Ġparam `() Ġ{ ĊĠĠĠ Ġref erral Controller . get Sent Ref err als ( assigned To Ġ= Ġ" some one @ provider . com ") ĊĠĠĠ Ġverify ( ref erral Service ). get Sent Ref err als Assigned To (" some one @ provider . com ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` get Sent Ref err als Ġthrows Ġerror Ġunless Ġa Ġsingle Ġquery Ġparam Ġis Ġpassed `() Ġ{ ĊĠĠĠ ĠassertThrows < Server Web Input Exception > Ġ{ ĊĠĠĠĠĠ Ġref erral Controller . get Sent Ref err als () ĊĠĠĠ Ġ} ĊĊĠĠĠ ĠassertThrows < Server Web Input Exception > Ġ{ ĊĠĠĠĠĠ Ġref erral Controller . get Sent Ref err als ( sent By Ġ= Ġ" bern ard . be aks ", Ġsent To Ġ= Ġ" H AR MON Y _ LI V ING ") ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` assign Sent Ref erral Ġreturns Ġ 4 0 4 Ġif Ġref erral Ġdoes Ġnot Ġexist `() Ġ{ ĊĠĠĠ Ġwhenever ( ref erral Service . get Sent Ref erral ( any ())). thenReturn ( null ) ĊĠĠĠ Ġval Ġe Ġ= ĠassertThrows < ResponseStatus Exception > Ġ{ ĊĠĠĠĠĠ Ġref erral Controller . assign Sent Ref erral ( ĊĠĠĠĠĠĠĠ ĠUUID . randomUUID (), ĊĠĠĠĠĠĠĠ ĠRef erral Assignment DTO ( Auth User DTO (" username ", Ġ" auth Source ", Ġ" userId ")), ĊĠĠĠĠĠĠĠ Ġtoken Factory . create () ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĠĠĠ ĠassertThat ( e . status ). isEqualTo ( HttpStatus . NOT _ FOUND ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` assign Sent Ref erral Ġuses Ġincoming Ġjwt Ġfor Ġ' assigned By ' Ġargument , Ġand Ġrequest Ġbody Ġfor Ġ' assigned To '` () Ġ{ ĊĠĠĠ Ġval Ġref erral Ġ= Ġref erral Factory . create Sent () ĊĠĠĠ Ġval Ġassigned To User Ġ= Ġuser Factory . create ( id Ġ= Ġ" to ") ĊĠĠĠ Ġwhenever ( ref erral Service . get Sent Ref erral ( any ())). thenReturn ( ref erral ) ĊĠĠĠ Ġwhenever ( ref erral Service . assign Sent Ref erral ( any (), Ġany (), Ġany ())). thenReturn ( ref erral ) ĊĠĠĠ Ġref erral Controller . assign Sent Ref erral ( ĊĠĠĠĠĠ ĠUUID . randomUUID (), ĊĠĠĠĠĠ ĠRef erral Assignment DTO ( Auth User DTO . from ( assigned To User )), ĊĠĠĠĠĠ Ġtoken Factory . create ( userID Ġ= Ġ" by ") ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġto Captor Ġ= Ġargument Captor < Auth User >() ĊĠĠĠ Ġval Ġby Captor Ġ= Ġargument Captor < Auth User >() ĊĠĠĠ Ġverify ( ref erral Service ). assign Sent Ref erral ( eq ( ref erral ), Ġby Captor . capture (), Ġto Captor . capture ()) ĊĠĠĠ ĠassertThat ( to Captor . first Value . id ). isEqualTo (" to ") ĊĠĠĠ ĠassertThat ( by Captor . first Value . id ). isEqualTo (" by ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` successfully Ġcall Ġend Ġref erral Ġendpoint `() Ġ{ ĊĠĠĠ Ġval Ġref erral Ġ= Ġref erral Factory . create Sent () ĊĠĠĠ Ġwhenever ( ref erral Service . get Sent Ref erral ( any ())). thenReturn ( ref erral ) ĊĊĠĠĠ Ġval Ġauth User Ġ= ĠAuth User (" CR N 1 2 3 ", Ġ" auth ", Ġ" user ") ĊĠĠĠ Ġval Ġjwt Authentication Token Ġ= ĠJwt Authentication Token ( mock ()) ĊĠĠĠ Ġwhenever ( jwt Auth User Mapper . map ( jwt Authentication Token )). thenReturn ( auth User ) ĊĠĠĠ Ġwhenever ( ref erral Service . end Sent Ref erral ( any (), Ġany ())). thenReturn ( ref erral Factory . create Ended ()) ĊĊĠĠĠ Ġref erral Controller . end Sent Ref erral ( ref erral . id , Ġjwt Authentication Token ) ĊĠĠĠ Ġverify ( ref erral Service ). end Sent Ref erral ( ref erral , Ġauth User ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` end Ġref erral Ġendpoint Ġdoes Ġnot Ġfind Ġref erral `() Ġ{ ĊĠĠĠ Ġwhenever ( ref erral Service . get Sent Ref erral ( any ())). thenReturn ( null ) ĊĠĠĠ Ġval Ġjwt Authentication Token Ġ= ĠJwt Authentication Token ( mock ()) ĊĠĠĠ Ġval Ġe Ġ= ĠassertThrows < ResponseStatus Exception > Ġ{ ĊĠĠĠĠĠ Ġref erral Controller . end Sent Ref erral ( UUID . randomUUID (), Ġjwt Authentication Token ) ĊĠĠĠ Ġ} ĊĠĠĠ ĠassertThat ( e . status ). isEqualTo ( HttpStatus . NOT _ FOUND ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` get Ġall Ġcancellation Ġreasons `() Ġ{ ĊĠĠĠ Ġval Ġcancellation Re asons Ġ= ĠlistOf ( ĊĠĠĠĠĠ ĠC ancellation Reason ( id Ġ= Ġ" aaa ", Ġdescription Ġ= Ġ" reason Ġ 1 "), ĊĠĠĠĠĠ ĠC ancellation Reason ( id Ġ= Ġ" bbb ", Ġdescription Ġ= Ġ" reason Ġ 2 ") ĊĠĠĠ Ġ) ĊĠĠĠ Ġwhenever ( ref erral Service . getC ancellation Re asons ()). thenReturn ( c ancellation Re asons ) ĊĠĠĠ Ġval Ġresponse Ġ= Ġref erral Controller . getC ancellation Re asons () ĊĠĠĠ ĠassertThat ( response ). isEqualTo ( Cancellation Re asons DTO . from ( c ancellation Re asons )) ĊĠ Ġ} Ċ } Ċ