Fix eclipse/checkstyle warnings
[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.base.Optional;
21 import com.google.common.collect.Lists;
22 import java.util.Collection;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28 public class DataTreeCandidateNodesTest {
29
30     @Test
31     public void testFromNormalizedNode() {
32         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
33         final DataTreeCandidateNode dataTreeCandidateNode = DataTreeCandidateNodes.fromNormalizedNode(
34                 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(any(PathArgument.class), 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(any(PathArgument.class));
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(Lists.newArrayList(mockedChildNode3ChildNode)).when(mockedChildNode3).getChildNodes();
80
81         final Collection<DataTreeCandidateNode> childNodes = Lists.newArrayList(mockedChildNode1, mockedChildNode2,
82                 mockedChildNode3);
83         doReturn(childNodes).when(mockedDataTreeCandidateNode).getChildNodes();
84
85         DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
86         verify(mockedCursor, times(2)).enter(any(PathArgument.class));
87         verify(mockedCursor, times(2)).delete(any(PathArgument.class));
88         verify(mockedCursor, times(1)).write(any(PathArgument.class), 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         try {
98             DataTreeCandidateNodes.applyToCursor(mockedCursor, mockedDataTreeCandidateNode);
99             fail("An IllegalArgumentException should have been thrown!");
100         } catch (IllegalArgumentException ex) {
101             assertTrue(ex.getMessage().contains("Unsupported modification"));
102         }
103     }
104
105     @Test
106     public void testApplyRootedNodeToCursorWithWriteModificationType() {
107         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
108         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
109         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
110
111         doReturn(ModificationType.WRITE).when(mockedDataTreeCandidateNode).getModificationType();
112         final NormalizedNode<?, ?> mockedNormalizedNode = mock(NormalizedNode.class);
113         doReturn(Optional.of(mockedNormalizedNode)).when(mockedDataTreeCandidateNode).getDataAfter();
114         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
115         verify(mockedCursor, times(1)).write(any(PathArgument.class), any(NormalizedNode.class));
116     }
117
118     @Test
119     public void testApplyRootedNodeToCursorWithDeleteModificationType() {
120         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
121         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
122         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
123
124         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
125         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
126         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
127     }
128
129     @Test
130     public void testApplyRootedNodeToCursorWithSubtreeModifiedModificationType() {
131         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
132         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
133         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
134
135         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
136
137         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
138         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
139         doReturn(Lists.newArrayList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
140
141         DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
142         verify(mockedCursor, times(1)).enter(any(PathArgument.class));
143         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
144     }
145
146     @Test
147     public void testApplyRootedNodeToCursorWithUnsupportedModificationType() {
148         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
149         final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class);
150         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
151
152         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
153         try {
154             DataTreeCandidateNodes.applyRootedNodeToCursor(mockedCursor, mockedRootPath, mockedDataTreeCandidateNode);
155             fail("An IllegalArgumentException should have been thrown!");
156         } catch (IllegalArgumentException ex) {
157             assertTrue(ex.getMessage().contains("Unsupported modification"));
158         }
159     }
160
161     @Test
162     public void testApplyRootToCursorWithSubtreeModifiedModificationType() {
163         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
164         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
165
166         doReturn(ModificationType.SUBTREE_MODIFIED).when(mockedDataTreeCandidateNode).getModificationType();
167
168         final DataTreeCandidateNode mockedChildNode1 = mock(DataTreeCandidateNode.class);
169         doReturn(ModificationType.DELETE).when(mockedChildNode1).getModificationType();
170         doReturn(Lists.newArrayList(mockedChildNode1)).when(mockedDataTreeCandidateNode).getChildNodes();
171
172         DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
173         verify(mockedCursor, times(1)).delete(any(PathArgument.class));
174     }
175
176     @Test
177     public void testApplyRootToCursorWithDeleteModificationType() {
178         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
179         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
180
181         doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType();
182         try {
183             DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
184             fail("An IllegalArgumentException should have been thrown!");
185         } catch (IllegalArgumentException ex) {
186             assertTrue(ex.getMessage().contains("Can not delete root"));
187         }
188     }
189
190     @Test
191     public void testApplyRootToCursorWithUnsupportedModificationType() {
192         final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class);
193         final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class);
194
195         doReturn(ModificationType.APPEARED).when(mockedDataTreeCandidateNode).getModificationType();
196         try {
197             DataTreeCandidateNodes.applyRootToCursor(mockedCursor, mockedDataTreeCandidateNode);
198             fail("An IllegalArgumentException should have been thrown!");
199         } catch (IllegalArgumentException ex) {
200             assertTrue(ex.getMessage().contains("Unsupported modification"));
201         }
202     }
203 }