package Ġio . piv otal . sh iny ay Ċ Ċ import Ġio . piv otal . sh iny ay . entity . Gender Ċ import Ġio . piv otal . sh iny ay . entity . Person Ċ import Ġio . rest ass ured . Rest Ass ured Ċ import Ġio . rest ass ured . Rest Ass ured . get Ċ import Ġio . rest ass ured . response . Response Ċ import Ġorg . apache . commons . lang 3 . Random StringUtils Ċ import Ġorg . apache . commons . lang 3 . Random Utils Ċ import Ġorg . hamcrest . Core Matchers . equalTo Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . boot . web . server . Local Server Port Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . MediaType Ċ Ċ @ SpringBootTest ( web Environment Ġ= ĠSpring Boot Test . Web Environment . RANDOM _ PORT ) Ċ class ĠSpring Live Coding Application Tests (@ Local Server Port Ġval Ġport : ĠInt ) Ġ{ ĊĊ ĉ val Ġuri Ġ= Ġ" http :// localhost :${ port }/ db / person " ĊĊ ĉ @ Test Ċ ĉ fun Ġwhen GetAll Pers ons _ then OK () Ġ{ Ċĉ ĉ get ( uri ). then () Ċĉĉĉ ĉ . assertThat () Ċĉĉĉ ĉ . statusCode ( HttpStatus . OK . value ()) Ċ ĉ } ĊĊ ĉ @ Test Ċ ĉ fun Ġwhen Get Person ById _ then OK () Ġ{ Ċĉ ĉ get ("$ uri / 1 "). then () Ċĉĉĉ ĉ . assertThat () Ċĉĉĉ ĉ . statusCode ( HttpStatus . OK . value ()) Ċĉĉĉ ĉ . body (" firstname ", ĠequalTo (" Re i ")) Ċ ĉ } Ċ Ċ // ĉ POST ĊĊ ĉ @ Test Ċ ĉ fun Ġwhen Create Person _ then Created () Ġ{ Ċĉ ĉ val Ġperson Ġ= Ġcreate Person () Ċĉ ĉ val Ġresponse : ĠResponse Ġ= ĠRest Ass ured . given () Ċĉĉĉ ĉ . contentType ( MediaType . APPLICATION _ JSON _ VALUE ) Ċĉĉĉ ĉ . body ( person ) Ċĉĉĉ ĉ . post ( uri ) Ċĉ ĉ assertThat ( response . statusCode ). isEqualTo ( HttpStatus . CREATED . value ()) Ċ ĉ } Ċ Ċ // ĉ DELETE ĊĊ ĉ @ Test Ċ ĉ fun Ġwhen Delete Created Person _ then Ok () Ġ{ Ċĉ ĉ val Ġperson Ġ= Ġcreate Person () Ċĉ ĉ val Ġendpoint Ġ= Ġcreate Person As Uri ( person ) ĊĊĉ ĉ Rest Ass ured . delete ( endpoint ) Ċĉĉĉ ĉ . then () Ċĉĉĉ ĉ . assertThat () Ċĉĉĉ ĉ . statusCode ( HttpStatus . OK . value ()) Ċĉ ĉ get ( endpoint ). then () Ċĉĉĉ ĉ . assertThat () Ċĉĉĉ ĉ . statusCode ( HttpStatus . OK . value ()) Ċ ĉ } ĊĊ ĉ private Ġfun Ġcreate Person (): ĠPerson Ġ= ĠPerson ( Ċĉĉ ĉ Random Utils . nextInt ( 1 0 , Ġ 1 0 0 ), Ċĉĉ ĉ Random StringUtils . random Alphabet ic ( 5 ), Ċĉĉ ĉ Random StringUtils . random Alphabet ic ( 5 ), Ċĉĉ ĉ Random Utils . nextInt ( 1 0 , Ġ 1 0 0 ), Ċĉĉ ĉ Gender . F EM ALE ) ĊĊ ĉ private Ġfun Ġcreate Person As Uri ( person : ĠPerson ): ĠString Ġ{ Ċĉ ĉ val Ġresponse Ġ= ĠRest Ass ured . given () Ċĉĉĉ ĉ . contentType ( MediaType . APPLICATION _ JSON _ VALUE ) Ċĉĉĉ ĉ . body ( person ) Ċĉĉĉ ĉ . post ( uri ) Ċĉ ĉ println ( response . json Path (). pretty Print ()) Ċĉ ĉ return Ġ"$ uri /${ response . json Path (). get < Int >(" id ") }" Ċ ĉ } Ċ } Ċ