### Question:
SessionService implements IService, IInputTranslator { public Session open(IOFSwitch sw) { return open(new MessageContext(), sw); }  Session open(IOFSwitch sw); Session open(MessageContext context, IOFSwitch sw); @Override void setup(FloodlightModuleContext moduleContext); @Override Command makeCommand(CommandContext context, OfInput input);  }### Answer:
@Test public void positive() throws Exception { IOFSwitch sw = createMock(IOFSwitch.class); setupSwitchMock(sw, dpId); swWriteAlwaysSuccess(sw); doneWithSetUp(sw); CompletableFuture<Optional<OFMessage>> future; OFPacketOut pktOut = makePacketOut(sw.getOFFactory(), 1); try (Session session = subject.open(context, sw)) { future = session.write(pktOut); } Assert.assertFalse(future.isDone()); List<OFMessage> swActualWrite = swWriteMessages.getValues(); Assert.assertEquals(2, swActualWrite.size()); Assert.assertEquals(pktOut, swActualWrite.get(0)); Assert.assertEquals(OFType.BARRIER_REQUEST, swActualWrite.get(1).getType()); completeSessions(sw); Assert.assertTrue(future.isDone()); Optional<OFMessage> response = future.get(); Assert.assertFalse(response.isPresent()); }
@Test public void switchWriteError() throws Throwable { IOFSwitch sw = createMock(IOFSwitch.class); setupSwitchMock(sw, dpId); swWriteSecondFail(sw); doneWithSetUp(sw); OFFactory ofFactory = sw.getOFFactory(); CompletableFuture<Optional<OFMessage>> future = null; try (Session session = subject.open(context, sw)) { session.write(makePacketOut(ofFactory, 1)); future = session.write(makePacketOut(ofFactory, 2)); } try { future.get(1, TimeUnit.SECONDS); Assert.fail(); } catch (ExecutionException e) { Assert.assertNotNull(e.getCause()); Assert.assertTrue(e.getCause() instanceof SwitchWriteException); } }
@Test public void sessionBarrierWriteError() throws Exception { IOFSwitch sw = createMock(IOFSwitch.class); setupSwitchMock(sw, dpId); swWriteSecondFail(sw); doneWithSetUp(sw); CompletableFuture<Optional<OFMessage>> future = null; try (Session session = subject.open(context, sw)) { future = session.write(makePacketOut(sw.getOFFactory(), 1)); } try { future.get(1, TimeUnit.SECONDS); Assert.fail(); } catch (ExecutionException e) { Assert.assertNotNull(e.getCause()); Assert.assertTrue(e.getCause() instanceof SessionCloseException); } }