BUG-4688: Rework SchemaContext module lookups
[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.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13
14 import java.util.Collections;
15 import java.util.Optional;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
27
28 public class SchemaContextUtilTest {
29     @Mock
30     private SchemaContext mockSchemaContext;
31     @Mock
32     private Module mockModule;
33
34     @Test
35     public void testFindDummyData() {
36         MockitoAnnotations.initMocks(this);
37         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
38
39         QName qname = QName.create("TestQName");
40         SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
41         assertEquals("Should be null. Module TestQName not found", null,
42                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
43
44         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
45         assertEquals("Should be null. Module bookstore not found", null,
46                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
47
48         SchemaNode schemaNode = BaseTypes.int32Type();
49         RevisionAwareXPath xpathRelative = new RevisionAwareXPathImpl("../prefix", false);
50         assertEquals("Should be null, Module prefix not found", null,
51                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
52                         mockSchemaContext, mockModule, schemaNode, xpathRelative));
53
54         assertEquals("Should be null. Module TestQName not found", null,
55                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
56
57         assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
58     }
59 }