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.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;
40 public class ShardedDOMDataBrokerDelegatingReadWriteTransactionTest {
43 private DOMDataWriteTransaction writeTx;
46 private DOMDataReadOnlyTransaction readTx;
48 private ShardedDOMDataBrokerDelegatingReadWriteTransaction rwTx;
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,
60 @Test(expected = IllegalStateException.class)
61 public void testFirstReadShouldFail() {
62 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
66 public void testGetIdentifier() {
67 assertEquals("TEST-TX", rwTx.getIdentifier());
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));
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));
80 assertEquals(testNodeWithOuter(1, 2, 3),
81 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
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());
87 rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
89 verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
90 assertEquals(Optional.absent(), rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
93 private static DataContainerChild<?, ?> outerNode(final int... ids) {
94 CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
96 outer.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, id));
102 private static NormalizedNode<?, ?> testNodeWithOuter(final int... ids) {
103 return testNodeWithOuter(outerNode(ids));
106 private static NormalizedNode<?, ?> testNodeWithOuter(final DataContainerChild<?, ?> outer) {
107 return ImmutableContainerNodeBuilder.create()
108 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer)