Do not pretty-print body class
[yangtools.git] / data / 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 package org.opendaylight.yangtools.yang.data.api.schema.tree;
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 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;
29
30 public class DataTreeCandidateNodesTest {
31     @Test
32     public void testFromNormalizedNode() {
33         final NormalizedNode mockedNormalizedNode = mock(NormalizedNode.class);
34         final DataTreeCandidateNode dataTreeCandidateNode = DataTreeCandidateNodes.written(mockedNormalizedNode);
35         assertNotNull(dataTreeCandidateNode);
36     }
37
38     @Test
39     public void testApplyToCursorWithWriteModificationType() {
40         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
41         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
42
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));
48     }
49
50     @Test
51     public void testApplyToCursorWithDeleteModificationType() {
52         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
53         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
54
55         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
56         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
57         verify(mockedCursor, times(1)).delete(isNull());
58     }
59
60     @Test
61     public void testApplyToCursorWithSubtreeModifiedModificationType() {
62         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
63         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
64
65         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
66
67         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
68         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
69
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();
74
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();
80
81         final Collection<DataTreeCandidateNode> childNodes = ImmutableList.of(mockedChildNode1, mockedChildNode2,
82                 mockedChildNode3);
83         doReturn(childNodes).when(mockedDataTreeCandidateNode).getChildNodes();
84
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));
89     }
90
91     @Test
92     public void testApplyToCursorWithUnsupportedModificationType() {
93         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
94         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
95
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"));
100     }
101
102     @Test
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);
107
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));
113     }
114
115     @Test
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);
120
121         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
122         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
123         verify(mockedCursor, times(1)).delete(isNull());
124     }
125
126     @Test
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);
131
132         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
133
134         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
135         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
136         doReturn(Collections.singletonList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
137
138         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
139         verify(mockedCursor, times(1)).enter((PathArgument) isNull());
140         verify(mockedCursor, times(1)).delete(isNull());
141     }
142
143     @Test
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);
148
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"));
154     }
155
156     @Test
157     public void testApplyRootToCursorWithSubtreeModifiedModificationType() {
158         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
159         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
160
161         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
162
163         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
164         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
165         doReturn(Collections.singletonList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
166
167         DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
168         verify(mockedCursor, times(1)).delete(isNull());
169     }
170
171     @Test
172     public void testApplyRootToCursorWithDeleteModificationType() {
173         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
174         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
175
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"));
180     }
181
182     @Test
183     public void testApplyRootToCursorWithUnsupportedModificationType() {
184         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
185         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
186
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"));
191     }
192 }