package Ġcom . ap ol log raph ql . apollo . subscription Ċ Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġokhttp 3 . Protocol Ċ import Ġokhttp 3 . Request Ċ import Ġokhttp 3 . Response Ċ import Ġokhttp 3 . WebSocket Ċ import Ġokhttp 3 . WebSocket Listener Ċ import Ġok io . ByteString Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġjava . util . concurrent . atomic . Atomic Boolean Ċ import Ġjava . util . concurrent . atomic . Atomic Reference Ċ Ċ class ĠWebSocket Subscription Transport Test Ġ{ ĊĠ Ġprivate Ġlateinit Ġvar Ġweb Socket Request : ĠRequest ĊĠ Ġprivate Ġlateinit Ġvar Ġweb Socket Factory : ĠMock WebSocket Factory ĊĠ Ġprivate Ġlateinit Ġvar Ġsubscription Transport : ĠWebSocket Subscription Transport ĊĠĠĊĠ Ġ@ Before ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġweb Socket Request Ġ= ĠRequest . Builder (). url (" wss :// localhost "). build () ĊĠĠĠ Ġweb Socket Factory Ġ= ĠMock WebSocket Factory () ĊĠĠĠ Ġval Ġfactory Ġ= ĠWebSocket Subscription Transport . Factory (" wss :// localhost /", Ġweb Socket Factory ) ĊĠĠĠ Ġsubscription Transport Ġ= Ġfactory . create ( object Ġ: ĠSubscription Transport . Callback Ġ{ ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Connected () Ġ{} ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Failure ( t : ĠThrowable ) Ġ{} ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Message ( message : ĠOperation Server Message ) Ġ{} ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Closed () Ġ{} ĊĠĠĠ Ġ}) Ġas ĠWebSocket Subscription Transport ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġconnect () Ġ{ ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket . get ()). isNull () ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket Listener . get ()). isNull () ĊĠĠĠ Ġsubscription Transport . connect () ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket . get ()). isNotNull () ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket Listener . get ()). isNotNull () ĊĠĠĠ ĠassertThat ( web Socket Factory . request . header (" Sec - WebSocket - Protocol ")). isEqualTo (" graphql - ws ") ĊĠĠĠ ĠassertThat ( web Socket Factory . request . header (" Cookie ")). isEqualTo ("") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġdisconnect () Ġ{ ĊĠĠĠ Ġsubscription Transport . connect () ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket . get ()). isNotNull () ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket Listener . get ()). isNotNull () ĊĠĠĠ Ġsubscription Transport . disconnect ( Operation Client Message . Terminate ()) ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket . get ()). isNull () ĊĠĠĠ ĠassertThat ( subscription Transport . web Socket Listener . get ()). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsend () Ġ{ ĊĠĠĠ Ġval Ġcallback Failure Ġ= ĠAtomic Reference < Throwable ?> () ĊĠĠĠ Ġsubscription Transport Ġ= ĠWebSocket Subscription Transport ( web Socket Request , Ġweb Socket Factory , Ġobject Ġ: ĠSubscription Transport . Callback Ġ{ ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Connected () Ġ{} ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Failure ( t : ĠThrowable ) Ġ{ ĊĠĠĠĠĠĠĠ Ġcallback Failure . set ( t ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Message ( message : ĠOperation Server Message ) Ġ{} ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Closed () Ġ{} ĊĠĠĠ Ġ}) ĊĠĠĠ Ġsubscription Transport . send ( Operation Client Message . Init ( empty Map < String , ĠAny > ())) ĊĠĠĠ ĠassertThat ( callback Failure . get ()). isInstanceOf ( IllegalStateException :: class . java ) ĊĠĠĠ Ġcallback Failure . set ( null ) ĊĠĠĠ Ġsubscription Transport . connect () ĊĠĠĠ Ġsubscription Transport . send ( Operation Client Message . Init ( empty Map < String , ĠAny > ())) ĊĠĠĠ ĠassertThat ( callback Failure . get ()). isNull () ĊĠĠĠ Ġsubscription Transport . disconnect ( Operation Client Message . Terminate ()) ĊĠĠĠ Ġsubscription Transport . send ( Operation Client Message . Init ( empty Map < String , ĠAny > ())) ĊĠĠĠ ĠassertThat ( callback Failure . get ()). isInstanceOf ( IllegalStateException :: class . java ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsubscription Transport Callback () Ġ{ ĊĠĠĠ Ġval Ġcallback Connected Ġ= ĠAtomic Boolean () ĊĠĠĠ Ġval Ġcallback Failure Ġ= ĠAtomic Reference < Throwable >() ĊĠĠĠ Ġval Ġcallback Message Ġ= ĠAtomic Reference < Operation Server Message >() ĊĠĠĠ Ġsubscription Transport Ġ= ĠWebSocket Subscription Transport ( web Socket Request , Ġweb Socket Factory , Ġobject Ġ: ĠSubscription Transport . Callback Ġ{ ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Connected () Ġ{ ĊĠĠĠĠĠĠĠ Ġcallback Connected . set ( true ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Failure ( t : ĠThrowable ) Ġ{ ĊĠĠĠĠĠĠĠ Ġcallback Failure . set ( t ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Message ( message : ĠOperation Server Message ) Ġ{ ĊĠĠĠĠĠĠĠ Ġcallback Message . set ( message ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Closed () Ġ{} ĊĠĠĠ Ġ}) ĊĠĠĠ Ġsubscription Transport . connect () ĊĠĠĠ Ġweb Socket Factory . web Socket . listener . on Message ( web Socket Factory . web Socket , Ġ"{\" type \":\" connection _ ack \" }") ĊĠĠĠ Ġweb Socket Factory . web Socket . listener . on Failure ( web Socket Factory . web Socket , ĠUnsupportedOperationException (), Ġnull ) ĊĠĠĠ ĠassertThat ( callback Connected . get ()). isTrue () ĊĠĠĠ ĠassertThat ( callback Message . get ()). isInstanceOf ( Operation Server Message . Connection Ack nowledge :: class . java ) ĊĠĠĠ ĠassertThat ( callback Failure . get ()). isInstanceOf ( Unsupported OperationException :: class . java ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsubscription Transport Closed Callback () Ġ{ ĊĠĠĠ Ġval Ġcallback Connected Ġ= ĠAtomic Boolean () ĊĠĠĠ Ġval Ġcallback Closed Ġ= ĠAtomic Boolean () ĊĠĠĠ Ġsubscription Transport Ġ= ĠWebSocket Subscription Transport ( web Socket Request , Ġweb Socket Factory , Ġobject Ġ: ĠSubscription Transport . Callback Ġ{ ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Connected () Ġ{ ĊĠĠĠĠĠĠĠ Ġcallback Connected . set ( true ) ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Failure ( t : ĠThrowable ) Ġ{ ĊĠĠĠĠĠĠĠ Ġthrow ĠUnsupportedOperationException (" Unexpected ") ĊĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠ Ġoverride Ġfun Ġon Message ( message : ĠOperation Server Message ) Ġ{} ĊĠĠĠĠĠ Ġoverride Ġfun Ġon Closed () Ġ{ ĊĠĠĠĠĠĠĠ Ġcallback Closed . set ( true ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ}) ĊĠĠĠ Ġsubscription Transport . connect () ĊĠĠĠ Ġweb Socket Factory . web Socket . listener . on Closed ( web Socket Factory . web Socket , Ġ 1 0 0 1 , Ġ"") ĊĠĠĠ ĠassertThat ( callback Connected . get ()). isTrue () ĊĠĠĠ ĠassertThat ( callback Closed . get ()). isTrue () ĊĠ Ġ} ĊĊĠ Ġprivate Ġclass ĠMock WebSocket Factory Ġ: ĠWebSocket . Factory Ġ{ ĊĠĠĠ Ġlateinit Ġvar Ġrequest : ĠRequest ĊĠĠĠ Ġlateinit Ġvar Ġweb Socket : ĠMock WebSocket ĊĊĠĠĠ Ġoverride Ġfun Ġnew WebSocket ( request : ĠRequest , Ġlistener : ĠWebSocket Listener ): ĠWebSocket Ġ{ ĊĠĠĠĠĠ Ġthis . request Ġ= Ġrequest ĊĠĠĠĠĠ Ġreturn ĠMock WebSocket ( request , Ġlistener ). also Ġ{ Ġweb Socket Ġ= Ġit Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġclass ĠMock WebSocket ( val Ġrequest : ĠRequest , Ġval Ġlistener : ĠWebSocket Listener ) Ġ: ĠWebSocket Ġ{ ĊĠĠĠ Ġvar Ġlast Sent Message : ĠString ? Ġ= Ġnull ĊĠĠĠ Ġvar Ġclosed Ġ= Ġfalse ĊĊĠĠĠ Ġoverride Ġfun Ġrequest (): ĠRequest Ġ{ ĊĠĠĠĠĠ Ġreturn Ġrequest ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġqueue Size (): ĠLong Ġ= Ġthrow ĠUnsupportedOperationException () ĊĊĠĠĠ Ġoverride Ġfun Ġsend ( text : ĠString ): ĠBoolean Ġ{ ĊĠĠĠĠĠ Ġlast Sent Message Ġ= Ġtext ĊĠĠĠĠĠ Ġreturn Ġtrue ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġsend ( bytes : ĠByte String ): ĠBoolean Ġ{ ĊĠĠĠĠĠ Ġthrow ĠUnsupportedOperationException () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġclose ( code : ĠInt , Ġreason : ĠString ?): ĠBoolean Ġ{ ĊĠĠĠĠĠ Ġclosed Ġ= Ġtrue ĊĠĠĠĠĠ Ġreturn Ġtrue ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġcancel () Ġ{ ĊĠĠĠĠĠ Ġthrow ĠUnsupportedOperationException () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠ Ġlistener . on Open ( this , ĠResponse . Builder () ĊĠĠĠĠĠĠĠĠĠ Ġ. request ( request ) ĊĠĠĠĠĠĠĠĠĠ Ġ. protocol ( Protocol . HTTP _ 1 _ 0 ) ĊĠĠĠĠĠĠĠĠĠ Ġ. code ( 2 0 0 ) ĊĠĠĠĠĠĠĠĠĠ Ġ. message (" Ok ") ĊĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ }