Implementation for enabling remote rpc calls between 2 instances of md-sal
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / test / java / org / opendaylight / controller / sal / connector / remoterpc / SocketManagerTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7
8 package org.opendaylight.controller.sal.connector.remoterpc;
9
10 import com.google.common.base.Optional;
11 import junit.framework.Assert;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.zeromq.ZMQ;
15 import org.opendaylight.controller.sal.connector.remoterpc.SocketManager;
16 import org.opendaylight.controller.sal.connector.remoterpc.RpcSocket;
17 import org.opendaylight.controller.sal.connector.remoterpc.Context;
18 import org.junit.Test;
19
20 public class SocketManagerTest {
21
22   SocketManager manager;
23
24   @Before
25   public void setup(){
26     manager = new SocketManager();
27   }
28
29   @After
30   public void tearDown() throws Exception {
31     manager.close();
32   }
33
34   @Test
35   public void getManagedSockets_When2NewAdded_ShouldContain2() throws Exception {
36
37     //Prepare data
38     manager.getManagedSocket("tcp://localhost:5554");
39     manager.getManagedSocket("tcp://localhost:5555");
40
41     Assert.assertTrue( 2 == manager.getManagedSockets().size());
42   }
43
44   @Test
45   public void getManagedSockets_When2NewAddedAnd1Existing_ShouldContain2() throws Exception {
46
47     //Prepare data
48     manager.getManagedSocket("tcp://localhost:5554");
49     manager.getManagedSocket("tcp://localhost:5555");
50     manager.getManagedSocket("tcp://localhost:5554"); //ask for the first one
51
52     Assert.assertTrue( 2 == manager.getManagedSockets().size());
53   }
54
55   @Test
56   public void getManagedSocket_WhenPassedAValidAddress_ShouldReturnARpcSocket() throws Exception {
57     String testAddress = "tcp://localhost:5554";
58     RpcSocket rpcSocket = manager.getManagedSocket(testAddress);
59     Assert.assertEquals(testAddress, rpcSocket.getAddress());
60   }
61
62   @Test(expected = IllegalArgumentException.class)
63   public void getManagedSocket_WhenPassedInvalidHostAddress_ShouldThrow() throws Exception {
64     String testAddress = "tcp://nonexistenthost:5554";
65     RpcSocket rpcSocket = manager.getManagedSocket(testAddress);
66   }
67
68   @Test(expected = IllegalArgumentException.class)
69   public void getManagedSocket_WhenPassedInvalidAddress_ShouldThrow() throws Exception {
70     String testAddress = "xxx";
71     RpcSocket rpcSocket = manager.getManagedSocket(testAddress);
72   }
73
74   @Test
75   public void getManagedSocket_WhenPassedAValidZmqSocket_ShouldReturnARpcSocket() throws Exception {
76     //Prepare data
77     String firstAddress = "tcp://localhost:5554";
78     RpcSocket firstRpcSocket = manager.getManagedSocket(firstAddress);
79     ZMQ.Socket firstZmqSocket = firstRpcSocket.getSocket();
80
81     String secondAddress = "tcp://localhost:5555";
82     RpcSocket secondRpcSocket = manager.getManagedSocket(secondAddress);
83     ZMQ.Socket secondZmqSocket = secondRpcSocket.getSocket();
84
85     Assert.assertEquals(firstRpcSocket, manager.getManagedSocketFor(firstZmqSocket).get());
86     Assert.assertEquals(secondRpcSocket, manager.getManagedSocketFor(secondZmqSocket).get());
87   }
88
89   @Test
90   public void getManagedSocket_WhenPassedNonManagedZmqSocket_ShouldReturnNone() throws Exception {
91     ZMQ.Socket nonManagedSocket = Context.getInstance().getZmqContext().socket(ZMQ.REQ);
92     nonManagedSocket.connect("tcp://localhost:5000");
93
94     //Prepare data
95     String firstAddress = "tcp://localhost:5554";
96     RpcSocket firstRpcSocket = manager.getManagedSocket(firstAddress);
97     ZMQ.Socket firstZmqSocket = firstRpcSocket.getSocket();
98
99     Assert.assertSame(Optional.<RpcSocket>absent(), manager.getManagedSocketFor(nonManagedSocket) );
100     Assert.assertSame(Optional.<RpcSocket>absent(), manager.getManagedSocketFor(null) );
101   }
102
103   @Test
104   public void stop_WhenCalled_ShouldEmptyManagedSockets() throws Exception {
105     manager.getManagedSocket("tcp://localhost:5554");
106     manager.getManagedSocket("tcp://localhost:5555");
107     Assert.assertTrue( 2 == manager.getManagedSockets().size());
108
109     manager.close();
110     Assert.assertTrue( 0 == manager.getManagedSockets().size());
111   }
112
113   @Test
114   public void poller_WhenCalled_ShouldReturnAnInstanceOfPoller() throws Exception {
115     Assert.assertTrue (manager.getPoller() instanceof ZMQ.Poller);
116   }
117
118 }