@Test
 public void testSetRoleNPE() {
   try {
     controller.setRole(null, "");
     fail("Should have thrown an Exception");
   } catch (NullPointerException e) {
     // exptected
   }
   try {
     controller.setRole(HARole.ACTIVE, null);
     fail("Should have thrown an Exception");
   } catch (NullPointerException e) {
     // exptected
   }
 }
  @Test
  public void testHandleMessagesSlave() throws Exception {
    doSetUp(HARole.STANDBY);
    IOFSwitch sw = createMock(IOFSwitch.class);
    expect(sw.getId()).andReturn(DatapathId.NONE).anyTimes();

    IOFMessageListener test1 = createMock(IOFMessageListener.class);
    expect(test1.getName()).andReturn("test1").atLeastOnce();
    expect(test1.isCallbackOrderingPrereq((OFType) anyObject(), (String) anyObject()))
        .andReturn(false)
        .atLeastOnce();
    expect(test1.isCallbackOrderingPostreq((OFType) anyObject(), (String) anyObject()))
        .andReturn(false)
        .atLeastOnce();

    replay(test1, sw);
    controller.addOFMessageListener(OFType.PACKET_IN, test1);
    // message should not be dispatched
    controller.handleMessage(sw, pi, null);
    verify(test1);

    // ---------------------------------
    // transition to Master
    // --------------------------------
    controller.setRole(HARole.ACTIVE, "FooBar");

    // transitioned but HA listeneres not yet notified.
    // message should not be dispatched
    reset(test1);
    replay(test1);
    controller.handleMessage(sw, pi, null);
    verify(test1);

    // notify HA listeners
    controller.processUpdateQueueForTesting();
    // no message should be dispatched
    reset(test1);
    expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.STOP);
    replay(test1);
    controller.handleMessage(sw, pi, null);
    verify(test1);

    verify(sw);
  }
  /** Test other setRole cases: re-setting role to the current role, setting role to equal, etc. */
  @Test
  public void testSetRoleOthercases() throws Exception {
    doSetUp(HARole.STANDBY);

    // Create and add the HA listener
    IHAListener listener = createMock(IHAListener.class);
    expect(listener.getName()).andReturn("foo").anyTimes();
    setupListenerOrdering(listener);
    replay(listener);
    controller.addHAListener(listener);

    // Set role to slave again. Nothing should happen
    controller.setRole(HARole.STANDBY, "FooBar");
    controller.processUpdateQueueForTesting();
    verify(listener);

    reset(listener);
    expect(listener.getName()).andReturn("foo").anyTimes();
    listener.transitionToActive();
    expectLastCall().once();
    replay(listener);
  }
  /**
   * Start as SLAVE then set role to MASTER Tests normal role change transition. Check that
   * connected channels receive a setRole request
   */
  @Test
  public void testSetRole() throws Exception {
    doSetUp(HARole.STANDBY);
    RoleInfo info = controller.getRoleInfo();
    assertEquals(HARole.STANDBY, info.getRole());
    assertEquals(HARole.STANDBY, controller.getRole());

    OFSwitchHandshakeHandler h = createMock(OFSwitchHandshakeHandler.class);

    // Reassert the role.
    reset(h);
    h.sendRoleRequestIfNotPending(OFControllerRole.ROLE_SLAVE);
    replay(h);
    controller.reassertRole(h, HARole.STANDBY);
    verify(h);

    // reassert a different role: no-op
    reset(h);
    replay(h);
    controller.reassertRole(h, HARole.ACTIVE);
    verify(h);

    IHAListener listener = createMock(IHAListener.class);
    expect(listener.getName()).andReturn("foo").anyTimes();
    setupListenerOrdering(listener);
    listener.transitionToActive();
    expectLastCall().once();
    replay(listener);
    controller.addHAListener(listener);
    controller.setRole(HARole.ACTIVE, "FooBar");
    controller.processUpdateQueueForTesting();
    verify(listener);
    info = controller.getRoleInfo();
    assertEquals(HARole.ACTIVE, info.getRole());
    assertEquals("FooBar", info.getRoleChangeDescription());
    assertEquals(HARole.ACTIVE, controller.getRole());
  }