TransactionProxy extends AbstractDOMStoreTransaction<TransactionIdentifier> implements DOMStoreReadWriteTransaction { @Override public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path) { checkState(type != TransactionType.WRITE_ONLY, "Reads from write-only transactions are not allowed"); requireNonNull(path, "path should not be null"); LOG.trace("Tx {} read {}", getIdentifier(), path); return path.isEmpty() ? readAllData() : singleShardRead(shardNameFromIdentifier(path), path); } @VisibleForTesting  TransactionProxy(final AbstractTransactionContextFactory<?> txContextFactory, final TransactionType type); @Override FluentFuture<Boolean> exists(final YangInstanceIdentifier path); @Override FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path); @Override void delete(final YangInstanceIdentifier path); @Override void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data); @Override void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data); @Override final void close(); @Override final AbstractThreePhaseCommitCohort<?> ready();  }
@Test public void testRead() throws Exception { ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY); TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY); doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync( eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class)); Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read( TestModel.TEST_PATH).get(5, TimeUnit.SECONDS); assertFalse("NormalizedNode isPresent", readOptional.isPresent()); NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME); doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync( eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class)); readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS); assertTrue("NormalizedNode isPresent", readOptional.isPresent()); assertEquals("Response NormalizedNode", expectedNode, readOptional.get()); }
@Test(expected = ReadFailedException.class) public void testReadWithInvalidReplyMessageType() throws Throwable { ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY); doReturn(Futures.successful(new Object())).when(mockActorContext) .executeOperationAsync(eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class)); TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY); try { transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS); } catch (ExecutionException e) { throw e.getCause(); } }
@Test(expected = TestException.class) public void testReadWithAsyncRemoteOperatonFailure() throws Throwable { ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY); doReturn(Futures.failed(new TestException())).when(mockActorContext) .executeOperationAsync(eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class)); TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY); propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH)); }
@Test(expected = IllegalStateException.class) public void testReadPreConditionCheck() { TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY); transactionProxy.read(TestModel.TEST_PATH); }
@Test(expected = IllegalArgumentException.class) public void testInvalidCreateTransactionReply() throws Throwable { ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class)); doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext) .actorSelection(actorRef.path().toString()); doReturn(primaryShardInfoReply(getSystem(), actorRef)).when(mockActorContext) .findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync( eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY), any(Timeout.class)); TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY); propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH)); }
@Test public void testReadThrottlingWhenShardFound() { throttleOperation(transactionProxy -> { doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync( any(ActorSelection.class), eqReadData()); transactionProxy.read(TestModel.TEST_PATH); transactionProxy.read(TestModel.TEST_PATH); }); }
@Test public void testReadThrottlingWhenShardNotFound() { completeOperation(transactionProxy -> { doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync( any(ActorSelection.class), eqReadData()); transactionProxy.read(TestModel.TEST_PATH); transactionProxy.read(TestModel.TEST_PATH); }, false); }
@Test public void testReadCompletion() { completeOperation(transactionProxy -> { NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME); doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync( any(ActorSelection.class), eqReadData(), any(Timeout.class)); transactionProxy.read(TestModel.TEST_PATH); transactionProxy.read(TestModel.TEST_PATH); }); }
@Test public void testReadCompletionForLocalShard() { final NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME); completeOperationLocal(transactionProxy -> { transactionProxy.read(TestModel.TEST_PATH); transactionProxy.read(TestModel.TEST_PATH); }, createDataTree(nodeToRead)); }
@Test public void testReadCompletionForLocalShardWhenExceptionOccurs() { completeOperationLocal(transactionProxy -> { transactionProxy.read(TestModel.TEST_PATH); transactionProxy.read(TestModel.TEST_PATH); }, createDataTree()); }
@Test public void testReadRoot() throws InterruptedException, ExecutionException, java.util.concurrent.TimeoutException { EffectiveModelContext schemaContext = SchemaContextHelper.full(); Configuration configuration = mock(Configuration.class); doReturn(configuration).when(mockActorContext).getConfiguration(); doReturn(schemaContext).when(mockActorContext).getSchemaContext(); doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames(); NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME); NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME); setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext)); setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext)); doReturn(MemberName.forName(memberName)).when(mockActorContext).getCurrentMemberName(); doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher(); TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY); Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read( YangInstanceIdentifier.empty()).get(5, TimeUnit.SECONDS); assertTrue("NormalizedNode isPresent", readOptional.isPresent()); NormalizedNode<?, ?> normalizedNode = readOptional.get(); assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection); @SuppressWarnings("unchecked") Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue(); for (NormalizedNode<?,?> node : collection) { assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode); } assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found", NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null); assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME)); assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found", NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null); assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME)); }