Skip to content

Commit

Permalink
17 remove the usage of webclientwebclient (#18)
Browse files Browse the repository at this point in the history
* Refactored the LnacClient (once Server) as the utility to access the server-api. There is now additionally an accessor for the specific method "WebApi".

* Increased test coverage for the server side. Extracted access control to separate class.

* Added test coverage for MessageProcessing

* Server Domain has now 100% test Coverage

* Test coverage for Result-Class

* Excluding test coverage for several classes where unit tests are not helpful
  • Loading branch information
stho32 authored Dec 31, 2022
1 parent 7c4e66e commit cd53950
Show file tree
Hide file tree
Showing 39 changed files with 750 additions and 272 deletions.
22 changes: 14 additions & 8 deletions Source/LocalNetAppChat/LocalNetAppChat.Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public static async Task Main(string[] args)

var parameters = commandLineParametersResult.Value;

ILnacServer lnacServer = new LnacServer(
var apiAccessor = new WebApiServerApiAccessor(
parameters.Server, parameters.Port, parameters.Https, parameters.IgnoreSslErrors,
parameters.ClientName, parameters.Key);
parameters.Key,
parameters.ClientName
);

ILnacClient lnacClient = new LnacClient(
apiAccessor,
parameters.ClientName);

var publicClientCommands = new ClientCommandCollection();
if (!Plugins.DefaultFunctionality.DefaultPlugin.AddCommands(publicClientCommands, args))
Expand All @@ -47,7 +53,7 @@ public static async Task Main(string[] args)
{
try
{
var messages = lnacServer.GetMessages();
var messages = await lnacClient.GetMessages();

foreach (var message in messages)
{
Expand All @@ -58,14 +64,14 @@ public static async Task Main(string[] args)
if (IsAPrivateMessage(message))
{
Result<string> result = privateClientCommands.Execute(message.Message.Text);
await SendResultBack(lnacServer, message.Message.Name, result);
await SendResultBack(lnacClient, message.Message.Name, result);
continue;
}

if (publicClientCommands.IsAKnownCommand(message.Message.Text))
{
await SendResultBack(
lnacServer,
lnacClient,
message.Message.Name,
publicClientCommands.Execute(message.Message.Text));
}
Expand All @@ -79,15 +85,15 @@ await SendResultBack(
}
}

private async static Task SendResultBack(ILnacServer lnacServer, string sender, Result<string> result)
private async static Task SendResultBack(ILnacClient lnacClient, string sender, Result<string> result)
{
if (result.IsSuccess)
{
await lnacServer.SendMessage($"/msg {sender} {result.Value}");
await lnacClient.SendMessage($"/msg {sender} {result.Value}");
return;
}

await lnacServer.SendMessage($"/msg {sender} {result.Error}");
await lnacClient.SendMessage($"/msg {sender} {result.Error}");
}

private static bool IsAPrivateMessage(ReceivedMessage message)
Expand Down
11 changes: 8 additions & 3 deletions Source/LocalNetAppChat/LocalNetAppChat.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ public static async Task Main(string[] args)
}

var parameters = commandLineParametersResult.Value;
ILnacServer lnacServer = new LnacServer(

var apiAccessor = new WebApiServerApiAccessor(
parameters.Server, parameters.Port, parameters.Https, parameters.IgnoreSslErrors,
parameters.ClientName, parameters.Key);
parameters.Key,
parameters.ClientName
);

ILnacClient lnacClient = new LnacClient(apiAccessor, parameters.ClientName);

var operatingModeCollection = new OperatingModeCollection();
operatingModeCollection.Add(new SendMessageOperatingMode());
Expand All @@ -44,7 +49,7 @@ public static async Task Main(string[] args)
}
else
{
await operatingMode?.Run(parameters, output, lnacServer, input)!;
await operatingMode?.Run(parameters, output, lnacClient, input)!;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using LocalNetAppChat.Domain.Shared;
using NUnit.Framework;

namespace LocalNetAppChat.Domain.Tests.Shared;

[TestFixture]
public class CommandMessageTokenizerTests
{
[Test]
public void When_in_the_message_there_is_no_space_left_the_rest_is_the_token()
{
var message= "token";
var token = CommandMessageTokenizer.GetToken(ref message);
Assert.AreEqual("token", token);
Assert.AreEqual("", message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using LocalNetAppChat.Domain.Shared;
using NUnit.Framework;

namespace LocalNetAppChat.Domain.Tests.Shared;

[TestFixture]
public class MessageForDisplayFormatterTests
{
private string GetFormattedMessage(string receiverPrivateMessage)
{
var message = new ReceivedMessage(0, new DateTime(2023, 2, 1, 1, 2, 3),
receiverPrivateMessage,
new LnacMessage("Id", "Name", "Text", Array.Empty<string>(), true, "Message")
);

var result = MessageForDisplayFormatter.GetTextFor(message);

return result;
}

[Test]
public void Normal_message_format_is_correct()
{
var result = GetFormattedMessage(string.Empty);

Assert.AreEqual(" - [2023-02-01 01:02:03] Name: Text", result);
}

[Test]
public void Private_message_format_is_correct()
{
var result = GetFormattedMessage("Receiver");

Assert.AreEqual(" - [2023-02-01 01:02:03] *PRIVATEMSG* Name: Text", result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using LocalNetAppChat.Domain.Shared;
using NUnit.Framework;

namespace LocalNetAppChat.Domain.Tests.Shared;

[TestFixture]
public class ResultTests
{
[Test]
public void When_Result_is_successful_but_without_value_an_exception_is_thrown()
{
Assert.Throws<ArgumentNullException>(
() => Result<string>.Success(null)
);
}

[Test]
public void When_Result_is_not_Successful_the_error_is_not_empty()
{
Assert.Throws<ArgumentException>(
() => Result<string>.Failure(null)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace LocalNetAppChat.Domain.Clientside;

[ExcludeFromCodeCoverage]
public record ClientSideCommandLineParameters(
bool Message,
bool Listener,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Diagnostics.CodeAnalysis;

namespace LocalNetAppChat.Domain.Clientside;

[ExcludeFromCodeCoverage]
public class MimeMapping
{
private static Dictionary<string, string> ExtensionMap = new Dictionary<string, string>();
private static Dictionary<string, string> ExtensionMap = new();

static MimeMapping()
{
Expand Down Expand Up @@ -207,7 +210,7 @@ public static string GetMimeMapping(string fileExtension)
{
if (ExtensionMap.ContainsKey(fileExtension))
return ExtensionMap[fileExtension];
else
return ExtensionMap[".*"];

return ExtensionMap[".*"];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.Chat;
}

public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
output.WriteLine($"Connecting to server {lnacServer}...");
output.WriteLine($"Connecting to server {lnacClient}...");
while (true)
{
var receivedMessages = lnacServer.GetMessages();
var receivedMessages = await lnacClient.GetMessages();

foreach (var receivedMessage in receivedMessages)
{
Expand All @@ -26,7 +26,7 @@ public async Task Run(ClientSideCommandLineParameters parameters, IOutput output
if (input.IsInputWaiting())
{
var message = input.GetInput();
await lnacServer.SendMessage(message);
await lnacClient.SendMessage(message);
}

Thread.Sleep(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.FileDelete;
}

public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
await lnacServer.DeleteFile(parameters.File);
await lnacClient.DeleteFile(parameters.File);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.FileDownload;
}

public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
await lnacServer.DownloadFile(parameters.File, parameters.TargetPath);
await lnacClient.DownloadFile(parameters.File, parameters.TargetPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IOperatingMode
{
bool IsResponsibleFor(ClientSideCommandLineParameters parameters);

Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input);
Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.ListServerFiles;
}

public Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
var files = lnacServer.GetServerFiles();
var files = await lnacClient.GetServerFiles();
foreach (var file in files)
{
output.WriteLineUnformatted(file);
}
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.Listener;
}

public Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
output.WriteLine($"Listening to server {lnacServer}...");
output.WriteLine($"Listening to server {lnacClient}...");
while (true)
{
try
{
var receivedMessages = lnacServer.GetMessages();
var receivedMessages = await lnacClient.GetMessages();

foreach (var receivedMessage in receivedMessages)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.Message;
}

public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
await lnacServer.SendMessage(parameters.Text);
await lnacClient.SendMessage(parameters.Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
return parameters.FileUpload;
}

public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
{
await lnacServer.SendFile(parameters.File);
await lnacClient.SendFile(parameters.File);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace LocalNetAppChat.Domain.Clientside.ServerApis;

public interface ILnacServer
public interface ILnacClient
{
ReceivedMessage[] GetMessages();
Task<ReceivedMessage[]> GetMessages();

Task SendMessage(string message, string[]? tags = null, string type = "Message");
Task SendFile(string filePath, string type = "Task");
string[] GetServerFiles();
Task SendFile(string filePath);
Task<string[]> GetServerFiles();
Task DownloadFile(string filename, string targetPath);
Task DeleteFile(string filename);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace LocalNetAppChat.Domain.Clientside;

public interface IServerApiAccessor
{
Task<string> GetAsync(string command);
Task<string> PostAsync(string command, Dictionary<string, string> parameters);

Task<string> PostAsJsonAsync(string command, object data);
Task<string> PostFileAsync(string command, string filename, Stream fileStream);
Task<Stream> GetFileAsync(string command, string filename);
}
Loading

0 comments on commit cd53950

Please sign in to comment.