blob: 90eb68dd7a242b05df41ac13bbb6a4081aaa4190 [file] [log] [blame]
Naoki Shiota25007aa2013-06-25 17:06:45 -07001package net.floodlightcontroller.core.internal;
2
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
Naoki Shiota25007aa2013-06-25 17:06:45 -07005
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.List;
10
11import org.easymock.EasyMock;
12import org.easymock.IAnswer;
13import org.junit.After;
14import org.junit.Before;
15import org.junit.Test;
16
17// Extends Controller class to access protected inner class
18public class RoleChangeCallbackTest extends Controller {
19 @Before
20 public void setUp() throws Exception {
21 }
22
23 @After
24 public void tearDown() throws Exception {
25 }
26
27 /**
28 * Test if {@link RoleChangeCallback#controlChanged(long, boolean)} correctly calls {@link RoleChanger#submitRequest(Collection, net.floodlightcontroller.core.IFloodlightProviderService.Role)}
29 * when connectedSwitch is not empty.
30 * @throws Exception
31 */
32 @SuppressWarnings("unchecked")
33 @Test
34 public void testNormalSwitches() throws Exception {
35 Long [] dpids = new Long [] { 1000L, 1001L, 1002L, 1003L };
36 final long dpidExist = 1000L;
37 final long dpidNotExist = 2000L;
38
39 roleChanger = EasyMock.createMock(RoleChanger.class);
40
41 // First call will be called with (dpidExist,true)
42 roleChanger.submitRequest(EasyMock.anyObject(Collection.class), EasyMock.anyObject(Role.class));
43 EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
44 @Override
45 public Object answer() throws Throwable {
46 Collection<OFSwitchImpl> switches = (Collection<OFSwitchImpl>)EasyMock.getCurrentArguments()[0];
47 Role role = (Role)EasyMock.getCurrentArguments()[1];
48
49 List<Long> dpids = new ArrayList<Long>();
50
51 for(OFSwitchImpl sw : switches) {
52 dpids.add(sw.getId());
53 }
54 assertTrue(dpids.contains(dpidExist));
55 assertEquals(role, Role.MASTER);
56
57 return null;
58 }
59 }).once();
60
61 // Second call will be called with (dpidExist,false)
62 roleChanger.submitRequest(EasyMock.anyObject(Collection.class), EasyMock.anyObject(Role.class));
63 EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
64 @Override
65 public Object answer() throws Throwable {
66 Collection<OFSwitchImpl> switches = (Collection<OFSwitchImpl>)EasyMock.getCurrentArguments()[0];
67 Role role = (Role)EasyMock.getCurrentArguments()[1];
68
69 List<Long> dpids = new ArrayList<Long>();
70
71 for(OFSwitchImpl sw : switches) {
72 dpids.add(sw.getId());
73 }
74 assertTrue(dpids.contains(dpidExist));
75 assertEquals(role, Role.SLAVE);
76
77 return null;
78 }
79 }).once();
80
81 EasyMock.replay(roleChanger);
82
83 initNetwork(roleChanger, dpids);
84
85 RoleChangeCallback callback = new RoleChangeCallback();
86 callback.controlChanged(dpidExist, true);
87 callback.controlChanged(dpidExist, false);
88 callback.controlChanged(dpidNotExist, true);
89 callback.controlChanged(dpidNotExist, false);
90
91 EasyMock.verify(roleChanger);
92 }
93
94 /**
95 * Test if {@link RoleChangeCallback#controlChanged(long, boolean)} doesn't call RoleChanger methods
96 * when connectedSwitch is empty.
97 * @throws Exception
98 */
99 @Test
100 public void testEmptySwitches() throws Exception {
101 Long [] dpids = new Long [] {};
102 final long dpidToTest = 1000L;
103
104 roleChanger = EasyMock.createMock(RoleChanger.class);
105 // roleChanger methods must not be used
106 EasyMock.replay(roleChanger);
107
108 initNetwork(roleChanger, dpids);
109
110 RoleChangeCallback callback = new RoleChangeCallback();
111 callback.controlChanged(dpidToTest, true);
112 callback.controlChanged(dpidToTest, false);
113
114 EasyMock.verify(roleChanger);
115 }
116
117 /**
118 * Create mock OFSwitchImpl object.
119 * @param id
120 * @return
121 */
122 private OFSwitchImpl createOFSwitchImplMock(Long id) {
123 OFSwitchImpl sw = EasyMock.createMock(OFSwitchImpl.class);
124
125 EasyMock.expect(sw.getId()).andReturn(id).anyTimes();
126 EasyMock.replay(sw);
127
128 return sw;
129 }
130
131 /**
132 * Setup connectedSwitches
133 * @param changer
134 * @param ids
135 * @throws Exception
136 */
137 private void initNetwork(RoleChanger changer, Long [] ids) throws Exception {
138 connectedSwitches = new HashSet<OFSwitchImpl>();
139
140 for(Long id : ids) {
141 connectedSwitches.add(createOFSwitchImplMock(id));
142 }
143 }
144}