22
33import static org .junit .Assert .assertEquals ;
44
5+ import java .util .Objects ;
56import java .util .concurrent .CountDownLatch ;
67import java .util .concurrent .TimeUnit ;
7- import java .util .concurrent .atomic .AtomicBoolean ;
88import java .util .concurrent .atomic .AtomicInteger ;
9- import java .util .concurrent .atomic .AtomicReference ;
109
10+ import com .openfin .desktop .channel .*;
1111import org .json .JSONObject ;
1212import org .junit .AfterClass ;
1313import org .junit .BeforeClass ;
14- import org .junit .Ignore ;
1514import org .junit .Test ;
1615import org .slf4j .Logger ;
1716import org .slf4j .LoggerFactory ;
1817
19- import com .openfin .desktop .channel .ChannelAction ;
20- import com .openfin .desktop .channel .ChannelClient ;
21- import com .openfin .desktop .channel .ChannelListener ;
22- import com .openfin .desktop .channel .ChannelProvider ;
23- import com .openfin .desktop .channel .ConnectionEvent ;
24- import com .openfin .desktop .channel .Middleware ;
25-
2618/**
2719 * JUnit tests for com.openfin.desktop.InterApplicationBus class
2820 *
@@ -54,15 +46,25 @@ public static void teardown() throws Exception {
5446 @ Test
5547 public void createChannelProvider () throws Exception {
5648 CountDownLatch latch = new CountDownLatch (1 );
57- desktopConnection .getChannel ("createChannelProviderTest " ).create (new AsyncCallback <ChannelProvider >() {
49+ desktopConnection .getChannel ("createChannelProvider " ).create (new AsyncCallback <ChannelProvider >() {
5850 @ Override
5951 public void onSuccess (ChannelProvider provider ) {
6052 latch .countDown ();
6153 }
6254 });
63-
6455 latch .await (10 , TimeUnit .SECONDS );
56+ assertEquals (0 , latch .getCount ());
57+ }
6558
59+ @ Test
60+ public void createChannelProviderAsync () throws Exception {
61+ CountDownLatch latch = new CountDownLatch (1 );
62+ desktopConnection .getChannel ("createChannelProviderAsync" ).createAsync ().thenAccept (provider -> {
63+ if (Objects .nonNull (provider )) {
64+ latch .countDown ();
65+ }
66+ });
67+ latch .await (10 , TimeUnit .SECONDS );
6668 assertEquals (0 , latch .getCount ());
6769 }
6870
@@ -81,12 +83,24 @@ public void onSuccess(ChannelClient result) {
8183 });
8284 }
8385 });
84-
8586 latch .await (10 , TimeUnit .SECONDS );
86-
8787 assertEquals (0 , latch .getCount ());
8888 }
89-
89+ @ Test
90+ public void createChannelClientAsync () throws Exception {
91+ CountDownLatch latch = new CountDownLatch (1 );
92+ final String channelName = "createChannelClientAsync" ;
93+ desktopConnection .getChannel (channelName ).createAsync ().thenAccept (provider -> {
94+ desktopConnection .getChannel (channelName ).connectAsync ().thenAccept (client -> {
95+ if (Objects .nonNull (client )) {
96+ latch .countDown ();
97+ }
98+ });
99+ });
100+ latch .await (10 , TimeUnit .SECONDS );
101+ assertEquals (0 , latch .getCount ());
102+ }
103+
90104 @ Test
91105 public void multipleChannelClients () throws Exception {
92106 CountDownLatch latch1 = new CountDownLatch (1 );
@@ -97,9 +111,7 @@ public void multipleChannelClients() throws Exception {
97111 desktopConnection .getChannel (channelName ).create (new AsyncCallback <ChannelProvider >() {
98112 @ Override
99113 public void onSuccess (ChannelProvider provider ) {
100-
101114 provider .register (providerActionName , new ChannelAction () {
102-
103115 @ Override
104116 public JSONObject invoke (String action , Object payload , JSONObject senderIdentity ) {
105117 return null ;
@@ -111,7 +123,6 @@ public JSONObject invoke(String action, Object payload, JSONObject senderIdentit
111123 @ Override
112124 public void onSuccess (ChannelClient client ) {
113125 client .dispatch (providerActionName , new JSONObject (), null );
114-
115126 client .register (clientActionName , new ChannelAction () {
116127 @ Override
117128 public JSONObject invoke (String action , Object payload , JSONObject senderIdentity ) {
@@ -132,7 +143,6 @@ public JSONObject invoke(String action, Object payload, JSONObject senderIdentit
132143 return null ;
133144 }
134145 });
135-
136146 provider .publish (clientActionName , new JSONObject (), null );
137147 }
138148 });
@@ -298,34 +308,23 @@ public JSONObject invoke(String action, Object payload, JSONObject senderIdentit
298308 public void connectionListener () throws Exception {
299309 final String channelName = "connectionListenerTest" ;
300310 CountDownLatch latch = new CountDownLatch (2 );
301-
302- desktopConnection .getChannel (channelName ).create (new AsyncCallback <ChannelProvider >() {
303- @ Override
304- public void onSuccess (ChannelProvider provider ) {
305- desktopConnection .getChannel (channelName ).addChannelListener (new ChannelListener () {
306- @ Override
307- public void onChannelConnect (ConnectionEvent connectionEvent ) {
308- latch .countDown ();
309- }
310-
311- @ Override
312- public void onChannelDisconnect (ConnectionEvent connectionEvent ) {
313- latch .countDown ();
314- }
315- });
316-
317- desktopConnection .getChannel (channelName ).connect (new AsyncCallback <ChannelClient >() {
318- @ Override
319- public void onSuccess (ChannelClient client ) {
320- client .disconnect (null );
321- }
322-
323- });
324- }
311+ desktopConnection .getChannel (channelName ).createAsync ().thenAccept (provider -> {
312+ provider .addProviderListener (new ChannelProviderListener () {
313+ @ Override
314+ public void onClientConnect (ChannelClientConnectEvent connectionEvent ) throws Exception {
315+ latch .countDown ();
316+ }
317+ @ Override
318+ public void onClientDisconnect (ChannelClientConnectEvent connectionEvent ) {
319+ latch .countDown ();
320+ }
321+ });
322+
323+ desktopConnection .getChannel (channelName ).connectAsync ().thenAccept (client -> {
324+ client .disconnect (null );
325+ });
325326 });
326-
327327 latch .await (10 , TimeUnit .SECONDS );
328-
329328 assertEquals (0 , latch .getCount ());
330329 }
331330
@@ -390,4 +389,38 @@ public void onError(Ack ack) {
390389 assertEquals (initValue + 3 , resultValue .get ());
391390 }
392391
392+ @ Test
393+ public void rejectConnection () throws Exception {
394+ final String channelName = "rejectConnectionTest" ;
395+ final String payloadValue = "reject me" ;
396+ final String rejectReason = "not allowed" ;
397+
398+ CountDownLatch latch = new CountDownLatch (1 );
399+
400+ desktopConnection .getChannel (channelName ).createAsync ().thenAccept (provider -> {
401+ provider .addProviderListener (new ChannelProviderListener () {
402+ @ Override
403+ public void onClientConnect (ChannelClientConnectEvent connectionEvent ) throws Exception {
404+ String payload = (String ) connectionEvent .getPayload ();
405+ if (payloadValue .equals (payload )) {
406+ throw new Exception (rejectReason );
407+ }
408+ }
409+ @ Override
410+ public void onClientDisconnect (ChannelClientConnectEvent connectionEvent ) {
411+ }
412+ });
413+
414+ desktopConnection .getChannel (channelName ).connectAsync (payloadValue ).thenAccept (client -> {
415+ }).exceptionally (ex -> {
416+ if (ex .getMessage ().contains (rejectReason )) {
417+ latch .countDown ();
418+ }
419+ return null ;
420+ });
421+ });
422+
423+ latch .await (10 , TimeUnit .SECONDS );
424+ assertEquals (0 , latch .getCount ());
425+ }
393426}
0 commit comments