Yangtools892Test does not need XML 34/86634/5
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Dec 2019 16:09:28 +0000 (17:09 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Dec 2019 20:14:29 +0000 (21:14 +0100)
This migrates the test to not use XML parsing for simple data
structures and defines them statically.

Change-Id: I1c4f71035d1949395cc6780a74a49133a1b28b98
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/Yangtools892Test.java
yang/yang-data-codec-xml/src/test/resources/yangtools892/neighbour.xml [deleted file]
yang/yang-data-codec-xml/src/test/resources/yangtools892/peer-groups.xml [deleted file]

index 6f4c07f4b62d5f08f5c507fd4bc9de29d0858573..2cf64c8fb532bf3f7babc7eb4222a31952d93dbb 100644 (file)
  */
 package org.opendaylight.yangtools.yang.data.codec.xml;
 
-import java.io.InputStream;
-import javax.xml.stream.XMLStreamReader;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+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.schema.tree.DataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class Yangtools892Test {
-    private static final String TEST_BGP_NAME = "test-bgp";
-    private static final String TEST_BGP_NS = "urn:opendaylight:params:xml:ns:yang:test:bgp";
-    private static final String TEST_BGP_REV = "2018-08-14";
-    private static final QName BGP = QName.create(TEST_BGP_NS, TEST_BGP_REV, "bgp");
+    private static final QName BGP = QName.create("urn:opendaylight:params:xml:ns:yang:test:bgp", "2018-08-14", "bgp");
+    private static final QName PEER_GROUPS = QName.create(BGP, "peer-groups");
+    private static final QName PEER_GROUP = QName.create(BGP, "peer-group");
+    private static final QName PEER_GROUP_NAME = QName.create(BGP, "peer-group-name");
     private static final YangInstanceIdentifier BGP_ID = YangInstanceIdentifier.of(BGP);
 
-    private static final String NETWORK_INSTANCE_NAME = "test-network-instance";
-    private static final String NETWORK_INSTANCE_NS = "urn:opendaylight:params:xml:ns:yang:test:network:instance";
-    private static final String NETWORK_INSTANCE_REV = "2018-08-14";
     private static final QName NETWORK_INSTANCES =
-        QName.create(NETWORK_INSTANCE_NS, NETWORK_INSTANCE_REV, "network-instances");
+            QName.create("urn:opendaylight:params:xml:ns:yang:test:network:instance", "2018-08-14",
+                "network-instances");
+    private static final QName NETWORK_INSTANCE = QName.create(NETWORK_INSTANCES, "network-instance");
+    private static final QName NAME = QName.create(NETWORK_INSTANCES, "name");
+    private static final QName CONFIG = QName.create(NETWORK_INSTANCES, "config");
+    private static final QName PROTOCOLS = QName.create(NETWORK_INSTANCES, "protocols");
+    private static final QName PROTOCOL = QName.create(NETWORK_INSTANCES, "protocol");
+    private static final QName IDENTIFIER = QName.create(NETWORK_INSTANCES, "identifier");
+    private static final QName BGP_POLICY = QName.create("urn:opendaylight:params:xml:ns:yang:test:policy:types",
+        "2018-08-14", "BGP");
+    private static final QName TEST_BGP = QName.create("urn:opendaylight:params:xml:ns:yang:bgp:test:extensions",
+        "2018-08-14", "bgp");
+    private static final QName NEIGHBORS = QName.create(TEST_BGP, "neighbors");
+    private static final QName NEIGHBOR = QName.create(TEST_BGP, "neighbor");
+    private static final QName NEIGHBOR_ADDRESS = QName.create(TEST_BGP, "neighbor-address");
+    private static final QName TEST_CONFIG = QName.create(TEST_BGP, "config");
+    private static final QName TEST_PEER_GROUP = QName.create(TEST_BGP, "peer-group");
+    private static final QName AFI_SAFIS = QName.create(TEST_BGP, "afi-safis");
+    private static final QName AFI_SAFI = QName.create(TEST_BGP, "afi-safi");
+    private static final QName AFI_SAFI_NAME = QName.create(TEST_BGP, "afi-safi-name");
+    private static final QName IPV4_UNICAST = QName.create("urn:opendaylight:params:xml:ns:yang:test:bgp:types",
+        "2018-08-14", "IPV4-UNICAST");
+    private static final QName RECEIVE = QName.create(TEST_BGP, "receive");
+    private static final QName SEND_MAX = QName.create(TEST_BGP, "send-max");
+
     private static final YangInstanceIdentifier NETWORK_INSTANCES_ID = YangInstanceIdentifier.of(NETWORK_INSTANCES);
 
     private SchemaContext schemaContext;
     private LeafRefContext leafRefContext;
     private DataTree dataTree;
-    private ContainerSchemaNode bgpNode;
-    private ContainerSchemaNode networkInstancesNode;
 
     @Before
     public void setup() {
         schemaContext = YangParserTestUtils.parseYangResourceDirectory("/yangtools892");
         leafRefContext = LeafRefContext.create(schemaContext);
         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
-        final Module testBgpModule = schemaContext.findModule(TEST_BGP_NAME, Revision.of(TEST_BGP_REV)).get();
-        bgpNode = (ContainerSchemaNode) testBgpModule.findDataChildByName(BGP).get();
-        final Module networkInstanceModule =
-            schemaContext.findModule(NETWORK_INSTANCE_NAME, Revision.of(NETWORK_INSTANCE_REV)).get();
-        networkInstancesNode = (ContainerSchemaNode) networkInstanceModule.findDataChildByName(NETWORK_INSTANCES).get();
     }
 
     @Test
     public void testWriteBgpNeighbour() throws Exception {
         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
-        final NormalizedNode<?, ?> bgp = readNode("/yangtools892/peer-groups.xml", bgpNode);
-        writeModification.write(BGP_ID, bgp);
-        final NormalizedNode<?, ?> networkInstances = readNode("/yangtools892/neighbour.xml", networkInstancesNode);
-        writeModification.write(NETWORK_INSTANCES_ID, networkInstances);
+        writeModification.write(BGP_ID, Builders.containerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(BGP))
+            .withChild(Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(PEER_GROUPS))
+                .withChild(Builders.mapBuilder()
+                    .withNodeIdentifier(new NodeIdentifier(PEER_GROUP))
+                    .withChild(Builders.mapEntryBuilder()
+                        .withNodeIdentifier(NodeIdentifierWithPredicates.of(PEER_GROUP,
+                            PEER_GROUP_NAME, "application-peers"))
+                        .withChild(ImmutableNodes.leafNode(PEER_GROUP_NAME, "application-peers"))
+                        .build())
+                    .build())
+                .build())
+            .build());
+
+        writeModification.write(NETWORK_INSTANCES_ID, Builders.containerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(NETWORK_INSTANCES))
+            .withChild(Builders.mapBuilder()
+                .withNodeIdentifier(new NodeIdentifier(NETWORK_INSTANCE))
+                .withChild(Builders.mapEntryBuilder()
+                    .withNodeIdentifier(NodeIdentifierWithPredicates.of(NETWORK_INSTANCE, NAME, "global-bgp"))
+                    .withChild(ImmutableNodes.leafNode(NAME, "global-bgp"))
+                    .withChild(Builders.containerBuilder()
+                        .withNodeIdentifier(new NodeIdentifier(CONFIG))
+                        .withChild(ImmutableNodes.leafNode(NAME, "global-bgp"))
+                        .build())
+                    .withChild(Builders.containerBuilder()
+                        .withNodeIdentifier(new NodeIdentifier(PROTOCOLS))
+                        .withChild(Builders.mapBuilder()
+                            .withNodeIdentifier(new NodeIdentifier(PROTOCOL))
+                            .withChild(Builders.mapEntryBuilder()
+                                .withNodeIdentifier(NodeIdentifierWithPredicates.of(PROTOCOL, ImmutableMap.of(
+                                    IDENTIFIER, BGP_POLICY,
+                                    NAME, "test-bgp-instance")))
+                                .withChild(ImmutableNodes.leafNode(IDENTIFIER, BGP_POLICY))
+                                .withChild(ImmutableNodes.leafNode(NAME, "test-bgp-instance"))
+                                .withChild(Builders.containerBuilder()
+                                    .withNodeIdentifier(new NodeIdentifier(CONFIG))
+                                    .withChild(ImmutableNodes.leafNode(IDENTIFIER, BGP_POLICY))
+                                    .withChild(ImmutableNodes.leafNode(NAME, "test-bgp-instance"))
+                                    .build())
+                                .withChild(Builders.augmentationBuilder()
+                                    .withNodeIdentifier(
+                                        AugmentationIdentifier.create(ImmutableSet.of(TEST_BGP)))
+                                    .withChild(Builders.containerBuilder()
+                                        .withNodeIdentifier(new NodeIdentifier(TEST_BGP))
+                                        .withChild(Builders.containerBuilder()
+                                            .withNodeIdentifier(new NodeIdentifier(NEIGHBORS))
+                                            .withChild(Builders.mapBuilder()
+                                                .withNodeIdentifier(new NodeIdentifier(NEIGHBOR))
+                                                .withChild(Builders.mapEntryBuilder()
+                                                    .withNodeIdentifier(NodeIdentifierWithPredicates.of(NEIGHBOR,
+                                                        NEIGHBOR_ADDRESS, "10.25.1.9"))
+                                                    .withChild(ImmutableNodes.leafNode(NEIGHBOR_ADDRESS,
+                                                            "10.25.1.9"))
+                                                    .withChild(Builders.containerBuilder()
+                                                        .withNodeIdentifier(new NodeIdentifier(TEST_CONFIG))
+                                                        .withChild(Builders.augmentationBuilder()
+                                                            .withNodeIdentifier(AugmentationIdentifier.create(
+                                                                ImmutableSet.of(TEST_PEER_GROUP)))
+                                                            .withChild(ImmutableNodes.leafNode(TEST_PEER_GROUP,
+                                                                    "application-peers"))
+                                                            .build())
+                                                        .build())
+                                                    .withChild(Builders.containerBuilder()
+                                                        .withNodeIdentifier(new NodeIdentifier(AFI_SAFIS))
+                                                        .withChild(Builders.mapBuilder()
+                                                            .withNodeIdentifier(new NodeIdentifier(AFI_SAFI))
+                                                            .withChild(Builders.mapEntryBuilder()
+                                                                .withNodeIdentifier(NodeIdentifierWithPredicates.of(
+                                                                    AFI_SAFI,
+                                                                    ImmutableMap.of(AFI_SAFI_NAME, IPV4_UNICAST)))
+                                                                .withChild(ImmutableNodes.leafNode(AFI_SAFI_NAME,
+                                                                    IPV4_UNICAST))
+                                                                .withChild(Builders.containerBuilder()
+                                                                    .withNodeIdentifier(
+                                                                        new NodeIdentifier(TEST_CONFIG))
+                                                                    .withChild(ImmutableNodes.leafNode(
+                                                                        AFI_SAFI_NAME, IPV4_UNICAST))
+                                                                    .build())
+                                                                .withChild(Builders.augmentationBuilder()
+                                                                    .withNodeIdentifier(
+                                                                        AugmentationIdentifier.create(
+                                                                            ImmutableSet.of(RECEIVE, SEND_MAX)))
+                                                                    .withChild(ImmutableNodes.leafNode(RECEIVE,
+                                                                        Boolean.TRUE))
+                                                                    .withChild(ImmutableNodes.leafNode(SEND_MAX,
+                                                                        Uint8.ZERO))
+                                                                    .build())
+                                                                .build())
+                                                            .build())
+                                                        .build())
+                                                    .build())
+                                                .build())
+                                            .build())
+                                        .build())
+                                    .build())
+                                .build())
+                            .build())
+                        .build())
+                    .build())
+                .build())
+            .build());
 
         writeModification.ready();
         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
         dataTree.commit(writeContributorsCandidate);
     }
