2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
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;
17 import com.google.common.base.Splitter;
18 import com.google.common.collect.ImmutableList;
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;
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");
45 public SchemaContext mockSchemaContext;
47 public Module mockModule;
49 public SchemaNode schemaNode;
52 public void before() {
53 doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
54 doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
56 doReturn("test").when(mockModule).getName();
57 doReturn("test").when(mockModule).getPrefix();
58 doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
62 public void testFindDummyData() {
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));
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));
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));
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));
84 assertNull("Should be null. Module TestQName not found",
85 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
87 assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
91 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
92 assertThrows(NullPointerException.class,
93 () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null));
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);
101 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath));
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);
109 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null, module, xpath));
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);
118 assertThrows(IllegalArgumentException.class,
119 () -> SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
123 public void findParentModuleIllegalArgumentTest() {
124 assertThrows(NullPointerException.class,
125 () -> SchemaContextUtil.findParentModule(mock(SchemaContext.class), null));
129 public void findParentModuleIllegalArgumentTest2() {
130 doReturn(SchemaPath.create(true, QName.create("foo", "bar"))).when(schemaNode).getPath();
131 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findParentModule(null, schemaNode));
135 public void findDataSchemaNodeIllegalArgumentTest() {
136 assertThrows(NullPointerException.class,
137 () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), (SchemaPath) null));
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"))));
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);
152 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
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");
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);