package Ġcom . uber to b . rest 4 sf tp . http Ċ Ċ import Ġassert k . assertThat Ċ import Ġassert k . assertions . isEqualTo Ċ import Ġassert k . fail Ċ import Ġcom . uber to b . rest 4 sf tp . model . CommandHandler Ċ import Ġcom . uber to b . rest 4 sf tp . model . Simple F tp Client Factory Ċ import Ġorg . apache . http . HttpHeaders Ċ import Ġorg . http 4 k . base 6 4 Encode Ċ import Ġorg . http 4 k . core . ContentType Ċ import Ġorg . http 4 k . core . ContentType . Companion . OCT ET _ STREAM Ċ import Ġorg . http 4 k . core . Method Ċ import Ġorg . http 4 k . core . Request Ċ import Ġorg . http 4 k . core . Response Ċ import Ġorg . http 4 k . core . Status Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġjava . time . LocalDateTime Ċ Ċ abstract Ġclass ĠRest ful Server Contract Ġ{ ĊĊĠĠĠ Ġprotected Ġabstract Ġval Ġftp Client Factory : ĠSimple F tp Client Factory ĊĊĠĠĠ Ġprotected Ġabstract Ġval Ġftp Host : ĠString ĊĠĠĠ Ġprotected Ġabstract Ġval Ġftp Port : ĠInt ĊĠĠĠ Ġprotected Ġabstract Ġval Ġftp User : ĠString ĊĠĠĠ Ġprotected Ġabstract Ġval Ġftp Password : ĠString ĊĊĠĠĠ Ġprivate Ġfun ĠRequest . with Authentication ( password : ĠString Ġ= Ġftp Password ) Ġ= ĊĠĠĠĠĠĠĠ Ġheaders ( listOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" FTP - Host " Ġto Ġftp Host , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" FTP - Port " Ġto Ġftp Port . toString (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" FTP - User " Ġto Ġftp User , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" FTP - Password " Ġto Ġpassword ĊĠĠĠĠĠĠĠ Ġ)) ĊĊĠĠĠ Ġprivate Ġfun ĠRequest . with Author isation ( password : ĠString Ġ= Ġftp Password ) Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġheaders ( listOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" FTP - Host " Ġto Ġftp Host , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" FTP - Port " Ġto Ġftp Port . toString (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Authorization " Ġto Ġ" Basic Ġ${ "$ ftp User :$ password ". base 6 4 Encode () }" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ)) ĊĊĠĠĠ Ġprivate Ġval Ġhandler : ĠRest ful Server Ġ= ĠRest ful Server ( CommandHandler Ġ{ Ġftp Client Factory ( it ) Ġ}) ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġun author ised Ġif Ġauthor isation Ġheaders Ġnot Ġpresent `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( Request ( Method . GET , Ġ"/ folder / upload ")) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . UN AUTHORIZ ED ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġun author ised Ġif Ġinvalid Ġcredentials Ġsupplied `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠRequest ( Method . GET , Ġ"/ folder / upload "). with Authentication (" bad Ġpassword ") ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( request ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . UN AUTHORIZ ED ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġun author ised Ġif Ġinvalid Ġcredentials Ġsupplied Ġwith Ġbasic Ġauth `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠRequest ( Method . GET , Ġ"/ folder / upload "). with Author isation (" bad Ġpassword ") ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( request ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . UN AUTHORIZ ED ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġfile Ġnot Ġfound Ġfor Ġinvalid Ġpath `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠRequest ( Method . GET , Ġ"/ file / upload / unknown _ file . txt "). with Author isation () ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( request ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . NOT _ FOUND ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġfile Ġnot Ġfound Ġif Ġgiven Ġa Ġfolder Ġpath `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠRequest ( Method . GET , Ġ"/ file / upload "). with Author isation () ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( request ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . NOT _ FOUND ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġfolder Ġnot Ġfound Ġif Ġfolder Ġdoes Ġnot Ġexist Ġon Ġserver `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠRequest ( Method . GET , Ġ"/ folder / upload / unknown _ sub folder "). with Author isation () ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( request ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . NOT _ FOUND ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` returns Ġvalid Ġfolder `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠRequest ( Method . GET , Ġ"/ folder / upload "). with Author isation () ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġhandler ( request ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . OK ) ĊĠĠĠĠĠĠĠ ĠassertThat ( response . contentType ). isEqualTo ( ContentType . APPLICATION _ JSON . to HeaderValue ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` can Ġupload Ġa Ġnew Ġfile , Ġretrieve Ġit , Ġupdate Ġit , Ġand Ġdelete Ġit `() Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġupload FilePath Ġ= Ġ"/ upload / uploaded - file -${ LocalDateTime . now ()}. txt " ĊĠĠĠĠĠĠĠ Ġval Ġtest File Contents Ġ= Ġ" this Ġis Ġan Ġuploaded Ġfile " ĊĠĠĠĠĠĠĠ Ġval Ġupdated File Contents Ġ= Ġ" this Ġis Ġa Ġrev ised Ġuploaded Ġfile " ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . GET , Ġ"/ file $ upload FilePath "). with Author isation ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. handle Response Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( it . status ). isEqualTo ( Status . NOT _ FOUND ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . PUT , Ġ"/ file $ upload FilePath "). body ( test File Contents ). with Author isation ()). handle Response Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( it . status ). isEqualTo ( Status . OK ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . GET , Ġ"/ file $ upload FilePath "). with Author isation ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. handle Response Ġ{ Ġresponse Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . OK ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . contentType ). isEqualTo ( OCT ET _ STREAM . value ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . body String ()). isEqualTo ( test File Contents ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . PUT , Ġ"/ file $ upload FilePath "). body ( updated File Contents ). with Author isation ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. handle Response Ġ{ Ġresponse Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . OK ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . GET , Ġ"/ file $ upload FilePath "). with Author isation ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. handle Response Ġ{ Ġresponse Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . OK ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . contentType ). isEqualTo ( OCT ET _ STREAM . value ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . body String ()). isEqualTo ( updated File Contents ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . DELETE , Ġ"/ file $ upload FilePath "). with Author isation ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. handle Response Ġ{ Ġresponse Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . OK ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġhandler ( Request ( Method . GET , Ġ"/ file $ upload FilePath "). with Author isation ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. handle Response Ġ{ Ġresponse Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status ). isEqualTo ( Status . NOT _ FOUND ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠResponse . handle Response ( body : Ġ( Response ) Ġ-> ĠUnit ) Ġ= Ġalso ( body ) ĊĊĠĠĠ Ġprivate Ġval ĠResponse . contentType : ĠString ĊĠĠĠĠĠĠĠ Ġget Ġ() Ġ= Ġheader ( HttpHeaders . CONTENT _ TYPE ) Ġ?: Ġfail (" no Ġcontent Ġtype ") Ċ Ċ }