Increase UnitTest coverage/refactor for MDsal Netconf. 56/23056/3
authorTomas Cere <tcere@cisco.com>
Thu, 18 Jun 2015 13:21:06 +0000 (15:21 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 26 Jun 2015 07:46:05 +0000 (07:46 +0000)
Change-Id: I486565bec7bbce0a85ee2dfa389887ca0d3a2903
Signed-off-by: Tomas Cere <tcere@cisco.com>
opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Lock.java
opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Unlock.java
opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/NetconfMDSalMappingTest.java
opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/RuntimeRpcTest.java
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_getConfig.xml [new file with mode: 0644]
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_namespace_getConfig.xml [new file with mode: 0644]
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-config-empty-filter.xml [new file with mode: 0644]
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-empty-filter.xml
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/lock_notarget.xml [new file with mode: 0644]
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/rpcs/rpc-bad-namespace.xml [new file with mode: 0644]
opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/unlock_notarget.xml [new file with mode: 0644]

index ef94f69..252f4a6 100644 (file)
@@ -11,8 +11,6 @@ package org.opendaylight.controller.netconf.mdsal.connector.ops;
 import com.google.common.base.Optional;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
@@ -45,15 +43,8 @@ public class Lock extends AbstractSingletonNetconfOperation {
     }
 
     static Datastore extractTargetParameter(final XmlElement operationElement) throws NetconfDocumentedException {
-        final XmlElement targetChildNode;
-        try {
-            final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
-            targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
-        } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
-            LOG.trace("Can't get only child element with same namespace", e);
-            throw NetconfDocumentedException.wrap(e);
-        }
-
+        final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
+        final XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
         return Datastore.valueOf(targetChildNode.getName());
     }
 
index 08ffe8b..634be3e 100644 (file)
@@ -24,7 +24,6 @@ public class Unlock extends AbstractSingletonNetconfOperation {
     private static final Logger LOG = LoggerFactory.getLogger(Unlock.class);
 
     private static final String OPERATION_NAME = "unlock";
-    private static final String TARGET_KEY = "target";
 
     public Unlock(final String netconfSessionIdForReporting) {
         super(netconfSessionIdForReporting);
index b4f5a97..1b68036 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import com.google.common.base.Preconditions;
 import com.google.common.io.ByteSource;
 import java.io.IOException;
 import java.io.InputStream;
@@ -44,7 +43,6 @@ import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
-import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
@@ -148,6 +146,43 @@ public class NetconfMDSalMappingTest {
         assertEmptyDatastore(getConfigRunning());
     }
 
+    @Test
+    public void testDiscard() throws Exception {
+
+        try {
+            discardChanges();
+            fail("Should have failed, need to execute an edit before discard");
+        } catch (NetconfDocumentedException e) {
+            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
+            assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
+            assertTrue(e.getErrorType() == ErrorType.application);
+        }
+    }
+
+    @Test
+    public void testIncorrectGet() throws Exception {
+
+        try {
+            executeOperation(new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider), "messages/mapping/bad_getConfig.xml");
+            fail("Should have failed, this is an incorrect request");
+        } catch (NetconfDocumentedException e) {
+            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
+            assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
+            assertTrue(e.getErrorType() == ErrorType.application);
+        }
+
+        try {
+            executeOperation(new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider), "messages/mapping/bad_namespace_getConfig.xml");
+            fail("Should have failed, this is an incorrect request");
+        } catch (NetconfDocumentedException e) {
+            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
+            assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
+            assertTrue(e.getErrorType() == ErrorType.application);
+        }
+
+
+    }
+
     @Test
     public void testEditRunning() throws Exception {
 
@@ -244,6 +279,16 @@ public class NetconfMDSalMappingTest {
             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
             assertTrue(e.getErrorType() == ErrorType.application);
         }
+
+
+        try {
+            lockWithoutTarget();
+            fail("Should have failed, target is missing");
+        } catch (NetconfDocumentedException e) {
+            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
+            assertTrue(e.getErrorTag() == ErrorTag.invalid_value);
+            assertTrue(e.getErrorType() == ErrorType.application);
+        }
     }
 
     @Test
@@ -259,6 +304,15 @@ public class NetconfMDSalMappingTest {
             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
             assertTrue(e.getErrorType() == ErrorType.application);
         }
+
+        try {
+            unlockWithoutTarget();
+            fail("Should have failed, target is missing");
+        } catch (NetconfDocumentedException e) {
+            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
+            assertTrue(e.getErrorTag() == ErrorTag.invalid_value);
+            assertTrue(e.getErrorType() == ErrorType.application);
+        }
     }
 
     @Test
