X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fimpl%2FGroupingTest.java;h=fc6ed21b7949bf1140d7153934050887ebb820a7;hb=abced3230f02588c577c17c0cc039c5707c85081;hp=44cb0d635d976cbff7f9c96a258ad036b1502b77;hpb=f75b7abb9dcc2ba02a14a316212da1c1b434a9b3;p=yangtools.git diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java index 44cb0d635d..fc6ed21b79 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java @@ -7,24 +7,28 @@ */ package org.opendaylight.yangtools.yang.parser.impl; -import static org.junit.Assert.*; - +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.FileNotFoundException; +import java.io.IOException; import java.net.URI; -import java.text.ParseException; +import java.net.URISyntaxException; +import java.text.SimpleDateFormat; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; - import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; @@ -34,16 +38,18 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.MustDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.UsesNode; -import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; +import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; +import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils; public class GroupingTest { private Set modules; @Before - public void init() throws FileNotFoundException { - modules = TestUtils.loadModules(getClass().getResource("/model").getPath()); + public void init() throws IOException, URISyntaxException { + modules = TestUtils.loadModules(getClass().getResource("/model").toURI()); assertEquals(3, modules.size()); } @@ -103,7 +109,7 @@ public class GroupingTest { assertEquals("addresses reference added by refine", refineList.getReference()); assertFalse(refineList.isConfiguration()); assertEquals(2, (int) refineList.getConstraints().getMinElements()); - assertEquals(12, (int) refineList.getConstraints().getMaxElements()); + assertEquals(Integer.MAX_VALUE, (int) refineList.getConstraints().getMaxElements()); // leaf id assertNotNull(refineInnerLeaf); @@ -116,7 +122,7 @@ public class GroupingTest { Set groupings = testModule.getGroupings(); assertEquals(1, groupings.size()); GroupingDefinition grouping = groupings.iterator().next(); - Set children = grouping.getChildNodes(); + Collection children = grouping.getChildNodes(); assertEquals(5, children.size()); } @@ -151,17 +157,19 @@ public class GroupingTest { assertNotNull(data_g); assertFalse(data_g.isAddedByUses()); assertFalse(data_u.equals(data_g)); + assertEquals(data_g, SchemaNodeUtils.getRootOriginalIfPossible(data_u)); - ChoiceNode how_u = (ChoiceNode) destination.getDataChildByName("how"); + ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName("how"); assertNotNull(how_u); TestUtils.checkIsAddedByUses(how_u, true); assertEquals(2, how_u.getCases().size()); - ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how"); + ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName("how"); assertNotNull(how_g); TestUtils.checkIsAddedByUses(how_g, false); assertEquals(2, how_g.getCases().size()); assertFalse(how_u.equals(how_g)); + assertEquals(how_g, SchemaNodeUtils.getRootOriginalIfPossible(how_u)); LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName("address"); assertNotNull(address_u); @@ -181,6 +189,7 @@ public class GroupingTest { assertTrue(address_g.isConfiguration()); assertFalse(address_u.equals(address_g)); assertTrue(address_g.getConstraints().isMandatory()); + assertEquals(address_g, SchemaNodeUtils.getRootOriginalIfPossible(address_u)); ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName("port"); assertNotNull(port_u); @@ -190,6 +199,7 @@ public class GroupingTest { assertNotNull(port_g); TestUtils.checkIsAddedByUses(port_g, false); assertFalse(port_u.equals(port_g)); + assertEquals(port_g, SchemaNodeUtils.getRootOriginalIfPossible(port_u)); ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName("addresses"); assertNotNull(addresses_u); @@ -199,6 +209,7 @@ public class GroupingTest { assertNotNull(addresses_g); TestUtils.checkIsAddedByUses(addresses_g, false); assertFalse(addresses_u.equals(addresses_g)); + assertEquals(addresses_g, SchemaNodeUtils.getRootOriginalIfPossible(addresses_u)); // grouping defined by 'uses' Set groupings_u = destination.getGroupings(); @@ -253,8 +264,9 @@ public class GroupingTest { assertNotNull(data_g); assertFalse(data_g.isAddedByUses()); assertFalse(data_u.equals(data_g)); + assertEquals(data_g, SchemaNodeUtils.getRootOriginalIfPossible(data_u)); - ChoiceNode how_u = (ChoiceNode) foo.getDataChildByName("how"); + ChoiceSchemaNode how_u = (ChoiceSchemaNode) foo.getDataChildByName("how"); assertNotNull(how_u); TestUtils.checkIsAddedByUses(how_u, true); assertFalse(how_u.isAugmenting()); @@ -267,10 +279,11 @@ public class GroupingTest { LeafSchemaNode intervalLeaf = (LeafSchemaNode) interval.getDataChildByName("interval"); assertFalse(intervalLeaf.isAugmenting()); - ChoiceNode how_g = (ChoiceNode) grouping.getDataChildByName("how"); + ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName("how"); assertNotNull(how_g); TestUtils.checkIsAddedByUses(how_g, false); assertFalse(how_u.equals(how_g)); + assertEquals(how_g, SchemaNodeUtils.getRootOriginalIfPossible(how_u)); LeafSchemaNode address_u = (LeafSchemaNode) foo.getDataChildByName("address"); assertNotNull(address_u); @@ -288,6 +301,7 @@ public class GroupingTest { assertNull(address_g.getReference()); assertTrue(address_g.isConfiguration()); assertFalse(address_u.equals(address_g)); + assertEquals(address_g, SchemaNodeUtils.getRootOriginalIfPossible(address_u)); ContainerSchemaNode port_u = (ContainerSchemaNode) foo.getDataChildByName("port"); assertNotNull(port_u); @@ -297,6 +311,7 @@ public class GroupingTest { assertNotNull(port_g); TestUtils.checkIsAddedByUses(port_g, false); assertFalse(port_u.equals(port_g)); + assertEquals(port_g, SchemaNodeUtils.getRootOriginalIfPossible(port_u)); ListSchemaNode addresses_u = (ListSchemaNode) foo.getDataChildByName("addresses"); assertNotNull(addresses_u); @@ -306,6 +321,7 @@ public class GroupingTest { assertNotNull(addresses_g); TestUtils.checkIsAddedByUses(addresses_g, false); assertFalse(addresses_u.equals(addresses_g)); + assertEquals(addresses_g, SchemaNodeUtils.getRootOriginalIfPossible(addresses_u)); // grouping defined by 'uses' Set groupings_u = foo.getGroupings(); @@ -336,7 +352,7 @@ public class GroupingTest { assertEquals(1, usesAugments.size()); AugmentationSchema augment = usesAugments.iterator().next(); assertEquals("inner augment", augment.getDescription()); - Set children = augment.getChildNodes(); + Collection children = augment.getChildNodes(); assertEquals(1, children.size()); DataSchemaNode leaf = children.iterator().next(); assertTrue(leaf instanceof LeafSchemaNode); @@ -344,10 +360,10 @@ public class GroupingTest { } @Test - public void testCascadeUses() throws FileNotFoundException, ParseException { - File yangFile = new File(getClass().getResource("/grouping-test/cascade-uses.yang").getPath()); - YangModelParser parser = new YangParserImpl(); - modules = parser.parseYangModels(Collections.singletonList(yangFile)); + public void testCascadeUses() throws Exception { + File yangFile = new File(getClass().getResource("/grouping-test/cascade-uses.yang").toURI()); + YangContextParser parser = new YangParserImpl(); + modules = parser.parseFiles(Collections.singleton(yangFile)).getModules(); assertEquals(1, modules.size()); Module testModule = TestUtils.findModule(modules, "cascade-uses"); @@ -390,23 +406,24 @@ public class GroupingTest { assertNotNull(gzz); URI expectedNS = URI.create("urn:grouping:cascade-uses"); - Date expectedRev = TestUtils.simpleDateFormat.parse("2013-07-18"); + Date expectedRev = new SimpleDateFormat("yyyy-MM-dd").parse("2013-07-18"); String expectedPref = "cu"; SchemaPath expectedPath; // grouping-U - Set childNodes = gu.getChildNodes(); + Collection childNodes = gu.getChildNodes(); assertEquals(7, childNodes.size()); - LeafSchemaNode leafGroupingU = null; + + LeafSchemaNode leafGroupingU = (LeafSchemaNode) gu.getDataChildByName("leaf-grouping-U"); + assertNotNull(leafGroupingU); + assertFalse(leafGroupingU.isAddedByUses()); + assertFalse(SchemaNodeUtils.getOriginalIfPossible(leafGroupingU).isPresent()); + for (DataSchemaNode childNode : childNodes) { - if ("leaf-grouping-U".equals(childNode.getQName().getLocalName())) { - leafGroupingU = (LeafSchemaNode) childNode; - } else { + if (!(childNode.getQName().equals(leafGroupingU.getQName()))) { TestUtils.checkIsAddedByUses(childNode, true); } } - assertNotNull(leafGroupingU); - assertFalse(leafGroupingU.isAddedByUses()); // grouping-V childNodes = gv.getChildNodes(); @@ -505,6 +522,64 @@ public class GroupingTest { expectedPath = TestUtils.createPath(true, expectedNS, expectedRev, expectedPref, "grouping-ZZ", "leaf-grouping-ZZ"); assertEquals(expectedPath, leafZZinGZZ.getPath()); + + // TEST getOriginal from grouping-U + assertEquals(gv.getDataChildByName("leaf-grouping-V"), SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName("leaf-grouping-V"))); + containerGroupingV = (ContainerSchemaNode) gu.getDataChildByName("container-grouping-V"); + assertEquals(gv.getDataChildByName("container-grouping-V"), SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV)); + assertEquals(gx.getDataChildByName("leaf-grouping-X"), SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV.getDataChildByName("leaf-grouping-X") + )); + assertEquals(gy.getDataChildByName("leaf-grouping-Y"), SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV.getDataChildByName("leaf-grouping-Y") + )); + + assertEquals(gz.getDataChildByName("leaf-grouping-Z"), SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName("leaf-grouping-Z"))); + assertEquals(gzz.getDataChildByName("leaf-grouping-ZZ"), SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName("leaf-grouping-ZZ") + )); + + // TEST getOriginal from grouping-V + assertEquals(gz.getDataChildByName("leaf-grouping-Z"), SchemaNodeUtils.getRootOriginalIfPossible(gv.getDataChildByName("leaf-grouping-Z"))); + assertEquals(gzz.getDataChildByName("leaf-grouping-ZZ"), SchemaNodeUtils.getRootOriginalIfPossible(gv.getDataChildByName("leaf-grouping-ZZ") + )); + + // TEST getOriginal from grouping-X + assertEquals(gy.getDataChildByName("leaf-grouping-Y"),SchemaNodeUtils.getRootOriginalIfPossible( gx.getDataChildByName("leaf-grouping-Y"))); + } + + @Test + public void testAddedByUsesLeafTypeQName() throws IOException, + URISyntaxException { + + Set loadModules = TestUtils.loadModules(getClass().getResource( + "/added-by-uses-leaf-test").toURI()); + + assertEquals(2, loadModules.size()); + + Module foo = null; + Module imp = null; + for (Module module : loadModules) { + if (module.getName().equals("foo")) { + foo = module; + } + if (module.getName().equals("import-module")) { + imp = module; + } + } + + LeafSchemaNode leaf = (LeafSchemaNode) ((ContainerSchemaNode) foo + .getDataChildByName("my-container")) + .getDataChildByName("my-leaf"); + + TypeDefinition impType = null; + Set> typeDefinitions = imp.getTypeDefinitions(); + for (TypeDefinition typeDefinition : typeDefinitions) { + if (typeDefinition.getQName().getLocalName().equals("imp-type")) { + impType = typeDefinition; + break; + } + } + + assertEquals(leaf.getType().getQName(), impType.getQName()); + } }