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, writeTx);
57 @Test(expected = IllegalStateException.class)
58 public void testFirstReadShouldFail() {
59 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
63 public void testGetIdentifier() {
64 assertEquals("TEST-TX", rwTx.getIdentifier());
68 public void testReadWriteOperations() throws Exception {
69 doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(readTx)
71 rwTx.put(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH,
72 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,
82 testNodeWithOuter(4, 5, 6));
83 assertEquals(testNodeWithOuter(1, 2, 3, 4, 5, 6),
84 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
86 rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
88 verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
89 assertEquals(Optional.absent(),
90 rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
93 private DataContainerChild<?, ?> outerNode(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 NormalizedNode<?, ?> testNodeWithOuter(int... ids) {
103 return testNodeWithOuter(outerNode(ids));
106 private NormalizedNode<?, ?> testNodeWithOuter(DataContainerChild<?, ?> outer) {
107 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
108 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer).build();