2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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;
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;
27 public class Bug6316Test {
29 public void test() throws Exception {
30 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6316");
31 assertNotNull(context);
32 verifyEnumTypedefinition(context);
33 verifyBitsTypedefinition(context);
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();
48 assertEquals(0, enumPair.getValue());
51 assertEquals(20, enumPair.getValue());
54 assertEquals(21, enumPair.getValue());
57 assertEquals(2, enumPair.getValue());
60 assertEquals(22, enumPair.getValue());
63 fail("Unexpected enum name.");
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();
80 assertEquals(0, bit.getPosition());
83 assertEquals(20, bit.getPosition());
86 assertEquals(21, bit.getPosition());
89 assertEquals(2, bit.getPosition());
92 assertEquals(22, bit.getPosition());
95 fail("Unexpected bit name.");