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