Introduce AbstractNetconfOperationTest to decrease code duplication 11/69611/5
authorMarek Gradzki <mgradzki@cisco.com>
Mon, 19 Mar 2018 13:00:50 +0000 (14:00 +0100)
committerMarek Gradzki <mgradzki@cisco.com>
Wed, 28 Mar 2018 19:53:51 +0000 (21:53 +0200)
Moves common code for NetconfOperation testing
to AbstractNetconfOperationTest.

Change-Id: Id70838c2b0c29cccb36c7a4e0c53519f65fd5bd7
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/AbstractNetconfOperationTest.java [new file with mode: 0644]
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/CopyConfigTest.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/NetconfMDSalMappingTest.java

diff --git a/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/AbstractNetconfOperationTest.java b/netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/AbstractNetconfOperationTest.java
new file mode 100644 (file)
index 0000000..555f201
--- /dev/null
@@ -0,0 +1,236 @@
+/*
+ * Copyright (c) 2018 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.mdsal.connector.ops;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import com.google.common.io.ByteSource;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
+import java.io.StringWriter;
+import java.util.EnumMap;
+import java.util.concurrent.ExecutorService;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.custommonkey.xmlunit.DetailedDiff;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Before;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
+import org.opendaylight.controller.sal.core.api.model.SchemaService;
+import org.opendaylight.controller.sal.core.spi.data.DOMStore;
+import org.opendaylight.netconf.mapping.api.NetconfOperation;
+import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
+import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
+import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
+import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
+import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
+import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
+import org.opendaylight.netconf.util.test.XmlFileLoader;
+import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+abstract class AbstractNetconfOperationTest {
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfOperationTest.class);
+    protected static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
+    private static final String RPC_REPLY_ELEMENT = "rpc-reply";
+    private static final String DATA_ELEMENT = "data";
+    protected static final Document RPC_REPLY_OK = getReplyOk();
+
+    private CurrentSchemaContext currentSchemaContext;
+    private TransactionProvider transactionProvider;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        XMLUnit.setIgnoreWhitespace(true);
+        XMLUnit.setIgnoreAttributeOrder(true);
+
+        final SchemaContext schemaContext = getSchemaContext();
+        final SchemaService schemaService = new SchemaServiceStub(schemaContext);
+        final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
+        final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
+
+        currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> {
+            final YangTextSchemaSource yangTextSchemaSource =
+                YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes()));
+            return Futures.immediateCheckedFuture(yangTextSchemaSource);
+        });
+
+        final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
+        datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
+        datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
+
+        final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
+            16, 16, "CommitFutures", CopyConfigTest.class);
+
+        final SerializedDOMDataBroker sdb = new SerializedDOMDataBroker(datastores,
+            MoreExecutors.listeningDecorator(listenableFutureExecutor));
+        this.transactionProvider = new TransactionProvider(sdb, SESSION_ID_FOR_REPORTING);
+    }
+
+    protected abstract SchemaContext getSchemaContext();
+
+    protected CurrentSchemaContext getCurrentSchemaContext() {
+        return currentSchemaContext;
+    }
+
+    protected TransactionProvider getTransactionProvider() {
+        return transactionProvider;
+    }
+
+    @SuppressWarnings("illegalCatch")
+    private static Document getReplyOk() {
+        Document doc;
+        try {
+            doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
+        } catch (final Exception e) {
+            LOG.debug("unable to load rpc reply ok.", e);
+            doc = XmlUtil.newDocument();
+        }
+        return doc;
+    }
+
+    protected Document commit() throws Exception {
+        final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
+        return executeOperation(commit, "messages/mapping/commit.xml");
+    }
+
+    protected Document discardChanges() throws Exception {
+        final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
+        return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
+    }
+
+    protected Document edit(final String resource) throws Exception {
+        final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
+            transactionProvider);
+        return executeOperation(editConfig, resource);
+    }
+
+    protected Document get() throws Exception {
+        final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
+        return executeOperation(get, "messages/mapping/get.xml");
+    }
+
+    protected Document getWithFilter(final String resource) throws Exception {
+        final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
+        return executeOperation(get, resource);
+    }
+
+    protected Document getConfigRunning() throws Exception {
+        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
+        return executeOperation(getConfig, "messages/mapping/getConfig.xml");
+    }
+
+    protected Document getConfigCandidate() throws Exception {
+        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
+        return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
+    }
+
+    protected Document getConfigWithFilter(final String resource) throws Exception {
+        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
+        return executeOperation(getConfig, resource);
+    }
+
+    protected static Document lock() throws Exception {
+        final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
+        return executeOperation(lock, "messages/mapping/lock.xml");
+    }
+
+    protected static Document unlock() throws Exception {
+        final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
+        return executeOperation(unlock, "messages/mapping/unlock.xml");
+    }
+
+    protected static Document lockWithoutTarget() throws Exception {
+        final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
+        return executeOperation(lock, "messages/mapping/lock_notarget.xml");
+    }
+
+    protected static Document unlockWithoutTarget() throws Exception {
+        final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
+        return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
+    }
+
+    protected static Document lockCandidate() throws Exception {
+        final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
+        return executeOperation(lock, "messages/mapping/lock_candidate.xml");
+    }
+
+    protected static Document unlockCandidate() throws Exception {
+        final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
+        return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
+    }
+
+    protected static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
+        final Document request = XmlFileLoader.xmlFileToDocument(filename);
+        final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
+
+        LOG.debug("Got response {}", response);
+        return response;
+    }
+
+    protected static void assertEmptyDatastore(final Document response) {
+        final NodeList nodes = response.getChildNodes();
+        assertTrue(nodes.getLength() == 1);
+
+        assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
+
+        final NodeList replyNodes = nodes.item(0).getChildNodes();
+        assertTrue(replyNodes.getLength() == 1);
+
+        final Node dataNode = replyNodes.item(0);
+        assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
+        assertFalse(dataNode.hasChildNodes());
+    }
+
+    protected static void verifyResponse(final Document response, final Document template) throws Exception {
+        final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
+        dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
+        if (!dd.similar()) {
+            LOG.warn("Actual response:");
+            printDocument(response);
+            LOG.warn("Expected response:");
+            printDocument(template);
+            fail("Differences found: " + dd.toString());
+        }
+    }
+
+    private static void printDocument(final Document doc) throws Exception {
+        final TransformerFactory tf = TransformerFactory.newInstance();
+        final Transformer transformer = tf.newTransformer();
+        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
+        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
+
+        final StringWriter writer = new StringWriter();
+        transformer.transform(new DOMSource(doc),
+            new StreamResult(writer));
+        LOG.warn(writer.getBuffer().toString());
+    }
+}
\ No newline at end of file
index 914ac1124135ea3c6b743b7efe2ccece25595a50..e634dc4cc596e97a53c1bdfa5527c89b9c2a55d9 100644 (file)
@@ -8,96 +8,28 @@
 
 package org.opendaylight.netconf.mdsal.connector.ops;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.opendaylight.yangtools.yang.test.util.YangParserTestUtils.parseYangResources;
 
-import com.google.common.io.ByteSource;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.MoreExecutors;
-import java.io.StringWriter;
-import java.util.EnumMap;
-import java.util.concurrent.ExecutorService;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-import org.custommonkey.xmlunit.DetailedDiff;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
-import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
-import org.junit.Before;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
-import org.opendaylight.controller.sal.core.spi.data.DOMStore;
-import org.opendaylight.netconf.mapping.api.NetconfOperation;
-import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
-import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
-import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
-import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 
-public class CopyConfigTest {
-    private static final Logger LOG = LoggerFactory.getLogger(CopyConfigTest.class);
-    private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
-    private static final String RPC_REPLY_ELEMENT = "rpc-reply";
-    private static final String DATA_ELEMENT = "data";
-    private static final Document RPC_REPLY_OK = getReplyOk();
+public class CopyConfigTest extends AbstractNetconfOperationTest {
 
-    private CurrentSchemaContext currentSchemaContext;
-    private TransactionProvider transactionProvider;
-
-    @Before
-    public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
-        XMLUnit.setIgnoreWhitespace(true);
-        XMLUnit.setIgnoreAttributeOrder(true);
-
-        final SchemaContext schemaContext = parseYangResources(CopyConfigTest.class,
+    @Override
+    protected SchemaContext getSchemaContext() {
+        return parseYangResources(CopyConfigTest.class,
             "/yang/mdsal-netconf-mapping-test.yang");
-        final SchemaService schemaService = new SchemaServiceStub(schemaContext);
-        final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
-        final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
-
-        currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> {
-            final YangTextSchemaSource yangTextSchemaSource =
-                YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes()));
-            return Futures.immediateCheckedFuture(yangTextSchemaSource);
-        });
-
-        final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
-        datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
-        datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
-
-        final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
-            16, 16, "CommitFutures", CopyConfigTest.class);
-
-        final SerializedDOMDataBroker sdb = new SerializedDOMDataBroker(datastores,
-            MoreExecutors.listeningDecorator(listenableFutureExecutor));
-        this.transactionProvider = new TransactionProvider(sdb, SESSION_ID_FOR_REPORTING);
     }
 
+
     @Test
     public void testTargetMissing() throws Exception {
         try {
@@ -198,12 +130,14 @@ public class CopyConfigTest {
 
     @Test
     public void testOrderedList() throws Exception {
-        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"), RPC_REPLY_OK);
+        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
+            RPC_REPLY_OK);
         verifyResponse(commit(), RPC_REPLY_OK);
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
             "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
 
-        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"), RPC_REPLY_OK);
+        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
+            RPC_REPLY_OK);
         verifyResponse(commit(), RPC_REPLY_OK);
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
             "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
@@ -211,12 +145,14 @@ public class CopyConfigTest {
 
     @Test
     public void testToplevelList() throws Exception {
-        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"), RPC_REPLY_OK);
+        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
+            RPC_REPLY_OK);
         verifyResponse(commit(), RPC_REPLY_OK);
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
             "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
 
-        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"), RPC_REPLY_OK);
+        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
+            RPC_REPLY_OK);
         verifyResponse(commit(), RPC_REPLY_OK);
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
             "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
@@ -225,7 +161,8 @@ public class CopyConfigTest {
     @Test
     public void testEmptyContainer() throws Exception {
         // Check that empty non-presence container is removed.
-        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"), RPC_REPLY_OK);
+        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"),
+            RPC_REPLY_OK);
         verifyResponse(commit(), RPC_REPLY_OK);
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
             "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
@@ -243,7 +180,8 @@ public class CopyConfigTest {
 
     @Test
     public void testAugmentations() throws Exception {
-        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"), RPC_REPLY_OK);
+        verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
+            RPC_REPLY_OK);
         verifyResponse(commit(), RPC_REPLY_OK);
         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
             "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
@@ -263,91 +201,9 @@ public class CopyConfigTest {
             "messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
     }
 
-    @SuppressWarnings("illegalCatch")
-    private static Document getReplyOk() {
-        Document doc;
-        try {
-            doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
-        } catch (final Exception e) {
-            LOG.debug("unable to load rpc reply ok.", e);
-            doc = XmlUtil.newDocument();
-        }
-        return doc;
-    }
-
-    private Document commit() throws Exception {
-        final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
-        return executeOperation(commit, "messages/mapping/commit.xml");
-    }
-
-    private Document discardChanges() throws Exception {
-        final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
-        return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
-    }
-
-    private Document getConfigRunning() throws Exception {
-        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(getConfig, "messages/mapping/getConfig.xml");
-    }
-
-    private Document getConfigCandidate() throws Exception {
-        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
-    }
-
     private Document copyConfig(final String resource) throws Exception {
-        final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
-            transactionProvider);
+        final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
+            getTransactionProvider());
         return executeOperation(copyConfig, resource);
     }
-
-    private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
-        final Document request = XmlFileLoader.xmlFileToDocument(filename);
-        final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
-
-        LOG.debug("Got response {}", response);
-        return response;
-    }
-
-    private static void verifyResponse(final Document response, final Document template) throws Exception {
-        final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
-        dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
-        if (!dd.similar()) {
-            LOG.warn("Actual response:");
-            printDocument(response);
-            LOG.warn("Expected response:");
-            printDocument(template);
-            fail("Differences found: " + dd.toString());
-        }
-    }
-
-    private static void printDocument(final Document doc) throws Exception {
-        final TransformerFactory tf = TransformerFactory.newInstance();
-        final Transformer transformer = tf.newTransformer();
-        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
-        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
-        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
-        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
-
-        final StringWriter writer = new StringWriter();
-        transformer.transform(new DOMSource(doc),
-            new StreamResult(writer));
-        LOG.warn(writer.getBuffer().toString());
-    }
-
-    private static void assertEmptyDatastore(final Document response) {
-        final NodeList nodes = response.getChildNodes();
-        assertTrue(nodes.getLength() == 1);
-
-        assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
-
-        final NodeList replyNodes = nodes.item(0).getChildNodes();
-        assertTrue(replyNodes.getLength() == 1);
-
-        final Node dataNode = replyNodes.item(0);
-        assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
-        assertFalse(dataNode.hasChildNodes());
-    }
-
 }
\ No newline at end of file
index a514ca46dd763aaf8c02873500c2580d889c921e..a7709ecd0ebb6b9db70c610d6a620d5a0ef9f3ab 100644 (file)
@@ -9,75 +9,42 @@
 package org.opendaylight.netconf.mdsal.connector.ops;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doAnswer;
 
-import com.google.common.io.ByteSource;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.MoreExecutors;
 import java.io.StringWriter;
-import java.util.EnumMap;
-import java.util.concurrent.ExecutorService;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
-import org.custommonkey.xmlunit.DetailedDiff;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
 import org.opendaylight.controller.config.util.xml.XmlElement;
 import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
-import org.opendaylight.controller.sal.core.spi.data.DOMStore;
-import org.opendaylight.netconf.mapping.api.NetconfOperation;
-import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
-import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
-import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
-public class NetconfMDSalMappingTest {
+public class NetconfMDSalMappingTest extends AbstractNetconfOperationTest {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
 
     private static final String TARGET_KEY = "target";
-    private static final String RPC_REPLY_ELEMENT = "rpc-reply";
-    private static final String DATA_ELEMENT = "data";
     private static final String FILTER_NODE = "filter";
     private static final String GET_CONFIG = "get-config";
     private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
@@ -101,63 +68,11 @@ public class NetconfMDSalMappingTest {
 
     private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
             YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
-    private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
-    private static final Document RPC_REPLY_OK = NetconfMDSalMappingTest.getReplyOk();
 
-    private CurrentSchemaContext currentSchemaContext = null;
-    private SchemaContext schemaContext = null;
-    private TransactionProvider transactionProvider = null;
-
-    @Mock
-    private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
-
-    @SuppressWarnings("illegalCatch")
-    private static Document getReplyOk() {
-        Document doc;
-        try {
-            doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
-        } catch (final Exception e) {
-            LOG.debug("unable to load rpc reply ok.", e);
-            doc = XmlUtil.newDocument();
-        }
-        return doc;
-    }
-
-    @Before
-    public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
-        XMLUnit.setIgnoreWhitespace(true);
-        XMLUnit.setIgnoreAttributeOrder(true);
-
-        this.schemaContext = YangParserTestUtils.parseYangResources(NetconfMDSalMappingTest.class,
+    @Override
+    protected SchemaContext getSchemaContext() {
+        return YangParserTestUtils.parseYangResources(NetconfMDSalMappingTest.class,
             "/META-INF/yang/config@2013-04-05.yang", "/yang/mdsal-netconf-mapping-test.yang");
-        schemaContext.getModules();
-        final SchemaService schemaService = createSchemaService();
-
-        final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
-        final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
-
-        final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
-        datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
-        datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
-
-        final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
-                16, 16, "CommitFutures", NetconfMDSalMappingTest.class);
-
-        final SerializedDOMDataBroker sdb = new SerializedDOMDataBroker(datastores,
-                MoreExecutors.listeningDecorator(listenableFutureExecutor));
-        this.transactionProvider = new TransactionProvider(sdb, SESSION_ID_FOR_REPORTING);
-
-        doAnswer(invocationOnMock -> {
-            final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
-            final YangTextSchemaSource yangTextSchemaSource =
-                    YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
-            return Futures.immediateFuture(yangTextSchemaSource);
-
-        }).when(sourceProvider).getSource(any(SourceIdentifier.class));
-
-        this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
     }
 
     @Test
@@ -170,8 +85,8 @@ public class NetconfMDSalMappingTest {
     @Test
     public void testIncorrectGet() throws Exception {
         try {
-            executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
-                    "messages/mapping/bad_getConfig.xml");
+            executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
+                    getTransactionProvider()), "messages/mapping/bad_getConfig.xml");
             fail("Should have failed, this is an incorrect request");
         } catch (final DocumentedException e) {
             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
@@ -180,8 +95,8 @@ public class NetconfMDSalMappingTest {
         }
 
         try {
-            executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
-                    "messages/mapping/bad_namespace_getConfig.xml");
+            executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
+                    getTransactionProvider()), "messages/mapping/bad_namespace_getConfig.xml");
             fail("Should have failed, this is an incorrect request");
         } catch (final DocumentedException e) {
             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
@@ -692,8 +607,8 @@ public class NetconfMDSalMappingTest {
 
     private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier)
             throws Exception {
-        final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
-                transactionProvider);
+        final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
+                getTransactionProvider());
         final Document request = XmlFileLoader.xmlFileToDocument(resource);
         final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
         assertEquals(identifier, iid);
@@ -719,148 +634,4 @@ public class NetconfMDSalMappingTest {
         verifyResponse(commit(), RPC_REPLY_OK);
         assertEmptyDatastore(getConfigRunning());
     }
-
-    private static void verifyResponse(final Document response, final Document template) throws Exception {
-        final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
-        dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
-
-        printDocument(response);
-        printDocument(template);
-
-        assertTrue(dd.toString(), dd.similar());
-    }
-
-    private static void assertEmptyDatastore(final Document response) {
-        final NodeList nodes = response.getChildNodes();
-        assertTrue(nodes.getLength() == 1);
-
-        assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
-
-        final NodeList replyNodes = nodes.item(0).getChildNodes();
-        assertTrue(replyNodes.getLength() == 1);
-
-        final Node dataNode = replyNodes.item(0);
-        assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
-        assertFalse(dataNode.hasChildNodes());
-    }
-
-    private Document commit() throws Exception {
-        final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
-        return executeOperation(commit, "messages/mapping/commit.xml");
-    }
-
-    private Document discardChanges() throws Exception {
-        final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
-        return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
-    }
-
-    private Document edit(final String resource) throws Exception {
-        final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
-                transactionProvider);
-        return executeOperation(editConfig, resource);
-    }
-
-    private Document get() throws Exception {
-        final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(get, "messages/mapping/get.xml");
-    }
-
-    private Document getWithFilter(final String resource) throws Exception {
-        final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(get, resource);
-    }
-
-    private Document getConfigRunning() throws Exception {
-        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(getConfig, "messages/mapping/getConfig.xml");
-    }
-
-    private Document getConfigCandidate() throws Exception {
-        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
-    }
-
-    private Document getConfigWithFilter(final String resource) throws Exception {
-        final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
-        return executeOperation(getConfig, resource);
-    }
-
-    private static Document lock() throws Exception {
-        final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
-        return executeOperation(lock, "messages/mapping/lock.xml");
-    }
-
-    private static Document unlock() throws Exception {
-        final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
-        return executeOperation(unlock, "messages/mapping/unlock.xml");
-    }
-
-    private static Document lockWithoutTarget() throws Exception {
-        final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
-        return executeOperation(lock, "messages/mapping/lock_notarget.xml");
-    }
-
-    private static Document unlockWithoutTarget() throws Exception {
-        final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
-        return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
-    }
-
-    private static Document lockCandidate() throws Exception {
-        final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
-        return executeOperation(lock, "messages/mapping/lock_candidate.xml");
-    }
-
-    private static Document unlockCandidate() throws Exception {
-        final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
-        return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
-    }
-
-    private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
-        final Document request = XmlFileLoader.xmlFileToDocument(filename);
-        final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
-
-        LOG.debug("Got response {}", response);
-        return response;
-    }
-
-    private SchemaService createSchemaService() {
-        return new SchemaService() {
-
-            @Override
-            public void addModule(final Module module) {
-            }
-
-            @Override
-            public void removeModule(final Module module) {
-
-            }
-
-            @Override
-            public SchemaContext getSessionContext() {
-                return schemaContext;
-            }
-
-            @Override
-            public SchemaContext getGlobalContext() {
-                return schemaContext;
-            }
-
-            @Override
-            public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
-                    final SchemaContextListener listener) {
-                listener.onGlobalContextUpdated(getGlobalContext());
-                return new ListenerRegistration<SchemaContextListener>() {
-                    @Override
-                    public void close() {
-
-                    }
-
-                    @Override
-                    public SchemaContextListener getInstance() {
-                        return listener;
-                    }
-                };
-            }
-        };
-    }
 }