Further cleanup of tests
[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.assertTrue;
12 import static org.junit.Assert.fail;
13
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.Uint32;
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 extends AbstractYangTest {
28     @Test
29     public void test() {
30         final var context = assertEffectiveModelDir("/bugs/bug6316");
31         verifyEnumTypedefinition(context);
32         verifyBitsTypedefinition(context);
33     }
34
35     private static void verifyEnumTypedefinition(final SchemaContext context) {
36         final DataSchemaNode dataChildByName = context.getDataChildByName(QName.create("foo", "enum-leaf"));
37         assertTrue(dataChildByName instanceof LeafSchemaNode);
38         final LeafSchemaNode enumLeaf = (LeafSchemaNode) dataChildByName;
39         final TypeDefinition<? extends TypeDefinition<?>> type = enumLeaf.getType();
40         assertTrue(type instanceof EnumTypeDefinition);
41         final EnumTypeDefinition myEnumeration = (EnumTypeDefinition) type;
42         final List<EnumPair> values = myEnumeration.getValues();
43         for (final EnumPair enumPair : values) {
44             final String name = enumPair.getName();
45             switch (name) {
46                 case "zero":
47                     assertEquals(0, enumPair.getValue());
48                     break;
49                 case "twenty":
50                     assertEquals(20, enumPair.getValue());
51                     break;
52                 case "twenty-one":
53                     assertEquals(21, enumPair.getValue());
54                     break;
55                 case "two":
56                     assertEquals(2, enumPair.getValue());
57                     break;
58                 case "twenty-two":
59                     assertEquals(22, enumPair.getValue());
60                     break;
61                 default:
62                     fail("Unexpected enum name.");
63             }
64         }
65     }
66
67     private static void verifyBitsTypedefinition(final SchemaContext context) {
68         final DataSchemaNode dataChildByName = context.getDataChildByName(QName.create("foo", "bits-leaf"));
69         assertTrue(dataChildByName instanceof LeafSchemaNode);
70         final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
71         final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
72         assertTrue(type instanceof BitsTypeDefinition);
73         final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
74         for (final Bit bit : myBits.getBits()) {
75             final String name = bit.getName();
76             switch (name) {
77                 case "zero":
78                     assertEquals(Uint32.ZERO, bit.getPosition());
79                     break;
80                 case "twenty":
81                     assertEquals(Uint32.valueOf(20), bit.getPosition());
82                     break;
83                 case "twenty-one":
84                     assertEquals(Uint32.valueOf(21), bit.getPosition());
85                     break;
86                 case "two":
87                     assertEquals(Uint32.TWO, bit.getPosition());
88                     break;
89                 case "twenty-two":
90                     assertEquals(Uint32.valueOf(22), bit.getPosition());
91                     break;
92                 default:
93                     fail("Unexpected bit name.");
94             }
95         }
96     }
97 }