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(NAMESPACE).when(mockModule).getNamespace();
59 doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
60 doReturn(Optional.empty()).when(mockModule).getRevision();
64 public void testFindDummyData() {
66 QName qname = QName.create("namespace", "localname");
67 SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
68 assertNull("Should be null. Module TestQName not found",
69 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
71 PathExpression xpath = new PathExpressionImpl("/test:bookstore/test:book/test:title", true);
72 assertNull("Should be null. Module bookstore not found",
73 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
76 final PathExpression xPath = new PathExpressionImpl("/bookstore/book/title", true);
77 assertEquals("Should be null. Module bookstore not found", null,
78 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
80 SchemaNode int32node = BaseTypes.int32Type();
81 PathExpression xpathRelative = new PathExpressionImpl("../prefix", false);
82 assertNull("Should be null, Module prefix not found",
83 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
84 mockSchemaContext, mockModule, int32node, xpathRelative));
86 assertNull("Should be null. Module TestQName not found",
87 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
89 assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
93 public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
94 assertThrows(NullPointerException.class,
95 () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null));
99 public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
100 final SchemaContext mockContext = mock(SchemaContext.class);
101 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
103 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath));
107 public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
108 final Module module = mock(Module.class);
109 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
111 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null, module, xpath));
115 public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
116 final SchemaContext mockContext = mock(SchemaContext.class);
117 final Module module = mock(Module.class);
118 final PathExpression xpath = new PathExpressionImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
120 assertThrows(IllegalArgumentException.class,
121 () -> SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
125 public void findParentModuleIllegalArgumentTest() {
126 assertThrows(NullPointerException.class,
127 () -> SchemaContextUtil.findParentModule(mock(SchemaContext.class), null));
131 public void findParentModuleIllegalArgumentTest2() {
132 doReturn(SchemaPath.create(true, QName.create("foo", "bar"))).when(schemaNode).getPath();
133 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findParentModule(null, schemaNode));
137 public void findDataSchemaNodeIllegalArgumentTest() {
138 assertThrows(NullPointerException.class,
139 () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), (SchemaPath) null));
143 public void findDataSchemaNodeIllegalArgumentTest2() {
144 assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null,
145 SchemaPath.create(true, QName.create(URI.create("uri:my-module"), Revision.of("2014-10-07"), "foo"))));
149 public void findDataSchemaNodeFromXPathNullTest2() {
150 final SchemaContext mockContext = mock(SchemaContext.class);
151 final Module module = mock(Module.class);
152 final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
154 assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
158 public void testNormalizeXPath() {
159 assertNormalizedPath(0, ImmutableList.of(""), "");
160 assertNormalizedPath(0, ImmutableList.of("a"), "a");
161 assertNormalizedPath(0, ImmutableList.of("a", "b"), "a b");
162 assertNormalizedPath(1, ImmutableList.of("..", "b"), ".. b");
163 assertNormalizedPath(0, ImmutableList.of(), "a ..");
164 assertNormalizedPath(0, ImmutableList.of("b"), "a .. b");
165 assertNormalizedPath(2, ImmutableList.of("..", "..", "a", "c"), ".. .. a b .. c");
166 assertNormalizedPath(3, ImmutableList.of("..", "..", "..", "b"), ".. .. a .. .. b");
169 private static void assertNormalizedPath(final int expectedLead, final List<String> expectedList,
170 final String input) {
171 final List<String> list = new ArrayList<>(SPACE_SPLITTER.splitToList(input));
172 assertEquals(expectedLead, SchemaContextUtil.normalizeXPath(list));
173 assertEquals(expectedList, list);