33406be696722799d715baa958925bd85551ae34
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TypedefConstraintsTest.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.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.Range;
16 import java.util.Collection;
17 import java.util.Set;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.Decimal64;
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.TypeDefinitions;
27
28 public class TypedefConstraintsTest {
29     @Test
30     public void decimalRangeConstraintsTest() throws Exception {
31         final SchemaContext context = StmtTestUtils.parseYangSources("/stmt-test/constraints");
32
33         assertNotNull(context);
34
35         final Collection<? extends TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
36         assertNotNull(typeDefinitions);
37         assertEquals(1, typeDefinitions.size());
38
39         final TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();
40
41         assertNotNull(myDecimal);
42         assertTrue(myDecimal instanceof DecimalTypeDefinition);
43
44         final Set<? extends Range<?>> rangeConstraints = ((DecimalTypeDefinition) myDecimal).getRangeConstraint()
45                 .get().getAllowedRanges().asRanges();
46
47         assertNotNull(rangeConstraints);
48         assertEquals(1, rangeConstraints.size());
49
50         final DataSchemaNode dataNode = context.getDataChildByName(QName.create("urn:opendaylight.foo", "2013-10-08",
51             "id-decimal64"));
52         assertNotNull(dataNode);
53         assertTrue(dataNode instanceof LeafSchemaNode);
54
55         final LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
56         final TypeDefinition<?> type = leafDecimal.getType();
57
58         assertTrue(type instanceof DecimalTypeDefinition);
59         final DecimalTypeDefinition decType = (DecimalTypeDefinition) type;
60
61         final Set<? extends Range<?>> decRangeConstraints = decType.getRangeConstraint().get().getAllowedRanges()
62                 .asRanges();
63
64         assertEquals(1, decRangeConstraints.size());
65
66         final Range<?> range = decRangeConstraints.iterator().next();
67         assertEquals(Decimal64.valueOf(1.5), range.lowerEndpoint());
68         assertEquals(Decimal64.valueOf(5.5), range.upperEndpoint());
69
70         assertEquals(TypeDefinitions.DECIMAL64.bindTo(leafDecimal.getQName().getModule()), decType.getQName());
71         assertNull(decType.getBaseType());
72     }
73 }