From: Ivan Martiniak Date: Fri, 8 Jul 2022 12:24:51 +0000 (+0200) Subject: Remove unused parameter in CallHomeSessionContext.java X-Git-Tag: v4.0.2~14 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=netconf.git;a=commitdiff_plain;h=143edc0cedbe3f3f3f7942546d21549c35089277 Remove unused parameter in CallHomeSessionContext.java Parameter in the constructor 'final SocketAddress remoteAddress' was marked as unused. Method and tests related to this constructor were adjusted. Resolved FIXME: Enable test failureToOpenTheChannelShouldCauseTheSessionToClose(). Mock-maker-inline is used to be able to successfully run this test. JIRA: NETCONF-681 Change-Id: Ifd8a76c6edcf801e432b7b925873806014b43b1a Signed-off-by: Ivan Martiniak --- diff --git a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java index 83a62f4fcd..7fd5e6991d 100644 --- a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java +++ b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContext.java @@ -17,7 +17,6 @@ import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.Promise; import java.io.IOException; import java.net.InetSocketAddress; -import java.net.SocketAddress; import java.security.PublicKey; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -54,7 +53,7 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Passing 'this' around") CallHomeSessionContext(final ClientSession sshSession, final CallHomeAuthorization authorization, - final SocketAddress remoteAddress, final Factory factory) { + final Factory factory) { this.authorization = requireNonNull(authorization, "authorization"); checkArgument(this.authorization.isServerAllowed(), "Server was not allowed."); this.factory = requireNonNull(factory); @@ -182,17 +181,17 @@ class CallHomeSessionContext implements CallHomeProtocolSessionContext { } @Nullable CallHomeSessionContext createIfNotExists(final ClientSession sshSession, - final CallHomeAuthorization authorization, final SocketAddress remoteAddress) { - final var newSession = new CallHomeSessionContext(sshSession, authorization, remoteAddress, this); + final CallHomeAuthorization authorization) { + final var newSession = new CallHomeSessionContext(sshSession, authorization, this); final var existing = sessions.putIfAbsent(newSession.getSessionId(), newSession); if (existing == null) { - // There was no mapping, but now there is. Associate the the context with the session. + // There was no mapping, but now there is. Associate the context with the session. newSession.associate(); return newSession; } // We already have a mapping, do not create a new one. But also check if the current session matches - // the one stored in the session. This can happen during rekeying. + // the one stored in the session. This can happen during re-keying. return existing == CallHomeSessionContext.getFrom(sshSession) ? existing : null; } diff --git a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServer.java b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServer.java index 8feecf2506..378ccbe231 100644 --- a/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServer.java +++ b/netconf/callhome-protocol/src/main/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServer.java @@ -157,7 +157,7 @@ public final class NetconfCallHomeServer implements AutoCloseable, ServerKeyVeri return false; } - if (sessionFactory.createIfNotExists(sshClientSession, authorization, remoteAddress) == null) { + if (sessionFactory.createIfNotExists(sshClientSession, authorization) == null) { // Session was not created, session with same name exists LOG.info("Incoming session {} was rejected. Session with same name {} is already active.", sshClientSession, authorization.getSessionName()); diff --git a/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContextTest.java b/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContextTest.java index fd418064af..0691c1cb77 100644 --- a/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContextTest.java +++ b/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/CallHomeSessionContextTest.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.security.PublicKey; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.netconf.client.NetconfClientSessionListener; @@ -101,7 +100,7 @@ public class CallHomeSessionContextTest { @Test public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() { // when - instance = realFactory.createIfNotExists(mockSession, mockAuth, address); + instance = realFactory.createIfNotExists(mockSession, mockAuth); // then assertNotNull(instance); verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance); @@ -115,7 +114,7 @@ public class CallHomeSessionContextTest { @Test public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException { - instance = realFactory.createIfNotExists(mockSession, mockAuth, address); + instance = realFactory.createIfNotExists(mockSession, mockAuth); // when Mockito.doReturn(null).when(mockSession).auth(); instance.authorize(); @@ -133,29 +132,13 @@ public class CallHomeSessionContextTest { Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class)); doNothing().when(mockChannelSubsystem).setStreaming(any(StreamingChannel.Streaming.class)); - instance = realFactory.createIfNotExists(mockSession, mockAuth, address); + instance = realFactory.createIfNotExists(mockSession, mockAuth); // when instance.openNetconfChannel(); // then verify(mockFuture, times(1)).addListener(any(SshFutureListener.class)); } - static class TestableContext extends CallHomeSessionContext { - MinaSshNettyChannel minaMock; - - TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization, - final InetSocketAddress address, final CallHomeSessionContext.Factory factory, - final MinaSshNettyChannel minaMock) { - super(sshSession, authorization, address, factory); - this.minaMock = minaMock; - } - - @Override - protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) { - return minaMock; - } - } - @Test public void openingTheChannelSuccessfullyNotifyTheChannelListener() { // given @@ -180,7 +163,7 @@ public class CallHomeSessionContextTest { OpenFuture mockFuture = mock(OpenFuture.class); Mockito.doReturn(true).when(mockFuture).isOpened(); - instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel); + instance = new TestableContext(mockSession, mockAuth, mockFactory, mockMinaChannel); SshFutureListener listener = instance.newSshFutureListener(mockChannel); // when listener.operationComplete(mockFuture); @@ -190,11 +173,9 @@ public class CallHomeSessionContextTest { } @Test - @Ignore - // FIXME: enable this test public void failureToOpenTheChannelShouldCauseTheSessionToClose() { // given - instance = realFactory.createIfNotExists(mockSession, mockAuth, address); + instance = realFactory.createIfNotExists(mockSession, mockAuth); OpenFuture mockFuture = mock(OpenFuture.class); Mockito.doReturn(false).when(mockFuture).isOpened(); Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException(); @@ -211,8 +192,23 @@ public class CallHomeSessionContextTest { @Test public void theContextConstructorShouldNotModifySession() { - instance = new CallHomeSessionContext(mockSession, mockAuth, address, realFactory); + instance = new CallHomeSessionContext(mockSession, mockAuth, realFactory); verify(mockSession, times(0)).setAttribute(any(), any()); assertNull(CallHomeSessionContext.getFrom(mockSession)); } + + static class TestableContext extends CallHomeSessionContext { + MinaSshNettyChannel minaMock; + + TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization, + final CallHomeSessionContext.Factory factory, final MinaSshNettyChannel minaMock) { + super(sshSession, authorization, factory); + this.minaMock = minaMock; + } + + @Override + protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) { + return minaMock; + } + } } diff --git a/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServerTest.java b/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServerTest.java index 250ab80bfa..136d5055aa 100644 --- a/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServerTest.java +++ b/netconf/callhome-protocol/src/test/java/org/opendaylight/netconf/callhome/protocol/NetconfCallHomeServerTest.java @@ -139,7 +139,7 @@ public class NetconfCallHomeServerTest { Mockito.doReturn(true).when(mockAuth).isServerAllowed(); Mockito.doReturn("some-session-name").when(mockAuth).getSessionName(); Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey); - Mockito.doReturn(null).when(mockFactory).createIfNotExists(mockClientSession, mockAuth, mockSocketAddr); + Mockito.doReturn(null).when(mockFactory).createIfNotExists(mockClientSession, mockAuth); // expect assertFalse(instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey)); diff --git a/netconf/callhome-protocol/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/netconf/callhome-protocol/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 0000000000..ca6ee9cea8 --- /dev/null +++ b/netconf/callhome-protocol/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline \ No newline at end of file