blob: 880335b22e3f3d58c785b4fb51a50f88dec87721 [file] [log] [blame]
Teru68076a42013-06-27 14:57:49 -07001package net.onrc.onos.ofcontroller.core;
2
3import static org.junit.Assert.*;
4
5import java.util.HashMap;
6
7import net.onrc.onos.graph.GraphDBConnection;
8import net.onrc.onos.graph.GraphDBOperation;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Teru68076a42013-06-27 14:57:49 -070010import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
12import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
13import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
14import org.easymock.EasyMock;
15import org.junit.After;
16import org.junit.Before;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.powermock.api.easymock.PowerMock;
20import org.powermock.core.classloader.annotations.PrepareForTest;
21import org.slf4j.LoggerFactory;
22import org.powermock.modules.junit4.PowerMockRunner;
23
24import com.thinkaurelius.titan.core.TitanFactory;
25import com.thinkaurelius.titan.core.TitanGraph;
26
27//Add Powermock preparation
28@RunWith(PowerMockRunner.class)
29@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
30public class INetMapTopologyObjectsIDeviceObjectTest {
31
32 //The test network is ./titan/schema/test-network.xml
33
34 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
35
36 String conf;
37 private GraphDBConnection conn = null;
38 private GraphDBOperation ope = null;
39 private TitanGraph titanGraph = null;
40
41 @Before
42 public void setUp() throws Exception {
43 conf = "/dummy/path/to/db";
44
45 // Make mock cassandra DB
46 // Replace TitanFactory.open() to return mock DB
47 TestDatabaseManager.deleteTestDatabase();
48 titanGraph = TestDatabaseManager.getTestDatabase();
49 //TestDatabaseManager.populateTestData(titanGraph);
50 PowerMock.mockStatic(TitanFactory.class);
51 EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph);
52 PowerMock.replay(TitanFactory.class);
53
54 conn = GraphDBConnection.getInstance(conf);
55 ope = new GraphDBOperation(conn);
56 }
57
58 @After
59 public void tearDown() throws Exception {
60 titanGraph.shutdown();
61 TestDatabaseManager.deleteTestDatabase();
62 }
63
64 /**
65 * Desc:
66 * Test method for get and set MacAddress method.
67 * Condition:
68 * N/A
69 * Expect:
70 * 1. Should set the mac address.
71 * 2. Should get the mac address.
72 */
73 @Test
74 public void testSetGetMacAddress() {
75 String macaddr = "00:00:00:00:00:00:0a:07";
76 IDeviceObject devObj = ope.newDevice();
77 devObj.setMACAddress(macaddr);
78 assertEquals(devObj.getMACAddress(), macaddr);
79 }
80
81 /**
82 * Desc:
83 * Test method for get and set IPAddress method.
84 * Condition:
85 * N/A
86 * Expect:
87 * 1. Should set the ip address.
88 * 2. Should get the ip address.
89 */
90 @Test
91 public void testSetGetIPAddress() {
92 String ipaddr = "192.168.0.1";
93 IDeviceObject devObj = ope.newDevice();
94 devObj.setIPAddress(ipaddr);
95 assertEquals(devObj.getIPAddress(), ipaddr);
96 }
97
98 /**
99 * Desc:
100 * Test method for get attached port.
101 * Condition:
102 * N/A
103 * Expect:
104 * 1. Should get the attached ports.
105 */
106 @Test
107 public void testGetAttachedPort() {
108 String dpid = "00:00:00:00:00:00:0a:07";
109 Short number = 1;
110 Short number2 = 2;
111 IPortObject portObj = ope.newPort(dpid, number);
112 IPortObject portObj2 = ope.newPort(dpid, number2);
113
Teru68076a42013-06-27 14:57:49 -0700114 IDeviceObject devObj = ope.newDevice();
115
116 portObj.setDevice(devObj);
117 portObj2.setDevice(devObj);
118
119 HashMap<Short, IPortObject> portObjectList = new HashMap<Short, IPortObject>();
120 for(IPortObject port : devObj.getAttachedPorts())
121 {
122 portObjectList.put(port.getNumber(), port);
123 }
124
125 assertTrue(portObjectList.containsValue(portObj));
126 assertTrue(portObjectList.containsValue(portObj2));
127
128 }
129
130 /**
131 * Desc:
132 * Test method for set and remove host port method.
133 * Condition:
134 * N/A
135 * Expect:
136 * 1. Should set host port from the device.
137 * 2. Should remove host port from the device.
138 */
139 @Test
140 public void testSetRemoveHostPort() {
141 String dpid = "00:00:00:00:00:00:0a:07";
142 Short number = 1;
Teru68076a42013-06-27 14:57:49 -0700143 IPortObject portObj = ope.newPort(dpid, number);
Teru68076a42013-06-27 14:57:49 -0700144
Teru68076a42013-06-27 14:57:49 -0700145 IDeviceObject devObj = ope.newDevice();
146
147 devObj.setHostPort(portObj);
148
149 HashMap<String, IDeviceObject> portObjectList = new HashMap<String, IDeviceObject>();
150 for(IDeviceObject dev : portObj.getDevices())
151 {
152 portObjectList.put(dev.getMACAddress(), dev);
153 }
154 assertTrue(portObjectList.containsValue(devObj));
155
156 devObj.removeHostPort(portObj);
157
158 HashMap<String, IDeviceObject> portObjectList2 = new HashMap<String, IDeviceObject>();
159 for(IDeviceObject dev : portObj.getDevices())
160 {
161 portObjectList2.put(dev.getMACAddress(), dev);
162 }
163 assertTrue(!portObjectList2.containsValue(devObj));
164 }
165
166 /**
167 * Desc:
168 * Test method for getSwitch method.
169 * Condition:
170 * N/A
171 * Expect:
172 * 1. Should get the switch connected to the device.
173 */
174 @Test
175 public void testGetSwitches() {
176 String dpid = "00:00:00:00:00:00:0a:07";
177 String dpid2 = "00:00:00:00:00:00:0a:08";
178 Short number = 1;
179 Short number2 = 2;
180 ISwitchObject swObj = ope.newSwitch(dpid);
181 ISwitchObject swObj2 = ope.newSwitch(dpid2);
182 IPortObject portObj = ope.newPort(dpid, number);
183 IPortObject portObj2 = ope.newPort(dpid2, number2);
184 swObj.addPort(portObj);
185 swObj2.addPort(portObj2);
186 IDeviceObject devObj = ope.newDevice();
187 portObj.setDevice(devObj);
188 portObj2.setDevice(devObj);
189
190 HashMap<String, ISwitchObject> switchObjectList = new HashMap<String, ISwitchObject>();
191 for(ISwitchObject sw : devObj.getSwitch())
192 {
193 switchObjectList.put(sw.getDPID(), sw);
194 }
195 assertTrue(switchObjectList.containsValue(swObj));
196 assertTrue(switchObjectList.containsValue(swObj2));
197 }
198
199
200}