blob: 6e707caa0ee28c619268175234f491dc62f3087c [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 {
Ray Milkey269ffb92014-04-03 14:43:30 -070019 @Before
20 public void setUp() throws Exception {
21 }
Naoki Shiota25007aa2013-06-25 17:06:45 -070022
Ray Milkey269ffb92014-04-03 14:43:30 -070023 @After
24 public void tearDown() throws Exception {
25 }
Naoki Shiota25007aa2013-06-25 17:06:45 -070026
Ray Milkey269ffb92014-04-03 14:43:30 -070027 /**
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 *
31 * @throws Exception
32 */
33 @SuppressWarnings("unchecked")
34 @Test
35 public void testNormalSwitches() throws Exception {
36 Long[] dpids = new Long[]{1000L, 1001L, 1002L, 1003L};
37 final long dpidExist = 1000L;
38 final long dpidNotExist = 2000L;
Naoki Shiota25007aa2013-06-25 17:06:45 -070039
Ray Milkey269ffb92014-04-03 14:43:30 -070040 roleChanger = EasyMock.createMock(RoleChanger.class);
Naoki Shiota25007aa2013-06-25 17:06:45 -070041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 // First call will be called with (dpidExist,true)
43 roleChanger.submitRequest(EasyMock.anyObject(Collection.class), EasyMock.anyObject(Role.class));
44 EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
45 @Override
46 public Object answer() throws Throwable {
47 Collection<OFSwitchImpl> switches = (Collection<OFSwitchImpl>) EasyMock.getCurrentArguments()[0];
48 Role role = (Role) EasyMock.getCurrentArguments()[1];
Naoki Shiota25007aa2013-06-25 17:06:45 -070049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 List<Long> dpids = new ArrayList<Long>();
Naoki Shiota25007aa2013-06-25 17:06:45 -070051
Ray Milkey269ffb92014-04-03 14:43:30 -070052 for (OFSwitchImpl sw : switches) {
53 dpids.add(sw.getId());
54 }
55 assertTrue(dpids.contains(dpidExist));
56 assertEquals(role, Role.MASTER);
Naoki Shiota25007aa2013-06-25 17:06:45 -070057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 return null;
59 }
60 }).once();
Naoki Shiota25007aa2013-06-25 17:06:45 -070061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 // Second call will be called with (dpidExist,false)
63 roleChanger.submitRequest(EasyMock.anyObject(Collection.class), EasyMock.anyObject(Role.class));
64 EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
65 @Override
66 public Object answer() throws Throwable {
67 Collection<OFSwitchImpl> switches = (Collection<OFSwitchImpl>) EasyMock.getCurrentArguments()[0];
68 Role role = (Role) EasyMock.getCurrentArguments()[1];
69
70 List<Long> dpids = new ArrayList<Long>();
71
72 for (OFSwitchImpl sw : switches) {
73 dpids.add(sw.getId());
74 }
75 assertTrue(dpids.contains(dpidExist));
76 assertEquals(role, Role.SLAVE);
77
78 return null;
79 }
80 }).once();
81
82 EasyMock.replay(roleChanger);
83
84 initNetwork(roleChanger, dpids);
85
86 RoleChangeCallback callback = new RoleChangeCallback();
87 callback.controlChanged(dpidExist, true);
88 callback.controlChanged(dpidExist, false);
89 callback.controlChanged(dpidNotExist, true);
90 callback.controlChanged(dpidNotExist, false);
91
92 EasyMock.verify(roleChanger);
93 }
94
95 /**
96 * Test if {@link RoleChangeCallback#controlChanged(long, boolean)} doesn't call RoleChanger methods
97 * when connectedSwitch is empty.
98 *
99 * @throws Exception
100 */
101 @Test
102 public void testEmptySwitches() throws Exception {
103 Long[] dpids = new Long[]{};
104 final long dpidToTest = 1000L;
105
106 roleChanger = EasyMock.createMock(RoleChanger.class);
107 // roleChanger methods must not be used
108 EasyMock.replay(roleChanger);
109
110 initNetwork(roleChanger, dpids);
111
112 RoleChangeCallback callback = new RoleChangeCallback();
113 callback.controlChanged(dpidToTest, true);
114 callback.controlChanged(dpidToTest, false);
115
116 EasyMock.verify(roleChanger);
117 }
118
119 /**
120 * Create mock OFSwitchImpl object.
121 *
122 * @param id
123 * @return
124 */
125 private OFSwitchImpl createOFSwitchImplMock(Long id) {
126 OFSwitchImpl sw = EasyMock.createMock(OFSwitchImpl.class);
127
128 EasyMock.expect(sw.getId()).andReturn(id).anyTimes();
129 EasyMock.replay(sw);
130
131 return sw;
132 }
133
134 /**
135 * Setup connectedSwitches
136 *
137 * @param changer
138 * @param ids
139 * @throws Exception
140 */
141 private void initNetwork(RoleChanger changer, Long[] ids) throws Exception {
142 connectedSwitches = new HashSet<OFSwitchImpl>();
143
144 for (Long id : ids) {
145 connectedSwitches.add(createOFSwitchImplMock(id));
146 }
147 }
Naoki Shiota25007aa2013-06-25 17:06:45 -0700148}