package Ġcom . m ct ech . domain . interaction . auth Ċ Ċ import Ġcom . m ct ech . domain . errors . Auth Exception Ċ import Ġcom . m ct ech . domain . interaction . Result Ċ import Ġcom . m ct ech . domain . model . Auth Request Ċ import Ġcom . m ct ech . domain . model . User Ċ import Ġcom . m ct ech . domain . services . Auth Service Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ Ċ /** Ċ Ġ* Ġ@ author ĠMAY CON ĠCAR DO SO Ġon Ġ 2 0 1 9 - 0 9 - 2 2 . Ċ Ġ*/ Ċ @ Experimental Cor outines Api Ċ class ĠAuthentication UseCase Test Ġ{ ĊĠĠĠ Ġprivate Ġval Ġauth Service Ġ= Ġmock < Auth Service >() ĊĠĠĠ Ġprivate Ġval Ġauth Request Ġ= Ġmock < Auth Request >() ĊĠĠĠ Ġprivate Ġval Ġexpected User Ġ= ĠUser ( name Ġ= Ġ"", Ġemail Ġ= Ġ"") ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġauth Session UseCase : ĠAuthentication UseCase ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġ` before Ġeach Ġtest `() Ġ{ ĊĠĠĠĠĠĠĠ Ġauth Session UseCase Ġ= ĠAuthentication UseCase ( auth Service ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġcall Ġrequest Ġvalidation `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġauth Session UseCase . execute ( auth Request ) ĊĠĠĠĠĠĠĠ Ġverify ( auth Request ). validate OrThrow () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġsign Ġin `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġwhenever ( auth Service . login ( auth Request )). thenReturn ( true ) ĊĠĠĠĠĠĠĠ Ġwhenever ( auth Service . fetch Logged User ()). thenReturn ( expected User ) ĊĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġauth Session UseCase . execute ( auth Request ) ĊĠĠĠĠĠĠĠ Ġval Ġresult User Ġ= Ġ( result Ġas ĠResult . Success < User >). result ĊĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isInstanceOf ( Result . Success :: class . java ) ĊĠĠĠĠĠĠĠ ĠassertThat ( result User ). isEqualTo ( expected User ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġfail Ġthrowing Ġauth Ġexception `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġwhenever ( auth Service . login ( any ())). then Throw ( Auth Exception . Wrong Credentials Exception ) ĊĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġauth Session UseCase . execute ( auth Request ) ĊĠĠĠĠĠĠĠ Ġval Ġresult Exception Ġ= Ġ( result Ġas ĠResult . Failure ). throwable ĊĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isInstanceOf ( Result . Failure :: class . java ) ĊĠĠĠĠĠĠĠ ĠassertThat ( result Exception ). isEqualTo ( Auth Exception . Wrong Credentials Exception ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġfail Ġwith Ġunknown Ġexception Ġwhen Ġany Ġunknown Ġproblem Ġhappen `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġwhenever ( auth Service . login ( any ())). thenReturn ( false ) ĊĠĠĠĠĠĠĠ Ġwhenever ( auth Service . fetch Logged User ()). thenReturn ( expected User ) ĊĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġauth Session UseCase . execute ( auth Request ) ĊĠĠĠĠĠĠĠ Ġval Ġresult Exception Ġ= Ġ( result Ġas ĠResult . Failure ). throwable ĊĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isInstanceOf ( Result . Failure :: class . java ) ĊĠĠĠĠĠĠĠ ĠassertThat ( result Exception ). isEqualTo ( Auth Exception . Unknown Auth Exception ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` should Ġfail Ġmapping Ġunknown Ġexception `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġwhenever ( auth Service . login ( any ())). then Throw ( IllegalArgumentException ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġauth Session UseCase . execute ( auth Request ) ĊĠĠĠĠĠĠĠ Ġval Ġresult Exception Ġ= Ġ( result Ġas ĠResult . Failure ). throwable ĊĊĠĠĠĠĠĠĠ ĠassertThat ( result ). isInstanceOf ( Result . Failure :: class . java ) ĊĠĠĠĠĠĠĠ ĠassertThat ( result Exception ). isEqualTo ( Auth Exception . Unknown Auth Exception ) ĊĠĠĠ Ġ} Ċ }