1 package org.opendaylight.yangtools.yang.stmt.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
7 import java.io.FileNotFoundException;
8 import java.math.BigDecimal;
9 import java.net.URISyntaxException;
10 import java.util.List;
12 import org.junit.Test;
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
23 public class TypedefConstraintsTest {
26 public void decimalRangeConstraintsTest() throws SourceException,
27 FileNotFoundException, ReactorException, URISyntaxException {
28 SchemaContext context = StmtTestUtils
29 .parseYangSources("/stmt-test/constraints");
31 assertNotNull(context);
33 Set<TypeDefinition<?>> typeDefinitions = context.getTypeDefinitions();
34 assertNotNull(typeDefinitions);
35 assertEquals(1, typeDefinitions.size());
37 TypeDefinition<?> myDecimal = typeDefinitions.iterator().next();
39 assertNotNull(myDecimal);
40 assertTrue(myDecimal instanceof DecimalTypeDefinition);
42 List<RangeConstraint> rangeConstraints = ((DecimalTypeDefinition) myDecimal)
43 .getRangeConstraints();
45 assertNotNull(rangeConstraints);
46 assertEquals(1, rangeConstraints.size());
48 DataSchemaNode dataNode = context.getDataChildByName("id-decimal64");
49 assertNotNull(dataNode);
50 assertTrue(dataNode instanceof LeafSchemaNode);
52 LeafSchemaNode leafDecimal = (LeafSchemaNode) dataNode;
53 TypeDefinition<?> type = leafDecimal.getType();
55 assertTrue(type instanceof DecimalTypeDefinition);
56 DecimalTypeDefinition decType = (DecimalTypeDefinition) type;
58 List<RangeConstraint> decRangeConstraints = decType.getRangeConstraints();
60 assertEquals(1, decRangeConstraints.size());
62 RangeConstraint range = decRangeConstraints.iterator().next();
63 assertEquals(new BigDecimal(1.5), range.getMin());
64 assertEquals(new BigDecimal(5.5), range.getMax());
66 assertTrue(decType.getQName().getModule().equals(leafDecimal.getQName().getModule()));
67 assertTrue(decType.getQName().getLocalName().equals(TypeUtils.DECIMAL64));
68 assertNull(decType.getBaseType());