Bug 6771: Problem with typedefs nested in augment
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6771Test.java
1 /*
2  * Copyright (c) 2016 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.stmt;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.FileNotFoundException;
14 import java.net.URISyntaxException;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
26
27 public class Bug6771Test {
28     private static final String NS = "http://www.example.com/typedef-bug";
29     private static final String REV = "1970-01-01";
30     private static final QName ROOT = QName.create(NS, REV, "root");
31     private static final QName CONT_B = QName.create(NS, REV, "container-b");
32     private static final QName LEAF_CONT_B = QName.create(NS, REV, "leaf-container-b");
33     private static final QName INNER_CONTAINER = QName.create(NS, REV, "inner-container");
34
35     @Test
36     public void augmentTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
37         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6771/augment");
38         assertNotNull(context);
39
40         verifyLeafType(SchemaContextUtil
41                 .findDataSchemaNode(context, SchemaPath.create(true, ROOT, CONT_B, LEAF_CONT_B)));
42         verifyLeafType(SchemaContextUtil.findDataSchemaNode(context,
43                 SchemaPath.create(true, ROOT, CONT_B, INNER_CONTAINER, LEAF_CONT_B)));
44     }
45
46     @Test
47     public void choiceCaseTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
48         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6771/choice-case");
49         assertNotNull(context);
50
51         final QName myChoice = QName.create(NS, REV, "my-choice");
52         final QName caseOne = QName.create(NS, REV, "one");
53         final QName caseTwo = QName.create(NS, REV, "two");
54         final QName caseThree = QName.create(NS, REV, "three");
55         final QName containerOne = QName.create(NS, REV, "container-one");
56         final QName containerTwo = QName.create(NS, REV, "container-two");
57         final QName containerThree = QName.create(NS, REV, "container-three");
58
59         verifyLeafType(SchemaContextUtil.findDataSchemaNode(context,
60                 SchemaPath.create(true, ROOT, myChoice, caseOne, containerOne, LEAF_CONT_B)));
61         verifyLeafType(SchemaContextUtil.findDataSchemaNode(context,
62                 SchemaPath.create(true, ROOT, myChoice, caseTwo, containerTwo, LEAF_CONT_B)));
63         verifyLeafType(SchemaContextUtil.findDataSchemaNode(context,
64                 SchemaPath.create(true, ROOT, myChoice, caseThree, containerThree, INNER_CONTAINER, LEAF_CONT_B)));
65     }
66
67     @Test
68     public void groupingTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
69         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6771/grouping");
70         assertNotNull(context);
71         verifyLeafType(SchemaContextUtil
72                 .findDataSchemaNode(context, SchemaPath.create(true, ROOT, CONT_B, LEAF_CONT_B)));
73     }
74
75     private static void verifyLeafType(final SchemaNode schemaNode) {
76         assertTrue(schemaNode instanceof LeafSchemaNode);
77         assertTrue(((LeafSchemaNode) schemaNode).getType() instanceof UnsignedIntegerTypeDefinition);
78     }
79
80 }