Do not reference individual builders
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / RuntimeRpcTest.java
index 5356745247aefb530658c80ce7a09c0801852e65..a3019a0fa3b02f40f53c3dbb2eeb327f4e59c668 100644 (file)
@@ -5,69 +5,64 @@
  * 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.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.MockitoAnnotations.initMocks;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
 import com.google.common.base.Preconditions;
 import com.google.common.io.ByteSource;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.FluentFuture;
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import javax.xml.transform.TransformerException;
+import javax.xml.parsers.ParserConfigurationException;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
+import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-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.dom.api.DOMRpcAvailabilityListener;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
-import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.mdsal.dom.api.DOMRpcException;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.api.DocumentedException;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.mapping.api.HandlingPriority;
 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
+import org.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 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;
@@ -75,125 +70,122 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class RuntimeRpcTest {
-
     private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpcTest.class);
+    private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
+    private static final Document RPC_REPLY_OK = getReplyOk();
 
-    private final String sessionIdForReporting = "netconf-test-session1";
-
-    private static Document RPC_REPLY_OK = null;
-
-    static {
+    private static Document getReplyOk() {
         try {
-            RPC_REPLY_OK = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/runtimerpc-ok-reply.xml");
-        } catch (final Exception e) {
+            return XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/runtimerpc-ok-reply.xml");
+        } catch (final IOException | SAXException | ParserConfigurationException e) {
             LOG.debug("unable to load rpc reply ok.", e);
-            RPC_REPLY_OK = XmlUtil.newDocument();
+            return XmlUtil.newDocument();
         }
     }
 
-    private final DOMRpcService rpcServiceVoidInvoke = new DOMRpcService() {
-        @Nonnull
+    private static final DOMRpcService RPC_SERVICE_VOID_INVOKER = new DOMRpcService() {
         @Override
-        public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
-            return Futures.immediateCheckedFuture((DOMRpcResult) new DefaultDOMRpcResult(null, Collections.<RpcError>emptyList()));
+        public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
+            return immediateFluentFuture(new DefaultDOMRpcResult(null, List.of()));
         }
 
-        @Nonnull
         @Override
-        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
-            return null;
+        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
+            return NoOpListenerRegistration.of(listener);
         }
     };
 
-    private final DOMRpcService rpcServiceFailedInvocation = new DOMRpcService() {
-        @Nonnull
+    private static final DOMRpcService RPC_SERVICE_FAILED_INVOCATION = new DOMRpcService() {
         @Override
-        public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
-            return Futures.immediateFailedCheckedFuture((DOMRpcException) new DOMRpcException("rpc invocation not implemented yet") {
+        public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
+            return immediateFailedFluentFuture(new DOMRpcException("rpc invocation not implemented yet") {
+                private static final long serialVersionUID = 1L;
             });
         }
 
-        @Nonnull
         @Override
-        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
-            return null;
+        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
+            return NoOpListenerRegistration.of(listener);
         }
     };
 
-    private final DOMRpcService rpcServiceSuccesfullInvocation = new DOMRpcService() {
-        @Nonnull
+    private final DOMRpcService rpcServiceSuccessfulInvocation = new DOMRpcService() {
         @Override
-        public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
-            final Collection<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> children = (Collection) input.getValue();
-            final Module module = schemaContext.findModuleByNamespaceAndRevision(type.getLastComponent().getNamespace(), null);
-            final RpcDefinition rpcDefinition = getRpcDefinitionFromModule(module, module.getNamespace(), type.getLastComponent().getLocalName());
-            final ContainerSchemaNode outputSchemaNode = rpcDefinition.getOutput();
-            final ContainerNode node = ImmutableContainerNodeBuilder.create()
-                    .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(outputSchemaNode.getQName()))
-                    .withValue(children).build();
-
-            return Futures.immediateCheckedFuture((DOMRpcResult) new DefaultDOMRpcResult(node));
+        public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
+            final Collection<DataContainerChild> children = ((ContainerNode) input).body();
+            final Module module = SCHEMA_CONTEXT.findModules(type.getNamespace()).stream()
+                .findFirst().orElse(null);
+            final RpcDefinition rpcDefinition = getRpcDefinitionFromModule(module, module.getNamespace(),
+                type.getLocalName());
+            final ContainerNode node = Builders.containerBuilder()
+                    .withNodeIdentifier(new NodeIdentifier(rpcDefinition.getOutput().getQName()))
+                    .withValue(children)
+                    .build();
+
+            return immediateFluentFuture(new DefaultDOMRpcResult(node));
         }
 
-        @Nonnull
         @Override
-        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
-            return null;
+        public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
+            return NoOpListenerRegistration.of(lsnr);
         }
     };
 
-    private SchemaContext schemaContext = null;
+    private static EffectiveModelContext SCHEMA_CONTEXT = null;
     private CurrentSchemaContext currentSchemaContext = null;
+
     @Mock
-    private SchemaService schemaService;
+    private DOMSchemaService schemaService;
     @Mock
-    private SchemaContextListener listener;
+    private EffectiveModelContextListener listener;
     @Mock
