Migrate OSGI compendium reference
[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.gaul.modernizer_maven_annotations.SuppressModernizer;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Matchers;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
38
39 @Deprecated
40 public class ShardedDOMDataBrokerDelegatingReadWriteTransactionTest {
41
42     @Mock
43     private DOMDataWriteTransaction writeTx;
44
45     @Mock
46     private DOMDataReadOnlyTransaction readTx;
47
48     private ShardedDOMDataBrokerDelegatingReadWriteTransaction rwTx;
49
50     @Before
51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         doNothing().when(writeTx).put(any(), any(), any());
54         doNothing().when(writeTx).merge(any(), any(), any());
55         doNothing().when(writeTx).delete(any(), any());
56         rwTx = new ShardedDOMDataBrokerDelegatingReadWriteTransaction("TEST-TX", TestModel.createTestContext(), readTx,
57                                                                       writeTx);
58     }
59
60     @Test(expected = IllegalStateException.class)
61     public void testFirstReadShouldFail() {
62         rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
63     }
64
65     @Test
66     public void testGetIdentifier() {
67         assertEquals("TEST-TX", rwTx.getIdentifier());
68     }
69
70     @Test
71     @SuppressModernizer
72     public void testReadWriteOperations() throws Exception {
73         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(readTx).read(any(), any());
74         rwTx.put(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(1, 2, 3));
75
76         verify(writeTx).put(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH),
77                             Matchers.eq(testNodeWithOuter(1, 2, 3)));
78         verify(readTx).read(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
79
80         assertEquals(testNodeWithOuter(1, 2, 3),
81                      rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
82
83         rwTx.merge(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(4, 5, 6));
84         assertEquals(testNodeWithOuter(1, 2, 3, 4, 5, 6),
85                      rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
86
87         rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
88
89         verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
90         assertEquals(Optional.absent(), rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
91     }
92
93     private static DataContainerChild<?, ?> outerNode(final int... ids) {
94         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
95         for (int id : ids) {
96             outer.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, id));
97         }
98
99         return outer.build();
100     }
101
102     private static NormalizedNode<?, ?> testNodeWithOuter(final int... ids) {
103         return testNodeWithOuter(outerNode(ids));
104     }
105
106     private static NormalizedNode<?, ?> testNodeWithOuter(final DataContainerChild<?, ?> outer) {
107         return ImmutableContainerNodeBuilder.create()
108                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer)
109                 .build();
110     }
111 }