7fb8e4aebb5659a431f004e19ac3a1ce73c59925
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtilsTest.java
1 /*
2  * Copyright (c) 2017 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.data.impl.schema;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
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;
24
25 public class SchemaUtilsTest {
26     private static String NS = "my-namespace";
27     private static String REV = "1970-01-01";
28
29     @Test
30     public void test() throws Exception {
31         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource("/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);
38     }
39
40     @Test
41     public void testNameConflicts() throws Exception {
42         final SchemaContext schemaContext = YangParserTestUtils
43                 .parseYangSource("/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());
47
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);
53
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);
59
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);
64
65         l = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
66                 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-2"), qN("l")));
67         assertTrue(l.isEmpty());
68
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);
73     }
74
75     private QName qN(final String localName) {
76         return QName.create(NS, REV, localName);
77     }
78
79 }