Fix checkstyle issues in module sal-dom-broker
[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.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;
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 public class ShardedDOMDataBrokerDelegatingReadWriteTransactionTest {
39
40     @Mock
41     private DOMDataWriteTransaction writeTx;
42
43     @Mock
44     private DOMDataReadOnlyTransaction readTx;
45
46     private ShardedDOMDataBrokerDelegatingReadWriteTransaction rwTx;
47
48     @Before
49     public void setUp() {
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,
55                                                                       writeTx);
56     }
57
58     @Test(expected = IllegalStateException.class)
59     public void testFirstReadShouldFail() {
60         rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
61     }
62
63     @Test
64     public void testGetIdentifier() {
65         assertEquals("TEST-TX", rwTx.getIdentifier());
66     }
67
68     @Test
69     public void testReadWriteOperations() throws Exception {
70         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(readTx).read(any(), any());
71         rwTx.put(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(1, 2, 3));
72
73         verify(writeTx).put(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH),
74                             Matchers.eq(testNodeWithOuter(1, 2, 3)));
75         verify(readTx).read(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
76
77         assertEquals(testNodeWithOuter(1, 2, 3),
78                      rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
79
80         rwTx.merge(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH, testNodeWithOuter(4, 5, 6));
81         assertEquals(testNodeWithOuter(1, 2, 3, 4, 5, 6),
82                      rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet().get());
83
84         rwTx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
85
86         verify(writeTx).delete(eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(TestModel.TEST_PATH));
87         assertEquals(Optional.absent(), rwTx.read(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH).checkedGet());
88     }
89
90     private DataContainerChild<?, ?> outerNode(int... ids) {
91         CollectionNodeBuilder<MapEntryNode, MapNode> outer = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME);
92         for (int id : ids) {
93             outer.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, id));
94         }
95
96         return outer.build();
97     }
98
99     private NormalizedNode<?, ?> testNodeWithOuter(int... ids) {
100         return testNodeWithOuter(outerNode(ids));
101     }
102
103     private NormalizedNode<?, ?> testNodeWithOuter(DataContainerChild<?, ?> outer) {
104         return ImmutableContainerNodeBuilder.create()
105                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(outer)
106                 .build();
107     }
108 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.