f25b435982aa9cbe506764551456df079031fe56
[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.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.base.Splitter;
18 import com.google.common.collect.ImmutableList;
19 import java.net.URI;
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;
38
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");
43
44     @Mock
45     public SchemaContext mockSchemaContext;
46     @Mock
47     public Module mockModule;
48     @Mock
49     public SchemaNode schemaNode;
50
51     @Before
52     public void before() {
53         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
54         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
55
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();
61     }
62
63     @Test
64     public void testFindDummyData() {
65
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));
70
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));
74
75
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));
79
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));
85
86         assertNull("Should be null. Module TestQName not found",
87                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
88
89         assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
90     }
91
92     @Test
93     public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
94         assertThrows(NullPointerException.class,
95             () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null));
96     }
97
98     @Test
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);
102
103         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath));
104     }
105
106     @Test
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);
110
111         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null, module, xpath));
112     }
113
114     @Test
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);
119
120         assertThrows(IllegalArgumentException.class,
121             () -> SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
122     }
123
124     @Test
125     public void findParentModuleIllegalArgumentTest() {
126         assertThrows(NullPointerException.class,
127             () -> SchemaContextUtil.findParentModule(mock(SchemaContext.class), null));
128     }
129
130     @Test
131     public void findParentModuleIllegalArgumentTest2() {
132         doReturn(SchemaPath.create(true, QName.create("foo", "bar"))).when(schemaNode).getPath();
133         assertThrows(NullPointerException.class, () -> SchemaContextUtil.findParentModule(null, schemaNode));
134     }
135
136     @Test
137     public void findDataSchemaNodeIllegalArgumentTest() {
138         assertThrows(NullPointerException.class,
139             () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), (SchemaPath) null));
140     }
141
142     @Test
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"))));
146     }
147
148     @Test
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);
153
154         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
155     }
156
157     @Test
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");
167     }
168
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);
174     }
175 }