f7ee7b77c9b487046d0c4e0330231ffbaf250283
[yangtools.git] / parser / 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.common.Uint32;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
25 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
27
28 public class Bug6316Test {
29     @Test
30     public void test() throws Exception {
31         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6316");
32         assertNotNull(context);
33         verifyEnumTypedefinition(context);
34         verifyBitsTypedefinition(context);
35     }
36
37     private static void verifyEnumTypedefinition(final SchemaContext context) {
38         final DataSchemaNode dataChildByName = context.getDataChildByName(QName.create("foo", "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.create("foo", "bits-leaf"));
71         assertTrue(dataChildByName instanceof LeafSchemaNode);
72         final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
73         final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
74         assertTrue(type instanceof BitsTypeDefinition);
75         final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
76         for (final Bit bit : myBits.getBits()) {
77             final String name = bit.getName();
78             switch (name) {
79                 case "zero":
80                     assertEquals(Uint32.ZERO, bit.getPosition());
81                     break;
82                 case "twenty":
83                     assertEquals(Uint32.valueOf(20), bit.getPosition());
84                     break;
85                 case "twenty-one":
86                     assertEquals(Uint32.valueOf(21), bit.getPosition());
87                     break;
88                 case "two":
89                     assertEquals(Uint32.TWO, bit.getPosition());
90                     break;
91                 case "twenty-two":
92                     assertEquals(Uint32.valueOf(22), bit.getPosition());
93                     break;
94                 default:
95                     fail("Unexpected bit name.");
96             }
97         }
98     }
99 }