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));
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();
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));
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));
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));
62 assertEquals("Should be null. Module TestQName not found", null,
63 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
65 assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));