Allow Errata 5617 expressions in leafref
[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.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14
15 import com.google.common.base.Splitter;
16 import com.google.common.collect.ImmutableList;
17 import java.net.URI;
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;
35
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");
40
41     // The idea is:
42     // container baz {
43     //     leaf xyzzy {
44     //         type leafref;
45     //     }
46     //     leaf foo {
47     //         type string;
48     //     }
49     //     leaf bar {
50     //         type string;
51     //     }
52     // }
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");
57
58     @Mock
59     private SchemaContext mockSchemaContext;
60     @Mock
61     private Module mockModule;
62
63     @Mock
64     private SchemaNode schemaNode;
65
66     @Before
67     public void before() {
68         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
69         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
70
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();
76
77         doReturn(SchemaPath.create(true, BAZ, XYZZY)).when(schemaNode).getPath();
78     }
79
80     @Test
81     public void testFindDummyData() {
82
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));
87
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));
91
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));
97
98         assertNull("Should be null. Module TestQName not found",
99                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
100
101         assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
102     }
103
104     @Test
105     public void testDeref() {
106         PathExpression xpath = new PathExpressionImpl("deref(../foo)/../bar", false);
107         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
108             xpath));
109     }
110
111     @Test
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");
121     }
122
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);
128     }
129 }