@@ -384,6 +438,11 @@ public class NetconfMDSalMappingTest {
         assertEmptyDatastore(getConfigCandidate());
         assertEmptyDatastore(getConfigRunning());
 
+        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
+                XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
+        verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
+                XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
+
         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
@@ -480,7 +539,7 @@ public class NetconfMDSalMappingTest {
         NodeList nodes = response.getChildNodes();
         assertTrue(nodes.getLength() == 1);
 
-        assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
+        assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
 
         NodeList replyNodes = nodes.item(0).getChildNodes();
         assertTrue(replyNodes.getLength() == 1);
@@ -511,6 +570,11 @@ public class NetconfMDSalMappingTest {
         return executeOperation(get, "messages/mapping/get.xml");
     }
 
+    private Document getWithFilter(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
+        Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
+        return executeOperation(get, resource);
+    }
+
     private Document getConfigRunning() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
         return executeOperation(getConfig, "messages/mapping/getConfig.xml");
@@ -536,6 +600,16 @@ public class NetconfMDSalMappingTest {
         return executeOperation(unlock, "messages/mapping/unlock.xml");
     }
 
+    private Document lockWithoutTarget() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
+        Lock lock = new Lock(sessionIdForReporting);
+        return executeOperation(lock, "messages/mapping/lock_notarget.xml");
+    }
+
+    private Document unlockWithoutTarget() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
+        Unlock unlock = new Unlock(sessionIdForReporting);
+        return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
+    }
+
     private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
         Lock lock = new Lock(sessionIdForReporting);
         return executeOperation(lock, "messages/mapping/lock_candidate.xml");
@@ -548,14 +622,9 @@ public class NetconfMDSalMappingTest {
 
     private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
         final Document request = XmlFileLoader.xmlFileToDocument(filename);
-
-        HandlingPriority priority = op.canHandle(request);
-        Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
-
         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
 
         LOG.debug("Got response {}" , response);
-
         return response;
     }
 
index 040066d..aa6ff06 100644 (file)
@@ -245,6 +245,21 @@ public class RuntimeRpcTest {
         verifyResponse(response, RPC_REPLY_OK);
     }
 
+    @Test
+    public void testBadNamespaceInRpc() throws Exception {
+        RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceVoidInvoke);
+        Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-bad-namespace.xml");
+
+        try {
+            rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
+            fail("Should have failed, rpc has bad namespace");
+        } catch (NetconfDocumentedException e) {
+            assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
+            assertTrue(e.getErrorTag() == ErrorTag.bad_element);
+            assertTrue(e.getErrorType() == ErrorType.application);
+        }
+    }
+
     private void verifyResponse(Document response, Document template) throws IOException, TransformerException {
         DetailedDiff dd = new DetailedDiff(new Diff(response, template));
         dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_getConfig.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_getConfig.xml
new file mode 100644 (file)
index 0000000..e7c9c88
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2015 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
+  -->
+
+<rpc id="a" a="64" xmlnx="a:b:c:d" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
+    <bad-get-config>
+        <source>
+            <candidate/>
+        </source>
+    </bad-get-config>
+</rpc>
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_namespace_getConfig.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_namespace_getConfig.xml
new file mode 100644 (file)
index 0000000..6390f6a
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2015 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
+  -->
+
+<rpc id="a" a="64" xmlnx="a:b:c:d" xmlns="bad-namespace" message-id="101">
+    <get-config>
+        <source>
+            <running/>
+        </source>
+    </get-config>
+</rpc>
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-config-empty-filter.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-config-empty-filter.xml
new file mode 100644 (file)
index 0000000..0cc9c12
--- /dev/null
@@ -0,0 +1,17 @@
+<!--
+  ~ Copyright (c) 2015 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
+  -->
+
+<rpc id="a" a="64" xmlnx="a:b:c:d" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
+    <get-config>
+        <filter type="subtree">
+        </filter>
+        <source>
+            <running/>
+        </source>
+    </get-config>
+</rpc>
index 0cc9c12..4edf384 100644 (file)
@@ -7,11 +7,8 @@
   -->
 
 <rpc id="a" a="64" xmlnx="a:b:c:d" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
-    <get-config>
+    <get>
         <filter type="subtree">
         </filter>
-        <source>
-            <running/>
-        </source>
-    </get-config>
+    </get>
 </rpc>
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/lock_notarget.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/lock_notarget.xml
new file mode 100644 (file)
index 0000000..372c419
--- /dev/null
@@ -0,0 +1,12 @@
+<!--
+  ~ Copyright (c) 2015 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
+  -->
+
+<rpc a="64" message-id="a" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <lock>
+    </lock>
+</rpc>
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/rpcs/rpc-bad-namespace.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/rpcs/rpc-bad-namespace.xml
new file mode 100644 (file)
index 0000000..4f589ea
--- /dev/null
@@ -0,0 +1,19 @@
+<!--
+  ~ Copyright (c) 2015 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
+  -->
+
+<rpc message-id="2"
+     xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <nonvoid-rpca>
+        <test-string>
+            test rpc input string 1
+        </test-string>
+        <test-string2>
+            test rpc input string 2
+        </test-string2>
+    </nonvoid-rpca>
+</rpc>
\ No newline at end of file
diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/unlock_notarget.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/unlock_notarget.xml
new file mode 100644 (file)
index 0000000..04067fd
--- /dev/null
@@ -0,0 +1,12 @@
+<!--
+  ~ Copyright (c) 2015 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
+  -->
+
+<rpc a="64" message-id="a" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <unlock>
+    </unlock>
+</rpc>

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.