* @throws Exception
*/
@Test
public void testInjectMessage() throws Exception {
FloodlightContext cntx = new FloodlightContext();
IOFSwitch sw = createMock(IOFSwitch.class);
expect(sw.getId()).andReturn(0L).anyTimes();
expect(sw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes();
// Add listeners
IOFMessageListener test1 = createMock(IOFMessageListener.class);
expect(test1.getName()).andReturn("test1").anyTimes();
setupListenerOrdering(test1);
IOFMessageListener test2 = createMock(IOFMessageListener.class);
expect(test2.getName()).andReturn("test2").anyTimes();
test2.isCallbackOrderingPostreq(OFType.PACKET_IN, "test1");
expectLastCall().andReturn(true).atLeastOnce();
setupListenerOrdering(test2);
replay(test1, test2);
controller.addOFMessageListener(OFType.PACKET_IN, test1);
controller.addOFMessageListener(OFType.PACKET_IN, test2);
verify(test1);
verify(test2);
// Test inject with null switch and no message. Should not work.
reset(test1, test2);
replay(test1, test2, sw);
try {
controller.injectOfMessage(null, pi);
fail("InjectOfMessage should have thrown a NPE");
} catch (NullPointerException e) {
// expected
}
try {
controller.injectOfMessage(null, pi, cntx);
fail("InjectOfMessage should have thrown a NPE");
} catch (NullPointerException e) {
// expected
}
try {
controller.injectOfMessage(sw, null);
fail("InjectOfMessage should have thrown a NPE");
} catch (NullPointerException e) {
// expected
}
try {
controller.injectOfMessage(sw, null, cntx);
fail("InjectOfMessage should have thrown a NPE");
} catch (NullPointerException e) {
// expected
}
verify(test1);
verify(test2);
verify(sw);
//
// Test inject with inActive switch. Should not work.
reset(test1, test2, sw);
expect(sw.getId()).andReturn(0L).anyTimes();
expect(sw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes();
expect(sw.isActive()).andReturn(false).atLeastOnce();
replay(test1, test2, sw);
assertFalse("Inject should have failed",
controller.injectOfMessage(sw, pi));
assertFalse("Inject should have failed",
controller.injectOfMessage(sw, pi, cntx));
verify(test1);
verify(test2);
verify(sw);
// Test inject in the "normal" case without context
reset(test1, test2, sw);
expect(sw.getId()).andReturn(0L).anyTimes();
expect(sw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes();
expect(sw.isActive()).andReturn(true).atLeastOnce();
expect(test2.receive(same(sw), same(pi) , isA(FloodlightContext.class)))
.andReturn(Command.STOP);
// test1 will not receive any message!
replay(test1, test2, sw);
assertTrue("Inject should have worked",
controller.injectOfMessage(sw, pi));
verify(test1);
verify(test2);
verify(sw);
// Test inject in the "normal" case with context
reset(test1, test2, sw);
expect(sw.getId()).andReturn(0L).anyTimes();
expect(sw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes();
expect(sw.isActive()).andReturn(true).atLeastOnce();
expect(test2.receive(same(sw), same(pi) , same(cntx)))
.andReturn(Command.STOP);
// test1 will not receive any message!
replay(test1, test2, sw);
assertTrue("Inject should have worked",