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