Migrate getDataChildByName() users
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtilTest.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.assertNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.base.Splitter;
18 import com.google.common.collect.ImmutableList;
19 import java.net.URI;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Optional;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.Revision;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.PathExpression;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
38
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class SchemaContextUtilTest {
41     public static final Splitter SPACE_SPLITTER = Splitter.on(' ');
42     public static final URI NAMESPACE = URI.create("abc");
43
44     @Mock
45     public SchemaContext mockSchemaContext;
46     @Mock
47     public Module mockModule;
48     @Mock
49     public SchemaNode schemaNode;
50
51     @Before
52     public void before() {
53         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
54         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
55
56         doReturn("test").when(mockModule).getName();
57         doReturn("test").when(mockModule).getPrefix();
58         doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
59     }
60
61     @Test
62     public void testFindDummyData() {
63
64         QName qname = QName.create("namespace", "localname");
65         SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
66         assertNull("Should be null. Module TestQName not found",
67                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
68
69         PathExpression xpath = new PathExpressionImpl("/test:bookstore/test:book/test:title", true);
70         assertNull("Should be null. Module bookstore not found",
71                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
72
73
74         final PathExpression xPath = new PathExpressionImpl("/bookstore/book/title", true);
75         assertEquals("Should be null. Module bookstore not found", null,
76                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
77
78         SchemaNode int32node = BaseTypes.int32Type();
79         PathExpression xpathRelative = new PathExpressionImpl("../prefix", false);
80         assertNull("Should be null, Module prefix not found",
81                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
82                         mockSchemaContext, mockModule, int32node, xpathRelative));
83
84         assertNull("Should be null. Module TestQName not found",
85                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
86
87         assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
88     }
89
90     @Test
91     public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
92         assertThrows(NullPointerException.class,
93             () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null));
94     }
95
96     @Test
97     public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
98         final SchemaContext mockContext = mock(SchemaContext.class);
99         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
100
101         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath));
102     }
103
104     @Test
105     public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
106         final Module module = mock(Module.class);
107         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
108
109         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null, module, xpath));
110     }
111
112     @Test
113     public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
114         final SchemaContext mockContext = mock(SchemaContext.class);
115         final Module module = mock(Module.class);
116         final PathExpression xpath = new PathExpressionImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
117
118         assertThrows(IllegalArgumentException.class,
119             () -> SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
120     }
121
122     @Test
123     public void findParentModuleIllegalArgumentTest() {
124         assertThrows(NullPointerException.class,
125             () -> SchemaContextUtil.findParentModule(mock(SchemaContext.class), null));
126     }
127
128     @Test
129     public void findParentModuleIllegalArgumentTest2() {
130         doReturn(SchemaPath.create(true, QName.create("foo", "bar"))).when(schemaNode).getPath();
131         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findParentModule(null, schemaNode));
132     }
133
134     @Test
135     public void findDataSchemaNodeIllegalArgumentTest() {
136         assertThrows(NullPointerException.class,
137             () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), (SchemaPath) null));
138     }
139
140     @Test
141     public void findDataSchemaNodeIllegalArgumentTest2() {
142         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null,
143             SchemaPath.create(true, QName.create(URI.create("uri:my-module"), Revision.of("2014-10-07"), "foo"))));
144     }
145
146     @Test
147     public void findDataSchemaNodeFromXPathNullTest2() {
148         final SchemaContext mockContext = mock(SchemaContext.class);
149         final Module module = mock(Module.class);
150         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
151
152         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
153     }
154
155     @Test
156     public void testNormalizeXPath() {
157         assertNormalizedPath(0, ImmutableList.of(""), "");
158         assertNormalizedPath(0, ImmutableList.of("a"), "a");
159         assertNormalizedPath(0, ImmutableList.of("a", "b"), "a b");
160         assertNormalizedPath(1, ImmutableList.of("..", "b"), ".. b");
161         assertNormalizedPath(0, ImmutableList.of(), "a ..");
162         assertNormalizedPath(0, ImmutableList.of("b"), "a .. b");
163         assertNormalizedPath(2, ImmutableList.of("..", "..", "a", "c"), ".. .. a b .. c");
164         assertNormalizedPath(3, ImmutableList.of("..", "..", "..", "b"), ".. .. a .. .. b");
165     }
166
167     private static void assertNormalizedPath(final int expectedLead, final List<String> expectedList,
168             final String input) {
169         final List<String> list = new ArrayList<>(SPACE_SPLITTER.splitToList(input));
170         assertEquals(expectedLead, SchemaContextUtil.normalizeXPath(list));
171         assertEquals(expectedList, list);
172     }
173 }