Move SchemaContextUtilTest
[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 import static org.mockito.Mockito.mock;
15
16 import com.google.common.base.Splitter;
17 import com.google.common.collect.ImmutableList;
18 import java.net.URI;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.PathExpression;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
37
38 @RunWith(MockitoJUnitRunner.StrictStubs.class)
39 public class SchemaContextUtilTest {
40     private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
41     private static final URI NAMESPACE = URI.create("abc");
42
43     @Mock
44     private SchemaContext mockSchemaContext;
45     @Mock
46     private Module mockModule;
47     @Mock
48     private SchemaNode schemaNode;
49
50     @Before
51     public void before() {
52         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
53         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
54
55         doReturn("test").when(mockModule).getName();
56         doReturn("test").when(mockModule).getPrefix();
57         doReturn(NAMESPACE).when(mockModule).getNamespace();
58         doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
59         doReturn(Optional.empty()).when(mockModule).getRevision();
60     }
61
62     @Test
63     public void testFindDummyData() {
64
65         QName qname = QName.create("namespace", "localname");
66         SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
67         assertNull("Should be null. Module TestQName not found",
68                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
69
70         PathExpression xpath = new PathExpressionImpl("/test:bookstore/test:book/test:title", true);
71         assertNull("Should be null. Module bookstore not found",
72                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
73
74
75         final PathExpression xPath = new PathExpressionImpl("/bookstore/book/title", true);
76         assertEquals("Should be null. Module bookstore not found", null,
77                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
78
79         SchemaNode int32node = BaseTypes.int32Type();
80         PathExpression xpathRelative = new PathExpressionImpl("../prefix", false);
81         assertNull("Should be null, Module prefix not found",
82                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
83                         mockSchemaContext, mockModule, int32node, xpathRelative));
84
85         assertNull("Should be null. Module TestQName not found",
86                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
87
88         assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
89     }
90
91     @Test(expected = NullPointerException.class)
92     public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
93         SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null);
94     }
95
96     @Test(expected = NullPointerException.class)
97     public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
98         final SchemaContext mockContext = mock(SchemaContext.class);
99         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
100
101         SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
102     }
103
104     @Test(expected = NullPointerException.class)
105     public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
106         final Module module = mock(Module.class);
107         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
108
109         SchemaContextUtil.findDataSchemaNode(null, module, xpath);
110     }
111
112     @Test(expected = IllegalArgumentException.class)
113     public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
114         final SchemaContext mockContext = mock(SchemaContext.class);
115         final Module module = mock(Module.class);
116         final PathExpression xpath = new PathExpressionImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
117
118         SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
119     }
120
121     @Test(expected = NullPointerException.class)
122     public void findParentModuleIllegalArgumentTest() {
123         SchemaContextUtil.findParentModule(mock(SchemaContext.class), null);
124     }
125
126     @Test(expected = NullPointerException.class)
127     public void findParentModuleIllegalArgumentTest2() {
128         doReturn(SchemaPath.create(true, QName.create("foo", "bar"))).when(schemaNode).getPath();
129         SchemaContextUtil.findParentModule(null, schemaNode);
130     }
131
132     @Test(expected = NullPointerException.class)
133     public void findDataSchemaNodeIllegalArgumentTest() {
134         SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), null);
135     }
136
137     @Test(expected = NullPointerException.class)
138     public void findDataSchemaNodeIllegalArgumentTest2() {
139         SchemaContextUtil.findDataSchemaNode(null, SchemaPath.create(true,
140             QName.create(URI.create("uri:my-module"), Revision.of("2014-10-07"), "foo")));
141     }
142
143     @Test
144     public void findDataSchemaNodeFromXPathNullTest2() {
145         final SchemaContext mockContext = mock(SchemaContext.class);
146         final Module module = mock(Module.class);
147         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
148
149         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
150     }
151
152     @Test
153     public void testNormalizeXPath() {
154         assertNormalizedPath(0, ImmutableList.of(""), "");
155         assertNormalizedPath(0, ImmutableList.of("a"), "a");
156         assertNormalizedPath(0, ImmutableList.of("a", "b"), "a b");
157         assertNormalizedPath(1, ImmutableList.of("..", "b"), ".. b");
158         assertNormalizedPath(0, ImmutableList.of(), "a ..");
159         assertNormalizedPath(0, ImmutableList.of("b"), "a .. b");
160         assertNormalizedPath(2, ImmutableList.of("..", "..", "a", "c"), ".. .. a b .. c");
161         assertNormalizedPath(3, ImmutableList.of("..", "..", "..", "b"), ".. .. a .. .. b");
162     }
163
164     private static void assertNormalizedPath(final int expectedLead, final List<String> expectedList,
165             final String input) {
166         final List<String> list = new ArrayList<>(SPACE_SPLITTER.splitToList(input));
167         assertEquals(expectedLead, SchemaContextUtil.normalizeXPath(list));
168         assertEquals(expectedList, list);
169     }
170 }