e1a71d6414bc9b67abb43b93af9ad18627a90a47
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / legacy / sharded / adapter / ShardedDOMDataBrokerDelegatingReadWriteTransactionTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter;
10
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;
17
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;
37
38 @Deprecated
39 public class ShardedDOMDataBrokerDelegatingReadWriteTransactionTest {
40
41     @Mock
42     private DOMDataWriteTransaction writeTx;
43
44     @Mock
45     private DOMDataReadOnlyTransaction readTx;
46
47     private ShardedDOMDataBrokerDelegatingReadWriteTransaction rwTx;
48
49     @Before
50     public void setUp() {
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,
56                                                                       writeTx);
57     }
58
59     @Test(expected = IllegalStateException.class)
60     public void testFirstReadShouldFail() {
61         rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
62     }
63
64     @Test
65     public void testGetIdentifier() {
66         assertEquals("TEST-TX", rwTx.getIdentifier());
67     }
68
69     @Test
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));
73
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));
77
78         assertEquals(testNodeWithOuter(1, 2, 3),
79                      rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
80
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());
84
85         rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
86
87         verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
88         assertEquals(Optional.absent(), rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
89     }
90
91     private static DataContainerChild<?, ?> outerNode(int... ids) {
92         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
93         for (int id : ids) {
94             outer.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, id));
95         }
96
97         return outer.build();
98     }
99
100     private static NormalizedNode<?, ?> testNodeWithOuter(int... ids) {
101         return testNodeWithOuter(outerNode(ids));
102     }
103
104     private static NormalizedNode<?, ?> testNodeWithOuter(DataContainerChild<?, ?> outer) {
105         return ImmutableContainerNodeBuilder.create()
106                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer)
107                 .build();
108     }
109 }