Merge "Bug 460 - Fix warning throughout netconf subsystem"
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / ConcurrentClientsTest.java
index 0ef2c285e407ac65b9eb62e0bbfc2d14b555c242..02889b62a5bf71fe6cdb1161c7ac3fdaa0c00eaf 100644 (file)
@@ -8,59 +8,54 @@
 
 package org.opendaylight.controller.netconf.impl;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.Sets;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.util.HashedWheelTimer;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+
+import java.io.DataOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.management.ManagementFactory;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
 import org.apache.commons.io.IOUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
-import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
-import org.opendaylight.controller.config.yang.store.api.YangStoreService;
-import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
-import org.opendaylight.controller.netconf.client.NetconfClient;
 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
+import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
 import org.opendaylight.controller.netconf.mapping.api.Capability;
 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
+import org.opendaylight.controller.netconf.util.messages.NetconfStartExiMessage;
 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
-import javax.management.ObjectName;
-import java.io.DataOutputStream;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.lang.management.ManagementFactory;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.util.HashedWheelTimer;
 
 public class ConcurrentClientsTest {
 
@@ -68,11 +63,6 @@ public class ConcurrentClientsTest {
     private EventLoopGroup nettyGroup;
     private NetconfClientDispatcher netconfClientDispatcher;
 
-    @Mock
-    private YangStoreService yangStoreService;
-    @Mock
-    private ConfigRegistryJMXClient jmxClient;
-
     private final InetSocketAddress netconfAddress = new InetSocketAddress("127.0.0.1", 8303);
 
     static final Logger logger = LoggerFactory.getLogger(ConcurrentClientsTest.class);
@@ -80,44 +70,36 @@ public class ConcurrentClientsTest {
     private DefaultCommitNotificationProducer commitNot;
     private NetconfServerDispatcher dispatch;
 
-    @Mock
-    private SessionMonitoringService monitoring;
+
 
     HashedWheelTimer hashedWheelTimer;
 
+    public static SessionMonitoringService createMockedMonitoringService() {
+        SessionMonitoringService monitoring = mock(SessionMonitoringService.class);
+        doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class));
+        doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class));
+        return monitoring;
+    }
+
     @Before
     public void setUp() throws Exception {
-        { // init mocks
-            MockitoAnnotations.initMocks(this);
-            final YangStoreSnapshot yStore = mock(YangStoreSnapshot.class);
-            doReturn(yStore).when(this.yangStoreService).getYangStoreSnapshot();
-            doReturn(Collections.emptyMap()).when(yStore).getModuleMXBeanEntryMap();
-
-            final ConfigTransactionJMXClient mockedTCl = mock(ConfigTransactionJMXClient.class);
-            doReturn(mockedTCl).when(this.jmxClient).getConfigTransactionClient(any(ObjectName.class));
-
-            doReturn(Collections.emptySet()).when(jmxClient).lookupConfigBeans();
-        }
 
         nettyGroup = new NioEventLoopGroup();
-        netconfClientDispatcher = new NetconfClientDispatcher( nettyGroup, nettyGroup, 5000);
+        NetconfHelloMessageAdditionalHeader additionalHeader = new NetconfHelloMessageAdditionalHeader("uname", "10.10.10.1", "830", "tcp", "client");
+        netconfClientDispatcher = new NetconfClientDispatcher( nettyGroup, nettyGroup, additionalHeader, 5000);
 
         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
         factoriesListener.onAddNetconfOperationServiceFactory(mockOpF());
 
         SessionIdProvider idProvider = new SessionIdProvider();
         hashedWheelTimer = new HashedWheelTimer();
+
         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
-                hashedWheelTimer, factoriesListener, idProvider, 5000);
+                hashedWheelTimer, factoriesListener, idProvider, 5000, commitNot, createMockedMonitoringService());
 
         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
 
-        doNothing().when(monitoring).onSessionUp(any(NetconfServerSession.class));
-        doNothing().when(monitoring).onSessionDown(any(NetconfServerSession.class));
-
-        NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
-                factoriesListener, commitNot, idProvider, monitoring);
-        NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(serverNegotiatorFactory, listenerFactory);
+        NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(serverNegotiatorFactory);
         dispatch = new NetconfServerDispatcher(serverChannelInitializer, nettyGroup, nettyGroup);
 
         ChannelFuture s = dispatch.createServer(netconfAddress);
@@ -133,7 +115,7 @@ public class ConcurrentClientsTest {
     private NetconfOperationServiceFactory mockOpF() {
         return new NetconfOperationServiceFactory() {
             @Override
-            public NetconfOperationService createService(long netconfSessionId, String netconfSessionIdForReporting) {
+            public NetconfOperationService createService(String netconfSessionIdForReporting) {
                 return new NetconfOperationService() {
                     @Override
                     public Set<Capability> getCapabilities() {
@@ -145,12 +127,13 @@ public class ConcurrentClientsTest {
                         return Sets.<NetconfOperation> newHashSet(new NetconfOperation() {
                             @Override
                             public HandlingPriority canHandle(Document message) {
-                                return HandlingPriority.getHandlingPriority(Integer.MAX_VALUE);
+                                return XmlUtil.toString(message).contains(NetconfStartExiMessage.START_EXI) ?
+                                        HandlingPriority.CANNOT_HANDLE :
+                                        HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
                             }
 
                             @Override
-                            public Document handle(Document message, NetconfOperationRouter operationRouter)
-                                    throws NetconfDocumentedException {
+                            public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
                                 try {
                                     return XmlUtil.readXmlToDocument("<test/>");
                                 } catch (Exception e) {
@@ -160,11 +143,6 @@ public class ConcurrentClientsTest {
                         });
                     }
 
-                    @Override
-                    public Set<NetconfOperationFilter> getFilters() {
-                        return Collections.emptySet();
-                    }
-
                     @Override
                     public void close() {
                     }
@@ -178,7 +156,7 @@ public class ConcurrentClientsTest {
         commitNot.close();
     }
 
-    @Test
+    @Test(timeout = 30 * 1000)
     public void multipleClients() throws Exception {
         List<TestingThread> threads = new ArrayList<>();
 
@@ -199,12 +177,12 @@ public class ConcurrentClientsTest {
         }
     }
 
-    @Test
+    @Test(timeout = 30 * 1000)
     public void synchronizationTest() throws Exception {
         new BlockingThread("foo").run2();
     }
 
-    @Test
+    @Test(timeout = 30 * 1000)
     public void multipleBlockingClients() throws Exception {
         List<BlockingThread> threads = new ArrayList<>();
         for (int i = 0; i < CONCURRENCY; i++) {
@@ -287,13 +265,13 @@ public class ConcurrentClientsTest {
         @Override
         public void run() {
             try {
-                final NetconfClient netconfClient = new NetconfClient(clientId, netconfAddress, netconfClientDispatcher);
+                final TestingNetconfClient netconfClient = new TestingNetconfClient(clientId, netconfAddress, netconfClientDispatcher);
                 long sessionId = netconfClient.getSessionId();
                 logger.info("Client with sessionid {} hello exchanged", sessionId);
 
                 final NetconfMessage getMessage = XmlFileLoader
                         .xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
-                NetconfMessage result = netconfClient.sendMessage(getMessage);
+                NetconfMessage result = netconfClient.sendRequest(getMessage).get();
                 logger.info("Client with sessionid {} got result {}", sessionId, result);
                 netconfClient.close();
                 logger.info("Client with session id {} ended", sessionId);