Further rework of base schemas
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfBaseOpsTest.java
index 4ece5f4ae313530e430d2726456a5efcebe8d3bb..0320a6e2c04327aca0afd21748502caad5dbd617 100644 (file)
@@ -5,62 +5,61 @@
  * 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.sal.connect.netconf.util;
 
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FluentFuture;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.ArgumentMatcher;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.netconf.api.ModifyAction;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
+import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
+import org.opendaylight.netconf.util.NetconfUtil;
+import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.xml.sax.SAXException;
 
-public class NetconfBaseOpsTest {
+public class NetconfBaseOpsTest extends AbstractTestModelTest {
+    private static final QName CONTAINER_Q_NAME = QName.create("test:namespace", "2013-07-22", "c");
 
     static {
         XMLUnit.setIgnoreWhitespace(true);
         XMLUnit.setIgnoreComments(true);
     }
 
-    private static final QName CONTAINER_Q_NAME = QName.create("test:namespace", "2013-07-22", "c");
-
     @Mock
     private RemoteDeviceCommunicator<NetconfMessage> listener;
     private NetconfRpcFutureCallback callback;
@@ -74,31 +73,30 @@ public class NetconfBaseOpsTest {
         final NetconfMessage ok = new NetconfMessage(XmlUtil.readXmlToDocument(okStream));
         final NetconfMessage data = new NetconfMessage(XmlUtil.readXmlToDocument(dataStream));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME)))
-                .thenReturn(RpcResultBuilder.success(data).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(data).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_GET_QNAME)))
-                .thenReturn(RpcResultBuilder.success(data).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(data).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_COPY_CONFIG_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_VALIDATE_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME)))
-                .thenReturn(RpcResultBuilder.success(ok).buildFuture());
-        final SchemaContext schemaContext =
-                YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
-        final MessageTransformer<NetconfMessage> transformer = new NetconfMessageTransformer(schemaContext, true);
-        final DOMRpcService rpc = new NetconfDeviceRpc(schemaContext, listener, transformer);
+                .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
+        final MessageTransformer<NetconfMessage> transformer = new NetconfMessageTransformer(
+            new EmptyMountPointContext(SCHEMA_CONTEXT), true, BASE_SCHEMAS.getBaseSchema());
+        final DOMRpcService rpc = new NetconfDeviceRpc(SCHEMA_CONTEXT, listener, transformer);
         final RemoteDeviceId id =
                 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
         callback = new NetconfRpcFutureCallback("prefix", id);
-        baseOps = new NetconfBaseOps(rpc, schemaContext);
+        baseOps = new NetconfBaseOps(rpc, SCHEMA_CONTEXT);
     }
 
     @Test
@@ -179,28 +177,28 @@ public class NetconfBaseOpsTest {
     @Test
     public void testGetConfigRunningData() throws Exception {
         final Optional<NormalizedNode<?, ?>> dataOpt =
-                baseOps.getConfigRunningData(callback, Optional.of(YangInstanceIdentifier.EMPTY)).get();
+                baseOps.getConfigRunningData(callback, Optional.of(YangInstanceIdentifier.empty())).get();
         Assert.assertTrue(dataOpt.isPresent());
-        Assert.assertEquals(NetconfMessageTransformUtil.NETCONF_DATA_QNAME, dataOpt.get().getNodeType());
+        Assert.assertEquals(NetconfUtil.NETCONF_DATA_QNAME, dataOpt.get().getNodeType());
     }
 
     @Test
     public void testGetData() throws Exception {
         final Optional<NormalizedNode<?, ?>> dataOpt =
-                baseOps.getData(callback, Optional.of(YangInstanceIdentifier.EMPTY)).get();
+                baseOps.getData(callback, Optional.of(YangInstanceIdentifier.empty())).get();
         Assert.assertTrue(dataOpt.isPresent());
-        Assert.assertEquals(NetconfMessageTransformUtil.NETCONF_DATA_QNAME, dataOpt.get().getNodeType());
+        Assert.assertEquals(NetconfUtil.NETCONF_DATA_QNAME, dataOpt.get().getNodeType());
     }
 
     @Test
     public void testGetConfigRunning() throws Exception {
-        baseOps.getConfigRunning(callback, Optional.absent());
+        baseOps.getConfigRunning(callback, Optional.empty());
         verifyMessageSent("getConfig", NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME);
     }
 
     @Test
     public void testGetConfigCandidate() throws Exception {
-        baseOps.getConfigCandidate(callback, Optional.absent());
+        baseOps.getConfigCandidate(callback, Optional.empty());
         verifyMessageSent("getConfig_candidate", NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME);
     }
 
@@ -215,7 +213,7 @@ public class NetconfBaseOpsTest {
 
     @Test
     public void testGet() throws Exception {
-        baseOps.get(callback, Optional.absent());
+        baseOps.get(callback, Optional.empty());
         verifyMessageSent("get", NetconfMessageTransformUtil.NETCONF_GET_QNAME);
     }
 
@@ -268,7 +266,7 @@ public class NetconfBaseOpsTest {
         }
     }
 
-    private static class NetconfMessageMatcher extends BaseMatcher<NetconfMessage> {
+    private static class NetconfMessageMatcher implements ArgumentMatcher<NetconfMessage> {
 
         private final Document expected;
 
@@ -277,11 +275,7 @@ public class NetconfBaseOpsTest {
         }
 
         @Override
-        public boolean matches(final Object item) {
-            if (!(item instanceof NetconfMessage)) {
-                return false;
-            }
-            final NetconfMessage message = (NetconfMessage) item;
+        public boolean matches(final NetconfMessage message) {
             final Document actualDoc = removeAttrs(message.getDocument());
             actualDoc.normalizeDocument();
             expected.normalizeDocument();
@@ -289,11 +283,6 @@ public class NetconfBaseOpsTest {
             return diff.similar();
         }
 
-        @Override
-        public void describeTo(final Description description) {
-            description.appendText(XmlUtil.toString(expected));
-        }
-
         private static Document removeAttrs(final Document input) {
             final Document copy = XmlUtil.newDocument();
             copy.appendChild(copy.importNode(input.getDocumentElement(), true));