Use try-with-resources in SSHTest 07/103107/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Nov 2022 17:45:35 +0000 (18:45 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Nov 2022 17:47:17 +0000 (18:47 +0100)
SshProxyServer is autocloseable, we should use try-with-resources to
manage it.

Change-Id: I797064a83b793767a7041d51fa1ab507b79b781f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/mdsal-netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java

index 126f425097acce9c99191974c12c6ad526aa9a2f..eb757eb01e5b8dd9e9eb0dc099c0181102f39180 100644 (file)
@@ -5,7 +5,6 @@
  * 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.netty;
 
 import static org.junit.Assert.assertEquals;
@@ -36,11 +35,8 @@ import org.opendaylight.netconf.shaded.sshd.common.util.security.SecurityUtils;
 import org.opendaylight.netconf.ssh.SshProxyServer;
 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
 import org.opendaylight.netconf.util.NetconfConfiguration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class SSHTest {
-    private static final Logger LOG = LoggerFactory.getLogger(SSHTest.class);
     public static final String AHOJ = "ahoj\n";
 
     private static EventLoopGroup nettyGroup;
@@ -71,35 +67,30 @@ public class SSHTest {
         new Thread(new EchoServer(), "EchoServer").start();
 
         final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 10831);
-        final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec);
-        sshProxyServer.bind(new SshProxyServerConfigurationBuilder()
+        try (var sshProxyServer = new SshProxyServer(minaTimerEx, nettyGroup, nioExec)) {
+            sshProxyServer.bind(new SshProxyServerConfigurationBuilder()
                 .setBindingAddress(addr).setLocalAddress(NetconfConfiguration.NETCONF_LOCAL_ADDRESS)
                 .setAuthenticator((username, password) -> true)
                 .setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(sshKeyPair.toPath()))
                 .setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration());
 
-        final EchoClientHandler echoClientHandler = connectClient(addr);
+            final EchoClientHandler echoClientHandler = connectClient(addr);
 
-        Stopwatch stopwatch = Stopwatch.createStarted();
-        while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
-            Thread.sleep(500);
-        }
-        assertTrue(echoClientHandler.isConnected());
-        LOG.info("connected, writing to client");
-        echoClientHandler.write(AHOJ);
-
-        // check that server sent back the same string
-        stopwatch = stopwatch.reset().start();
-        while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
-            Thread.sleep(500);
-        }
+            Stopwatch stopwatch = Stopwatch.createStarted();
+            while (echoClientHandler.isConnected() == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
+                Thread.sleep(500);
+            }
+            assertTrue(echoClientHandler.isConnected());
+            echoClientHandler.write(AHOJ);
+
+            // check that server sent back the same string
+            stopwatch = stopwatch.reset().start();
+            while (echoClientHandler.read().endsWith(AHOJ) == false && stopwatch.elapsed(TimeUnit.SECONDS) < 30) {
+                Thread.sleep(500);
+            }
 
-        try {
             final String read = echoClientHandler.read();
             assertTrue(read + " should end with " + AHOJ, read.endsWith(AHOJ));
-        } finally {
-            LOG.info("Closing socket");
-            sshProxyServer.close();
         }
     }