@Test
  public void testClose() {
    ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
    InetSocketAddress mockRemoteAddress = mock(InetSocketAddress.class);
    when(mockedConnectionAdapter.getRemoteAddress()).thenReturn(mockRemoteAddress);
    when(connectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);

    NodeId dummyNodeId = new NodeId("dummyNodeId");
    when(deviceState.getNodeId()).thenReturn(dummyNodeId);

    ConnectionContext mockedAuxiliaryConnectionContext = prepareConnectionContext();
    deviceContext.addAuxiliaryConenctionContext(mockedAuxiliaryConnectionContext);
    DeviceContextClosedHandler mockedDeviceContextClosedHandler =
        mock(DeviceContextClosedHandler.class);
    deviceContext.addDeviceContextClosedHandler(mockedDeviceContextClosedHandler);
    deviceContext.close();
    verify(connectionContext).closeConnection(eq(false));
    verify(deviceState).setValid(eq(false));
    verify(txChainManager).close();
    verify(mockedAuxiliaryConnectionContext).closeConnection(eq(false));
  }
  @Test
  public void testPortStatusMessage() {
    PortStatusMessage mockedPortStatusMessage = mock(PortStatusMessage.class);
    Class dummyClass = Class.class;
    when(mockedPortStatusMessage.getImplementedInterface()).thenReturn(dummyClass);

    GetFeaturesOutput mockedFeature = mock(GetFeaturesOutput.class);
    when(mockedFeature.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
    when(deviceState.getFeatures()).thenReturn(mockedFeature);

    when(mockedPortStatusMessage.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
    when(mockedPortStatusMessage.getReason()).thenReturn(PortReason.OFPPRADD);

    OpenflowPortsUtil.init();
    deviceContext.processPortStatusMessage(mockedPortStatusMessage);
    verify(txChainManager)
        .writeToTransaction(
            eq(LogicalDatastoreType.OPERATIONAL),
            any(InstanceIdentifier.class),
            any(DataObject.class));
  }
  @Before
  public void setUp() {
    final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture =
        Futures.immediateCheckedFuture(Optional.<Node>absent());
    Mockito.when(rTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent))
        .thenReturn(noExistNodeFuture);
    Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
    Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class)))
        .thenReturn(txChainFactory);
    Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent);

    final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
    final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply =
        SettableFuture.create();
    Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
    Mockito.doAnswer(
            new Answer<Object>() {
              @SuppressWarnings("unchecked")
              @Override
              public Object answer(final InvocationOnMock invocation) {
                settableFuture.set((RpcResult<GetAsyncReply>) invocation.getArguments()[0]);
                return null;
              }
            })
        .when(requestContext)
        .setResult(any(RpcResult.class));

    Mockito.when(requestContextMultiReply.getFuture()).thenReturn(settableFutureMultiReply);
    Mockito.doAnswer(
            new Answer<Object>() {
              @SuppressWarnings("unchecked")
              @Override
              public Object answer(final InvocationOnMock invocation) {
                settableFutureMultiReply.set(
                    (RpcResult<MultipartReply>) invocation.getArguments()[0]);
                return null;
              }
            })
        .when(requestContextMultiReply)
        .setResult(any(RpcResult.class));
    Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
    Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
    Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
    Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);

    Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
    Mockito.when(
            messageTranslatorPacketReceived.translate(
                any(Object.class), any(DeviceContext.class), any(Object.class)))
        .thenReturn(mock(PacketReceived.class));
    Mockito.when(
            messageTranslatorFlowCapableNodeConnector.translate(
                any(Object.class), any(DeviceContext.class), any(Object.class)))
        .thenReturn(mock(FlowCapableNodeConnector.class));
    Mockito.when(
            translatorLibrary.lookupTranslator(
                eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PacketIn.class.getName()))))
        .thenReturn(messageTranslatorPacketReceived);
    Mockito.when(
            translatorLibrary.lookupTranslator(
                eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PortGrouping.class.getName()))))
        .thenReturn(messageTranslatorFlowCapableNodeConnector);

    deviceContext =
        new DeviceContextImpl(
            connectionContext,
            deviceState,
            dataBroker,
            timer,
            messageIntelligenceAgency,
            outboundQueueProvider,
            translatorLibrary,
            txChainManager);

    xid = new Xid(atomicLong.incrementAndGet());
    xidMulti = new Xid(atomicLong.incrementAndGet());
  }