From c98e4f819d20799d29c92568c659093fa9977ba2 Mon Sep 17 00:00:00 2001 From: Tomas Cere Date: Thu, 18 Jun 2015 15:21:06 +0200 Subject: [PATCH] Increase UnitTest coverage/refactor for MDsal Netconf. Change-Id: I486565bec7bbce0a85ee2dfa389887ca0d3a2903 Signed-off-by: Tomas Cere --- .../netconf/mdsal/connector/ops/Lock.java | 13 +-- .../netconf/mdsal/connector/ops/Unlock.java | 1 - .../ops/NetconfMDSalMappingTest.java | 85 +++++++++++++++++-- .../mdsal/connector/ops/RuntimeRpcTest.java | 15 ++++ .../messages/mapping/bad_getConfig.xml | 15 ++++ .../mapping/bad_namespace_getConfig.xml | 15 ++++ .../filters/get-config-empty-filter.xml | 17 ++++ .../mapping/filters/get-empty-filter.xml | 7 +- .../messages/mapping/lock_notarget.xml | 12 +++ .../mapping/rpcs/rpc-bad-namespace.xml | 19 +++++ .../messages/mapping/unlock_notarget.xml | 12 +++ 11 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_getConfig.xml create mode 100644 opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_namespace_getConfig.xml create mode 100644 opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-config-empty-filter.xml create mode 100644 opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/lock_notarget.xml create mode 100644 opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/rpcs/rpc-bad-namespace.xml create mode 100644 opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/unlock_notarget.xml diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Lock.java b/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Lock.java index ef94f69f7a..252f4a637f 100644 --- a/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Lock.java +++ b/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Lock.java @@ -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()); } diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Unlock.java b/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Unlock.java index 08ffe8b2a0..634be3e22f 100644 --- a/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Unlock.java +++ b/opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/Unlock.java @@ -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); diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/NetconfMDSalMappingTest.java b/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/NetconfMDSalMappingTest.java index b4f5a9756b..1b680368aa 100644 --- a/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/NetconfMDSalMappingTest.java +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/NetconfMDSalMappingTest.java @@ -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; } diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/RuntimeRpcTest.java b/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/RuntimeRpcTest.java index 040066d91c..aa6ff06c5f 100644 --- a/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/RuntimeRpcTest.java +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/controller/netconf/mdsal/connector/ops/RuntimeRpcTest.java @@ -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 index 0000000000..e7c9c88474 --- /dev/null +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_getConfig.xml @@ -0,0 +1,15 @@ + + + + + + + + + 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 index 0000000000..6390f6ae86 --- /dev/null +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/bad_namespace_getConfig.xml @@ -0,0 +1,15 @@ + + + + + + + + + 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 index 0000000000..0cc9c12eec --- /dev/null +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-config-empty-filter.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + diff --git a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-empty-filter.xml b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-empty-filter.xml index 0cc9c12eec..4edf3841cc 100644 --- a/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-empty-filter.xml +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/filters/get-empty-filter.xml @@ -7,11 +7,8 @@ --> - + - - - - + 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 index 0000000000..372c419925 --- /dev/null +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/lock_notarget.xml @@ -0,0 +1,12 @@ + + + + + + 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 index 0000000000..4f589ea2cc --- /dev/null +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/rpcs/rpc-bad-namespace.xml @@ -0,0 +1,19 @@ + + + + + + test rpc input string 1 + + + test rpc input string 2 + + + \ 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 index 0000000000..04067fd586 --- /dev/null +++ b/opendaylight/netconf/mdsal-netconf-connector/src/test/resources/messages/mapping/unlock_notarget.xml @@ -0,0 +1,12 @@ + + + + + + -- 2.36.6