Integrate netconf-mapping-api into netconf-server
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / AbstractNetconfOperationTest.java
index 555f201e8db371a9e0af889b3e499230d725a6c5..2dd04a84344f9117fe21b0557b2185309a9d8379 100644 (file)
@@ -5,20 +5,22 @@
  * 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 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.IOException;
 import java.io.StringWriter;
 import java.util.EnumMap;
 import java.util.concurrent.ExecutorService;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -27,57 +29,72 @@ import javax.xml.transform.stream.StreamResult;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.AfterClass;
 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.junit.BeforeClass;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
+import org.opendaylight.mdsal.dom.spi.store.DOMStore;
+import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStoreFactory;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 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.server.api.operations.NetconfOperation;
+import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
 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.api.EffectiveModelContext;
 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;
+import org.xml.sax.SAXException;
 
-abstract class AbstractNetconfOperationTest {
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public 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 static EffectiveModelContext SCHEMA_CONTEXT;
+
     private CurrentSchemaContext currentSchemaContext;
     private TransactionProvider transactionProvider;
 
-    @Before
-    public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = parseYangResources(AbstractNetconfOperationTest.class,
+            "/yang/mdsal-netconf-mapping-test.yang");
+    }
 
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
+    }
+
+    @Before
+    public void setUp() {
         XMLUnit.setIgnoreWhitespace(true);
         XMLUnit.setIgnoreAttributeOrder(true);
 
-        final SchemaContext schemaContext = getSchemaContext();
-        final SchemaService schemaService = new SchemaServiceStub(schemaContext);
+        final DOMSchemaService schemaService = new SchemaServiceStub(SCHEMA_CONTEXT);
         final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
         final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
 
-        currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> {
+        currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceIdentifier -> {
             final YangTextSchemaSource yangTextSchemaSource =
                 YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes()));
-            return Futures.immediateCheckedFuture(yangTextSchemaSource);
+            return Futures.immediateFuture(yangTextSchemaSource);
         });
 
         final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
@@ -89,11 +106,9 @@ abstract class AbstractNetconfOperationTest {
 
         final SerializedDOMDataBroker sdb = new SerializedDOMDataBroker(datastores,
             MoreExecutors.listeningDecorator(listenableFutureExecutor));
-        this.transactionProvider = new TransactionProvider(sdb, SESSION_ID_FOR_REPORTING);
+        transactionProvider = new TransactionProvider(sdb, SESSION_ID_FOR_REPORTING);
     }
 
-    protected abstract SchemaContext getSchemaContext();
-
     protected CurrentSchemaContext getCurrentSchemaContext() {
         return currentSchemaContext;
     }
@@ -102,12 +117,11 @@ abstract class AbstractNetconfOperationTest {
         return transactionProvider;
     }
 
-    @SuppressWarnings("illegalCatch")
     private static Document getReplyOk() {
         Document doc;
         try {
             doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
-        } catch (final Exception e) {
+        } catch (final IOException | SAXException | ParserConfigurationException e) {
             LOG.debug("unable to load rpc reply ok.", e);
             doc = XmlUtil.newDocument();
         }
@@ -130,6 +144,12 @@ abstract class AbstractNetconfOperationTest {
         return executeOperation(editConfig, resource);
     }
 
+    protected Document edit(final Document request) throws Exception {
+        final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
+            transactionProvider);
+        return executeOperation(editConfig, request);
+    }
+
     protected Document get() throws Exception {
         final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
         return executeOperation(get, "messages/mapping/get.xml");
@@ -187,8 +207,11 @@ abstract class AbstractNetconfOperationTest {
 
     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);
+        return executeOperation(op, request);
+    }
 
+    protected static Document executeOperation(final NetconfOperation op, final Document request) throws Exception {
+        final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
         LOG.debug("Got response {}", response);
         return response;
     }
@@ -233,4 +256,4 @@ abstract class AbstractNetconfOperationTest {
             new StreamResult(writer));
         LOG.warn(writer.getBuffer().toString());
     }
-}
\ No newline at end of file
+}