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.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
15 import com.google.common.base.Splitter;
16 import com.google.common.collect.ImmutableList;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Optional;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.PathExpression;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
36 @RunWith(MockitoJUnitRunner.StrictStubs.class)
37 public class SchemaContextUtilTest {
38 private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
39 private static final URI NAMESPACE = URI.create("abc");
53 private static final QName FOO = QName.create(NAMESPACE, "foo");
54 private static final QName BAR = QName.create(NAMESPACE, "bar");
55 private static final QName BAZ = QName.create(NAMESPACE, "baz");
56 private static final QName XYZZY = QName.create(NAMESPACE, "xyzzy");
59 private SchemaContext mockSchemaContext;
61 private Module mockModule;
64 private SchemaNode schemaNode;
67 public void before() {
68 doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
69 doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
71 doReturn("test").when(mockModule).getName();
72 doReturn("test").when(mockModule).getPrefix();
73 doReturn(NAMESPACE).when(mockModule).getNamespace();
74 doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
75 doReturn(Optional.empty()).when(mockModule).getRevision();
77 doReturn(SchemaPath.create(true, BAZ, XYZZY)).when(schemaNode).getPath();
81 public void testFindDummyData() {
83 QName qname = QName.create("namespace", "localname");
84 SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
85 assertNull("Should be null. Module TestQName not found",
86 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
88 PathExpression xpath = new PathExpressionImpl("/test:bookstore/test:book/test:title", true);
89 assertNull("Should be null. Module bookstore not found",
90 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
92 SchemaNode int32node = BaseTypes.int32Type();
93 PathExpression xpathRelative = new PathExpressionImpl("../prefix", false);
94 assertNull("Should be null, Module prefix not found",
95 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
96 mockSchemaContext, mockModule, int32node, xpathRelative));
98 assertNull("Should be null. Module TestQName not found",
99 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
101 assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
105 public void testDeref() {
106 PathExpression xpath = new PathExpressionImpl("deref(../foo)/../bar", false);
107 assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
112 public void testNormalizeXPath() {
113 assertNormalizedPath(0, ImmutableList.of(""), "");
114 assertNormalizedPath(0, ImmutableList.of("a"), "a");
115 assertNormalizedPath(0, ImmutableList.of("a", "b"), "a b");
116 assertNormalizedPath(1, ImmutableList.of("..", "b"), ".. b");
117 assertNormalizedPath(0, ImmutableList.of(), "a ..");
118 assertNormalizedPath(0, ImmutableList.of("b"), "a .. b");
119 assertNormalizedPath(2, ImmutableList.of("..", "..", "a", "c"), ".. .. a b .. c");
120 assertNormalizedPath(3, ImmutableList.of("..", "..", "..", "b"), ".. .. a .. .. b");
123 private static void assertNormalizedPath(final int expectedLead, final List<String> expectedList,
124 final String input) {
125 final List<String> list = new ArrayList<>(SPACE_SPLITTER.splitToList(input));
126 assertEquals(expectedLead, SchemaContextUtil.normalizeXPath(list));
127 assertEquals(expectedList, list);