Ietf-routing identity issue test
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6316Test.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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
24 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
26
27 public class Bug6316Test {
28     @Test
29     public void test() throws Exception {
30         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6316");
31         assertNotNull(context);
32         verifyEnumTypedefinition(context);
33         verifyBitsTypedefinition(context);
34     }
35
36     private static void verifyEnumTypedefinition(final SchemaContext context) {
37         final DataSchemaNode dataChildByName = context.getDataChildByName(QName
38                 .create("foo", "1970-01-01", "enum-leaf"));
39         assertTrue(dataChildByName instanceof LeafSchemaNode);
40         final LeafSchemaNode enumLeaf = (LeafSchemaNode) dataChildByName;
41         final TypeDefinition<? extends TypeDefinition<?>> type = enumLeaf.getType();
42         assertTrue(type instanceof EnumTypeDefinition);
43         final EnumTypeDefinition myEnumeration = (EnumTypeDefinition) type;
44         final List<EnumPair> values = myEnumeration.getValues();
45         for (final EnumPair enumPair : values) {
46             final String name = enumPair.getName();
47             switch (name) {
48             case "zero":
49                 assertEquals(0, enumPair.getValue());
50                 break;
51             case "twenty":
52                 assertEquals(20, enumPair.getValue());
53                 break;
54             case "twenty-one":
55                 assertEquals(21, enumPair.getValue());
56                 break;
57             case "two":
58                 assertEquals(2, enumPair.getValue());
59                 break;
60             case "twenty-two":
61                 assertEquals(22, enumPair.getValue());
62                 break;
63             default:
64                 fail("Unexpected enum name.");
65             }
66         }
67     }
68
69     private static void verifyBitsTypedefinition(final SchemaContext context) {
70         final DataSchemaNode dataChildByName = context.getDataChildByName(QName
71                 .create("foo", "1970-01-01", "bits-leaf"));
72         assertTrue(dataChildByName instanceof LeafSchemaNode);
73         final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
74         final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
75         assertTrue(type instanceof BitsTypeDefinition);
76         final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
77         final List<Bit> positions = myBits.getBits();
78         for (final Bit bit : positions) {
79             final String name = bit.getName();
80             switch (name) {
81             case "zero":
82                 assertEquals(0, bit.getPosition());
83                 break;
84             case "twenty":
85                 assertEquals(20, bit.getPosition());
86                 break;
87             case "twenty-one":
88                 assertEquals(21, bit.getPosition());
89                 break;
90             case "two":
91                 assertEquals(2, bit.getPosition());
92                 break;
93             case "twenty-two":
94                 assertEquals(22, bit.getPosition());
95                 break;
96             default:
97                 fail("Unexpected bit name.");
98             }
99         }
100     }
101 }