package Ġcom . sw iss chain . matching . engine . services . validator Ċ Ċ import Ġcom . sw iss chain . matching . engine . Abstract Test . Companion . DEFAULT _ BRO KER Ċ import Ġcom . sw iss chain . matching . engine . config . Test ApplicationContext Ċ import Ġcom . sw iss chain . matching . engine . da os . Fee Type Ċ import Ġcom . sw iss chain . matching . engine . da os . Limit Order Ċ import Ġcom . sw iss chain . matching . engine . da os . Market Order Ċ import Ġcom . sw iss chain . matching . engine . da os . fee . v 2 . New Fee Instruction Ċ import Ġcom . sw iss chain . matching . engine . da os . setting . Available Setting Group Ċ import Ġcom . sw iss chain . matching . engine . database . Test Di ctionaries Database Accessor Ċ import Ġcom . sw iss chain . matching . engine . database . Test Settings Database Accessor Ċ import Ġcom . sw iss chain . matching . engine . messages . incoming . Incoming Messages Ċ import Ġcom . sw iss chain . matching . engine . order . Order Status Ċ import Ġcom . sw iss chain . matching . engine . outgoing . messages . v 2 . create Protobuf Timestamp Builder Ċ import Ġcom . sw iss chain . matching . engine . services . Asset Order Book Ċ import Ġcom . sw iss chain . matching . engine . services . validators . Market Order Validator Ċ import Ġcom . sw iss chain . matching . engine . services . validators . impl . Order ValidationException Ċ import Ġcom . sw iss chain . matching . engine . utils . Di ctionaries Init Ċ import Ġcom . sw iss chain . matching . engine . utils . get Setting Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . boot . test . context . Test Configuration Ċ import Ġorg . springframework . context . annotation . Bean Ċ import Ġorg . springframework . context . annotation . Primary Ċ import Ġorg . springframework . test . annotation . Dir ties Context Ċ import Ġorg . springframework . test . context . junit 4 . Spring Runner Ċ import Ġjava . math . BigDecimal Ċ import Ġjava . util . Date Ċ import Ġjava . util . UUID Ċ import Ġjava . util . concurrent . Priority BlockingQueue Ċ import Ġkotlin . test . assertEquals Ċ Ċ @ RunWith ( Spring Runner :: class ) Ċ @ SpringBootTest ( classes Ġ= Ġ[( Test ApplicationContext :: class ), Ġ( Market Order Validator Test . Config :: class )]) Ċ @ Dir ties Context ( class Mode Ġ= ĠD irt ies Context . Class Mode . AFTER _ EACH _ TEST _ METHOD ) Ċ class ĠMarket Order Validator Test Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠCLIENT _ NAME Ġ= Ġ 0 L ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠOPER ATION _ ID Ġ= Ġ" test " ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠAS SET _ PAIR _ ID Ġ= Ġ" EUR USD " ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠBASE _ AS SET _ ID Ġ= Ġ" EUR " ĊĠĠĠĠĠĠĠ Ġconst Ġval ĠQU OT ING _ AS SET _ ID Ġ= Ġ" USD " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test Configuration ĊĠĠĠ Ġclass ĠConfig Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ Bean ĊĠĠĠĠĠĠĠ Ġfun Ġtest Di ctionaries Database Accessor (): ĠTest Di ctionaries Database Accessor Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtest Di ctionaries Database Accessor Ġ= ĠTest Di ctionaries Database Accessor () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtest Di ctionaries Database Accessor . add Asset ( Di ctionaries Init . create Asset ( BASE _ AS SET _ ID , Ġ 2 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtest Di ctionaries Database Accessor . add Asset ( Di ctionaries Init . create Asset ( QUOT ING _ AS SET _ ID , Ġ 2 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtest Di ctionaries Database Accessor . add Asset ( Di ctionaries Init . create Asset (" BTC ", Ġ 2 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtest Di ctionaries Database Accessor . add Asset Pair ( Di ctionaries Init . create Asset Pair ( ĠAS SET _ PAIR _ ID , ĠBASE _ AS SET _ ID , ĠQU OT ING _ AS SET _ ID , Ġ 2 , ĠBigDecimal . valueOf ( 0 . 9 ))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġtest Di ctionaries Database Accessor ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġ@ Bean ĊĠĠĠĠĠĠĠ Ġ@ Primary ĊĠĠĠĠĠĠĠ Ġfun Ġtest (): ĠTest Settings Database Accessor Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtest Settings Database Accessor Ġ= ĠTest Settings Database Accessor () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtest Settings Database Accessor . create OrUpdate Setting ( Available Setting Group . DISABLED _ AS SETS , Ġget Setting (" BTC ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġtest Settings Database Accessor ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmarket Order Validator : ĠMarket Order Validator ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġtest Di ctionaries Database Accessor : ĠTest Di ctionaries Database Accessor ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġtest Unknown Asset () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġmarket Order Builder . asset Pair Id Ġ= Ġ" BTC USD " ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġget Order Book ( order . is Buy Side ()), listOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . Unknown Asset , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġtest Asset Disabled () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġmarket Order Builder . asset Pair Id Ġ= Ġ" BTC USD " ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĠĠĠĠĠĠĠ Ġtest Di ctionaries Database Accessor . add Asset Pair ( Di ctionaries Init . create Asset Pair ( Ġ" BTC USD ", Ġ" BTC ", Ġ" USD ", Ġ 2 )) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġget Order Book ( order . is Buy Side ()), ĠlistOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . Disabled Asset , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġtest Invalid Volume () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġmarket Order Builder . volume Ġ= Ġ" 0 . 1 " ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġ Ġget Order Book ( order . is Buy Side ()), ĠlistOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . Too Small Volume , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġtest Invalid Fee () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġget Order Book ( order . is Buy Side ()), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( ĠNew Fee Instruction . create ( get Invalid Fee ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . Invalid Fee , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġinvalid Order Book () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġ ĠAsset Order Book ( DEFAULT _ BRO KER , ĠAS SET _ PAIR _ ID ). getOrder Book ( order . is Buy Side ()), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . No Li quidity , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġtest Invalid Volume Accuracy () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġmarket Order Builder . volume Ġ= Ġ" 1 . 1 1 1 1 " ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġget Order Book ( order . is Buy Side ()), ĠlistOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . Invalid Volume Accuracy , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ( expected Ġ= ĠOrder ValidationException :: class ) ĊĠĠĠ Ġfun Ġtest Invalid Price Accuracy () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĠĠĠĠĠĠĠ Ġorder . price Ġ= ĠBigDecimal . valueOf ( 1 . 1 1 1 1 ) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġget Order Book ( order . is Buy Side ()), ĠlistOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠOrder ValidationException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertEquals ( Order Status . Invalid Price Accuracy , Ġe . order Status ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow Ġe ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Valid Data () Ġ{ ĊĠĠĠĠĠĠĠ Ġ// given ĊĠĠĠĠĠĠĠ Ġval Ġmarket Order Builder Ġ= ĠgetDefault Market Order Builder () ĊĠĠĠĠĠĠĠ Ġval Ġorder Ġ= Ġto Market Order ( market Order Builder . build ()) ĊĊĠĠĠĠĠĠĠ Ġ// when ĊĠĠĠĠĠĠĠ Ġmarket Order Validator . perform Validation ( order , Ġget Order Book ( order . is Buy Side ()), ĠlistOf ( ĠNew Fee Instruction . create ( get Fee Instruction ()))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġto Market Order ( message : ĠIncoming Messages . Market Order ): ĠMarket Order Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnow Ġ= ĠDate () ĊĠĠĠĠĠĠĠ Ġreturn ĠMarket Order ( UUID . randomUUID (). toString (), Ġmessage . id , Ġmessage . asset Pair Id , ĠDEFAULT _ BRO KER , Ġmessage . wallet Id , Ġmessage . wallet Id , ĠBigDecimal ( message . volume ), Ġnull , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠOrder Status . Processing . name , Ġnow , ĠDate ( message . timestamp . seconds ), Ġnow , Ġnull , Ġtrue , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnull , ĠlistOf ( New Fee Instruction . create ( message . fees List . first ()))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġget Order Book ( is Buy : ĠBoolean ): ĠPriority BlockingQueue < Limit Order > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġasset Order Book Ġ= ĠAsset Order Book ( DEFAULT _ BRO KER , ĠAS SET _ PAIR _ ID ) ĊĠĠĠĠĠĠĠ Ġval Ġnow Ġ= ĠDate () ĊĠĠĠĠĠĠĠ Ġasset Order Book . add Order ( Limit Order (" test ", Ġ" test ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠAS SET _ PAIR _ ID , ĠDEFAULT _ BRO KER , ĠCLIENT _ NAME , ĠCLIENT _ NAME , ĠBigDecimal . valueOf ( 1 . 0 ), ĠBigDecimal . valueOf ( 1 . 0 ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠOrder Status . In Order Book . name , Ġnow , Ġnow , Ġnow , ĠBigDecimal . valueOf ( 1 . 0 ), Ġnow , ĠBigDecimal . valueOf ( 1 . 0 ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnull , Ġnull , Ġnull , Ġnull , Ġnull , Ġnull , Ġnull , Ġnull , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnull , Ġnull , Ġnull )) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġasset Order Book . getOrder Book ( is Buy ) ĊĠĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠgetDefault Market Order Builder (): ĠIncoming Messages . Market Order . Builder Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠIncoming Messages . Market Order . newBuilder () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. setId ( OPERATION _ ID ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Asset Pair Id (" EUR USD ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Timestamp ( Date (). create Protobuf Timestamp Builder ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Wallet Id ( CLIENT _ NAME ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Volume (" 1 . 0 ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. add Fees ( get Fee Instruction ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġget Fee Instruction (): ĠIncoming Messages . Fee Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġ ĠIncoming Messages . Fee . newBuilder () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. setType ( Fee Type . NO _ F EE . external Id ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġget Invalid Fee (): ĠIncoming Messages . Fee Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġ ĠIncoming Messages . Fee . newBuilder () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. setType ( Fee Type . EXTERNAL _ F EE . external Id ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠ Ġ} Ċ }