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