package Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . component Ċ Ċ import Ġcom . fasterxml . jackson . databind . ObjectMapper Ċ import Ġcom . microsoft . application insights . Telemetry Client Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . argument Captor Ċ 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 . verify Zero Interactions Ċ 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 Ġsoftware . amazon . aw ssdk . services . sns . S ns Client Ċ import Ġsoftware . amazon . aw ssdk . services . sns . model . Publish Request Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Event DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . jpa . entity . Auth User Ċ import Ġjava . time . Offset DateTime Ċ import Ġjava . util . UUID Ċ Ċ class ĠSN SP ub lish er Test Ġ{ ĊĠ Ġprivate Ġval Ġsns Client Ġ= Ġmock < S ns Client >() ĊĠ Ġprivate Ġval ĠobjectMapper Ġ= Ġmock < ObjectMapper >() ĊĠ Ġprivate Ġval Ġtelemetry Client Ġ= Ġmock < Telemetry Client >() ĊĊĠ Ġprivate Ġval Ġa Ref erral Id Ġ= ĠUUID . fromString (" 8 2 1 3 8 d 1 4 - 3 8 3 5 - 4 4 2 b - b 3 9 b - 9 f 8 a 0 7 6 5 0 bbe ") ĊĠ Ġprivate Ġval Ġa User Ġ= ĠAuth User (" d 7 c 4 c 3 a 7 a 7 ", Ġ" ir relevant ", Ġ" d 7 c 4 c 3 a 7 a 7 @ example . org ") ĊĊĠ Ġval Ġevent Ġ= ĠEvent DTO ( ĊĠĠĠ Ġ" inter vention . test . event ", ĊĠĠĠ Ġ" A Ġref erral Ġhas Ġbeen Ġsent Ġto Ġa ĠService ĠProvider ", ĊĠĠĠ Ġ" http : test / abc ", ĊĠĠĠ ĠOffset DateTime . parse (" 2 0 2 0 - 1 2 - 0 4 T 1 0 : 4 2 : 4 3 + 0 0 : 0 0 "), ĊĠĠĠ Ġmap Of () ĊĠ Ġ) ĊĊĠ Ġprivate Ġfun Ġsns Publisher ( enabled : ĠBoolean ): ĠSN SP ub lish er Ġ{ ĊĠĠĠ Ġreturn ĠSN SP ub lish er ( sns Client , ĠobjectMapper , Ġtelemetry Client , Ġenabled , Ġ" arn ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` successful Ġevent Ġpublished `() Ġ{ ĊĠĠĠ Ġwhenever ( object Mapper . writeValue AsString ( event )). thenReturn ("{} ") ĊĠĠĠ Ġsns Publisher ( true ). publish ( a Ref erral Id , Ġa User , Ġevent ) ĊĊĠĠĠ Ġval Ġrequest Captor Ġ= Ġargument Captor < Publish Request >() ĊĠĠĠ Ġverify ( sns Client ). publish ( request Captor . capture ()) ĊĠĠĠ ĠassertThat ( request Captor . first Value . message Attributes ()[" eventType "] !!. dataType ()). isEqualTo (" String ") ĊĠĠĠ ĠassertThat ( request Captor . first Value . message Attributes ()[" eventType "] !!. stringValue ()). isEqualTo ( event . eventType ) ĊĠĠĠ ĠassertThat ( request Captor . first Value . message ()). isEqualTo ("{} ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` s ends Ġcustom Ġevent Ġto Ġapplication Ġinsights Ġon Ġpublish `() Ġ{ ĊĠĠĠ Ġsns Publisher ( true ). publish ( a Ref erral Id , Ġa User , Ġevent ) ĊĠĠĠ Ġverify ( telemetry Client ). track Event ( ĊĠĠĠĠĠ Ġ" Inter ventions Domain Event ", ĊĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠ Ġ" event " Ġto Ġ" inter vention . test . event ", ĊĠĠĠĠĠĠĠ Ġ" ref erral Id " Ġto Ġ" 8 2 1 3 8 d 1 4 - 3 8 3 5 - 4 4 2 b - b 3 9 b - 9 f 8 a 0 7 6 5 0 bbe ", ĊĠĠĠĠĠĠĠ Ġ" actor UserId " Ġto Ġ" d 7 c 4 c 3 a 7 a 7 ", ĊĠĠĠĠĠĠĠ Ġ" actor UserName " Ġto Ġ" d 7 c 4 c 3 a 7 a 7 @ example . org ", ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` throw n Ġexception Ġis Ġnot Ġsw allowed `() Ġ{ ĊĠĠĠ Ġwhenever ( sns Client . publish ( any < Publish Request >() )). then Throw ( RuntimeException :: class . java ) ĊĠĠĠ ĠassertThrows < RuntimeException > Ġ{ Ġsns Publisher ( true ). publish ( a Ref erral Id , Ġa User , Ġevent ) Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` event Ġdoes Ġnot Ġpublish Ġwhen Ġservice Ġis Ġdisabled `() Ġ{ ĊĠĠĠ Ġsns Publisher ( false ). publish ( a Ref erral Id , Ġa User , Ġevent ) ĊĠĠĠ Ġverify Zero Interactions ( sns Client ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` event Ġstill Ġsends Ġtelemetry Ġwhen Ġservice Ġis Ġdisabled `() Ġ{ ĊĠĠĠ Ġsns Publisher ( false ). publish ( a Ref erral Id , Ġa User , Ġevent ) ĊĠĠĠ Ġverify ( telemetry Client ). track Event ( ĊĠĠĠĠĠ Ġ" Inter ventions Domain Event ", ĊĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠ Ġ" event " Ġto Ġ" inter vention . test . event ", ĊĠĠĠĠĠĠĠ Ġ" ref erral Id " Ġto Ġ" 8 2 1 3 8 d 1 4 - 3 8 3 5 - 4 4 2 b - b 3 9 b - 9 f 8 a 0 7 6 5 0 bbe ", ĊĠĠĠĠĠĠĠ Ġ" actor UserId " Ġto Ġ" d 7 c 4 c 3 a 7 a 7 ", ĊĠĠĠĠĠĠĠ Ġ" actor UserName " Ġto Ġ" d 7 c 4 c 3 a 7 a 7 @ example . org ", ĊĠĠĠĠĠ Ġ), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} Ċ } Ċ