package Ġorg . rap ture main . currency converter Ċ Ċ import Ġcom . fasterxml . jackson . databind . ObjectMapper Ċ import Ġorg . assertj . core . api . With Assertions Ċ import Ġorg . hamcrest . core . String Contains Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . rap ture main . currency converter . model . Currency Ċ import Ġorg . rap ture main . currency converter . service . Currency Converter ApiService Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . http . HttpMethod Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . MediaType Ċ import Ġorg . springframework . test . web . client . Expected Count Ċ import Ġorg . springframework . test . web . client . Mock Rest ServiceServer Ċ import Ġorg . springframework . test . web . client . Simple Request Expectation Manager Ċ import Ġorg . springframework . test . web . client . match . Mock Rest Request Matchers . method Ċ import Ġorg . springframework . test . web . client . match . Mock Rest Request Matchers . request To Ċ import Ġorg . springframework . test . web . client . response . Mock Rest Response Creators . with Status Ċ import Ġorg . springframework . web . client . Rest Template Ċ import Ġjava . math . BigDecimal Ċ Ċ @ SpringBootTest Ċ class ĠCurrency Converter ApiService Test Ġ: ĠWith Assertions Ġ{ ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġapi Service : ĠCurrency Converter ApiService ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġrest Template : ĠRest Template ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmock Server : ĠMock Rest ServiceServer ĊĠĠĠ Ġprivate Ġval ĠobjectMapper Ġ= ĠObjectMapper () ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun Ġinit (){ ĊĠĠĠĠĠĠĠ Ġmock Server Ġ= ĠMock Rest ServiceServer . bind To ( rest Template ). build ( Simple Request Expectation Manager ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` this Ġtest Ġchecks Ġcache Ġof Ġavailable Ġc urrencies `() Ġ{ ĊĠĠĠĠĠĠĠ Ġmock C urrencies ( mock Server , ĠlistOf (" 1 ", Ġ" 2 ", Ġ" 3 ")) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . is Currency Available (" 1 ")). isTrue ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . is Currency Available (" 2 ")). isTrue ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . is Currency Available (" 3 ")). isTrue ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . is Currency Available (" 4 ")). isFalse ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . is Currency Available (" 5 ")). isFalse ĊĊĠĠĠĠĠĠĠ Ġmock Server . verify () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` this Ġtest Ġchecks Ġcurrency Ġexchange Ġrate Ġgetting Ġand Ġcaching `() Ġ{ ĊĠĠĠĠĠĠĠ Ġmock Exchange Rate ( mock Server , Ġ" 1 _ 2 ", Ġ 1 0 . 0 ) ĊĠĠĠĠĠĠĠ Ġmock Exchange Rate ( mock Server , Ġ" 2 _ 1 ", Ġ 0 . 1 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . getC urrency Exchange Rate ( Currency (" 1 "), ĠCurrency (" 2 ")) ). isEqualTo ( BigDecimal (" 1 0 . 0 ")) ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . getC urrency Exchange Rate ( Currency (" 1 "), ĠCurrency (" 2 ")) ). isEqualTo ( BigDecimal (" 1 0 . 0 ")) ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . getC urrency Exchange Rate ( Currency (" 2 "), ĠCurrency (" 1 ")) ). isEqualTo ( BigDecimal (" 0 . 1 ")) ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . getC urrency Exchange Rate ( Currency (" 2 "), ĠCurrency (" 1 ")) ). isEqualTo ( BigDecimal (" 0 . 1 ")) ĊĠĠĠĠĠĠĠ ĠassertThat ( api Service . getC urrency Exchange Rate ( Currency (" 1 "), ĠCurrency (" 4 ")) ). isNull () ĊĊĠĠĠĠĠĠĠ Ġmock Server . verify () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġmock C urrencies ( mock Server : ĠMock Rest ServiceServer , Ġc urrencies : ĠList < String >) Ġ{ ĊĠĠĠĠĠĠĠ Ġmock Server . expect ( Expected Count . once (), Ġrequest To ( String Contains (" currencies "))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( method ( HttpMethod . GET )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. and Respond ( with Status ( HttpStatus . OK ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. contentType ( MediaType . APPLICATION _ JSON ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. body ( object Mapper . writeValue AsString ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" results " Ġto Ġmap Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ* currencies . map Ġ{ Ġit Ġto Ġmap Of < String , ĠString >() Ġ}. to Typed Array () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġmock Exchange Rate ( mock Server : ĠMock Rest ServiceServer , Ġcurrency : ĠString , Ġrate : ĠDouble ) Ġ{ ĊĠĠĠĠĠĠĠ Ġmock Server . expect ( Expected Count . once (), Ġrequest To ( String Contains ( currency ))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( method ( HttpMethod . GET )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. and Respond ( with Status ( HttpStatus . OK ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. contentType ( MediaType . APPLICATION _ JSON ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. body ( object Mapper . writeValue AsString ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcurrency Ġto Ġrate ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} Ċ } Ċ