799dd63463f519444c5b4ad7caf4ea278221d719
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / DataNodeIteratorTest.java
1 /*
2  * Copyright (c) 2014 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.model.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.ImmutableSortedMap;
18 import java.util.Collections;
19 import java.util.NoSuchElementException;
20 import java.util.Set;
21 import java.util.SortedMap;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35
36 public class DataNodeIteratorTest {
37
38     private DataNodeIterator dataNodeIterator;
39
40     @Before
41     public void setUp() {
42         DataNodeContainer dataNodeContainer = mock(DataNodeContainer.class);
43         this.dataNodeIterator = new DataNodeIterator(dataNodeContainer);
44     }
45
46     @Test(expected = IllegalArgumentException.class)
47     public void createDataNodeIteratorWithNullArgument() {
48         new DataNodeIterator(null);
49     }
50
51     @Test(expected = UnsupportedOperationException.class)
52     public void removeFromEmptyDataNodeContainer() {
53         dataNodeIterator.remove();
54     }
55
56     @Test(expected = NoSuchElementException.class)
57     public void tryNextOnEmptyDataContainer() {
58         dataNodeIterator.next();
59     }
60
61     @Test
62     public void createDataNodeIteratorWith() {
63         assertFalse("Has no next", dataNodeIterator.hasNext());
64         assertEquals("Should be empty list", Collections.EMPTY_LIST, dataNodeIterator.allChoices());
65         assertEquals("Should be empty list", Collections.EMPTY_LIST, dataNodeIterator.allContainers());
66         assertEquals("Should be empty list", Collections.EMPTY_LIST, dataNodeIterator.allTypedefs());
67         assertEquals("Should be empty list", Collections.EMPTY_LIST, dataNodeIterator.allGroupings());
68         assertEquals("Should be empty list", Collections.EMPTY_LIST, dataNodeIterator.allLists());
69     }
70
71     @Test
72     public void testTraversal() {
73         final Module mockedModule = mock(Module.class);
74
75         final ContainerSchemaNode mockedAugmentingContainer = mock(ContainerSchemaNode.class);
76         doReturn(true).when(mockedAugmentingContainer).isAugmenting();
77
78         final ContainerSchemaNode mockedContainer = mock(ContainerSchemaNode.class);
79
80         final ListSchemaNode mockedList = mock(ListSchemaNode.class);
81
82         final ChoiceSchemaNode mockedChoice = mock(ChoiceSchemaNode.class);
83         final CaseSchemaNode mockedCase1 = mock(CaseSchemaNode.class);
84         final QName mockedCase1QName = QName.create("", "case1");
85         final CaseSchemaNode mockedCase2 = mock(CaseSchemaNode.class);
86         final QName mockedCase2QName = QName.create("", "case2");
87         final SortedMap<QName, CaseSchemaNode> cases = ImmutableSortedMap.of(mockedCase1QName, mockedCase1,
88             mockedCase2QName, mockedCase2);
89         doReturn(cases).when(mockedChoice).getCases();
90
91         final Set<DataSchemaNode> childNodes = ImmutableSet.of(mockedAugmentingContainer, mockedContainer, mockedList,
92                 mockedChoice);
93         doReturn(childNodes).when(mockedModule).getChildNodes();
94
95         final NotificationDefinition mockedNotification = mock(NotificationDefinition.class);
96         final ContainerSchemaNode mockedContainerInNotification = mock(ContainerSchemaNode.class);
97         final Set<DataSchemaNode> notificationChildNodes = ImmutableSet.of(mockedContainerInNotification);
98         doReturn(notificationChildNodes).when(mockedNotification).getChildNodes();
99         final Set<NotificationDefinition> notifications = ImmutableSet.of(mockedNotification);
100
101         doReturn(notifications).when(mockedModule).getNotifications();
102
103         final RpcDefinition mockedRpc = mock(RpcDefinition.class);
104         final ContainerSchemaNode mockedContainerInRpcInput = mock(ContainerSchemaNode.class);
105         final ListSchemaNode mockedListInRpcInputContainer = mock(ListSchemaNode.class);
106         final Set<DataSchemaNode> rpcInputChildNodes = ImmutableSet.of(mockedListInRpcInputContainer);
107         doReturn(rpcInputChildNodes).when(mockedContainerInRpcInput).getChildNodes();
108         doReturn(mockedContainerInRpcInput).when(mockedRpc).getInput();
109         final Set<RpcDefinition> rpcs = ImmutableSet.of(mockedRpc);
110
111         doReturn(rpcs).when(mockedModule).getRpcs();
112
113         final GroupingDefinition mockedGrouping = mock(GroupingDefinition.class);
114         final Set<GroupingDefinition> groupings = ImmutableSet.of(mockedGrouping);
115
116         doReturn(groupings).when(mockedModule).getGroupings();
117
118         final DataNodeIterator it = new DataNodeIterator(mockedModule);
119         assertFalse(it.allContainers().contains(mockedAugmentingContainer));
120         assertTrue(it.allContainers().contains(mockedContainer));
121         assertTrue(it.allLists().contains(mockedList));
122         assertTrue(it.allChoices().contains(mockedChoice));
123         assertTrue(it.allChoices().get(0).getCases().values().contains(mockedCase1));
124         assertTrue(it.allChoices().get(0).getCases().values().contains(mockedCase2));
125         assertTrue(it.allContainers().contains(mockedContainerInNotification));
126         assertTrue(it.allLists().contains(mockedListInRpcInputContainer));
127         assertTrue(it.allGroupings().contains(mockedGrouping));
128     }
129 }