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.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;
28 public class TypedefConstraintsTest {
30 public void decimalRangeConstraintsTest() throws Exception {
31 final SchemaContext context = StmtTestUtils.parseYangSources("/stmt-test/constraints");
33 assertNotNull(context);
35 final Collection<? extends TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
36 assertNotNull(typeDefinitions);
37 assertEquals(1, typeDefinitions.size());
39 final TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();
41 assertNotNull(myDecimal);
42 assertTrue(myDecimal instanceof DecimalTypeDefinition);
44 final Set<? extends Range<?>> rangeConstraints = ((DecimalTypeDefinition) myDecimal).getRangeConstraint()
45 .get().getAllowedRanges().asRanges();
47 assertNotNull(rangeConstraints);
48 assertEquals(1, rangeConstraints.size());
50 final DataSchemaNode dataNode = context.getDataChildByName(QName.create("urn:opendaylight.foo", "2013-10-08",
52 assertNotNull(dataNode);
53 assertTrue(dataNode instanceof LeafSchemaNode);
55 final LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
56 final TypeDefinition<?> type = leafDecimal.getType();
58 assertTrue(type instanceof DecimalTypeDefinition);
59 final DecimalTypeDefinition decType = (DecimalTypeDefinition) type;
61 final Set<? extends Range<?>> decRangeConstraints = decType.getRangeConstraint().get().getAllowedRanges()
64 assertEquals(1, decRangeConstraints.size());
66 final Range<?> range = decRangeConstraints.iterator().next();
67 assertEquals(Decimal64.valueOf(1.5), range.lowerEndpoint());
68 assertEquals(Decimal64.valueOf(5.5), range.upperEndpoint());
70 assertEquals(TypeDefinitions.DECIMAL64.bindTo(leafDecimal.getQName().getModule()), decType.getQName());
71 assertNull(decType.getBaseType());