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;
@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);
}
@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;
}
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;
@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);
@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();
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
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<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
// when
listener.operationComplete(mockFuture);
}
@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();
@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;
+ }
+ }
}