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