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