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 final String NS = "my-namespace";
29 public void test() throws Exception {
30 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/schema-utils-test/foo.yang");
31 assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
32 SchemaPath.create(true, qN("my-name"), qN("my-name"))) instanceof ContainerSchemaNode);
33 assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
34 SchemaPath.create(true, qN("my-name-2"), qN("my-name"))) instanceof NotificationDefinition);
35 assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
36 SchemaPath.create(true, qN("my-name-2"), qN("my-name-2"))) instanceof ActionDefinition);
40 public void testNameConflicts() throws Exception {
41 final SchemaContext schemaContext = YangParserTestUtils
42 .parseYangResource("/schema-utils-test/name-conflicts.yang");
43 // test my-name conflicts
44 assertEquals(8, SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
45 SchemaPath.create(true, qN("my-name"), qN("my-name"), qN("my-name"))).size());
47 // test target container
48 final Collection<SchemaNode> target = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
49 SchemaPath.create(true, qN("my-name-2"), qN("my-name-2"), qN("target")));
50 assertEquals(1, target.size());
51 assertTrue(target.iterator().next() instanceof ContainerSchemaNode);
53 // test l schema nodes (i.e. container and two leafs)
54 Collection<SchemaNode> schema = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
55 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-3"), qN("l")));
56 assertEquals(1, schema.size());
57 assertTrue(schema.iterator().next() instanceof ContainerSchemaNode);
59 schema = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
60 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-1"), qN("l")));
61 assertEquals(1, schema.size());
62 assertTrue(schema.iterator().next() instanceof LeafSchemaNode);
64 schema = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
65 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-2"), qN("l")));
66 assertTrue(schema.isEmpty());
68 schema = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
69 SchemaPath.create(true, qN("my-name-3"), qN("output"), qN("con-2"), qN("l")));
70 assertEquals(1, schema.size());
71 assertTrue(schema.iterator().next() instanceof LeafSchemaNode);
74 private static QName qN(final String localName) {
75 return QName.create(NS, localName);