2 * Copyright (c) 2016 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
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
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;
21 import com.google.common.collect.ImmutableList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Optional;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 public class DataTreeCandidateNodesTest {
32 public void testFromNormalizedNode() {
33 final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
34 final DataTreeCandidateNode dataTreeCandidateNode = DataTreeCandidateNodes.written(mockedNormalizedNode);
35 assertNotNull(dataTreeCandidateNode);
39 public void testApplyToCursorWithWriteModificationType() {
40 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
41 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
43 doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
44 final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
45 doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
46 DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
47 verify(mockedCursor, times(1)).write(isNull(), any(NormalizedNode.class));
51 public void testApplyToCursorWithDeleteModificationType() {
52 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
53 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
55 doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
56 DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
57 verify(mockedCursor, times(1)).delete(isNull());
61 public void testApplyToCursorWithSubtreeModifiedModificationType() {
62 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
63 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
65 doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
67 final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
68 doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
70 final DataTreeCandidateNode mockedChildNode2 = mock(DataTreeCandidateNode.class);
71 doReturn(ModificationType.WRITE).when(mockedChildNode2).getModificationType();
72 final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
73 doReturn(Optional.of(mockedNormalizedNode)).when(mockedChildNode2).getDataAfter();
75 final DataTreeCandidateNode mockedChildNode3 = mock(DataTreeCandidateNode.class);
76 doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedChildNode3).getModificationType();
77 final DataTreeCandidateNode mockedChildNode3ChildNode = mock(DataTreeCandidateNode.class);
78 doReturn(ModificationType.DELETE).when(mockedChildNode3ChildNode).getModificationType();
79 doReturn(Collections.singletonList(mockedChildNode3ChildNode)).when(mockedChildNode3).getChildNodes();
81 final Collection<DataTreeCandidateNode> childNodes = ImmutableList.of(mockedChildNode1, mockedChildNode2,
83 doReturn(childNodes).when(mockedDataTreeCandidateNode).getChildNodes();
85 DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
86 verify(mockedCursor, times(2)).enter((PathArgument) isNull());
87 verify(mockedCursor, times(2)).delete(isNull());
88 verify(mockedCursor, times(1)).write(isNull(), any(NormalizedNode.class));
92 public void testApplyToCursorWithUnsupportedModificationType() {
93 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
94 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
96 doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
97 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
98 () -> DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode));
99 assertThat(ex.getMessage(), containsString("Unsupported modification"));
103 public void testApplyRootedNodeToCursorWithWriteModificationType() {
104 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
105 final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
106 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
108 doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
109 final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
110 doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
111 DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
112 verify(mockedCursor, times(1)).write(isNull(), any(NormalizedNode.class));
116 public void testApplyRootedNodeToCursorWithDeleteModificationType() {
117 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
118 final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
119 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
121 doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
122 DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
123 verify(mockedCursor, times(1)).delete(isNull());
127 public void testApplyRootedNodeToCursorWithSubtreeModifiedModificationType() {
128 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
129 final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
130 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
132 doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
134 final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
135 doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
136 doReturn(Collections.singletonList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
138 DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
139 verify(mockedCursor, times(1)).enter((PathArgument) isNull());
140 verify(mockedCursor, times(1)).delete(isNull());
144 public void testApplyRootedNodeToCursorWithUnsupportedModificationType() {
145 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
146 final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
147 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
149 doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
150 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
151 () -> DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath,
152 mockedDataTreeCandidateNode));
153 assertThat(ex.getMessage(), containsString("Unsupported modification"));
157 public void testApplyRootToCursorWithSubtreeModifiedModificationType() {
158 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
159 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
161 doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
163 final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
164 doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
165 doReturn(Collections.singletonList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
167 DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
168 verify(mockedCursor, times(1)).delete(isNull());
172 public void testApplyRootToCursorWithDeleteModificationType() {
173 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
174 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
176 doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
177 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
178 () -> DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode));
179 assertThat(ex.getMessage(), containsString("Can not delete root"));
183 public void testApplyRootToCursorWithUnsupportedModificationType() {
184 final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
185 final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
187 doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
188 final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
189 () -> DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode));
190 assertThat(ex.getMessage(), containsString("Unsupported modification"));