### Question:
NetconfTransactionCreator { public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) { int attempt = 0; do { try { return Optional.of(mountpoint.newReadWriteTransaction()); } catch (RuntimeException e) { final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause()); final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass); if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) { attempt++; LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt); try { Thread.sleep(timeout); } catch (InterruptedException i) { LOG.error("Thread interrupted while waiting ... {} ", i); } } else { LOG.error("Runtime exception ... {}", e.getMessage()); return Optional.empty(); } } } while (attempt <= 5); LOG.error("Maximum number of attempts reached"); return Optional.empty(); } private  NetconfTransactionCreator(); static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint); static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint); static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint); @VisibleForTesting static void setTimeout(long newTimeout);  }### Answer:
@Test public void testNetconfReadWriteTransaction_success() { ReadWriteTransaction wtx = mock(ReadWriteTransaction.class); when(dataBroker.newReadWriteTransaction()).thenReturn(wtx); Optional<ReadWriteTransaction> newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker); assertEquals(wtx, newRtx.get()); }
@Test public void testNetconfReadWriteTransaction_NoNetconfException() { when(dataBroker.newReadWriteTransaction()).thenThrow(RuntimeException.class); Optional<ReadWriteTransaction> newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker); assertEquals(false, newRtx.isPresent()); verify(dataBroker, times(1)).newReadWriteTransaction(); }
@Test public void testNetconfReadWriteTransaction_NetconfException() { when(dataBroker.newReadWriteTransaction()).thenThrow(new RuntimeException(new NetconfDocumentedException(""))); Optional<ReadWriteTransaction> newRtx = NetconfTransactionCreator.netconfReadWriteTransaction(dataBroker); assertEquals(false, newRtx.isPresent()); verify(dataBroker, times(6)).newReadWriteTransaction(); }