Merge "Add netconf-util tests and remove unused classes"
authorJakub Morvay <jmorvay@cisco.com>
Wed, 2 Nov 2016 18:35:29 +0000 (18:35 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 2 Nov 2016 18:35:29 +0000 (18:35 +0000)
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfTopologyPathCreator.java [deleted file]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/xml/XmlNetconfValidator.java [deleted file]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfUtilTest.java
netconf/netconf-util/src/test/resources/sessions.xml [new file with mode: 0644]

diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfTopologyPathCreator.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfTopologyPathCreator.java
deleted file mode 100644 (file)
index 1a16eca..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.netconf.util;
-
-public class NetconfTopologyPathCreator {
-
-    public static final String CLUSTERED_DEVICE_SOURCES_RESOLVER = "clusteredDeviceSourcesResolver";
-    public static final String MASTER_SOURCE_PROVIDER
-            = "masterSourceProvider";
-
-    private static final String USER = "/user/";
-
-    private String mainPath;
-
-    public NetconfTopologyPathCreator(final String topologyId) {
-        mainPath = createMainPath("", topologyId);
-    }
-
-    public NetconfTopologyPathCreator(final String memberAddress, final String topologyId) {
-        mainPath = createMainPath(memberAddress, topologyId);
-    }
-
-    private String createMainPath(final String memberAddress, final String topologyId) {
-        return memberAddress + USER + topologyId;
-    }
-
-    public NetconfTopologyPathCreator withSuffix(final String suffix) {
-        mainPath += "/"+suffix;
-        return this;
-    }
-
-    public String build(){
-        return mainPath;
-    }
-
-}
diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/xml/XmlNetconfValidator.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/xml/XmlNetconfValidator.java
deleted file mode 100644 (file)
index eeb9126..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (c) 2013 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,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.netconf.util.xml;
-
-import com.google.common.base.Preconditions;
-import java.io.IOException;
-import java.io.InputStream;
-import javax.xml.transform.Source;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.validation.Schema;
-import javax.xml.validation.Validator;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.w3c.dom.Document;
-import org.xml.sax.SAXException;
-
-public final class XmlNetconfValidator {
-
-    private static final Schema SCHEMA;
-
-    private XmlNetconfValidator() {}
-
-    static {
-        final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
-        Preconditions.checkNotNull(xmlSchema, "Cannot find xml.xsd");
-
-        final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
-        Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
-        SCHEMA = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
-    }
-
-    public static void validate(Document inputDocument) throws SAXException, IOException {
-        final Validator validator = SCHEMA.newValidator();
-        final Source source = new DOMSource(inputDocument);
-        validator.validate(source);
-    }
-}
index 098002b3b0713d6c8e31d14546745ad719541b08..4f5ca39acc925a5f282188a34cb4c002de67b588 100644 (file)
@@ -11,20 +11,75 @@ import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 
+import java.util.Collections;
+import javax.xml.transform.dom.DOMResult;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Assert;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
+import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.w3c.dom.Document;
 
 public class NetconfUtilTest {
 
+    @BeforeClass
+    public static void classSetUp() {
+        XMLUnit.setIgnoreWhitespace(true);
+    }
+
     @Test
     public void testConflictingVersionDetection() throws Exception {
-        Document document = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconfMessages/conflictingversion/conflictingVersionResponse.xml"));
+        final Document document = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconfMessages/conflictingversion/conflictingVersionResponse.xml"));
         try{
             NetconfUtil.checkIsMessageOk(document);
             fail();
-        }catch(IllegalStateException e){
+        } catch (final IllegalStateException e) {
             assertThat(e.getMessage(), containsString("Optimistic lock failed. Expected parent version 21, was 18"));
         }
     }
+
+    @Test
+    public void testWriteNormalizedNode() throws Exception {
+        final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
+        moduleInfoBackedContext.addModuleInfos(Collections.singletonList($YangModuleInfoImpl.getInstance()));
+        final SchemaContext context = moduleInfoBackedContext.getSchemaContext();
+        final LeafNode username = Builders.leafBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(Session.QNAME, "username")))
+                .withValue("admin")
+                .build();
+        final MapEntryNode session1 = Builders.mapEntryBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifierWithPredicates(Session.QNAME, QName.create(Session.QNAME, "session-id"), 1L))
+                .withChild(username)
+                .build();
+        final MapNode sessionList = Builders.mapBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(Session.QNAME))
+                .withChild(session1)
+                .build();
+        final ContainerNode sessions = Builders.containerBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(Sessions.QNAME))
+                .withChild(sessionList)
+                .build();
+        final DOMResult result = new DOMResult(XmlUtil.newDocument());
+        final SchemaPath path = SchemaPath.create(true, NetconfState.QNAME);
+        NetconfUtil.writeNormalizedNode(sessions, result, path, context);
+        final Document expected = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/sessions.xml"));
+        final Document actual = (Document) result.getNode();
+        final Diff diff = XMLUnit.compareXML(expected, actual);
+        Assert.assertTrue(diff.toString(), diff.similar());
+    }
 }
diff --git a/netconf/netconf-util/src/test/resources/sessions.xml b/netconf/netconf-util/src/test/resources/sessions.xml
new file mode 100644 (file)
index 0000000..398b537
--- /dev/null
@@ -0,0 +1,14 @@
+<!--
+  ~ 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,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+
+<sessions xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
+    <session>
+        <session-id>1</session-id>
+        <username>admin</username>
+    </session>
+</sessions>
\ No newline at end of file