first commit
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Nitrox.Test.Client.Communication.MultiplayerSession;
|
||||
using NitroxClient.Communication.Abstract;
|
||||
using NitroxModel.MultiplayerSession;
|
||||
using NitroxModel.Packets;
|
||||
using NSubstitute;
|
||||
|
||||
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
|
||||
{
|
||||
[TestClass]
|
||||
public class AwaitingReservationCredentialsStateTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void NegotiateShouldSendServerAuthorityReservationRequest()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
serverClient.IsConnected.Returns(true);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
connectionContext.PlayerSettings.Returns(TestConstants.TEST_PLAYER_SETTINGS);
|
||||
connectionContext.AuthenticationContext.Returns(TestConstants.TEST_AUTHENTICATION_CONTEXT);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
serverClient.Received().Send(Arg.Any<MultiplayerSessionReservationRequest>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldTransitionToAwaitingSessionReservationState()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
serverClient.IsConnected.Returns(true);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
connectionContext.PlayerSettings.Returns(TestConstants.TEST_PLAYER_SETTINGS);
|
||||
connectionContext.AuthenticationContext.Returns(TestConstants.TEST_AUTHENTICATION_CONTEXT);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<AwaitingSessionReservation>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldThrowInvalidOperationExceptionWhenPlayerSettingsIsNull()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
serverClient.IsConnected.Returns(true);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.PlayerSettings.Returns((PlayerSettings)null);
|
||||
connectionContext.AuthenticationContext.Returns(TestConstants.TEST_AUTHENTICATION_CONTEXT);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldThrowInvalidOperationExceptionWhenAuthenticationContextIsNull()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
serverClient.IsConnected.Returns(true);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.SessionPolicy.Returns(TestConstants.TEST_SESSION_POLICY);
|
||||
connectionContext.AuthenticationContext.Returns((AuthenticationContext)null);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void JoinSessionShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.JoinSession(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldStopTheClient()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
serverClient.Received().Stop();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldResetTheConnectionContext()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().ClearSessionState();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldTransitionToDisconnectedState()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Nitrox.Test.Client.Communication.MultiplayerSession;
|
||||
using NitroxClient.Communication.Abstract;
|
||||
using NitroxModel.Packets;
|
||||
using NitroxModel.Packets.Exceptions;
|
||||
using NSubstitute;
|
||||
|
||||
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
|
||||
{
|
||||
[TestClass]
|
||||
public class AwaitingSessionReservationStateTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void NegotiateShouldTransitionToSessionRevervedAfterReceivingSuccessfulReservation()
|
||||
{
|
||||
// Arrange
|
||||
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
|
||||
TestConstants.TEST_CORRELATION_ID,
|
||||
TestConstants.TEST_PLAYER_ID,
|
||||
TestConstants.TEST_RESERVATION_KEY);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Reservation.Returns(successfulReservation);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<SessionReserved>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldThrowUncorrelatedPacketExceptionWhenTheReservationHasTheWrongCorrelationId()
|
||||
{
|
||||
// Arrange
|
||||
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
|
||||
"wrong",
|
||||
TestConstants.TEST_PLAYER_ID,
|
||||
TestConstants.TEST_RESERVATION_KEY);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Reservation.Returns(successfulReservation);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<UncorrelatedPacketException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldTransitionToSessionReservationRejectedAfterReceivingRejectedReservation()
|
||||
{
|
||||
// Arrange
|
||||
MultiplayerSessionReservation rejectedReservation = new MultiplayerSessionReservation(TestConstants.TEST_CORRELATION_ID, TestConstants.TEST_REJECTION_STATE);
|
||||
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Reservation.Returns(rejectedReservation);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<SessionReservationRejected>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldThrowInvalidOperationExceptionWhenTheReservationIsNull()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Reservation.Returns((MultiplayerSessionReservation)null);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void JoinSessionShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.JoinSession(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldStopTheClient()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
serverClient.Received().Stop();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldResetTheConnectionContext()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().ClearSessionState();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldTransitionToDisconnectedState()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Nitrox.Test.Client.Communication.MultiplayerSession;
|
||||
using NitroxClient.Communication.Abstract;
|
||||
using NitroxModel.Packets;
|
||||
using NitroxModel.Packets.Exceptions;
|
||||
using NSubstitute;
|
||||
|
||||
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
|
||||
{
|
||||
[TestClass]
|
||||
public class EstablishingSessionPolicyStateTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void NegotiateShouldTransitionToAwaitingReservationCredentialsState()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.SessionPolicy.Returns(new MultiplayerSessionPolicy(TestConstants.TEST_CORRELATION_ID, false, TestConstants.TEST_MAX_PLAYER_CONNECTIONS, false));
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<AwaitingReservationCredentials>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldTransitionToAwaitingReservationCredentialsStateWithPassword()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.SessionPolicy.Returns(new MultiplayerSessionPolicy(TestConstants.TEST_CORRELATION_ID, false, TestConstants.TEST_MAX_PLAYER_CONNECTIONS, true));
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<AwaitingReservationCredentials>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldThrowUncorrelatedPacketExceptionWhenThePolicyHasTheWrongCorrelationId()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.SessionPolicy.Returns(new MultiplayerSessionPolicy("wrong", false, TestConstants.TEST_MAX_PLAYER_CONNECTIONS, false));
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<UncorrelatedPacketException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegotiateShouldThrowInvalidOperationExceptionIfTheSessionPolicyIsNull()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.SessionPolicy.Returns((MultiplayerSessionPolicy)null);
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void JoinSessionShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
Action action = () => connectionState.JoinSession(connectionContext);
|
||||
|
||||
// Assert
|
||||
action.Should().Throw<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldStopTheClient()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
serverClient.Received().Stop();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldResetTheConnectionContext()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().ClearSessionState();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisconnectShouldTransitionToDisconnectedState()
|
||||
{
|
||||
// Arrange
|
||||
IClient serverClient = Substitute.For<IClient>();
|
||||
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
|
||||
connectionContext.Client.Returns(serverClient);
|
||||
|
||||
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
|
||||
|
||||
// Act
|
||||
connectionState.Disconnect(connectionContext);
|
||||
|
||||
// Assert
|
||||
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user