Remove redundant code constructs
[netconf.git] / netconf / netconf-impl / src / test / java / org / opendaylight / netconf / impl / ConcurrentClientsTest.java
index 420a8f5d59ec0a6eca3dd299f737cd22ac44af25..4b8f8591dfd7c6a6b3e674fa41b995cd5f8dc6c6 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.netconf.impl;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
@@ -48,16 +47,16 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
-import org.opendaylight.controller.config.util.capability.Capability;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.api.monitoring.SessionEvent;
-import org.opendaylight.netconf.api.monitoring.SessionListener;
+import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.api.capability.Capability;
 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
+import org.opendaylight.netconf.api.monitoring.SessionEvent;
+import org.opendaylight.netconf.api.monitoring.SessionListener;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.client.NetconfClientDispatcher;
 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
 import org.opendaylight.netconf.client.SimpleNetconfClientSessionListener;
@@ -74,7 +73,6 @@ import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
 import org.opendaylight.netconf.util.test.XmlFileLoader;
 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -87,11 +85,11 @@ public class ConcurrentClientsTest {
     private static ExecutorService clientExecutor;
 
     private static final int CONCURRENCY = 32;
-    private static final InetSocketAddress netconfAddress = new InetSocketAddress("127.0.0.1", 8303);
+    private static final InetSocketAddress NETCONF_ADDRESS = new InetSocketAddress("127.0.0.1", 8303);
 
-    private int nettyThreads;
-    private Class<? extends Runnable> clientRunnable;
-    private Set<String> serverCaps;
+    private final int nettyThreads;
+    private final Class<? extends Runnable> clientRunnable;
+    private final Set<String> serverCaps;
 
     public ConcurrentClientsTest(int nettyThreads, Class<? extends Runnable> clientRunnable, Set<String> serverCaps) {
         this.nettyThreads = nettyThreads;
@@ -101,14 +99,15 @@ public class ConcurrentClientsTest {
 
     @Parameterized.Parameters()
     public static Collection<Object[]> data() {
-        return Arrays.asList(new Object[][]{{4, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
-                                            {1, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
-                                            // empty set of capabilities = only base 1.0 netconf capability
-                                            {4, TestingNetconfClientRunnable.class, Collections.emptySet()},
-                                            {4, TestingNetconfClientRunnable.class, getOnlyExiServerCaps()},
-                                            {4, TestingNetconfClientRunnable.class, getOnlyChunkServerCaps()},
-                                            {4, BlockingClientRunnable.class, getOnlyExiServerCaps()},
-                                            {1, BlockingClientRunnable.class, getOnlyExiServerCaps()},
+        return Arrays.asList(new Object[][]{{4, TestingNetconfClientRunnable.class,
+                NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
+            {1, TestingNetconfClientRunnable.class, NetconfServerSessionNegotiatorFactory.DEFAULT_BASE_CAPABILITIES},
+            // empty set of capabilities = only base 1.0 netconf capability
+            {4, TestingNetconfClientRunnable.class, Collections.emptySet()},
+            {4, TestingNetconfClientRunnable.class, getOnlyExiServerCaps()},
+            {4, TestingNetconfClientRunnable.class, getOnlyChunkServerCaps()},
+            {4, BlockingClientRunnable.class, getOnlyExiServerCaps()},
+            {1, BlockingClientRunnable.class, getOnlyExiServerCaps()},
         });
     }
 
@@ -124,26 +123,24 @@ public class ConcurrentClientsTest {
         doNothing().when(sessionListener).onSessionUp(any(NetconfServerSession.class));
         doNothing().when(sessionListener).onSessionDown(any(NetconfServerSession.class));
         doNothing().when(sessionListener).onSessionEvent(any(SessionEvent.class));
-        doReturn(new AutoCloseable() {
-            @Override
-            public void close() throws Exception {
+        doReturn((AutoCloseable) () -> {
 
-            }
         }).when(monitoring).registerCapabilitiesListener(any(NetconfMonitoringService.CapabilitiesListener.class));
         doReturn(sessionListener).when(monitoring).getSessionListener();
-        doReturn(new CapabilitiesBuilder().setCapability(Collections.<Uri>emptyList()).build()).when(monitoring).getCapabilities();
+        doReturn(new CapabilitiesBuilder().setCapability(Collections.emptyList()).build()).when(monitoring)
+                .getCapabilities();
         return monitoring;
     }
 
     @BeforeClass
     public static void setUpClientExecutor() {
         clientExecutor = Executors.newFixedThreadPool(CONCURRENCY, new ThreadFactory() {
-            int i = 1;
+            int index = 1;
 
             @Override
-            public Thread newThread(final Runnable r) {
-                Thread thread = new Thread(r);
-                thread.setName("client-" + i++);
+            public Thread newThread(final Runnable runnable) {
+                Thread thread = new Thread(runnable);
+                thread.setName("client-" + index++);
                 thread.setDaemon(true);
                 return thread;
             }
@@ -159,11 +156,13 @@ public class ConcurrentClientsTest {
         AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
 
         testingNetconfOperation = new TestingNetconfOperation();
-        factoriesListener.onAddNetconfOperationServiceFactory(new TestingOperationServiceFactory(testingNetconfOperation));
+        factoriesListener.onAddNetconfOperationServiceFactory(
+                new TestingOperationServiceFactory(testingNetconfOperation));
 
         SessionIdProvider idProvider = new SessionIdProvider();
 
-        NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactoryBuilder()
+        NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new
+                NetconfServerSessionNegotiatorFactoryBuilder()
                 .setTimer(hashedWheelTimer)
                 .setAggregatedOpService(factoriesListener)
                 .setIdProvider(idProvider)
@@ -172,15 +171,17 @@ public class ConcurrentClientsTest {
                 .setBaseCapabilities(serverCaps)
                 .build();
 
-        NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(serverNegotiatorFactory);
-        final NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, nettyGroup, nettyGroup);
+        NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer =
+                new NetconfServerDispatcherImpl.ServerChannelInitializer(serverNegotiatorFactory);
+        final NetconfServerDispatcherImpl dispatch =
+                new NetconfServerDispatcherImpl(serverChannelInitializer, nettyGroup, nettyGroup);
 
-        ChannelFuture s = dispatch.createServer(netconfAddress);
-        s.await();
+        ChannelFuture server = dispatch.createServer(NETCONF_ADDRESS);
+        server.await();
     }
 
     @After
-    public void tearDown(){
+    public void tearDown() {
         hashedWheelTimer.stop();
         try {
             nettyGroup.shutdownGracefully().get();
@@ -210,7 +211,7 @@ public class ConcurrentClientsTest {
                 throw new IllegalStateException(e);
             } catch (ExecutionException e) {
                 LOG.error("Thread for testing client failed", e);
-                fail("Client failed: " + e.getMessage());
+                throw e;
             }
         }
 
@@ -236,7 +237,7 @@ public class ConcurrentClientsTest {
     }
 
     /**
-     * Responds to all operations except start-exi and counts all requests
+     * Responds to all operations except start-exi and counts all requests.
      */
     private static class TestingNetconfOperation implements NetconfOperation {
 
@@ -244,13 +245,15 @@ public class ConcurrentClientsTest {
 
         @Override
         public HandlingPriority canHandle(Document message) {
-            return XmlUtil.toString(message).contains(NetconfStartExiMessage.START_EXI) ?
-                    HandlingPriority.CANNOT_HANDLE :
+            return XmlUtil.toString(message).contains(NetconfStartExiMessage.START_EXI)
+                    HandlingPriority.CANNOT_HANDLE :
                     HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
         }
 
+        @SuppressWarnings("checkstyle:IllegalCatch")
         @Override
-        public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
+        public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation)
+                throws DocumentedException {
             try {
                 LOG.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage));
                 counter.getAndIncrement();
@@ -266,12 +269,12 @@ public class ConcurrentClientsTest {
     }
 
     /**
-     * Hardcoded operation service factory
+     * Hardcoded operation service factory.
      */
     private static class TestingOperationServiceFactory implements NetconfOperationServiceFactory {
         private final NetconfOperation[] operations;
 
-        public TestingOperationServiceFactory(final NetconfOperation... operations) {
+        TestingOperationServiceFactory(final NetconfOperation... operations) {
             this.operations = operations;
         }
 
@@ -282,9 +285,7 @@ public class ConcurrentClientsTest {
 
         @Override
         public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
-            return new AutoCloseable(){
-                @Override
-                public void close() throws Exception {}
+            return () -> {
             };
         }
 
@@ -298,14 +299,16 @@ public class ConcurrentClientsTest {
                 }
 
                 @Override
-                public void close() {}
+                public void close() {
+                }
             };
         }
     }
 
     /**
-     * Pure socket based blocking client
+     * Pure socket based blocking client.
      */
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public final class BlockingClientRunnable implements Runnable {
 
         @Override
@@ -320,9 +323,10 @@ public class ConcurrentClientsTest {
         private void run2() throws Exception {
             InputStream clientHello = checkNotNull(XmlFileLoader
                     .getResourceAsStream("netconfMessages/client_hello.xml"));
-            InputStream getConfig = checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml"));
+            final InputStream getConfig =
+                    checkNotNull(XmlFileLoader.getResourceAsStream("netconfMessages/getConfig.xml"));
 
-            Socket clientSocket = new Socket(netconfAddress.getHostString(), netconfAddress.getPort());
+            Socket clientSocket = new Socket(NETCONF_ADDRESS.getHostString(), NETCONF_ADDRESS.getPort());
             DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
             InputStreamReader inFromServer = new InputStreamReader(clientSocket.getInputStream());
 
@@ -350,15 +354,17 @@ public class ConcurrentClientsTest {
     }
 
     /**
-     * TestingNetconfClient based runnable
+     * TestingNetconfClient based runnable.
      */
     public final class TestingNetconfClientRunnable implements Runnable {
 
+        @SuppressWarnings("checkstyle:IllegalCatch")
         @Override
         public void run() {
             try {
                 final TestingNetconfClient netconfClient =
-                        new TestingNetconfClient(Thread.currentThread().getName(), netconfClientDispatcher, getClientConfig());
+                        new TestingNetconfClient(Thread.currentThread().getName(), netconfClientDispatcher,
+                                getClientConfig());
                 long sessionId = netconfClient.getSessionId();
                 LOG.info("Client with session id {}: hello exchanged", sessionId);
 
@@ -380,7 +386,7 @@ public class ConcurrentClientsTest {
 
         private NetconfClientConfiguration getClientConfig() {
             final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
-            b.withAddress(netconfAddress);
+            b.withAddress(NETCONF_ADDRESS);
             b.withAdditionalHeader(new NetconfHelloMessageAdditionalHeader("uname", "10.10.10.1", "830", "tcp",
                     "client"));
             b.withSessionListener(new SimpleNetconfClientSessionListener());