FlowTableManager implements AutoCloseable { private VirtualNetwork getVirtualNetwork(UserId userId) { VirtualNetworkId virtualNetworkId = new VirtualNetworkId(userId.getValue()); VirtualNetworkKey virtualNetworkKey = new VirtualNetworkKey(virtualNetworkId); ReadOnlyTransaction readOnlyTransaction = dataProvider.newReadOnlyTransaction(); InstanceIdentifier<VirtualNetwork> virtualNetworkIid = InstanceIdentifier .builder(VirtualNetworks.class) .child(VirtualNetwork.class, virtualNetworkKey) .build(); Optional<VirtualNetwork> result = null; try { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, virtualNetworkIid).get(); } catch (Exception e) { LOG.error("Exception:",e); } if (result.isPresent()) { LOG.debug("getVirtualNetwork OK"); return result.get(); }else{ LOG.debug("getVirtualNetwork ERROR"); return null; } } FlowTableManager(DataBroker dataProvider,
                            PacketProcessingService packetProcessingService,
                            PhyConfigLoader phyConfigLoader); @Override void close();  }
@Test public void testGetVirtualNetwork()throws Exception{ UserId userId = mock(UserId.class); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); CheckedFuture checkedFuture = mock(CheckedFuture.class); Optional<VirtualNetwork> result = mock(Optional.class); VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); VirtualNetwork virtualNetwork1 ; Class<FlowTableManager> class1 = FlowTableManager.class; Method method = class1.getDeclaredMethod("getVirtualNetwork", new Class[]{UserId.class}); method.setAccessible(true); when(userId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); when(dataProvider.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture); when(checkedFuture.get()).thenReturn(result); when(result.isPresent()) .thenReturn(false) .thenReturn(true); when(result.get()).thenReturn(virtualNetwork); virtualNetwork1 = (VirtualNetwork) method.invoke(flowTableManager,userId); Assert.assertTrue(virtualNetwork1 == null); virtualNetwork1 = (VirtualNetwork) method.invoke(flowTableManager,userId); Assert.assertTrue(virtualNetwork1 != null); }