X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2FNormalizedDataBuilderTest.java;h=dc7f12f1315aaeb48d4310a0bc130ebe17648a20;hb=c07aad0f89c57a66089df3c431c1767ab4a04eea;hp=9c69016e19c4a086dd2496be4523fcf9b0d02b1b;hpb=9e5540fb30986583d69e51f9290dbb651338f4b0;p=yangtools.git diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java index 9c69016e19..dc7f12f131 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -7,20 +7,20 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema; -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; -import java.io.InputStream; +import static com.google.common.base.Preconditions.checkState; + +import java.io.File; +import java.io.FileNotFoundException; import java.net.URI; +import java.net.URISyntaxException; import java.util.Collections; -import java.util.List; -import java.util.Set; import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; @@ -31,7 +31,8 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeSchemaAwareBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder; -import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; +import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; +import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; @@ -41,71 +42,54 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; public class NormalizedDataBuilderTest { private ContainerSchemaNode containerNode; private SchemaContext schema; - SchemaContext parseTestSchema(final String... yangPath) { - YangParserImpl yangParserImpl = new YangParserImpl(); - Set modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath)); - return yangParserImpl.resolveSchemaContext(modules); - } - - List getTestYangs(final String... yangPaths) { - - return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths), - new Function() { - @Override - public InputStream apply(final String input) { - InputStream resourceAsStream = getClass().getResourceAsStream(input); - Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream); - return resourceAsStream; - } - })); - } - @Before - public void setUp() throws Exception { - schema = parseTestSchema("test.yang"); + public void setUp() throws URISyntaxException, FileNotFoundException, ReactorException { + final File resourceFile = new File(getClass().getResource( + "test.yang").toURI()); + schema = YangParserTestUtils.parseYangFiles(resourceFile); containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container"); } @Test - public void testSchemaUnaware() throws Exception { + public void testSchemaUnaware() { // Container - DataContainerNodeBuilder builder = Builders + DataContainerNodeBuilder builder = Builders .containerBuilder().withNodeIdentifier(getNodeIdentifier("container")); // leaf - LeafNode leafChild = Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf")) + LeafNode leafChild = Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("leaf")) .withValue("String").build(); builder.withChild(leafChild); // leafList - LeafSetNode leafList = Builders - . leafSetBuilder() + LeafSetNode leafList = Builders.leafSetBuilder() .withNodeIdentifier(getNodeIdentifier("leaf")) .withChildValue(1) .withChild( - Builders. leafSetEntryBuilder() - .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build()) - .build(); + Builders.leafSetEntryBuilder() + .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build()) + .build(); builder.withChild(leafList); // list MapEntryNode listChild1 = Builders .mapEntryBuilder() .withChild( - Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList")) - .withValue(1).build()) - .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build()) - .withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(), - Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1))) - .build(); + Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList")) + .withValue(1).build()) + .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build()) + .withNodeIdentifier( + new NodeIdentifierWithPredicates( + getNodeIdentifier("list").getNodeType(), Collections.singletonMap( + getNodeIdentifier("uint32InList").getNodeType(), 1))).build(); MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list")) .build(); @@ -114,31 +98,30 @@ public class NormalizedDataBuilderTest { AugmentationNode augmentation = Builders .augmentationBuilder() .withNodeIdentifier( - new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32")))) - .withChild( - Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32")) + new AugmentationIdentifier(Collections.singleton(getQName("augmentUint32")))) + .withChild( + Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32")) .withValue(11).build()).build(); builder.withChild(augmentation); // This works without schema (adding child from augment as a direct // child) - builder.withChild(Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32")) + builder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32")) .withValue(11).build()); } @Test - public void testSchemaAware() throws Exception { - DataContainerNodeBuilder builder = Builders - .containerBuilder(containerNode); + public void testSchemaAware() { + DataContainerNodeBuilder builder = Builders.containerBuilder(containerNode); LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32"); - LeafNode leafChild = Builders. leafBuilder(schemaNode).withValue("String").build(); + LeafNode leafChild = Builders.leafBuilder(schemaNode).withValue("String").build(); builder.withChild(leafChild); LeafListSchemaNode leafListSchemaNode = (LeafListSchemaNode) getSchemaNode(schema, "test", "leafList"); - LeafSetNode leafList = Builders. leafSetBuilder(leafListSchemaNode).withChildValue(1) - .withChild(Builders. leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build(); + LeafSetNode leafList = Builders.leafSetBuilder(leafListSchemaNode).withChildValue(1) + .withChild(Builders.leafSetEntryBuilder(leafListSchemaNode).withValue(3).build()).build(); builder.withChild(leafList); ListSchemaNode listSchema = (ListSchemaNode) getSchemaNode(schema, "test", "list"); @@ -147,14 +130,14 @@ public class NormalizedDataBuilderTest { "containerInList"); MapEntryNode listChild1 = Builders.mapEntryBuilder(listSchema) - .withChild(Builders. leafBuilder(uint32InListSchemaNode).withValue(1).build()) + .withChild(Builders.leafBuilder(uint32InListSchemaNode).withValue(1).build()) .withChild(Builders.containerBuilder(containerInListSchemaNode).build()).build(); MapNode list = ImmutableMapNodeSchemaAwareBuilder.create(listSchema).withChild(listChild1).build(); builder.withChild(list); LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32"); - AugmentationSchema augmentationSchema = getAugmentationSchemaForChild(containerNode, + AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode, augmentUint32SchemaNode.getQName()); AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema) @@ -163,21 +146,20 @@ public class NormalizedDataBuilderTest { builder.withChild(augmentation); // This should fail with schema, since the leaf comes from augmentation - // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.get(augmentUint32SchemaNode).withValue(11).build()); + // builder.withChild(ImmutableLeafNodeSchemaAwareBuilder.get(augmentUint32SchemaNode).withValue(11) + // .build()); LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1"); LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2"); ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice"); - ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder - .create(choice1SchemaNode) - .withChild(Builders. leafBuilder(augumentString1SchemaNode).withValue("case1").build()) + ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode) + .withChild(Builders.leafBuilder(augumentString1SchemaNode).withValue("case1").build()) // This should fail, since child node belongs to different case // .withChild(Builders.leafBuilder(augumentString2SchemaNode).withValue("case2") // .build()) .build(); - ; builder.withChild(choice); // This should fail, child from case @@ -185,60 +167,59 @@ public class NormalizedDataBuilderTest { // .build()); } - private AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode, final QName qName) { - for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) { - if (augmentationSchema.getDataChildByName(qName) != null) { + private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode, + final QName qname) { + for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) { + if (augmentationSchema.getDataChildByName(qname) != null) { return augmentationSchema; } } throw new IllegalStateException("Unable to find child augmentation in " + containerNode); } - private YangInstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(final String localName, final Object value) { - return new YangInstanceIdentifier.NodeWithValue(getQName(localName), value); + private static NodeWithValue getNodeWithValueIdentifier(final String localName, final T value) { + return new NodeWithValue<>(getQName(localName), value); } - private QName getQName(final String localName) { - String namespace = "namespace"; - return new QName(URI.create(namespace), localName); + private static QName getQName(final String localName) { + return QName.create(URI.create("namespace"), localName); } - private YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) { - return new YangInstanceIdentifier.NodeIdentifier(getQName(localName)); + private static NodeIdentifier getNodeIdentifier(final String localName) { + return new NodeIdentifier(getQName(localName)); } - public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName, final String childNodeName) { + public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName, + final String childNodeName) { for (Module module : context.getModules()) { if (module.getName().equals(moduleName)) { - DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName); - Preconditions.checkState(found != null, "Unable to find %s", childNodeName); + DataSchemaNode found = findChildNode(module, childNodeName); + checkState(found != null, "Unable to find %s", childNodeName); return found; } } throw new IllegalStateException("Unable to find child node " + childNodeName); } - private static DataSchemaNode findChildNode(final Iterable children, final String name) { - List containers = Lists.newArrayList(); - - for (DataSchemaNode dataSchemaNode : children) { + private static DataSchemaNode findChildNode(final DataNodeContainer container, final String name) { + for (DataSchemaNode dataSchemaNode : container.getChildNodes()) { if (dataSchemaNode.getQName().getLocalName().equals(name)) { return dataSchemaNode; } if (dataSchemaNode instanceof DataNodeContainer) { - containers.add((DataNodeContainer) dataSchemaNode); + DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name); + if (retVal != null) { + return retVal; + } } else if (dataSchemaNode instanceof ChoiceSchemaNode) { - containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases()); + for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases().values()) { + DataSchemaNode retVal = findChildNode(caseNode, name); + if (retVal != null) { + return retVal; + } + } } } - - for (DataNodeContainer container : containers) { - DataSchemaNode retVal = findChildNode(container.getChildNodes(), name); - if (retVal != null) { - return retVal; - } - } - return null; } }