2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
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;
29 public class SchemaContextUtilTest {
30 private static final URI NAMESPACE = URI.create("abc");
32 private SchemaContext mockSchemaContext;
34 private Module mockModule;
37 public void testFindDummyData() {
38 MockitoAnnotations.initMocks(this);
39 doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
40 doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
42 doReturn("test").when(mockModule).getName();
43 doReturn("test").when(mockModule).getPrefix();
44 doReturn(NAMESPACE).when(mockModule).getNamespace();
45 doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
46 doReturn(Optional.empty()).when(mockModule).getRevision();
48 QName qname = QName.create("namespace", "localname");
49 SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
50 assertEquals("Should be null. Module TestQName not found", null,
51 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
53 RevisionAwareXPath xpath = new RevisionAwareXPathImpl("/test:bookstore/test:book/test:title", true);
54 assertEquals("Should be null. Module bookstore not found", null,
55 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
57 SchemaNode schemaNode = BaseTypes.int32Type();
58 RevisionAwareXPath xpathRelative = new RevisionAwareXPathImpl("../prefix", false);
59 assertEquals("Should be null, Module prefix not found", null,
60 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
61 mockSchemaContext, mockModule, schemaNode, xpathRelative));
63 assertEquals("Should be null. Module TestQName not found", null,
64 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
66 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));