Build with Mockito 2.1
[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.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
32     @Test
33     public void testFromNormalizedNode() {
34         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
35         final DataTreeCandidateNode dataTreeCandidateNode = DataTreeCandidateNodes.fromNormalizedNode(
36                 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(Collections.singletonList(mockedChildNode3ChildNode)).when(mockedChildNode3).getChildNodes();
82
83         final Collection<DataTreeCandidateNode> childNodes = ImmutableList.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         try {
100             DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
101             fail("An IllegalArgumentException should have been thrown!");
102         } catch (IllegalArgumentException ex) {
103             assertTrue(ex.getMessage().contains("Unsupported modification"));
104         }
105     }
106
107     @Test
108     public void testApplyRootedNodeToCursorWithWriteModificationType() {
109         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
110         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
111         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
112
113         doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
114         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
115         doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
116         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
117         verify(mockedCursor, times(1)).write(isNull(), any(NormalizedNode.class));
118     }
119
120     @Test
121     public void testApplyRootedNodeToCursorWithDeleteModificationType() {
122         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
123         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
124         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
125
126         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
127         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
128         verify(mockedCursor, times(1)).delete(isNull());
129     }
130
131     @Test
132     public void testApplyRootedNodeToCursorWithSubtreeModifiedModificationType() {
133         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
134         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
135         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
136
137         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
138
139         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
140         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
141         doReturn(Collections.singletonList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
142
143         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
144         verify(mockedCursor, times(1)).enter((PathArgument) isNull());
145         verify(mockedCursor, times(1)).delete(isNull());
146     }
147
148     @Test
149     public void testApplyRootedNodeToCursorWithUnsupportedModificationType() {
150         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
151         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
152         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
153
154         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
155         try {
156             DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
157             fail("An IllegalArgumentException should have been thrown!");
158         } catch (IllegalArgumentException ex) {
159             assertTrue(ex.getMessage().contains("Unsupported modification"));
160         }
161     }
162
163     @Test
164     public void testApplyRootToCursorWithSubtreeModifiedModificationType() {
165         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
166         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
167
168         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
169
170         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
171         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
172         doReturn(Collections.singletonList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
173
174         DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
175         verify(mockedCursor, times(1)).delete(isNull());
176     }
177
178     @Test
179     public void testApplyRootToCursorWithDeleteModificationType() {
180         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
181         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
182
183         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
184         try {
185             DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
186             fail("An IllegalArgumentException should have been thrown!");
187         } catch (IllegalArgumentException ex) {
188             assertTrue(ex.getMessage().contains("Can not delete root"));
189         }
190     }
191
192     @Test
193     public void testApplyRootToCursorWithUnsupportedModificationType() {
194         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
195         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
196
197         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
198         try {
199             DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
200             fail("An IllegalArgumentException should have been thrown!");
201         } catch (IllegalArgumentException ex) {
202             assertTrue(ex.getMessage().contains("Unsupported modification"));
203         }
204     }
205 }