Further testing cleanup
[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.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.TypeDefinitions;
26
27 public class TypedefConstraintsTest extends AbstractYangTest {
28     @Test
29     public void decimalRangeConstraintsTest() {
30         final var context = assertEffectiveModelDir("/stmt-test/constraints");
31
32         assertNotNull(context);
33
34         final Collection<? extends TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
35         assertNotNull(typeDefinitions);
36         assertEquals(1, typeDefinitions.size());
37
38         final TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();
39
40         assertNotNull(myDecimal);
41         assertTrue(myDecimal instanceof DecimalTypeDefinition);
42
43         final Set<? extends Range<?>> rangeConstraints = ((DecimalTypeDefinition) myDecimal).getRangeConstraint()
44                 .get().getAllowedRanges().asRanges();
45
46         assertNotNull(rangeConstraints);
47         assertEquals(1, rangeConstraints.size());
48
49         final DataSchemaNode dataNode = context.getDataChildByName(QName.create("urn:opendaylight.foo", "2013-10-08",
50             "id-decimal64"));
51         assertNotNull(dataNode);
52         assertTrue(dataNode instanceof LeafSchemaNode);
53
54         final LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
55         final TypeDefinition<?> type = leafDecimal.getType();
56
57         assertTrue(type instanceof DecimalTypeDefinition);
58         final DecimalTypeDefinition decType = (DecimalTypeDefinition) type;
59
60         final Set<? extends Range<?>> decRangeConstraints = decType.getRangeConstraint().get().getAllowedRanges()
61                 .asRanges();
62
63         assertEquals(1, decRangeConstraints.size());
64
65         final Range<?> range = decRangeConstraints.iterator().next();
66         assertEquals(Decimal64.valueOf("1.5"), range.lowerEndpoint());
67         assertEquals(Decimal64.valueOf("5.5"), range.upperEndpoint());
68
69         assertEquals(TypeDefinitions.DECIMAL64.bindTo(leafDecimal.getQName().getModule()), decType.getQName());
70         assertNull(decType.getBaseType());
71     }
72 }