package Ġru . ch urch tools . desk bible . data . config Ċ Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġio . mock k . every Ċ import Ġio . mock k . just Ċ import Ġio . mock k . mock k Ċ import Ġio . mock k . runs Ċ import Ġio . mock k . slot Ċ import Ġio . mock k . verify Ċ import Ġokhttp 3 . Http Url . Companion . to Http Url Ċ import Ġokhttp 3 . Ok HttpClient Ċ import Ġokhttp 3 . Request Ċ import Ġokhttp 3 . Response Ċ import Ġokhttp 3 . ResponseBody . Companion . to ResponseBody Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġru . ch urch tools . desk bible . data . cache . Cache Ċ import Ġjava . io . IOException Ċ import Ġjava . io . InputStream Ċ Ċ /** Ċ Ġ* ĠÐ¢ ÐµÑģÑĤ Ñĭ ĠÐ´Ð»Ñı Ġ[ Feature Toggle Repository Impl ] Ċ Ġ*/ Ċ class ĠFeature Toggle Repository Impl Test Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġrequest Slot Ġ= Ġslot < Request >() ĊĠĠĠ Ġprivate Ġval Ġresponse : ĠResponse Ġ= Ġmock k () ĊĠĠĠ Ġprivate Ġval ĠhttpClient : ĠOk HttpClient Ġ= Ġmock k () ĊĠĠĠ Ġprivate Ġval Ġcache : ĠCache < String > Ġ= Ġmock k Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġput ( CACHE _ KEY , ĠRESP ONSE _ STRING ) Ġ} Ġjust Ġruns ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġval Ġrepository Ġ= ĠFeature Toggle Repository Impl ( httpClient , ĠTO GG LES _ URL , Ġcache ) ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ with Exception And Not Cached () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )) Ġ} Ġthrows ĠIOException () ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} Ġreturns Ġnull ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()). isNull () ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ with Exception And Cached () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )) Ġ} Ġthrows ĠIOException () ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} Ġreturns ĠRESP ONSE _ STRING ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()?. read Text ()). isEqualTo ( RESPONSE _ STRING ) ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ with Response Not Successful And Not Cached () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )). execute () Ġ} Ġreturns Ġresponse ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . is Successful Ġ} Ġreturns Ġfalse ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} Ġreturns Ġnull ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()). isNull () ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ with Response Not Successful And Cached () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )). execute () Ġ} Ġreturns Ġresponse ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . is Successful Ġ} Ġreturns Ġfalse ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} Ġreturns ĠRESP ONSE _ STRING ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()?. read Text ()). isEqualTo ( RESPONSE _ STRING ) ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ without ResponseBody And Not Cached () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )). execute () Ġ} Ġreturns Ġresponse ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . is Successful Ġ} Ġreturns Ġtrue ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . body Ġ} Ġreturns Ġnull ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} Ġreturns Ġnull ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()). isNull () ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ without ResponseBody And Cached () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )). execute () Ġ} Ġreturns Ġresponse ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . is Successful Ġ} Ġreturns Ġtrue ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . body Ġ} Ġreturns Ġnull ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} Ġreturns ĠRESP ONSE _ STRING ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()?. read Text ()). isEqualTo ( RESPONSE _ STRING ) ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġget Toggle s Stream _ with Response Success () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . body Ġ} Ġreturns ĠRESP ONSE _ STRING . to ResponseBody () ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġresponse . is Successful Ġ} Ġreturns Ġtrue ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠhttpClient . new Call ( capture ( request Slot )). execute () Ġ} Ġreturns Ġresponse ĊĠĠĠĠĠĠĠ ĠassertThat ( repository . get Toggle s Stream ()?. read Text ()). isEqualTo ( RESPONSE _ STRING ) ĊĠĠĠĠĠĠĠ ĠassertThat ( request Slot . captured . url ). isEqualTo ( TO GG LES _ URL . to Http Url ()) ĊĠĠĠĠĠĠĠ Ġverify ( exact ly Ġ= Ġ 0 ) Ġ{ Ġcache . get OrNull ( CACHE _ KEY ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠInputStream . read Text () Ġ= Ġthis . reader ( Charsets . UTF _ 8 ). read Text () ĊĊĠĠĠ Ġprivate Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠTO GG LES _ URL Ġ= Ġ" https :// test host . ru / toggle s " ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠCACHE _ KEY Ġ= Ġ" toggle s _ stream " ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠRESP ONSE _ STRING Ġ= Ġ" response " ĊĠĠĠ Ġ} Ċ }