-
-    private NormalizedNode<?, ?> readNode(final String filename, final ContainerSchemaNode node) throws Exception {
-        final InputStream resourceAsStream = Yangtools892Test.class.getResourceAsStream(filename);
-        final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
-        final NormalizedNodeResult result = new NormalizedNodeResult();
-        final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
-        final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, node);
-        xmlParser.parse(reader);
-        return result.getResult();
-    }
-}
\ No newline at end of file
+}
diff --git a/yang/yang-data-codec-xml/src/test/resources/yangtools892/neighbour.xml b/yang/yang-data-codec-xml/src/test/resources/yangtools892/neighbour.xml
deleted file mode 100644 (file)
index 690c41d..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-<network-instances xmlns="urn:opendaylight:params:xml:ns:yang:test:network:instance">
-    <network-instance>
-        <name>global-bgp</name>
-        <config>
-            <name>global-bgp</name>
-        </config>
-        <protocols>
-            <protocol>
-                <identifier xmlns:x="urn:opendaylight:params:xml:ns:yang:test:policy:types">x:BGP</identifier>
-                <name>test-bgp-instance</name>
-                <config>
-                    <name>test-bgp-instance</name>
-                    <identifier xmlns:x="urn:opendaylight:params:xml:ns:yang:test:policy:types">x:BGP</identifier>
-                </config>
-                <bgp xmlns="urn:opendaylight:params:xml:ns:yang:bgp:test:extensions">
-                    <neighbors>
-                        <neighbor>
-                            <neighbor-address>10.25.1.9</neighbor-address>
-                            <config>
-                                <peer-group>application-peers</peer-group>
-                            </config>
-                            <afi-safis>
-                                <afi-safi>
-                                    <afi-safi-name xmlns:x="urn:opendaylight:params:xml:ns:yang:test:bgp:types">x:IPV4-UNICAST</afi-safi-name>
-                                    <config>
-                                        <afi-safi-name xmlns:x="urn:opendaylight:params:xml:ns:yang:test:bgp:types">x:IPV4-UNICAST</afi-safi-name>
-                                    </config>
-                                    <receive>true</receive>
-                                    <send-max>0</send-max>
-                                </afi-safi>
-                            </afi-safis>
-                        </neighbor>
-                    </neighbors>
-                </bgp>
-            </protocol>
-        </protocols>
-    </network-instance>
-</network-instances>
\ No newline at end of file
diff --git a/yang/yang-data-codec-xml/src/test/resources/yangtools892/peer-groups.xml b/yang/yang-data-codec-xml/src/test/resources/yangtools892/peer-groups.xml
deleted file mode 100644 (file)
index 07c4e55..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<bgp xmlns="urn:opendaylight:params:xml:ns:yang:test:bgp">
-    <peer-groups>
-        <peer-group>
-            <peer-group-name>application-peers</peer-group-name>
-        </peer-group>
-    </peer-groups>
-</bgp>
\ No newline at end of file