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
38 .create("foo", "1970-01-01", "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();
49 assertEquals(0, enumPair.getValue());
52 assertEquals(20, enumPair.getValue());
55 assertEquals(21, enumPair.getValue());
58 assertEquals(2, enumPair.getValue());
61 assertEquals(22, enumPair.getValue());
64 fail("Unexpected enum name.");
69 private static void verifyBitsTypedefinition(final SchemaContext context) {
70 final DataSchemaNode dataChildByName = context.getDataChildByName(QName
71 .create("foo", "1970-01-01", "bits-leaf"));
72 assertTrue(dataChildByName instanceof LeafSchemaNode);
73 final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
74 final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
75 assertTrue(type instanceof BitsTypeDefinition);
76 final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
77 final List<Bit> positions = myBits.getBits();
78 for (final Bit bit : positions) {
79 final String name = bit.getName();
82 assertEquals(0, bit.getPosition());
85 assertEquals(20, bit.getPosition());
88 assertEquals(21, bit.getPosition());
91 assertEquals(2, bit.getPosition());
94 assertEquals(22, bit.getPosition());
97 fail("Unexpected bit name.");