-    private ListenerRegistration registration;
+    private ListenerRegistration<?> registration;
     @Mock
     private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
 
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yang/mdsal-netconf-rpc-test.yang");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
+    }
+
     @Before
     public void setUp() throws Exception {
-
-        initMocks(this);
         doNothing().when(registration).close();
-        doReturn(listener).when(registration).getInstance();
-        doNothing().when(schemaService).addModule(any(Module.class));
-        doNothing().when(schemaService).removeModule(any(Module.class));
-        doReturn(schemaContext).when(schemaService).getGlobalContext();
-        doReturn(schemaContext).when(schemaService).getSessionContext();
-        doAnswer(new Answer() {
-            @Override
-            public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
-                ((SchemaContextListener) invocationOnMock.getArguments()[0]).onGlobalContextUpdated(schemaContext);
-                return registration;
-            }
-        }).when(schemaService).registerSchemaContextListener(any(SchemaContextListener.class));
+        doAnswer(invocationOnMock -> {
+            ((EffectiveModelContextListener) invocationOnMock.getArguments()[0]).onModelContextUpdated(SCHEMA_CONTEXT);
+            return registration;
+        }).when(schemaService).registerSchemaContextListener(any(EffectiveModelContextListener.class));
 
         XMLUnit.setIgnoreWhitespace(true);
         XMLUnit.setIgnoreAttributeOrder(true);
 
-        doAnswer(new Answer() {
-            @Override
-            public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
-                final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
-                final YangTextSchemaSource yangTextSchemaSource =
-                        YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
-                return Futures.immediateCheckedFuture(yangTextSchemaSource);
-
-            }
+        doAnswer(invocationOnMock -> {
+            final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
+            final YangTextSchemaSource yangTextSchemaSource =
+                    YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
+            return immediateFluentFuture(yangTextSchemaSource);
         }).when(sourceProvider).getSource(any(SourceIdentifier.class));
 
-        this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
-        this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
+        currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceProvider);
+    }
+
+    @After
+    public void tearDown() {
+        currentSchemaContext.close();
     }
 
     @Test
     public void testVoidOutputRpc() throws Exception {
-        final RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceVoidInvoke);
+        final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-output.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
@@ -206,7 +198,8 @@ public class RuntimeRpcTest {
 
     @Test
     public void testSuccesfullNonVoidInvocation() throws Exception {
-        final RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceSuccesfullInvocation);
+        final RuntimeRpc rpc = new RuntimeRpc(
+            SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
@@ -218,7 +211,8 @@ public class RuntimeRpcTest {
 
     @Test
     public void testSuccesfullContainerInvocation() throws Exception {
-        final RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceSuccesfullInvocation);
+        final RuntimeRpc rpc = new RuntimeRpc(
+            SESSION_ID_FOR_REPORTING, currentSchemaContext, rpcServiceSuccessfulInvocation);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
@@ -230,25 +224,24 @@ public class RuntimeRpcTest {
 
     @Test
     public void testFailedInvocation() throws Exception {
-        final RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceFailedInvocation);
+        final RuntimeRpc rpc = new RuntimeRpc(
+                SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_FAILED_INVOCATION);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
 
-        try {
-            rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
-            fail("should have failed with rpc invocation not implemented yet");
-        } catch (final DocumentedException e) {
-            assertTrue(e.getErrorType() == ErrorType.APPLICATION);
-            assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
-            assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
-        }
+        final DocumentedException e = assertThrows(DocumentedException.class,
+                () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
+
+        assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
+        assertEquals(e.getErrorTag(), ErrorTag.OPERATION_FAILED);
+        assertEquals(e.getErrorType(), ErrorType.APPLICATION);
     }
 
     @Test
     public void testVoidInputOutputRpc() throws Exception {
-        final RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceVoidInvoke);
+        final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
 
         final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-input-output.xml");
         final HandlingPriority priority = rpc.canHandle(rpcDocument);
@@ -261,27 +254,26 @@ public class RuntimeRpcTest {
 
     @Test
     public void testBadNamespaceInRpc() throws Exception {
-        final RuntimeRpc rpc = new RuntimeRpc(sessionIdForReporting, currentSchemaContext, rpcServiceVoidInvoke);
+        final RuntimeRpc rpc = new RuntimeRpc(SESSION_ID_FOR_REPORTING, currentSchemaContext, RPC_SERVICE_VOID_INVOKER);
         final 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 (final DocumentedException e) {
-            assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
-            assertTrue(e.getErrorTag() == ErrorTag.BAD_ELEMENT);
-            assertTrue(e.getErrorType() == ErrorType.APPLICATION);
-        }
+        final DocumentedException e = assertThrows(DocumentedException.class,
+                () -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
+
+        assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
+        assertEquals(e.getErrorTag(), ErrorTag.BAD_ELEMENT);
+        assertEquals(e.getErrorType(), ErrorType.APPLICATION);
     }
 
-    private void verifyResponse(final Document response, final Document template) throws IOException, TransformerException {
+    private static void verifyResponse(final Document response, final Document template) {
         final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
         dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
         //we care about order so response has to be identical
         assertTrue(dd.identical());
     }
 
-    private RpcDefinition getRpcDefinitionFromModule(final Module module, final URI namespaceURI, final String name) {
+    private static RpcDefinition getRpcDefinitionFromModule(final Module module, final XMLNamespace namespaceURI,
+            final String name) {
         for (final RpcDefinition rpcDef : module.getRpcs()) {
             if (rpcDef.getQName().getNamespace().equals(namespaceURI)
                     && rpcDef.getQName().getLocalName().equals(name)) {
@@ -290,18 +282,5 @@ public class RuntimeRpcTest {
         }
 
         return null;
-
-    }
-
-    private List<InputStream> getYangSchemas() {
-        final List<String> schemaPaths = Arrays.asList("/yang/mdsal-netconf-rpc-test.yang");
-        final List<InputStream> schemas = new ArrayList<>();
-
-        for (final String schemaPath : schemaPaths) {
-            final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
-            schemas.add(resourceAsStream);
-        }
-
-        return schemas;
     }
-}
\ No newline at end of file
+}