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.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.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;
39 public class ShardedDOMDataBrokerDelegatingReadWriteTransactionTest {
42 private DOMDataWriteTransaction writeTx;
45 private DOMDataReadOnlyTransaction readTx;
47 private ShardedDOMDataBrokerDelegatingReadWriteTransaction rwTx;
51 MockitoAnnotations.initMocks(this);
52 doNothing().when(writeTx).put(any(), any(), any());
53 doNothing().when(writeTx).merge(any(), any(), any());
54 doNothing().when(writeTx).delete(any(), any());
55 rwTx = new ShardedDOMDataBrokerDelegatingReadWriteTransaction("TEST-TX", TestModel.createTestContext(), readTx,
59 @Test(expected = IllegalStateException.class)
60 public void testFirstReadShouldFail() {
61 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
65 public void testGetIdentifier() {
66 assertEquals("TEST-TX", rwTx.getIdentifier());
70 public void testReadWriteOperations() throws Exception {
71 doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(readTx).read(any(), any());
72 rwTx.put(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(1, 2, 3));
74 verify(writeTx).put(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH),
75 Matchers.eq(testNodeWithOuter(1, 2, 3)));
76 verify(readTx).read(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
78 assertEquals(testNodeWithOuter(1, 2, 3),
79 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
81 rwTx.merge(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(4, 5, 6));
82 assertEquals(testNodeWithOuter(1, 2, 3, 4, 5, 6),
83 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
85 rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
87 verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
88 assertEquals(Optional.absent(), rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
91 private static DataContainerChild<?, ?> outerNode(int... ids) {
92 CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
94 outer.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, id));
100 private static NormalizedNode<?, ?> testNodeWithOuter(int... ids) {
101 return testNodeWithOuter(outerNode(ids));
104 private static NormalizedNode<?, ?> testNodeWithOuter(DataContainerChild<?, ?> outer) {
105 return ImmutableContainerNodeBuilder.create()
106 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer)