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
9 package org.opendaylight.yangtools.yang.stmt;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
16 import java.math.BigDecimal;
17 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
29 public class TypedefConstraintsTest {
32 public void decimalRangeConstraintsTest() throws Exception {
33 final SchemaContext context = StmtTestUtils.parseYangSources("/stmt-test/constraints");
35 assertNotNull(context);
37 final Set<TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
38 assertNotNull(typeDefinitions);
39 assertEquals(1, typeDefinitions.size());
41 final TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();
43 assertNotNull(myDecimal);
44 assertTrue(myDecimal instanceof DecimalTypeDefinition);
46 final List<RangeConstraint> rangeConstraints = ((DecimalTypeDefinition) myDecimal)
47 .getRangeConstraints();
49 assertNotNull(rangeConstraints);
50 assertEquals(1, rangeConstraints.size());
52 final DataSchemaNode dataNode = context.getDataChildByName(QName.create("urn:opendaylight.foo", "2013-10-08",
54 assertNotNull(dataNode);
55 assertTrue(dataNode instanceof LeafSchemaNode);
57 final LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
58 final TypeDefinition<?> type = leafDecimal.getType();
60 assertTrue(type instanceof DecimalTypeDefinition);
61 final DecimalTypeDefinition decType = (DecimalTypeDefinition) type;
63 final List<RangeConstraint> decRangeConstraints = decType.getRangeConstraints();
65 assertEquals(1, decRangeConstraints.size());
67 final RangeConstraint range = decRangeConstraints.iterator().next();
68 assertEquals(new BigDecimal(1.5), range.getMin());
69 assertEquals(new BigDecimal(5.5), range.getMax());
71 assertTrue(decType.getQName().getModule().equals(leafDecimal.getQName().getModule()));
72 assertTrue(decType.getQName().getLocalName().equals(TypeUtils.DECIMAL64));
73 assertNull(decType.getBaseType());