09cdaed62ec10ded984009727f12b4e68d2ab536
[yangtools.git] / yang / yang-data-api / src / test / java / org / opendaylight / yangtools / yang / data / api / schema / tree / DataTreeCandidateNodesTest.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.data.api.schema.tree;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.Lists;
22 import java.util.Collection;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28 public class DataTreeCandidateNodesTest {
29
30     @Test
31     public void testFromNormalizedNode() {
32         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
33         final DataTreeCandidateNode dataTreeCandidateNode = DataTreeCandidateNodes.fromNormalizedNode(mockedNormalizedNode);
34         assertNotNull(dataTreeCandidateNode);
35     }
36
37     @Test
38     public void testApplyToCursorWithWriteModificationType() {
39         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
40         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
41
42         doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
43         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
44         doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
45         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
46         verify(mockedCursor, times(1)).write(any(PathArgument.class), any(NormalizedNode.class));
47     }
48
49     @Test
50     public void testApplyToCursorWithDeleteModificationType() {
51         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
52         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
53
54         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
55         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
56         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
57     }
58
59     @Test
60     public void testApplyToCursorWithSubtreeModifiedModificationType() {
61         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
62         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
63
64         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
65
66         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
67         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
68
69         final DataTreeCandidateNode mockedChildNode2 = mock(DataTreeCandidateNode.class);
70         doReturn(ModificationType.WRITE).when(mockedChildNode2).getModificationType();
71         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
72         doReturn(Optional.of(mockedNormalizedNode)).when(mockedChildNode2).getDataAfter();
73
74         final DataTreeCandidateNode mockedChildNode3 = mock(DataTreeCandidateNode.class);
75         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedChildNode3).getModificationType();
76         final DataTreeCandidateNode mockedChildNode3ChildNode = mock(DataTreeCandidateNode.class);
77         doReturn(ModificationType.DELETE).when(mockedChildNode3ChildNode).getModificationType();
78         doReturn(Lists.newArrayList(mockedChildNode3ChildNode)).when(mockedChildNode3).getChildNodes();
79
80         final Collection<DataTreeCandidateNode> childNodes = Lists.newArrayList(mockedChildNode1, mockedChildNode2,
81                 mockedChildNode3);
82         doReturn(childNodes).when(mockedDataTreeCandidateNode).getChildNodes();
83
84         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
85         verify(mockedCursor, times(2)).enter(any(PathArgument.class));
86         verify(mockedCursor, times(2)).delete(any(PathArgument.class));
87         verify(mockedCursor, times(1)).write(any(PathArgument.class), any(NormalizedNode.class));
88     }
89
90     @Test
91     public void testApplyToCursorWithUnsupportedModificationType() {
92         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
93         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
94
95         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
96         try {
97             DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
98             fail("An IllegalArgumentException should have been thrown!");
99         } catch (IllegalArgumentException ex) {
100             assertTrue(ex.getMessage().contains("Unsupported modification"));
101         }
102     }
103
104     @Test
105     public void testApplyRootedNodeToCursorWithWriteModificationType() {
106         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
107         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
108         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
109
110         doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
111         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
112         doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
113         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
114         verify(mockedCursor, times(1)).write(any(PathArgument.class), any(NormalizedNode.class));
115     }
116
117     @Test
118     public void testApplyRootedNodeToCursorWithDeleteModificationType() {
119         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
120         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
121         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
122
123         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
124         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
125         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
126     }
127
128     @Test
129     public void testApplyRootedNodeToCursorWithSubtreeModifiedModificationType() {
130         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
131         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
132         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
133
134         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
135
136         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
137         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
138         doReturn(Lists.newArrayList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
139
140         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
141         verify(mockedCursor, times(1)).enter(any(PathArgument.class));
142         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
143     }
144
145     @Test
146     public void testApplyRootedNodeToCursorWithUnsupportedModificationType() {
147         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
148         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
149         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
150
151         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
152         try {
153             DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
154             fail("An IllegalArgumentException should have been thrown!");
155         } catch (IllegalArgumentException ex) {
156             assertTrue(ex.getMessage().contains("Unsupported modification"));
157         }
158     }
159
160     @Test
161     public void testApplyRootToCursorWithSubtreeModifiedModificationType() {
162         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
163         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
164
165         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
166
167         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
168         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
169         doReturn(Lists.newArrayList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
170
171         DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
172         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
173     }
174
175     @Test
176     public void testApplyRootToCursorWithDeleteModificationType() {
177         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
178         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
179
180         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
181         try {
182             DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
183             fail("An IllegalArgumentException should have been thrown!");
184         } catch (IllegalArgumentException ex) {
185             assertTrue(ex.getMessage().contains("Can not delete root"));
186         }
187     }
188
189     @Test
190     public void testApplyRootToCursorWithUnsupportedModificationType() {
191         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
192         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
193
194         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
195         try {
196             DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
197             fail("An IllegalArgumentException should have been thrown!");
198         } catch (IllegalArgumentException ex) {
199             assertTrue(ex.getMessage().contains("Unsupported modification"));
200         }
201     }
202 }