package Ġuk . gov . just ice . h mp ps . prob ation teams . repository Ċ Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . extension . Extend With Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . jdbc . core . Jdbc Template Ċ import Ġorg . springframework . security . test . context . support . With Anonymous User Ċ import Ġorg . springframework . test . context . Active Profiles Ċ import Ġorg . springframework . test . context . junit . jupiter . Spring Extension Ċ import Ġorg . springframework . test . context . transaction . Test Transaction Ċ import Ġorg . springframework . transaction . annotation . Transactional Ċ import Ġuk . gov . just ice . h mp ps . prob ation teams . model . Local Delivery Unit Ċ import Ġuk . gov . just ice . h mp ps . prob ation teams . model . Prob ation Team Ċ import Ġuk . gov . just ice . h mp ps . prob ation teams . utils . unique L du Code Ċ import Ġjava . util . UUID Ċ Ċ @ Active Profiles (" test ") Ċ @ Extend With ( Spring Extension :: class ) Ċ @ SpringBootTest ( web Environment Ġ= ĠSpring Boot Test . Web Environment . NONE ) Ċ @ Transactional Ċ @ With Anonymous User Ċ Ċ class ĠLocal Delivery Unit Repository Test ( ĊĠ Ġ@ Autowired Ġval Ġrepository : ĠLocal Delivery Unit Repository , ĊĠ Ġ@ Autowired Ġval Ġjdbc Template : ĠJdbc Template Ċ ) Ġ{ ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Persist ĠLocal ĠDelivery ĠUnit `() Ġ{ ĊĠĠĠ Ġval Ġl du Code Ġ= Ġunique L du Code () ĊĠĠĠ Ġval Ġl du Ġ= ĠLocal Delivery Unit ( ĊĠĠĠĠĠ Ġprob ation Area Code Ġ= Ġ" ABC ", ĊĠĠĠĠĠ Ġlocal Delivery Unit Code Ġ= Ġl du Code , ĊĠĠĠĠĠ Ġfunctional Mailbox Ġ= Ġ" p qr @ stu . l td . uk " ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġrepository . save ( ld u ) ĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĊĠĠĠ ĠTest Transaction . start () ĊĊĠĠĠ ĠassertThat ( repository . findBy Prob ation Area Code And Local Delivery Unit Code (" ABC ", Ġl du Code )) ĊĠĠĠĠĠ Ġ. hasValue Satisf ying Ġ{ Ġpersistent L du Ġ-> ĊĠĠĠĠĠĠĠ ĠassertThat ( persistent L du . id ). isNotNull () ĊĊĠĠĠĠĠĠĠ Ġ// ĠBusiness Ġkey Ġequality ĊĠĠĠĠĠĠĠ ĠassertThat ( persistent L du ). isEqualTo ( ld u ) ĊĠĠĠĠĠĠĠ ĠassertThat ( ld u . create UserId ). isEqualTo (" anonymous ") ĊĠĠĠĠĠĠĠ ĠassertThat ( ld u Count ( persistent L du . id )). isEqualTo ( 1 ) ĊĠĠĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Persist ĠLocal ĠDelivery ĠUnit Ġwith ĠProb ation ĠTeams `() Ġ{ ĊĠĠĠ Ġval Ġl du Code Ġ= Ġunique L du Code () ĊĠĠĠ Ġval Ġl du : ĠLocal Delivery Unit Ġ= Ġl du With Prob ation Teams ( ld u Code ) ĊĊĠĠĠ Ġrepository . save ( ld u ) ĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĊĠĠĠ ĠTest Transaction . start () ĊĠĠĠ ĠassertThat ( repository . findBy Prob ation Area Code And Local Delivery Unit Code (" ABC ", Ġl du Code )) ĊĠĠĠĠĠ Ġ. hasValue Satisf ying Ġ{ Ġpersistent L du Ġ-> ĊĠĠĠĠĠĠĠ ĠassertThat ( persistent L du . id ). isNotNull () ĊĠĠĠĠĠĠĠ ĠassertThat ( persistent L du . prob ation Teams ). isEqualTo ( ld u With Prob ation Teams ( ld u Code ). prob ation Teams ) ĊĠĠĠĠĠĠĠ ĠassertThat ( prob ation Team Count ( persistent L du . id )). isEqualTo ( 2 ) ĊĠĠĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Update ĠProb ation ĠTeam `() Ġ{ ĊĠĠĠ Ġval Ġl du Code Ġ= Ġunique L du Code () ĊĠĠĠ Ġval Ġl du : ĠLocal Delivery Unit Ġ= Ġl du With Prob ation Teams ( ld u Code ) ĊĠĠĠ Ġrepository . save ( ld u ) ĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĊĠĠĠ ĠTest Transaction . start () ĊĊĠĠĠ ĠassertThat ( repository . findBy Prob ation Area Code And Local Delivery Unit Code (" ABC ", Ġl du Code )) ĊĠĠĠĠĠ Ġ. hasValue Satisf ying Ġ{ Ġpersistent L du Ġ-> ĊĠĠĠĠĠĠĠ Ġpersistent L du . prob ation Teams . remove (" T 1 ") ĊĠĠĠĠĠĠĠ Ġpersistent L du . prob ation Teams [" T 2 "] Ġ= ĠProb ation Team (" zz z @ zz z . com ") ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĊĠĠĠ ĠTest Transaction . start () ĊĊĠĠĠ ĠassertThat ( repository . findBy Prob ation Area Code And Local Delivery Unit Code (" ABC ", Ġl du Code )) ĊĠĠĠĠĠ Ġ. hasValue Satisf ying Ġ{ Ġpersistent L du Ġ-> ĊĠĠĠĠĠĠĠ ĠassertThat ( persistent L du . id ). isNotNull () ĊĠĠĠĠĠĠĠ ĠassertThat ( persistent L du . prob ation Teams ). isEqualTo ( map Of (" T 2 " Ġto ĠProb ation Team (" zz z @ zz z . com "))) ĊĠĠĠĠĠĠĠ ĠassertThat ( prob ation Team Count ( persistent L du . id )). isEqualTo ( 1 ) ĊĠĠĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Deleting Ġan ĠL DU Ġshould Ġdelete Ġits Ġprob ation Ġteams `() Ġ{ ĊĠĠĠ Ġval Ġl du Code Ġ= Ġunique L du Code () ĊĠĠĠ Ġval Ġl du : ĠLocal Delivery Unit Ġ= Ġl du With Prob ation Teams ( ld u Code ) ĊĊĠĠĠ Ġval Ġpersistent L du Ġ= Ġrepository . save ( ld u ) ĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĊĠĠĠ Ġ// ĠCheck Ġthe Ġdb ... ĊĠĠĠ Ġval Ġl du Id Ġ= Ġpersistent L du . id ĊĊĠĠĠ ĠassertThat ( ld u Id ). isNotNull () ĊĊĠĠĠ ĠTest Transaction . start () ĊĠĠĠ ĠassertThat ( ld u Count ( ld u Id )). isEqualTo ( 1 ) ĊĠĠĠ ĠassertThat ( prob ation Team Count ( ld u Id )). isEqualTo ( 2 ) ĊĠĠĠ Ġrepository . delete ById ( ld u Id !! ) ĊĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĊĠĠĠ ĠTest Transaction . start () ĊĊĠĠĠ ĠassertThat ( ld u Count ( ld u Id )). isEqualTo ( 0 ) ĊĠĠĠ ĠassertThat ( prob ation Team Count ( ld u Id )). isEqualTo ( 0 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun ĠfindBy Prob ation Area Code () Ġ{ ĊĠĠĠ Ġval Ġprob ation Area Code Ġ= Ġ" XYZ " ĊĠĠĠ Ġrepository . save ( ld u With Prob ation Teams ( unique L du Code (), Ġprob ation Area Code )) ĊĠĠĠ Ġrepository . save ( ld u With Prob ation Teams ( unique L du Code (), Ġprob ation Area Code )) ĊĠĠĠ Ġrepository . save ( ld u With Prob ation Teams ( unique L du Code (), Ġprob ation Area Code )) ĊĊĠĠĠ ĠTest Transaction . flag For Commit () ĊĠĠĠ ĠTest Transaction . end () ĊĠĠĠ ĠTest Transaction . start () ĊĊĠĠĠ Ġval Ġld us Ġ= Ġrepository . findBy Prob ation Area Code ( prob ation Area Code ) ĊĠĠĠ ĠassertThat ( ld us ). has Size ( 3 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġget Prob ation Area Codes () Ġ{ ĊĠĠĠ Ġval Ġprob ation Area Codes Ġ= Ġrepository . get Prob ation Area Codes () ĊĠĠĠ Ġval Ġactual Prob ation Area Codes Ġ= Ġprob ation Area Codes () ĊĠĠĠ ĠassertThat ( prob ation Area Codes ). isEqualTo ( actual Prob ation Area Codes ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġprob ation Area Codes () Ġ= ĊĠĠĠ Ġjdbc Template . query For List ( ĊĠĠĠĠĠ Ġ""" ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġselect Ġdistinct ĠPRO B ATION _ AREA _ CODE ĠĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfrom ĠLOCAL _ DE LI VERY _ UNIT 2 ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġorder Ġby ĠPRO B ATION _ AREA _ CODE ĊĠĠĠĠĠ Ġ""" . trim Indent (), ĊĠĠĠĠĠ ĠString :: class . java ĊĠĠĠ Ġ) ĊĊĠ Ġprivate Ġfun Ġprob ation Team Count ( ld u Id : ĠUUID ?) Ġ= ĊĠĠĠ Ġjdbc Template . query For Object ( ĊĠĠĠĠĠ Ġ""" ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġselect Ġcount (*) ĠĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfrom ĠPRO B ATION _ TEAM ĠĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwhere ĠLOCAL _ DE LI VERY _ UNIT _ ID Ġ= Ġ? ĊĠĠĠĠĠ Ġ""" . trim Indent (), ĊĠĠĠĠĠ ĠLong :: class . java , ĊĠĠĠĠĠ Ġl du Id ĊĠĠĠ Ġ) ĊĊĠ Ġprivate Ġfun Ġl du Count ( ld u Id : ĠUUID ?) Ġ= ĊĠĠĠ Ġjdbc Template . query For Object ( ĊĠĠĠĠĠ Ġ""" ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġselect Ġcount (*) ĠĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfrom ĠLOCAL _ DE LI VERY _ UNIT 2 ĠĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwhere ĠLOCAL _ DE LI VERY _ UNIT _ ID Ġ= Ġ? ĊĠĠĠĠĠ Ġ""" . trim Indent (), ĊĠĠĠĠĠ ĠLong :: class . java , ĊĠĠĠĠĠ Ġl du Id ĊĠĠĠ Ġ) ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġfun Ġl du With Prob ation Teams ( ld u Code : ĠString , Ġprob ation Area Code : ĠString Ġ= Ġ" ABC "): ĠLocal Delivery Unit Ġ= ĠLocal Delivery Unit ( ĊĠĠĠĠĠ Ġprob ation Area Code , ĊĠĠĠĠĠ Ġlocal Delivery Unit Code Ġ= Ġl du Code , ĊĠĠĠĠĠ Ġprob ation Teams Ġ= Ġmutable Map Of ( ĊĠĠĠĠĠĠĠ Ġ" T 1 " Ġto ĠProb ation Team (" t 1 @ team . com "), ĊĠĠĠĠĠĠĠ Ġ" T 2 " Ġto ĠProb ation Team (" t 2 @ team . com ") ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠ Ġ} Ċ } Ċ