package Ġno . sk at te et aten . aur ora . mock mvc . extensions . mock webserver Ċ Ċ import Ġassert k . Assert Ċ import Ġassert k . assertThat Ċ import Ġassert k . assertions . has Size Ċ import Ġassert k . assertions . isEqualTo Ċ import Ġassert k . assertions . is Failure Ċ import Ġassert k . assertions . isInstanceOf Ċ import Ġassert k . assertions . isNotNull Ċ import Ġassert k . assertions . isNull Ċ import Ġassert k . assertions . prop Ċ import Ġassert k . assertions . support . expected Ċ import Ġcom . fasterxml . jackson . module . kotlin . jackson ObjectMapper Ċ import Ġkotlinx . coroutines . delay Ċ import Ġokhttp 3 . mock webserver . Mock Response Ċ import Ġokhttp 3 . mock webserver . Mock WebServer Ċ import Ġokhttp 3 . mock webserver . Re corded Request Ċ import Ġorg . junit . jupiter . api . After Each Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . Test Instance Ċ import Ġorg . springframework . http . HttpMethod Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . web . client . HttpClient ErrorException Ċ import Ġorg . springframework . web . client . Rest Template Ċ import Ġorg . springframework . web . client . get For Entity Ċ import Ġorg . springframework . web . client . post For Entity Ċ Ċ data Ġclass ĠTest Object ( val Ġvalue : ĠString ) Ċ Ċ @ Test Instance ( Test Instance . Lifecycle . PER _ METHOD ) Ċ class ĠMock WebServer Test Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġserver Ġ= ĠMock WebServer () ĊĊĠĠĠ Ġ@ After Each ĊĠĠĠ Ġfun ĠtearDown () Ġ{ ĊĠĠĠĠĠĠĠ Ġserver . shutdown () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Enqueue Ġjson Ġresponses Ġand Ġassert Ġrequests `() Ġ{ ĊĠĠĠĠĠĠĠ Ġserver . enqueue Json ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresponse With Body (" test 1 "), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresponse With Body (" test 2 ") ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġresponse 1 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĠĠĠĠĠĠĠ Ġval Ġresponse 2 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( response 1 . body ). isEqualTo (" test 1 ") ĊĠĠĠĠĠĠĠ ĠassertThat ( response 2 . body ). isEqualTo (" test 2 ") ĊĊĠĠĠĠĠĠĠ Ġserver . assert () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. contains Request ( HttpMethod . GET , Ġ"/") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. contains Request ( HttpMethod . GET , Ġ"/") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Test Ġexecute Ġwith ĠMock Responses `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmock Response Ġ= ĠMock Response (). set Body (" test ") ĊĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= Ġserver . execute ( mock Response , Ġmock Response ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse 1 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse 2 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 1 ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 1 . body ). isEqualTo (" test ") ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 2 ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 2 . body ). isEqualTo (" test ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertThat ( request . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( request . first ()). isNotNull (). has Default Path () ĊĠĠĠĠĠĠĠ ĠassertThat ( request [ 1 ]). isNotNull (). has Default Path () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Test Ġexecute Ġwith Ġstatus Ġand Ġresponse Ġobject `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= Ġserver . execute ( 2 0 1 Ġto ĠTest Object (" test "), ĠobjectMapper Ġ= Ġj ack son ObjectMapper ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= ĠRest Template (). post For Entity < String >( server . url , ĠTest Object (" test - request - body ")) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . statusCode ). isEqualTo ( HttpStatus . CREATED ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . body ). isEqualTo (""" {" value ":" test "} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertThat ( request . first ()). isNotNull (). has Default Path () ĊĠĠĠĠĠĠĠ ĠassertThat ( request . first ()?. body AsString ()). isEqualTo (""" {" value ":" test - request - body "} """) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Test Ġexecute Ġwith Ġrequest Ġand Ġresponse Ġobject `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= Ġserver . execute ( Test Object (" test "), ĠobjectMapper Ġ= Ġj ack son ObjectMapper ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= ĠRest Template (). post For Entity < String >( server . url , ĠTest Object (" test - request - body ")) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . body ). isEqualTo (""" {" value ":" test "} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġbody Ġ= Ġrequest . first ()?. body As Object < Test Object >() ĊĊĠĠĠĠĠĠĠ ĠassertThat ( request . first ()). isNotNull (). has Default Path () ĊĠĠĠĠĠĠĠ ĠassertThat ( body ?. value ). isEqualTo (" test - request - body ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Test Ġexecute Ġwith Ġvar arg Ġresponse Ġobjects `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequests Ġ= Ġserver . execute ( Test Object (" test "), ĠTest Object (" test ")) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse 1 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse 2 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 1 ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 2 ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 1 . body ). isEqualTo (""" {" value ":" test "} """) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 2 . body ). isEqualTo (""" {" value ":" test "} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertThat ( requests . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( requests [ 0 ]). isNotNull (). has Default Path () ĊĠĠĠĠĠĠĠ ĠassertThat ( requests [ 1 ]). isNotNull (). has Default Path () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Test Ġexecute Ġwith Ġvar arg Ġresponse Ġpairs `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequests Ġ= Ġserver . execute ( 2 0 0 Ġto ĠTest Object (" test "), Ġ 4 0 4 Ġto ĠTest Object (" test ")) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse 1 Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat Ġ{ ĠRest Template (). get For Entity < String >( server . url ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. is Failure () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. isInstanceOf ( HttpClient ErrorException :: class ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. prop ( HttpClient ErrorException :: getStatusCode ). isEqualTo ( HttpStatus . NOT _ FOUND ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 1 ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response 1 . body ). isEqualTo (""" {" value ":" test "} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertThat ( requests . size ). isEqualTo ( 2 ) ĊĠĠĠĠĠĠĠ ĠassertThat ( requests [ 0 ]). isNotNull (). has Default Path () ĊĠĠĠĠĠĠĠ ĠassertThat ( requests [ 1 ]). isNotNull (). has Default Path () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Set Ġjson Ġfile Ġas Ġbody `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= Ġserver . execute ( Mock Response (). set Json File As Body (" test . json ")) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= ĠRest Template (). get For Entity < String >( server . url ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response ). is Ok () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . body ). isEqualTo (""" {" key ":" test 1 2 3 "} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertThat ( request . first ()). isNotNull (). has Default Path () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Response Ġtimeout Ġfrom Ġserver `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= Ġserver . execute ( responses Ġ= Ġ* array Of ( json Response ()), Ġtimeout In Ms Ġ= Ġ 5 ) Ġ{} ĊĠĠĠĠĠĠĠ ĠassertThat ( request . first ()). isNull () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Execute Ġblocking `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequests Ġ= Ġserver . execute Blocking ( json Response ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠRest Template (). get For Entity < String >( server . url ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ ĠassertThat ( requests ). has Size ( 1 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠAssert < Response Entity <* >> . is Ok () Ġ= Ġgiven Ġ{ Ġrequest Ġ-> ĊĠĠĠĠĠĠĠ Ġif Ġ( request . statusCode Ġ== ĠHttpStatus . OK ) Ġreturn ĊĠĠĠĠĠĠĠ Ġexpected (" Status Ġ 2 0 0 Ġ- ĠOK ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠAssert < Re corded Request >. has Default Path () Ġ= Ġgiven Ġ{ Ġrequest Ġ-> ĊĠĠĠĠĠĠĠ Ġif Ġ( request . path Ġ== Ġ"/") Ġreturn ĊĠĠĠĠĠĠĠ Ġexpected (" Request Ġto Ġcontain Ġ'/' Ġpath ") ĊĠĠĠ Ġ} Ċ }