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.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.Range;
16 import java.util.Collection;
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;
27 public class TypedefConstraintsTest extends AbstractYangTest {
29 public void decimalRangeConstraintsTest() {
30 final var context = assertEffectiveModelDir("/stmt-test/constraints");
32 assertNotNull(context);
34 final Collection<? extends TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
35 assertNotNull(typeDefinitions);
36 assertEquals(1, typeDefinitions.size());
38 final TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();
40 assertNotNull(myDecimal);
41 assertTrue(myDecimal instanceof DecimalTypeDefinition);
43 final Set<? extends Range<?>> rangeConstraints = ((DecimalTypeDefinition) myDecimal).getRangeConstraint()
44 .get().getAllowedRanges().asRanges();
46 assertNotNull(rangeConstraints);
47 assertEquals(1, rangeConstraints.size());
49 final DataSchemaNode dataNode = context.getDataChildByName(QName.create("urn:opendaylight.foo", "2013-10-08",
51 assertNotNull(dataNode);
52 assertTrue(dataNode instanceof LeafSchemaNode);
54 final LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
55 final TypeDefinition<?> type = leafDecimal.getType();
57 assertTrue(type instanceof DecimalTypeDefinition);
58 final DecimalTypeDefinition decType = (DecimalTypeDefinition) type;
60 final Set<? extends Range<?>> decRangeConstraints = decType.getRangeConstraint().get().getAllowedRanges()
63 assertEquals(1, decRangeConstraints.size());
65 final Range<?> range = decRangeConstraints.iterator().next();
66 assertEquals(Decimal64.valueOf("1.5"), range.lowerEndpoint());
67 assertEquals(Decimal64.valueOf("5.5"), range.upperEndpoint());
69 assertEquals(TypeDefinitions.DECIMAL64.bindTo(leafDecimal.getQName().getModule()), decType.getQName());
70 assertNull(decType.getBaseType());