2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.verify;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.Futures;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
38 public class ShardedDOMDataBrokerDelegatingReadWriteTransactionTest {
41 private DOMDataWriteTransaction writeTx;
44 private DOMDataReadOnlyTransaction readTx;
46 private ShardedDOMDataBrokerDelegatingReadWriteTransaction rwTx;
50 MockitoAnnotations.initMocks(this);
51 doNothing().when(writeTx).put(any(), any(), any());
52 doNothing().when(writeTx).merge(any(), any(), any());
53 doNothing().when(writeTx).delete(any(), any());
54 rwTx = new ShardedDOMDataBrokerDelegatingReadWriteTransaction("TEST-TX", TestModel.createTestContext(), readTx,
58 @Test(expected = IllegalStateException.class)
59 public void testFirstReadShouldFail() {
60 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
64 public void testGetIdentifier() {
65 assertEquals("TEST-TX", rwTx.getIdentifier());
69 public void testReadWriteOperations() throws Exception {
70 doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(readTx).read(any(), any());
71 rwTx.put(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(1, 2, 3));
73 verify(writeTx).put(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH),
74 Matchers.eq(testNodeWithOuter(1, 2, 3)));
75 verify(readTx).read(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
77 assertEquals(testNodeWithOuter(1, 2, 3),
78 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
80 rwTx.merge(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(4, 5, 6));
81 assertEquals(testNodeWithOuter(1, 2, 3, 4, 5, 6),
82 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
84 rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
86 verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
87 assertEquals(Optional.absent(), rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
90 private DataContainerChild<?, ?> outerNode(int... ids) {
91 CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
93 outer.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, id));
99 private NormalizedNode<?, ?> testNodeWithOuter(int... ids) {
100 return testNodeWithOuter(outerNode(ids));
103 private NormalizedNode<?, ?> testNodeWithOuter(DataContainerChild<?, ?> outer) {
104 return ImmutableContainerNodeBuilder.create()
105 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer)