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");
42 private SchemaContext mockSchemaContext;
44 private Module mockModule;
47 private SchemaNode schemaNode;
50 public void before() {
51 doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
52 doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
54 doReturn("test").when(mockModule).getName();
55 doReturn("test").when(mockModule).getPrefix();
56 doReturn(NAMESPACE).when(mockModule).getNamespace();
57 doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
58 doReturn(Optional.empty()).when(mockModule).getRevision();
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));
73 SchemaNode int32node = BaseTypes.int32Type();
74 PathExpression xpathRelative = new PathExpressionImpl("../prefix", false);
75 assertNull("Should be null, Module prefix not found",
76 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
77 mockSchemaContext, mockModule, int32node, xpathRelative));
79 assertNull("Should be null. Module TestQName not found",
80 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
82 assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
86 public void testNormalizeXPath() {
87 assertNormalizedPath(0, ImmutableList.of(""), "");
88 assertNormalizedPath(0, ImmutableList.of("a"), "a");
89 assertNormalizedPath(0, ImmutableList.of("a", "b"), "a b");
90 assertNormalizedPath(1, ImmutableList.of("..", "b"), ".. b");
91 assertNormalizedPath(0, ImmutableList.of(), "a ..");
92 assertNormalizedPath(0, ImmutableList.of("b"), "a .. b");
93 assertNormalizedPath(2, ImmutableList.of("..", "..", "a", "c"), ".. .. a b .. c");
94 assertNormalizedPath(3, ImmutableList.of("..", "..", "..", "b"), ".. .. a .. .. b");
97 private static void assertNormalizedPath(final int expectedLead, final List<String> expectedList,
99 final List<String> list = new ArrayList<>(SPACE_SPLITTER.splitToList(input));
100 assertEquals(expectedLead, SchemaContextUtil.normalizeXPath(list));
101 assertEquals(expectedList, list);