YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / 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.create("foo", "enum-leaf"));
38         assertTrue(dataChildByName instanceof LeafSchemaNode);
39         final LeafSchemaNode enumLeaf = (LeafSchemaNode) dataChildByName;
40         final TypeDefinition<? extends TypeDefinition<?>> type = enumLeaf.getType();
41         assertTrue(type instanceof EnumTypeDefinition);
42         final EnumTypeDefinition myEnumeration = (EnumTypeDefinition) type;
43         final List<EnumPair> values = myEnumeration.getValues();
44         for (final EnumPair enumPair : values) {
45             final String name = enumPair.getName();
46             switch (name) {
47                 case "zero":
48                     assertEquals(0, enumPair.getValue());
49                     break;
50                 case "twenty":
51                     assertEquals(20, enumPair.getValue());
52                     break;
53                 case "twenty-one":
54                     assertEquals(21, enumPair.getValue());
55                     break;
56                 case "two":
57                     assertEquals(2, enumPair.getValue());
58                     break;
59                 case "twenty-two":
60                     assertEquals(22, enumPair.getValue());
61                     break;
62                 default:
63                     fail("Unexpected enum name.");
64             }
65         }
66     }
67
68     private static void verifyBitsTypedefinition(final SchemaContext context) {
69         final DataSchemaNode dataChildByName = context.getDataChildByName(QName.create("foo", "bits-leaf"));
70         assertTrue(dataChildByName instanceof LeafSchemaNode);
71         final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
72         final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
73         assertTrue(type instanceof BitsTypeDefinition);
74         final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
75         final List<Bit> positions = myBits.getBits();
76         for (final Bit bit : positions) {
77             final String name = bit.getName();
78             switch (name) {
79                 case "zero":
80                     assertEquals(0, bit.getPosition());
81                     break;
82                 case "twenty":
83                     assertEquals(20, bit.getPosition());
84                     break;
85                 case "twenty-one":
86                     assertEquals(21, bit.getPosition());
87                     break;
88                 case "two":
89                     assertEquals(2, bit.getPosition());
90                     break;
91                 case "twenty-two":
92                     assertEquals(22, bit.getPosition());
93                     break;
94                 default:
95                     fail("Unexpected bit name.");
96             }
97         }
98     }
99 }