2 * Copyright (c) 2017 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.data.impl.schema;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.util.Collection;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class SchemaUtilsTest {
26 private static String NS = "my-namespace";
27 private static String REV = "1970-01-01";
30 public void test() throws Exception {
31 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/schema-utils-test/foo.yang");
32 assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
33 SchemaPath.create(true, qN("my-name"), qN("my-name"))) instanceof ContainerSchemaNode);
34 assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
35 SchemaPath.create(true, qN("my-name-2"), qN("my-name"))) instanceof NotificationDefinition);
36 assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
37 SchemaPath.create(true, qN("my-name-2"), qN("my-name-2"))) instanceof ActionDefinition);
41 public void testNameConflicts() throws Exception {
42 final SchemaContext schemaContext = YangParserTestUtils
43 .parseYangResource("/schema-utils-test/name-conflicts.yang");
44 // test my-name conflicts
45 assertEquals(8, SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
46 SchemaPath.create(true, qN("my-name"), qN("my-name"), qN("my-name"))).size());
48 // test target container
49 final Collection<SchemaNode> target = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
50 SchemaPath.create(true, qN("my-name-2"), qN("my-name-2"), qN("target")));
51 assertEquals(1, target.size());
52 assertTrue(target.iterator().next() instanceof ContainerSchemaNode);
54 // test l schema nodes (i.e. container and two leafs)
55 Collection<SchemaNode> l = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
56 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-3"), qN("l")));
57 assertEquals(1, l.size());
58 assertTrue(l.iterator().next() instanceof ContainerSchemaNode);
60 l = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
61 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-1"), qN("l")));
62 assertEquals(1, l.size());
63 assertTrue(l.iterator().next() instanceof LeafSchemaNode);
65 l = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
66 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-2"), qN("l")));
67 assertTrue(l.isEmpty());
69 l = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
70 SchemaPath.create(true, qN("my-name-3"), qN("output"), qN("con-2"), qN("l")));
71 assertEquals(1, l.size());
72 assertTrue(l.iterator().next() instanceof LeafSchemaNode);
75 private QName qN(final String localName) {
76 return QName.create(NS, REV, localName);