import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import java.net.InetSocketAddress;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
false);
tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
+ tx.submit();
// verify discard changes was sent
final InOrder inOrder = inOrder(rpc);
inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
}
+ @Test
+ public void testListenerSuccess() throws Exception {
+ doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
+ .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+ final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
+ final TxListener listener = mock(TxListener.class);
+ tx.addListener(listener);
+ tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
+ tx.submit();
+ verify(listener).onTransactionSubmitted(tx);
+ verify(listener).onTransactionSuccessful(tx);
+ verify(listener, never()).onTransactionFailed(eq(tx), any());
+ verify(listener, never()).onTransactionCancelled(tx);
+ }
+
+ @Test
+ public void testListenerCancellation() throws Exception {
+ doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
+ .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+ final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
+ final TxListener listener = mock(TxListener.class);
+ tx.addListener(listener);
+ tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
+ tx.cancel();
+ verify(listener).onTransactionCancelled(tx);
+ verify(listener, never()).onTransactionSubmitted(tx);
+ verify(listener, never()).onTransactionSuccessful(tx);
+ verify(listener, never()).onTransactionFailed(eq(tx), any());
+ }
+
+ @Test
+ public void testListenerFailure() throws Exception {
+ final IllegalStateException cause = new IllegalStateException("Failed tx");
+ doReturn(Futures.immediateFailedCheckedFuture(cause))
+ .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+ final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
+ final TxListener listener = mock(TxListener.class);
+ tx.addListener(listener);
+ tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
+ tx.submit();
+ final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
+ verify(listener).onTransactionSubmitted(tx);
+ verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
+ Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
+ verify(listener, never()).onTransactionSuccessful(tx);
+ verify(listener, never()).onTransactionCancelled(tx);
+ }
}