blob: 4956136644dbf0a02cb3a2bafc98722d309968e6 [file] [log] [blame]
Teruab4b01a2013-06-20 10:09:57 -07001package net.onrc.onos.ofcontroller.core.internal;
2
3import static org.easymock.EasyMock.*;
4
Pankaj Berde38646d62013-06-21 11:34:04 -07005import net.onrc.onos.graph.GraphDBConnection;
6import net.onrc.onos.graph.GraphDBOperation;
Teruab4b01a2013-06-20 10:09:57 -07007import net.onrc.onos.ofcontroller.core.ISwitchStorage;
8import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
9import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
Teruab4b01a2013-06-20 10:09:57 -070010import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
13import org.easymock.EasyMock;
14import org.junit.After;
15import org.junit.Before;
16import org.junit.Ignore;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.openflow.protocol.OFPhysicalPort;
20import org.openflow.protocol.OFPhysicalPort.OFPortState;
21import org.powermock.api.easymock.PowerMock;
22import org.powermock.core.classloader.annotations.PrepareForTest;
23import org.powermock.modules.junit4.PowerMockRunner;
24import org.slf4j.LoggerFactory;
25
26import com.thinkaurelius.titan.core.TitanFactory;
27import com.thinkaurelius.titan.core.TitanGraph;
28
29//Add Powermock preparation
30@RunWith(PowerMockRunner.class)
31@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
32public class SwitchStorageImplTest {
33
34 protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
35
36 String conf;
37 private GraphDBConnection mockConn = null;
38 private GraphDBOperation mockOpe = null;
39 private GraphDBOperation realOpe = null;
40 private TitanGraph titanGraph = null;
41 ISwitchStorage swSt = null;
42
43 @Before
44 public void setUp() throws Exception {
45
46 swSt = new SwitchStorageImpl();
47 conf = "/dummy/path/to/db";
48
49 // Make mock cassandra DB
50 // Replace TitanFactory.open() to return mock DB
51
52 PowerMock.mockStatic(GraphDBConnection.class);
53 mockConn = createMock(GraphDBConnection.class);
54 PowerMock.suppress(PowerMock.constructor(GraphDBConnection.class));
55 EasyMock.expect(GraphDBConnection.getInstance((String)EasyMock.anyObject())).andReturn(mockConn);
56 PowerMock.replay(GraphDBConnection.class);
57
58 PowerMock.mockStatic(GraphDBOperation.class);
59 mockOpe = PowerMock.createStrictMock(GraphDBOperation.class);
60 PowerMock.expectNew(GraphDBOperation.class, mockConn).andReturn(mockOpe);
61 PowerMock.replay(GraphDBOperation.class);
62 // Replace the conf to dummy conf
63 // String conf = "/tmp/cassandra.titan";
64
65
66 }
67
68 @After
69 public void tearDown() throws Exception {
70 swSt.close();
71 swSt = null;
72
73 }
74
75 /**
76 * Desc:
77 * Test method for addSwitch method.
78 * Condition:
79 * Normal
80 * Expect:
81 * Call SwitchStorageImpl.addSwitch func with proper properties.
82 */
Teruab4b01a2013-06-20 10:09:57 -070083 @Test
84 public void testAddSwitch() {
85 String dpid = "00:00:00:00:00:00:0a:07";
86 String state = "ACTIVE";
87
88 //Mock Switch
89 ISwitchObject mockISw = createMock(ISwitchObject.class);
90 mockISw.setState(state);
91 replay(mockISw);
92
93 //Expectation of mock operation.
94 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
95 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
96 mockOpe.commit();
97 mockOpe.close();
98 replay(mockOpe);
99
100 swSt.init(conf);
101 swSt.addSwitch(dpid);
102 }
103
104 /**
105 * Desc:
106 * Test method for addSwitch method.
107 * Condition:
108 * The switch is already existing.
109 * Expect:
110 * Call SwitchStorageImpl.addSwitch func with proper properties.
111 */
112 //@Ignore
113 @Test
114 public void testAddSwitchExisting() {
115 String dpid = "00:00:00:00:00:00:0a:07";
116 String state = "ACTIVE";
117
118 //Mock Switch
119 ISwitchObject mockISw = createMock(ISwitchObject.class);
120 mockISw.setState(state);
121 mockISw.setState(state);
122 replay(mockISw);
123
124 //Expectation of mock operation.
125 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
126 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
127 mockOpe.commit();
128 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
129 mockOpe.commit();
130 mockOpe.close();
131 replay(mockOpe);
132
133 swSt.init(conf);
134 swSt.addSwitch(dpid);
135 swSt.addSwitch(dpid);
136 }
137
138 /**
139 * Desc:
140 * Test method for addSwitch method.
141 * Condition:
142 * The switch construction is fail and return null
143 * Expect:
144 * Write the status as info log.
145 */
Teruab4b01a2013-06-20 10:09:57 -0700146 @Test
147 public void testAddSwitchAbnormal() {
148 String dpid = "00:00:00:00:00:00:0a:07";
149
150 //Expectation of mock operation.
151 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
152 expect(mockOpe.newSwitch(dpid)).andReturn(null);
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700153 mockOpe.rollback();
Teruab4b01a2013-06-20 10:09:57 -0700154 mockOpe.close();
155 replay(mockOpe);
156
157 swSt.init(conf);
158 swSt.addSwitch(dpid);
159 }
160
161 /**
162 * Desc:
163 * Test method for addSwitch method.
164 * Condition:
HIGUCHI Yutafabb0032013-06-28 11:31:33 -0700165 * Throw runtimeException.
Teruab4b01a2013-06-20 10:09:57 -0700166 * Expect:
167 * The rollback method is called.
168 */
169 //@Ignore
170 @Test
171 public void testAddSwitchException() {
172 String dpid = "00:00:00:00:00:00:0a:07";
173 String state = "ACTIVE";
174
175 //Mock Switch
176 ISwitchObject mockISw = createMock(ISwitchObject.class);
177 mockISw.setState(state);
178 replay(mockISw);
179
180 //Expectation of mock operation.
181 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
182 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
183 mockOpe.commit();
184 expectLastCall().andThrow(new RuntimeException());
185 mockOpe.rollback();
186 mockOpe.close();
187 replay(mockOpe);
188
189 swSt.init(conf);
190 swSt.addSwitch(dpid);
191 }
192
193 /**
194 * Desc:
195 * Test method for updateSwitch method.
196 * Condition:
197 * SwitchState : INACTIVE
198 * DMOPERATION : UPDATE
199 * Expect:
200 * Should call addSwitch function and commit.
201 */
202 //@Ignore
203 @Test
204 public void testUpdateUPDATE() {
205 String dpid = "00:00:00:00:00:00:0a:07";
206 SwitchState stateINACTIVE = SwitchState.INACTIVE;
207 DM_OPERATION opUPDATE = DM_OPERATION.UPDATE;
208
209 //Mock Switch
210 ISwitchObject mockISw = createMock(ISwitchObject.class);
211 mockISw.setState("ACTIVE");
212 mockISw.setState(stateINACTIVE.toString());
213 replay(mockISw);
214
215 //Expectation of mock operation.
216 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
217 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
218 mockOpe.commit();
219 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
220 mockOpe.commit();
221 mockOpe.close();
222 replay(mockOpe);
223
224 swSt.init(conf);
225 swSt.update(dpid, stateINACTIVE, opUPDATE);
226 }
227
228 /**
229 * Desc:
230 * Test method for updateSwitch method.
231 * Condition:
232 * SwitchState : INACTIVE
233 * DMOPERATION : CREATE
234 * Expect:
235 * Should call addSwitch function and commit.
236 */
237 //@Ignore
238 @Test
239 public void testUpdateCREATE() {
240 String dpid = "00:00:00:00:00:00:0a:07";
241 SwitchState stateINACTIVE = SwitchState.INACTIVE;
242 DM_OPERATION opCREATE = DM_OPERATION.CREATE;
243
244 //Mock Switch
245 ISwitchObject mockISw = createMock(ISwitchObject.class);
246 mockISw.setState("ACTIVE");
247 mockISw.setState(stateINACTIVE.toString());
248 replay(mockISw);
249
250 //Expectation of mock operation.
251 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
252 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
253 mockOpe.commit();
254 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
255 mockOpe.commit();
256 mockOpe.close();
257 replay(mockOpe);
258
259 swSt.init(conf);
260 swSt.update(dpid, stateINACTIVE, opCREATE);
261 }
262
263 /**
264 * Desc:
265 * Test method for updateSwitch method.
266 * Condition:
267 * SwitchState : INACTIVE
268 * DMOPERATION : INSERT
269 * Expect:
270 * Should call addSwitch function and commit.
271 */
272 //@Ignore
273 @Test
274 public void testUpdateINSERT() {
275 String dpid = "00:00:00:00:00:00:0a:07";
276 SwitchState stateINACTIVE = SwitchState.INACTIVE;
277 DM_OPERATION opINSERT = DM_OPERATION.INSERT;
278
279 //Mock Switch
280 ISwitchObject mockISw = createMock(ISwitchObject.class);
281 mockISw.setState("ACTIVE");
282 mockISw.setState(stateINACTIVE.toString());
283 replay(mockISw);
284
285 //Expectation of mock operation.
286 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
287 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
288 mockOpe.commit();
289 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
290 mockOpe.commit();
291 mockOpe.close();
292 replay(mockOpe);
293
294 swSt.init(conf);
295 swSt.update(dpid, stateINACTIVE, opINSERT);
296 }
297
298 /**
299 * Desc:
300 * Test method for updateSwitch method.
301 * Condition:
302 * SwitchState : ACTIVE
303 * DMOPERATION : DELETE
304 * Expect:
305 * Should call removeSwitch function and commit.
306 */
307 //@Ignore
308 @Test
309 public void testUpdateDELETE() {
310 String dpid = "00:00:00:00:00:00:0a:07";
311 SwitchState stateACTIVE = SwitchState.ACTIVE;
312 DM_OPERATION opDELETE = DM_OPERATION.DELETE;
313
314 //Mock Switch
315 ISwitchObject mockISw = createMock(ISwitchObject.class);
316 mockISw.setState(stateACTIVE.toString());
317 replay(mockISw);
318
319 //Expectation of mock operation.
320 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
321 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
322 mockOpe.commit();
323 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
324 mockOpe.removeSwitch(mockISw);
325 mockOpe.commit();
326 mockOpe.close();
327 replay(mockOpe);
328
329 swSt.init(conf);
330 swSt.addSwitch(dpid);
331 swSt.update(dpid, stateACTIVE, opDELETE);
332 }
333
334 /**
335 * Desc:
336 * Test method for deleteSwitch method.
337 * Condition:
338 * The switch is existing.
339 * Expect:
340 * Should call removeSwitch function and commit.
341 */
342 //@Ignore
343 @Test
344 public void testDeleteSwitch() {
345 String dpid = "00:00:00:00:00:00:0a:07";
346 String state = "ACTIVE";
347
348 //Mock Switch
349 ISwitchObject mockISw = createMock(ISwitchObject.class);
350 mockISw.setState(state);
351 replay(mockISw);
352
353 //Expectation of mock operation.
354 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
355 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
356 mockOpe.commit();
357 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
358 mockOpe.removeSwitch(mockISw);
359 mockOpe.commit();
360 mockOpe.close();
361 replay(mockOpe);
362
363 swSt.init(conf);
364 swSt.addSwitch(dpid);
365 swSt.deleteSwitch(dpid);
366
367 //Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator();
368 //assertFalse(it.hasNext());
369 }
370
371 /**
372 * Desc:
373 * Test method for deleteSwitch method.
374 * Condition:
375 * The commit func throw exception.
376 * Expect:
377 * Should call rollback.
378 */
379 //@Ignore
380 @Test
381 public void testDeleteSwitchException() {
382 String dpid = "00:00:00:00:00:00:0a:07";
383 String state = "ACTIVE";
384 String type = "";
385
386 //Mock Switch
387 ISwitchObject mockISw = createMock(ISwitchObject.class);
388 mockISw.setState(state);
389 replay(mockISw);
390
391 //Expectation of mock operation.
392 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
393 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
394 mockOpe.commit();
395 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
396 mockOpe.removeSwitch(mockISw);
397 mockOpe.commit();
398 expectLastCall().andThrow(new RuntimeException());
399 mockOpe.rollback();
400 mockOpe.close();
401 replay(mockOpe);
402
403 swSt.init(conf);
404 swSt.addSwitch(dpid);
405 swSt.deleteSwitch(dpid);
406 }
407
408 /**
409 * Desc:
410 * Test method for addPort method.
411 * Condition:
412 * port is existing.
413 * Expect:
414 * Should call addPort and commit.
415 */
416 //@Ignore
417 @Test
418 public void testAddPort() {
419 String dpid = "00:00:00:00:00:00:0a:01";
420 short portNumber = 5;
421 String state = "ACTIVE";
422 String name = "port 5 at SEA switch";
423
424 OFPhysicalPort portToAdd = new OFPhysicalPort();
425 portToAdd.setName(name);
426 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
427 portToAdd.setPortNumber(portNumber);
428 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
429
430 //Expectation of mock Port
431 IPortObject mockIPort = createMock(IPortObject.class);
432 mockIPort.setState(state);
433 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
434 mockIPort.setDesc(name);
435 replay(mockIPort);
436
437 //Expectation of mock Switch
438 ISwitchObject mockISw = createMock(ISwitchObject.class);
439 mockISw.setState(state);
440 mockISw.addPort(mockIPort);
441 replay(mockISw);
442
443 //Expectation of mock operation.
444 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
445 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
446 mockOpe.commit();
447 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
448 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700449 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700450 mockOpe.commit();
451 mockOpe.close();
452 replay(mockOpe);
453
454 swSt.init(conf);
455 swSt.addSwitch(dpid);
456 swSt.addPort(dpid, portToAdd);
457 }
458
459 /**
460 * Desc:
461 * Test method for addPort method.
462 * Condition:
463 * Port status is down.
464 * Expect:
465 * Should call removePort and commit.
466 */
467 //@Ignore
468 @Test
469 public void testAddPortWithPortLinkDown() {
470 String dpid = "00:00:00:00:00:00:0a:01";
471 short portNumber = 5;
472 String state = "ACTIVE";
473 String name = "port 5 at SEA switch";
474
475 OFPhysicalPort portToAdd = new OFPhysicalPort();
476 portToAdd.setName(name);
477 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
478 portToAdd.setPortNumber(portNumber);
479 portToAdd.setState(OFPortState.OFPPS_LINK_DOWN.getValue());
480
481 //Expectation of mock Port
482 IPortObject mockIPort = createMock(IPortObject.class);
483 mockIPort.setState(state);
484 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
485 mockIPort.setDesc(name);
486 replay(mockIPort);
487
488 //Expectation of mock Switch
489 ISwitchObject mockISw = createMock(ISwitchObject.class);
490 mockISw.setState(state);
491 mockISw.removePort(mockIPort);
492 replay(mockISw);
493
494 //Expectation of mock operation.
495 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
496 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
497 mockOpe.commit();
498 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
499 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
500 mockOpe.removePort(mockIPort);
501 mockOpe.commit();
502 mockOpe.close();
503 replay(mockOpe);
504
505 swSt.init(conf);
506 swSt.addSwitch(dpid);
507 swSt.addPort(dpid, portToAdd);
508 }
509
510 /**
511 * Desc:
512 * Test method for addPort method.
513 * Condition:
514 * The switch is not existing.
515 * Expect:
516 * Nothing happens.
517 */
518 //@Ignore
519 @Test
520 public void testAddPortAbnormalNoSwitch() {
521 String dpid = "00:00:00:00:00:00:0a:01";
522 short portNumber = 5;
523 String state = "ACTIVE";
524 String name = "port 5 at SEA switch";
525
526 OFPhysicalPort portToAdd = new OFPhysicalPort();
527 portToAdd.setName(name);
528 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
529 portToAdd.setPortNumber(portNumber);
530 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
531
532 //Expectation of mock Port
533 IPortObject mockIPort = createStrictMock(IPortObject.class);
534 replay(mockIPort);
535
536 //Expectation of mock Switch
537 ISwitchObject mockISw = createStrictMock(ISwitchObject.class);
538 replay(mockISw);
539
540 //Expectation of mock operation.
541 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
542 mockOpe.close();
543 replay(mockOpe);
544
545 swSt.init(conf);
546 swSt.addPort(dpid, portToAdd);
547 }
548
549 /**
550 * Desc:
551 * Test method for addPort method.
552 * Condition:
553 * port is not existing.
554 * Expect:
555 * Should call addPort and commit.
556 */
557 //@Ignore
558 @Test
559 public void testAddPortAbnormalNoPort() {
560 String dpid = "00:00:00:00:00:00:0a:01";
561 short portNumber = 5;
562 String state = "ACTIVE";
563 String name = "port 5 at SEA switch";
564
565 OFPhysicalPort portToAdd = new OFPhysicalPort();
566 portToAdd.setName(name);
567 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
568 portToAdd.setPortNumber(portNumber);
569 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
570
571 //Expectation of mock Port
572 IPortObject mockIPort = createMock(IPortObject.class);
573 mockIPort.setState(state);
574 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
575 mockIPort.setDesc(name);
576 replay(mockIPort);
577
578 //Expectation of mock Switch
579 ISwitchObject mockISw = createMock(ISwitchObject.class);
580 mockISw.setState(state);
581 mockISw.addPort(mockIPort);
582 replay(mockISw);
583
584 //Expectation of mock operation.
585 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
586 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
587 mockOpe.commit();
588 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
589 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700590 expect(mockOpe.newPort(dpid, portNumber)).andReturn(null);
Teruab4b01a2013-06-20 10:09:57 -0700591 mockOpe.rollback();
592 mockOpe.close();
593 replay(mockOpe);
594
595 swSt.init(conf);
596 swSt.addSwitch(dpid);
597 swSt.addPort(dpid, portToAdd);
598 }
599
600 /**
601 * Desc:
602 * Test method for addPort method.
603 * Condition:
604 * commit throw the exception.
605 * Expect:
606 * Should call rollback.
607 */
608 //@Ignore
609 @Test
610 public void testAddPortWithException() {
611 String dpid = "00:00:00:00:00:00:0a:01";
612 short portNumber = 5;
613 String state = "ACTIVE";
614 String name = "port 5 at SEA switch";
615
616 OFPhysicalPort portToAdd = new OFPhysicalPort();
617 portToAdd.setName(name);
618 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
619 portToAdd.setPortNumber(portNumber);
620 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
621
622 //Expectation of mock Port
623 IPortObject mockIPort = createMock(IPortObject.class);
624 mockIPort.setState(state);
625 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
626 mockIPort.setDesc(name);
627 replay(mockIPort);
628
629 //Expectation of mock Switch
630 ISwitchObject mockISw = createMock(ISwitchObject.class);
631 mockISw.setState(state);
632 mockISw.addPort(mockIPort);
633 replay(mockISw);
634
635 //Expectation of mock operation.
636 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
637 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
638 mockOpe.commit();
639 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
640 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700641 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700642 mockOpe.commit();
643 expectLastCall().andThrow(new RuntimeException());
644 mockOpe.rollback();
645 mockOpe.close();
646 replay(mockOpe);
647
648 swSt.init(conf);
649 swSt.addSwitch(dpid);
650 swSt.addPort(dpid, portToAdd);
651 }
652
653 /**
654 * Desc:
655 * Test method for deletePort method.
656 * Condition:
657 * port is existing.
658 * Expect:
659 * Should call removePort and commit.
660 */
661 //@Ignore
662 @Test
663 public void testDeletePort() {
664 String dpid = "00:00:00:00:00:00:0a:01";
665 short portNumber = 5;
666 String state = "ACTIVE";
667 String name = "port 5 at SEA switch";
668
669 OFPhysicalPort portToAdd = new OFPhysicalPort();
670 portToAdd.setName(name);
671 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
672 portToAdd.setPortNumber(portNumber);
673 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
674
675 //Expectation of mock Port
676 IPortObject mockIPort = createMock(IPortObject.class);
677 mockIPort.setState(state);
678 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
679 mockIPort.setDesc(name);
680 replay(mockIPort);
681
682 //Expectation of mock Switch
683 ISwitchObject mockISw = createMock(ISwitchObject.class);
684 mockISw.setState(state);
685 mockISw.addPort(mockIPort);
686 mockISw.removePort(mockIPort);
687 replay(mockISw);
688
689 //Expectation of mock operation.
690 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
691 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
692 mockOpe.commit();
693 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
694 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700695 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700696 mockOpe.commit();
697 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
698 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
699 mockOpe.removePort(mockIPort);
700 mockOpe.commit();
701 mockOpe.close();
702 replay(mockOpe);
703
704 swSt.init(conf);
705 swSt.addSwitch(dpid);
706 swSt.addPort(dpid, portToAdd);
707 swSt.deletePort(dpid, portNumber);
708 }
709
710 /**
711 * Desc:
712 * Test method for addPort method.
713 * Condition:
714 * commit throws the exception.
715 * Expect:
716 * Should call rollback.
717 */
718 //@Ignore
719 @Test
720 public void testDeletePortException() {
721 String dpid = "00:00:00:00:00:00:0a:01";
722 short portNumber = 5;
723 String state = "ACTIVE";
724 String name = "port 5 at SEA switch";
725
726 OFPhysicalPort portToAdd = new OFPhysicalPort();
727 portToAdd.setName(name);
728 portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue());
729 portToAdd.setPortNumber(portNumber);
730 portToAdd.setState(OFPortState.OFPPS_STP_FORWARD.getValue());
731
732 //Expectation of mock Port
733 IPortObject mockIPort = createMock(IPortObject.class);
734 mockIPort.setState(state);
735 mockIPort.setPortState(OFPortState.OFPPS_STP_FORWARD.getValue());
736 mockIPort.setDesc(name);
737 replay(mockIPort);
738
739 //Expectation of mock Switch
740 ISwitchObject mockISw = createMock(ISwitchObject.class);
741 mockISw.setState(state);
742 mockISw.addPort(mockIPort);
743 mockISw.removePort(mockIPort);
744 replay(mockISw);
745
746 //Expectation of mock operation.
747 expect(mockOpe.searchSwitch(dpid)).andReturn(null);
748 expect(mockOpe.newSwitch(dpid)).andReturn(mockISw);
749 mockOpe.commit();
750 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
751 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(null);
Pankaj Berdebbd38612013-06-22 05:59:12 -0700752 expect(mockOpe.newPort(dpid, portNumber)).andReturn(mockIPort);
Teruab4b01a2013-06-20 10:09:57 -0700753 mockOpe.commit();
754 expect(mockOpe.searchSwitch(dpid)).andReturn(mockISw);
755 expect(mockOpe.searchPort(dpid, portNumber)).andReturn(mockIPort);
756 mockOpe.removePort(mockIPort);
757 expectLastCall().andThrow(new RuntimeException());
758 mockOpe.rollback();
759 mockOpe.close();
760 replay(mockOpe);
761
762 swSt.init(conf);
763 swSt.addSwitch(dpid);
764 swSt.addPort(dpid, portToAdd);
765 swSt.deletePort(dpid, portNumber);
766 }
767}