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%2Fparser%2Fimpl%2FYangParserTest.java;h=23bb4a41d2f25fc16c501f06ed9d65b316206d53;hb=6341181f02c8ebd4a2b77716d310c5a151eb556a;hp=fd2593ba4805fade12bb2193e7a3d4cf94a5b097;hpb=aa16124c5eff863f9d75a7514910008d34f3f40c;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java index fd2593ba48..23bb4a41d2 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/impl/YangParserTest.java @@ -12,6 +12,7 @@ import static org.junit.Assert.*; import java.io.FileNotFoundException; import java.net.URI; import java.text.DateFormat; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -56,12 +57,15 @@ 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.Int8; import org.opendaylight.controller.yang.model.util.Leafref; import org.opendaylight.controller.yang.model.util.StringType; import org.opendaylight.controller.yang.model.util.Uint32; import org.opendaylight.controller.yang.model.util.UnionType; public class YangParserTest { + private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + private Set modules; @Before @@ -198,6 +202,55 @@ public class YangParserTest { assertTrue(ifMtu.getType() instanceof Int32); } + @Test + public void testParseLeaf() throws ParseException { + Module test = TestUtils.findModule(modules, "types2"); + + // leaf if-name + LeafSchemaNode ifName = (LeafSchemaNode) test + .getDataChildByName("if-name"); + Leafref ifNameType = (Leafref)ifName.getType(); + QName qname = ifNameType.getQName(); + + URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1"); + assertEquals(baseYangTypeNS, qname.getNamespace()); + assertNull(qname.getRevision()); + assertEquals("", qname.getPrefix()); + assertEquals("leafref", qname.getLocalName()); + + // leaf name + LeafSchemaNode name = (LeafSchemaNode) test + .getDataChildByName("name"); + StringType nameType = (StringType)name.getType(); + QName nameQName = nameType.getQName(); + + assertEquals(baseYangTypeNS, nameQName.getNamespace()); + assertNull(nameQName.getRevision()); + assertEquals("", nameQName.getPrefix()); + assertEquals("string", nameQName.getLocalName()); + + // leaf count + LeafSchemaNode count = (LeafSchemaNode) test + .getDataChildByName("count"); + ExtendedType countType = (ExtendedType)count.getType(); + QName countTypeQName = countType.getQName(); + + URI expectedNS = URI.create("urn:simple.types.data.demo"); + Date expectedDate = simpleDateFormat.parse("2013-02-27"); + assertEquals(expectedNS, countTypeQName.getNamespace()); + assertEquals(expectedDate, countTypeQName.getRevision()); + assertEquals("t2", countTypeQName.getPrefix()); + assertEquals("int8", countTypeQName.getLocalName()); + + Int8 countTypeBase = (Int8)countType.getBaseType(); + QName countTypeBaseQName = countTypeBase.getQName(); + + assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace()); + assertNull(countTypeBaseQName.getRevision()); + assertEquals("", countTypeBaseQName.getPrefix()); + assertEquals("int8", countTypeBaseQName.getLocalName()); + } + @Test public void testAugmentResolving() { // testfile1 @@ -371,8 +424,14 @@ public class YangParserTest { ExtendedType baseType = (ExtendedType) testleafType.getBaseType(); assertEquals("my-base-int32-type", baseType.getQName().getLocalName()); - Int32 int32base = (Int32) baseType.getBaseType(); - List ranges = int32base.getRangeStatements(); + ExtendedType int32Type = (ExtendedType) baseType.getBaseType(); + Int32 int32TypeBase = (Int32)int32Type.getBaseType(); + QName qname = int32TypeBase.getQName(); + assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), qname.getNamespace()); + assertNull(qname.getRevision()); + assertEquals("", qname.getPrefix()); + assertEquals("int32", qname.getLocalName()); + List ranges = int32Type.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(2L, range.getMin()); @@ -419,14 +478,14 @@ public class YangParserTest { UnionType unionBase = (UnionType) baseType.getBaseType(); List> unionTypes = unionBase.getTypes(); - Int16 unionType1 = (Int16) unionTypes.get(0); - List ranges = unionType1.getRangeStatements(); + ExtendedType unionType1 = (ExtendedType) unionTypes.get(0); + List ranges = unionType1.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(1L, range.getMin()); assertEquals(100L, range.getMax()); - assertTrue(unionTypes.get(0) instanceof Int16); + assertTrue(unionType1.getBaseType() instanceof Int16); assertTrue(unionTypes.get(1) instanceof Int32); } @@ -458,11 +517,12 @@ public class YangParserTest { .getBaseType(); List> extendedTargetTypes = extendedTargetUnion .getTypes(); - assertTrue(extendedTargetTypes.get(0) instanceof Int16); + assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16); assertTrue(extendedTargetTypes.get(1) instanceof Int32); - Int16 int16 = (Int16) extendedTargetTypes.get(0); - List ranges = int16.getRangeStatements(); + ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0); + assertTrue(int16.getBaseType() instanceof Int16); + List ranges = int16.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(1L, range.getMin()); @@ -536,10 +596,10 @@ public class YangParserTest { UnionType myUnionBase = (UnionType) myUnion.getBaseType(); List> myUnionBaseTypes = myUnionBase.getTypes(); assertEquals(2, myUnionBaseTypes.size()); - assertTrue(myUnionBaseTypes.get(0) instanceof Int16); + assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16); assertTrue(myUnionBaseTypes.get(1) instanceof Int32); - Int16 int16 = (Int16) myUnionBaseTypes.get(0); - List ranges = int16.getRangeStatements(); + ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0); + List ranges = int16.getRanges(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); assertEquals(1L, range.getMin()); @@ -770,7 +830,6 @@ public class YangParserTest { @Test public void testAugmentNodesTypesSchemaPath() throws Exception { - final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Module testModule = TestUtils.findModule(modules, "types1"); Set augments = testModule.getAugmentations(); assertEquals(1, augments.size()); @@ -807,4 +866,93 @@ public class YangParserTest { assertEquals(q3, higherLayerTypePath.get(3)); } + @Test + public void testTypePath() throws ParseException { + Module test = TestUtils.findModule(modules, "types2"); + Set> types = test.getTypeDefinitions(); + + // my-base-int32-type + ExtendedType int32Typedef = (ExtendedType)TestUtils.findTypedef(types, "my-base-int32-type"); + QName int32TypedefQName = int32Typedef.getQName(); + + URI expectedNS = URI.create("urn:simple.types.data.demo"); + Date expectedDate = simpleDateFormat.parse("2013-02-27"); + assertEquals(expectedNS, int32TypedefQName.getNamespace()); + assertEquals(expectedDate, int32TypedefQName.getRevision()); + assertEquals("t2", int32TypedefQName.getPrefix()); + assertEquals("my-base-int32-type", int32TypedefQName.getLocalName()); + + SchemaPath typeSchemaPath = int32Typedef.getPath(); + List typePath = typeSchemaPath.getPath(); + assertEquals(1, typePath.size()); + assertEquals(int32TypedefQName, typePath.get(0)); + + // my-base-int32-type/int32 + ExtendedType int32Ext = (ExtendedType)int32Typedef.getBaseType(); + QName int32ExtQName = int32Ext.getQName(); + + assertEquals(expectedNS, int32ExtQName.getNamespace()); + assertEquals(expectedDate, int32ExtQName.getRevision()); + assertEquals("t2", int32ExtQName.getPrefix()); + assertEquals("int32", int32ExtQName.getLocalName()); + + SchemaPath int32ExtSchemaPath = int32Ext.getPath(); + List int32ExtPath = int32ExtSchemaPath.getPath(); + assertEquals(2, int32ExtPath.size()); + assertEquals(int32TypedefQName, int32ExtPath.get(0)); + assertEquals(int32ExtQName, int32ExtPath.get(1)); + + // my-base-int32-type/int32/int32 + Int32 int32 = (Int32)int32Ext.getBaseType(); + QName int32QName = int32.getQName(); + assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace()); + assertNull(int32QName.getRevision()); + assertEquals("", int32QName.getPrefix()); + assertEquals("int32", int32QName.getLocalName()); + + SchemaPath int32SchemaPath = int32.getPath(); + List int32Path = int32SchemaPath.getPath(); + assertEquals(3, int32Path.size()); + assertEquals(int32TypedefQName, int32Path.get(0)); + assertEquals(int32ExtQName, int32Path.get(1)); + assertEquals(int32QName, int32Path.get(2)); + } + + @Test + public void testTypePath2() throws ParseException { + Module test = TestUtils.findModule(modules, "types2"); + Set> types = test.getTypeDefinitions(); + + // my-base-int32-type + ExtendedType myDecType = (ExtendedType)TestUtils.findTypedef(types, "my-decimal-type"); + QName myDecTypeQName = myDecType.getQName(); + + URI expectedNS = URI.create("urn:simple.types.data.demo"); + Date expectedDate = simpleDateFormat.parse("2013-02-27"); + assertEquals(expectedNS, myDecTypeQName.getNamespace()); + assertEquals(expectedDate, myDecTypeQName.getRevision()); + assertEquals("t2", myDecTypeQName.getPrefix()); + assertEquals("my-decimal-type", myDecTypeQName.getLocalName()); + + SchemaPath typeSchemaPath = myDecType.getPath(); + List typePath = typeSchemaPath.getPath(); + assertEquals(1, typePath.size()); + assertEquals(myDecTypeQName, typePath.get(0)); + + // my-base-int32-type/int32 + Decimal64 dec64 = (Decimal64)myDecType.getBaseType(); + QName dec64QName = dec64.getQName(); + + assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace()); + assertNull(dec64QName.getRevision()); + assertEquals("", dec64QName.getPrefix()); + assertEquals("decimal64", dec64QName.getLocalName()); + + SchemaPath dec64SchemaPath = dec64.getPath(); + List dec64Path = dec64SchemaPath.getPath(); + assertEquals(2, dec64Path.size()); + assertEquals(myDecTypeQName, dec64Path.get(0)); + assertEquals(dec64QName, dec64Path.get(1)); + } + }