Skip to content

Commit

Permalink
Implemented automated tests for ConnectionProfileViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandKoenig committed Dec 3, 2020
1 parent 8dc9a00 commit 0397e71
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 16 deletions.
100 changes: 100 additions & 0 deletions MessageCommunicator.TestGui.Tests/ConnectionProfileViewTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using FakeItEasy;
using MessageCommunicator.TestGui.Data;
using MessageCommunicator.TestGui.Logic;
using MessageCommunicator.TestGui.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MessageCommunicator.TestGui.Tests
{
[TestClass]
public class ConnectionProfileViewTests
{
[TestMethod]
public void SendPlainMessage()
{
var connParams = new ConnectionParameters();
var connProfile = this.BuildFakeConnectionProfile(connParams);

// Catch outgoing message
string? sentMessage = null;
A.CallTo(connProfile)
.Where((callInfo) => callInfo.Method.Name == nameof(IConnectionProfile.SendMessageAsync))
.Invokes((callInfo) =>
{
sentMessage = (string?)callInfo.Arguments[0];
});

var testObject = new ConnectionProfileViewModel(connProfile);
testObject.SendFormattingMode = SendFormattingMode.Plain;
testObject.Command_SendMessage.Execute("DummyMessage \\\\");

Assert.IsTrue(sentMessage == "DummyMessage \\\\");
}

[TestMethod]
public void SendEscapedMessage()
{
var connParams = new ConnectionParameters();
var connProfile = this.BuildFakeConnectionProfile(connParams);

// Catch outgoing message
string? sentMessage = null;
A.CallTo(connProfile)
.Where((callInfo) => callInfo.Method.Name == nameof(IConnectionProfile.SendMessageAsync))
.Invokes((callInfo) =>
{
sentMessage = (string?)callInfo.Arguments[0];
});

var testObject = new ConnectionProfileViewModel(connProfile);
testObject.SendFormattingMode = SendFormattingMode.Escaped;
testObject.Command_SendMessage.Execute("DummyMessage \\\\");

Assert.IsTrue(sentMessage == "DummyMessage \\");
}

[TestMethod]
public void SendHexMessage()
{
var connParams = new ConnectionParameters();
var connProfile = this.BuildFakeConnectionProfile(connParams);

// Catch outgoing message
string? sentMessage = null;
A.CallTo(connProfile)
.Where((callInfo) => callInfo.Method.Name == nameof(IConnectionProfile.SendMessageAsync))
.Invokes((callInfo) =>
{
sentMessage = (string?)callInfo.Arguments[0];
});

var testObject = new ConnectionProfileViewModel(connProfile);
testObject.SendFormattingMode = SendFormattingMode.Hex;
testObject.Command_SendMessage.Execute("DummyMessage \\\\");

Assert.IsTrue(sentMessage == "DummyMessage \\");
}

private IConnectionProfile BuildFakeConnectionProfile(ConnectionParameters connParams)
{
var loggingMessages = new ObservableCollection<LoggingMessageWrapper>();
var sendReceiveMessages = new ObservableCollection<LoggingMessageWrapper>();

var connProfile = A.Fake<IConnectionProfile>();
A.CallTo(() => connProfile.Name).Returns("Dummy");
A.CallTo(() => connProfile.IsRunning).Returns(true);
A.CallTo(() => connProfile.DetailLogging).Returns(loggingMessages);
A.CallTo(() => connProfile.Messages).Returns(sendReceiveMessages);
A.CallTo(() => connProfile.Parameters).Returns(connParams);
A.CallTo(() => connProfile.RemoteEndpointDescription).Returns("Dummy Remote");
A.CallTo(() => connProfile.State).Returns(ConnectionState.Connected);

return connProfile;
}

}
}
2 changes: 1 addition & 1 deletion MessageCommunicator.TestGui/Logic/ConnectionProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace MessageCommunicator.TestGui.Logic
{
public class ConnectionProfile : IMessageReceiveHandler, IMessageCommunicatorLogger
public class ConnectionProfile : IMessageReceiveHandler, IMessageCommunicatorLogger, IConnectionProfile
{
private SynchronizationContext _syncContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ConnectionProfileStore
{
public static ConnectionProfileStore Current { get; } = new ConnectionProfileStore();

public void StoreConnectionProfiles(IEnumerable<ConnectionProfile> profiles)
public void StoreConnectionProfiles(IEnumerable<IConnectionProfile> profiles)
{
var connParams = new List<ConnectionParameters>(12);
foreach (var actProfile in profiles)
Expand Down
34 changes: 34 additions & 0 deletions MessageCommunicator.TestGui/Logic/IConnectionProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using MessageCommunicator.TestGui.Data;

namespace MessageCommunicator.TestGui.Logic
{
public interface IConnectionProfile
{
string Name { get; }

ConnectionParameters Parameters { get; }

ObservableCollection<LoggingMessageWrapper> DetailLogging { get; }

ObservableCollection<LoggingMessageWrapper> Messages { get; }

bool IsRunning { get; }

ConnectionState State { get; }

string RemoteEndpointDescription { get; }

Task ChangeParametersAsync(ConnectionParameters newConnParameters);

Task SendMessageAsync(string message);

Task StartAsync();

Task StopAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Command="{Binding Path=Command_SendMessage}"
CommandParameter="{Binding #TxtSendMessage.Text}" />
<ComboBox DockPanel.Dock="Right"
Width="60" Margin="3"
Width="75" Margin="3"
SelectedItem="{Binding Path=SendFormattingMode}"
Items="{Binding Path=SendFormattingModeList}" />
<TextBox Name="TxtSendMessage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ConnectionProfileViewModel : OwnViewModelBase
private ConnectionState _connState;
private string _remoteEndpointDescription;

public ConnectionProfile Model { get; }
public IConnectionProfile Model { get; }

public ReactiveCommand<object?, Unit> Command_Start { get; }

Expand Down Expand Up @@ -73,7 +73,7 @@ public string RemoteEndpointDescription

public LoggingViewModel DetailLoggingViewModel { get; }

public ConnectionProfileViewModel(ConnectionProfile connProfile)
public ConnectionProfileViewModel(IConnectionProfile connProfile)
{
this.Model = connProfile;
_remoteEndpointDescription = string.Empty;
Expand All @@ -100,8 +100,22 @@ public ConnectionProfileViewModel(ConnectionProfile connProfile)
try
{
if (message == null) { message = string.Empty; }
await this.Model.SendMessageAsync(
Regex.Unescape(message));

switch (this.SendFormattingMode)
{
case SendFormattingMode.Plain:
break;

case SendFormattingMode.Escaped:
message = Regex.Unescape(message);
break;

case SendFormattingMode.Hex:

break;
}

await this.Model.SendMessageAsync(message);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,6 @@ await Task.WhenAny(receiveTask, timeoutTask)
TcpAsyncUtil.SafeDispose(ref tcpClientInternal);
}

//private async Task ObserveConnection(TcpClient tcpClientToObserve, CancellationToken cancelToken)
//{

// while (!cancelToken.IsCancellationRequested)
// {

// }
//}

private void ProcessReceivedBytes(bool newConnection, byte[] buffer, int receivedBytesCount)
{
buffer.MustNotBeNull(nameof(buffer));
Expand Down

0 comments on commit 0397e71

Please sign in to comment.