TransactionAdapter { public static ReadWriteTransaction toReadWriteTransaction( final TypedReadWriteTransaction<? extends Datastore> datastoreTx) { if (datastoreTx instanceof TypedReadWriteTransactionImpl) { final TypedReadWriteTransactionImpl<?> txImpl = (TypedReadWriteTransactionImpl<?>) datastoreTx; return new ReadWriteTransactionAdapter<>(txImpl.getDatastoreType(), txImpl); } throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass()); } private  TransactionAdapter(); static ReadWriteTransaction toReadWriteTransaction(
            final TypedReadWriteTransaction<? extends Datastore> datastoreTx); static WriteTransaction toWriteTransaction(final TypedWriteTransaction<? extends Datastore> datastoreTx);  }
@Test public void testAdaptedReadWriteTransactionPutsSuccessfully() throws Exception { TopLevelList data = newTestDataObject(); managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, writeTx -> TransactionAdapter.toReadWriteTransaction(writeTx).put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get(); assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH)); }
@Test public void testAdaptedReadWriteTransactionFailsOnInvalidDatastore() throws Exception { Future<?> future = managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, writeTx -> TransactionAdapter.toReadWriteTransaction(writeTx).put(LogicalDatastoreType.CONFIGURATION, TEST_PATH, newTestDataObject())); ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get()); assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class)); assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)); }
@Test(expected = ExecutionException.class) public void testAdaptedReadWriteTransactionCannotCommit() throws Exception { managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> TransactionAdapter.toReadWriteTransaction(tx).commit()).get(); }
@Test(expected = ExecutionException.class) public void testAdaptedReadWriteTransactionCannotCancel() throws Exception { managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> TransactionAdapter.toReadWriteTransaction(tx).cancel()).get(); }