Split out yang-data-tree-{api,spi}
[yangtools.git] / data / yang-data-tree-spi / src / test / java / org / opendaylight / yangtools / yang / data / tree / spi / 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 package org.opendaylight.yangtools.yang.data.tree.spi;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThrows;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.isNull;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Optional;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModificationCursor;
30 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
31
32 public class DataTreeCandidateNodesTest {
33     @Test
34     public void testFromNormalizedNode() {
35         final NormalizedNode mockedNormalizedNode = mock(NormalizedNode.class);
36         final DataTreeCandidateNode dataTreeCandidateNode = DataTreeCandidateNodes.written(mockedNormalizedNode);
37         assertNotNull(dataTreeCandidateNode);
38     }
39
40     @Test
41     public void testApplyToCursorWithWriteModificationType() {
42         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
43         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
44
45         doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
46         final NormalizedNode mockedNormalizedNode = mock(NormalizedNode.class);
47         doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
48         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
49         verify(mockedCursor, times(1)).write(isNull(), any(NormalizedNode.class));
50     }
51
52     @Test
53     public void testApplyToCursorWithDeleteModificationType() {
54         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
55         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
56
57         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
58         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
59         verify(mockedCursor, times(1)).delete(isNull());
60     }
61
62     @Test
63     public void testApplyToCursorWithSubtreeModifiedModificationType() {
64         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
65         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
66
67         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
68
69         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
70         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
71
72         final DataTreeCandidateNode mockedChildNode2 = mock(DataTreeCandidateNode.class);
73         doReturn(ModificationType.WRITE).when(mockedChildNode2).getModificationType();
74         final NormalizedNode mockedNormalizedNode = mock(NormalizedNode.class);
75         doReturn(Optional.of(mockedNormalizedNode)).when(mockedChildNode2).getDataAfter();
76
77         final DataTreeCandidateNode mockedChildNode3 = mock(DataTreeCandidateNode.class);
78         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedChildNode3).getModificationType();
79         final DataTreeCandidateNode mockedChildNode3ChildNode = mock(DataTreeCandidateNode.class);
80         doReturn(ModificationType.DELETE).when(mockedChildNode3ChildNode).getModificationType();
81         doReturn(List.of(mockedChildNode3ChildNode)).when(mockedChildNode3).getChildNodes();
82
83         final Collection<DataTreeCandidateNode> childNodes = List.of(mockedChildNode1, mockedChildNode2,
84                 mockedChildNode3);
85         doReturn(childNodes).when(mockedDataTreeCandidateNode).getChildNodes();
86
87         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
88         verify(mockedCursor, times(2)).enter((PathArgument) isNull());
89         verify(mockedCursor, times(2)).delete(isNull());
90         verify(mockedCursor, times(1)).write(isNull(), any(NormalizedNode.class));
91     }
92
93     @Test
94     public void testApplyToCursorWithUnsupportedModificationType() {
95         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
96         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
97
98         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
99         final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
100             () -> DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode));
101         assertThat(ex.getMessage(), containsString("Unsupported modification"));
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(isNull(), 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(isNull());
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(List.of(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
139
140         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
141         verify(mockedCursor, times(1)).enter((PathArgument) isNull());
142         verify(mockedCursor, times(1)).delete(isNull());
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         final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
153             () -> DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath,
154                 mockedDataTreeCandidateNode));
155         assertThat(ex.getMessage(), containsString("Unsupported modification"));
156     }
157
158     @Test
159     public void testApplyRootToCursorWithSubtreeModifiedModificationType() {
160         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
161         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
162
163         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
164
165         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
166         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
167         doReturn(List.of(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
168
169         DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
170         verify(mockedCursor, times(1)).delete(isNull());
171     }
172
173     @Test
174     public void testApplyRootToCursorWithDeleteModificationType() {
175         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
176         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
177
178         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
179         final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
180             () -> DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode));
181         assertThat(ex.getMessage(), containsString("Can not delete root"));
182     }
183
184     @Test
185     public void testApplyRootToCursorWithUnsupportedModificationType() {
186         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
187         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
188
189         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
190         final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
191             () -> DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode));
192         assertThat(ex.getMessage(), containsString("Unsupported modification"));
193     }
194 }