Add ClientTransactionCursorTest
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / ClientTransactionCursorTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
10 import static org.mockito.Mockito.verify;
11
12 import java.util.Arrays;
13 import java.util.stream.Collectors;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22
23 public class ClientTransactionCursorTest {
24
25     private static final QName NODE_1 = QName.create("ns-1", "node-1");
26     private static final QName NODE_2 = QName.create(NODE_1, "node-2");
27     private static final QName NODE_3 = QName.create(NODE_1, "node-3");
28
29     @Mock
30     private ClientTransaction transaction;
31     private ClientTransactionCursor cursor;
32
33     @Before
34     public void setUp() throws Exception {
35         MockitoAnnotations.initMocks(this);
36         cursor = new ClientTransactionCursor(transaction);
37     }
38
39     @Test
40     public void testEnterOneNode() throws Exception {
41         cursor.enter(YangInstanceIdentifier.NodeIdentifier.create(NODE_1));
42         cursor.delete(YangInstanceIdentifier.NodeIdentifier.create(NODE_2));
43         final YangInstanceIdentifier expected = createId(NODE_1, NODE_2);
44         verify(transaction).delete(expected);
45     }
46
47     @Test
48     public void testEnterNodeIterables() throws Exception {
49         final Iterable<YangInstanceIdentifier.PathArgument> collect = toPathArg(NODE_1, NODE_2);
50         cursor.enter(collect);
51         cursor.delete(YangInstanceIdentifier.NodeIdentifier.create(NODE_3));
52         final YangInstanceIdentifier expected = createId(NODE_1, NODE_2, NODE_3);
53         verify(transaction).delete(expected);
54     }
55
56     @Test
57     public void testEnterNodeVarargs() throws Exception {
58         cursor.enter(YangInstanceIdentifier.NodeIdentifier.create(NODE_1),
59                 YangInstanceIdentifier.NodeIdentifier.create(NODE_2));
60         cursor.delete(YangInstanceIdentifier.NodeIdentifier.create(NODE_3));
61         final YangInstanceIdentifier expected = createId(NODE_1, NODE_2, NODE_3);
62         verify(transaction).delete(expected);
63     }
64
65     @Test
66     public void testExitOneLevel() throws Exception {
67         cursor.enter(toPathArg(NODE_1, NODE_2));
68         cursor.exit();
69         cursor.delete(YangInstanceIdentifier.NodeIdentifier.create(NODE_2));
70         final YangInstanceIdentifier expected = createId(NODE_1, NODE_2);
71         verify(transaction).delete(expected);
72     }
73
74     @Test
75     public void testExitTwoLevels() throws Exception {
76         cursor.enter(toPathArg(NODE_1, NODE_2, NODE_3));
77         cursor.exit(2);
78         cursor.delete(YangInstanceIdentifier.NodeIdentifier.create(NODE_2));
79         final YangInstanceIdentifier expected = createId(NODE_1, NODE_2);
80         verify(transaction).delete(expected);
81     }
82
83     @Test
84     public void testClose() throws Exception {
85         cursor.close();
86         verify(transaction).closeCursor(cursor);
87     }
88
89     @Test
90     public void testDelete() throws Exception {
91         cursor.delete(YangInstanceIdentifier.NodeIdentifier.create(NODE_1));
92         final YangInstanceIdentifier expected = createId(NODE_1);
93         verify(transaction).delete(expected);
94     }
95
96     @Test
97     public void testMerge() throws Exception {
98         final YangInstanceIdentifier.NodeIdentifier path = YangInstanceIdentifier.NodeIdentifier.create(NODE_1);
99         final ContainerNode data = createData(path.getNodeType());
100         cursor.merge(path, data);
101         final YangInstanceIdentifier expected = createId(NODE_1);
102         verify(transaction).merge(expected, data);
103     }
104
105     @Test
106     public void testWrite() throws Exception {
107         final YangInstanceIdentifier.NodeIdentifier path = YangInstanceIdentifier.NodeIdentifier.create(NODE_1);
108         final ContainerNode data = createData(path.getNodeType());
109         cursor.write(path, data);
110         final YangInstanceIdentifier expected = createId(NODE_1);
111         verify(transaction).write(expected, data);
112     }
113
114     private static Iterable<YangInstanceIdentifier.PathArgument> toPathArg(final QName... pathArguments) {
115         return Arrays.stream(pathArguments)
116                 .map(YangInstanceIdentifier.NodeIdentifier::create)
117                 .collect(Collectors.toList());
118     }
119
120     private static YangInstanceIdentifier createId(final QName... pathArguments) {
121         return YangInstanceIdentifier.create(toPathArg(pathArguments));
122     }
123
124     private static ContainerNode createData(final QName id) {
125         return Builders.containerBuilder()
126                 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifier.create(id))
127                 .build();
128     }
129
130 }