X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fyang-model-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fmodel%2Fparser%2Fimpl%2FYangModelParserTest.java;h=650eaeab8221f2c89fa53ff47c11d20c45661ea8;hb=6e72d221f55cfac71ee6e594b62cb5af9672614a;hp=74cfed5fb13bbfaf8eeed80298216869bc3a8286;hpb=ca85a6f3a39b406ab122fe985ee010a41489f658;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java index 74cfed5fb1..650eaeab82 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/model/parser/impl/YangModelParserTest.java @@ -9,85 +9,285 @@ package org.opendaylight.controller.yang.model.parser.impl; import static org.junit.Assert.*; -import java.io.IOException; +import java.util.HashSet; +import java.util.List; import java.util.Set; import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.model.util.Int32; +import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.AugmentationSchema; import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; import org.opendaylight.controller.yang.model.api.LeafSchemaNode; import org.opendaylight.controller.yang.model.api.ListSchemaNode; import org.opendaylight.controller.yang.model.api.Module; +import org.opendaylight.controller.yang.model.api.TypeDefinition; +import org.opendaylight.controller.yang.model.api.type.LengthConstraint; +import org.opendaylight.controller.yang.model.api.type.PatternConstraint; +import org.opendaylight.controller.yang.model.api.type.RangeConstraint; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; +import org.opendaylight.controller.yang.model.util.Decimal64; +import org.opendaylight.controller.yang.model.util.ExtendedType; +import org.opendaylight.controller.yang.model.util.Int16; +import org.opendaylight.controller.yang.model.util.Int32; +import org.opendaylight.controller.yang.model.util.StringType; +import org.opendaylight.controller.yang.model.util.UnionType; public class YangModelParserTest { - private final String testFile1 = "src/test/resources/model/testfile1.yang"; - private final String testFile2 = "src/test/resources/model/testfile2.yang"; + private static final String TEST_FILE1 = "src/test/resources/model/testfile1.yang"; + private static final String TEST_FILE2 = "src/test/resources/model/testfile2.yang"; private YangModelParser tested; @Before - public void init() throws IOException { + public void init() { tested = new YangModelParserImpl(); } @Test public void testAugment() { - Set modules = tested.parseYangModels(testFile1, testFile2); + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); assertEquals(2, modules.size()); - Module m2 = null; - for(Module m : modules) { - if(m.getName().equals("types2")) { - m2 = m; - } - } - assertNotNull(m2); + Module testModule = findModule(modules, "types2"); + assertNotNull(testModule); - AugmentationSchema augment = m2.getAugmentations().iterator().next(); + AugmentationSchema augment = testModule.getAugmentations().iterator().next(); assertNotNull(augment); } @Test public void testAugmentTarget() { - Set modules = tested.parseYangModels(testFile1, testFile2); + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); assertEquals(2, modules.size()); - Module m1 = null; - for(Module m : modules) { - if(m.getName().equals("types1")) { - m1 = m; - } - } - assertNotNull(m1); + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); - ContainerSchemaNode container = (ContainerSchemaNode)m1.getDataChildByName("interfaces"); + ContainerSchemaNode container = (ContainerSchemaNode)testModule.getDataChildByName("interfaces"); assertNotNull(container); ListSchemaNode list = (ListSchemaNode)container.getDataChildByName("ifEntry"); assertNotNull(list); + assertEquals(1, list.getAvailableAugmentations().size()); LeafSchemaNode leaf = (LeafSchemaNode)list.getDataChildByName("ds0ChannelNumber"); assertNotNull(leaf); } + @Test + public void testTypedefRangesResolving() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("testleaf"); + ExtendedType leafType = (ExtendedType)testleaf.getType(); + assertEquals("my-type1", leafType.getQName().getLocalName()); + assertEquals("t2", leafType.getQName().getPrefix()); + ExtendedType baseType = (ExtendedType)leafType.getBaseType(); + assertEquals("my-base-int32-type", baseType.getQName().getLocalName()); + assertEquals("t2", baseType.getQName().getPrefix()); + + List ranges = leafType.getRanges(); + assertEquals(1, ranges.size()); + RangeConstraint range = ranges.get(0); + assertEquals(11L, range.getMin()); + assertEquals(20L, range.getMax()); + } + + @Test + public void testTypedefPatternsResolving() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("test-string-leaf"); + ExtendedType testleafType = (ExtendedType)testleaf.getType(); + QName testleafTypeQName = testleafType.getQName(); + assertEquals("my-string-type-ext", testleafTypeQName.getLocalName()); + assertEquals("t2", testleafTypeQName.getPrefix()); + + Set expectedRegex = new HashSet(); + expectedRegex.add("[a-k]*"); + expectedRegex.add("[b-u]*"); + expectedRegex.add("[e-z]*"); + + Set actualRegex = new HashSet(); + List patterns = testleafType.getPatterns(); + assertEquals(3, patterns.size()); + for (PatternConstraint pc : patterns) { + actualRegex.add(pc.getRegularExpression()); + } + assertEquals(expectedRegex, actualRegex); + + TypeDefinition baseType = testleafType.getBaseType(); + assertEquals("my-string-type2", baseType.getQName().getLocalName()); + + List lengths = testleafType.getLengths(); + assertEquals(1, lengths.size()); + + LengthConstraint length = lengths.get(0); + assertEquals(5L, length.getMin()); + assertEquals(10L, length.getMax()); + } + + @Test + public void testTypedefLengthsResolving() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("leaf-with-length"); + ExtendedType testleafType = (ExtendedType)testleaf.getType(); + assertEquals("my-string-type", testleafType.getQName().getLocalName()); + + List lengths = testleafType.getLengths(); + assertEquals(1, lengths.size()); + + LengthConstraint length = lengths.get(0); + assertEquals(7L, length.getMin()); + assertEquals(10L, length.getMax()); + } + @Test public void testTypeDef() { - Set modules = tested.parseYangModels(testFile1, testFile2); + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types2"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("nested-type-leaf"); + ExtendedType testleafType = (ExtendedType)testleaf.getType(); + assertEquals("my-type1", testleafType.getQName().getLocalName()); + + ExtendedType baseType = (ExtendedType)testleafType.getBaseType(); + assertEquals("my-base-int32-type", baseType.getQName().getLocalName()); + + Int32 int32base = (Int32)baseType.getBaseType(); + List ranges = int32base.getRangeStatements(); + assertEquals(1, ranges.size()); + RangeConstraint range = ranges.get(0); + assertEquals(2L, range.getMin()); + assertEquals(20L, range.getMax()); + } + + @Test + public void testTypedefDecimal1() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("test-decimal-leaf"); + ExtendedType type = (ExtendedType)testleaf.getType(); + + TypeDefinition baseType = type.getBaseType(); + assertTrue(baseType instanceof Decimal64); + Decimal64 baseTypeCast = (Decimal64)baseType; + assertEquals(6, (int)baseTypeCast.getFractionDigits()); + } + + @Test + public void testTypedefDecimal2() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("test-decimal-leaf2"); + TypeDefinition baseType = testleaf.getType().getBaseType(); + assertTrue(testleaf.getType().getBaseType() instanceof Decimal64); + Decimal64 baseTypeCast = (Decimal64)baseType; + assertEquals(5, (int)baseTypeCast.getFractionDigits()); + } + + @Test + public void testTypedefUnion() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); + assertEquals(2, modules.size()); + + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("union-leaf"); + ExtendedType testleafType = (ExtendedType)testleaf.getType(); + assertEquals("my-union-ext", testleafType.getQName().getLocalName()); + + ExtendedType baseType = (ExtendedType)testleafType.getBaseType(); + assertEquals("my-union", baseType.getQName().getLocalName()); + + UnionType unionBase = (UnionType) baseType.getBaseType(); + + List> unionTypes = unionBase.getTypes(); + Int16 unionType1 = (Int16)unionTypes.get(0); + List ranges = unionType1.getRangeStatements(); + assertEquals(1, ranges.size()); + RangeConstraint range = ranges.get(0); + assertEquals(1L, range.getMin()); + assertEquals(100L, range.getMax()); + + assertTrue(unionTypes.get(0) instanceof Int16); + assertTrue(unionTypes.get(1) instanceof Int32); + } + + @Test + public void testNestedUnionResolving() { + Set modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2); assertEquals(2, modules.size()); - Module m1 = null; - for(Module m : modules) { - if(m.getName().equals("types1")) { - m1 = m; + Module testModule = findModule(modules, "types1"); + assertNotNull(testModule); + + LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("nested-union-leaf"); + + ExtendedType nestedUnion1 = (ExtendedType)testleaf.getType(); + assertEquals("nested-union1", nestedUnion1.getQName().getLocalName()); + + ExtendedType nestedUnion2 = (ExtendedType)nestedUnion1.getBaseType(); + assertEquals("nested-union2", nestedUnion2.getQName().getLocalName()); + + UnionType unionType1 = (UnionType)nestedUnion2.getBaseType(); + List> unionTypes = unionType1.getTypes(); + assertEquals(2, unionTypes.size()); + assertTrue(unionTypes.get(0) instanceof StringType); + assertTrue(unionTypes.get(1) instanceof ExtendedType); + + ExtendedType extendedUnion = (ExtendedType)unionTypes.get(1); + ExtendedType extendedUnionBase = (ExtendedType)extendedUnion.getBaseType(); + assertEquals("my-union", extendedUnionBase.getQName().getLocalName()); + + UnionType extendedTargetUnion = (UnionType)extendedUnionBase.getBaseType(); + List> extendedTargetTypes = extendedTargetUnion.getTypes(); + assertTrue(extendedTargetTypes.get(0) instanceof Int16); + assertTrue(extendedTargetTypes.get(1) instanceof Int32); + + Int16 int16 = (Int16) extendedTargetTypes.get(0); + List ranges = int16.getRangeStatements(); + assertEquals(1, ranges.size()); + RangeConstraint range = ranges.get(0); + assertEquals(1L, range.getMin()); + assertEquals(100L, range.getMax()); + } + + private Module findModule(Set modules, String moduleName) { + Module result = null; + for(Module module : modules) { + if(module.getName().equals(moduleName)) { + result = module; + break; } } - assertNotNull(m1); - - LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("testleaf"); - assertTrue(testleaf.getType().getBaseType() instanceof Int32); + return result; } }