Merge branch 'master' of ../controller
[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 final String NS = "my-namespace";
27
28     @Test
29     public void test() {
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-a"))) instanceof ContainerSchemaNode);
33         assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
34                 SchemaPath.create(true, qN("my-name-2"), qN("my-name-b"))) instanceof NotificationDefinition);
35         assertTrue(SchemaUtils.findDataParentSchemaOnPath(schemaContext,
36                 SchemaPath.create(true, qN("my-name-2"), qN("my-name-2-b"))) instanceof ActionDefinition);
37     }
38
39     @Test
40     public void testNameConflicts() {
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-nested"), qN("my-name-nested2"))).size());
46
47         // test target container
48         final Collection<SchemaNode> target = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
49                 SchemaPath.create(true, qN("my-name-2"), qN("my-name-nested"), qN("target")));
50         assertEquals(1, target.size());
51         assertTrue(target.iterator().next() instanceof ContainerSchemaNode);
52
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);
58
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);
63
64         schema = SchemaUtils.findParentSchemaNodesOnPath(schemaContext,
65                 SchemaPath.create(true, qN("my-name-3"), qN("input"), qN("con-2"), qN("l")));
66         assertTrue(schema.isEmpty());
67
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);
72     }
73
74     private static QName qN(final String localName) {
75         return QName.create(NS, localName);
76     }
77 }