Skip to content

Commit cd53950

Browse files
authored
17 remove the usage of webclientwebclient (#18)
* 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
1 parent 7c4e66e commit cd53950

39 files changed

+750
-272
lines changed

Source/LocalNetAppChat/LocalNetAppChat.Bot/Program.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ public static async Task Main(string[] args)
2626

2727
var parameters = commandLineParametersResult.Value;
2828

29-
ILnacServer lnacServer = new LnacServer(
29+
var apiAccessor = new WebApiServerApiAccessor(
3030
parameters.Server, parameters.Port, parameters.Https, parameters.IgnoreSslErrors,
31-
parameters.ClientName, parameters.Key);
31+
parameters.Key,
32+
parameters.ClientName
33+
);
34+
35+
ILnacClient lnacClient = new LnacClient(
36+
apiAccessor,
37+
parameters.ClientName);
3238

3339
var publicClientCommands = new ClientCommandCollection();
3440
if (!Plugins.DefaultFunctionality.DefaultPlugin.AddCommands(publicClientCommands, args))
@@ -47,7 +53,7 @@ public static async Task Main(string[] args)
4753
{
4854
try
4955
{
50-
var messages = lnacServer.GetMessages();
56+
var messages = await lnacClient.GetMessages();
5157

5258
foreach (var message in messages)
5359
{
@@ -58,14 +64,14 @@ public static async Task Main(string[] args)
5864
if (IsAPrivateMessage(message))
5965
{
6066
Result<string> result = privateClientCommands.Execute(message.Message.Text);
61-
await SendResultBack(lnacServer, message.Message.Name, result);
67+
await SendResultBack(lnacClient, message.Message.Name, result);
6268
continue;
6369
}
6470

6571
if (publicClientCommands.IsAKnownCommand(message.Message.Text))
6672
{
6773
await SendResultBack(
68-
lnacServer,
74+
lnacClient,
6975
message.Message.Name,
7076
publicClientCommands.Execute(message.Message.Text));
7177
}
@@ -79,15 +85,15 @@ await SendResultBack(
7985
}
8086
}
8187

82-
private async static Task SendResultBack(ILnacServer lnacServer, string sender, Result<string> result)
88+
private async static Task SendResultBack(ILnacClient lnacClient, string sender, Result<string> result)
8389
{
8490
if (result.IsSuccess)
8591
{
86-
await lnacServer.SendMessage($"/msg {sender} {result.Value}");
92+
await lnacClient.SendMessage($"/msg {sender} {result.Value}");
8793
return;
8894
}
8995

90-
await lnacServer.SendMessage($"/msg {sender} {result.Error}");
96+
await lnacClient.SendMessage($"/msg {sender} {result.Error}");
9197
}
9298

9399
private static bool IsAPrivateMessage(ReceivedMessage message)

Source/LocalNetAppChat/LocalNetAppChat.ConsoleClient/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ public static async Task Main(string[] args)
2424
}
2525

2626
var parameters = commandLineParametersResult.Value;
27-
ILnacServer lnacServer = new LnacServer(
27+
28+
var apiAccessor = new WebApiServerApiAccessor(
2829
parameters.Server, parameters.Port, parameters.Https, parameters.IgnoreSslErrors,
29-
parameters.ClientName, parameters.Key);
30+
parameters.Key,
31+
parameters.ClientName
32+
);
33+
34+
ILnacClient lnacClient = new LnacClient(apiAccessor, parameters.ClientName);
3035

3136
var operatingModeCollection = new OperatingModeCollection();
3237
operatingModeCollection.Add(new SendMessageOperatingMode());
@@ -44,7 +49,7 @@ public static async Task Main(string[] args)
4449
}
4550
else
4651
{
47-
await operatingMode?.Run(parameters, output, lnacServer, input)!;
52+
await operatingMode?.Run(parameters, output, lnacClient, input)!;
4853
}
4954
}
5055
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using LocalNetAppChat.Domain.Shared;
2+
using NUnit.Framework;
3+
4+
namespace LocalNetAppChat.Domain.Tests.Shared;
5+
6+
[TestFixture]
7+
public class CommandMessageTokenizerTests
8+
{
9+
[Test]
10+
public void When_in_the_message_there_is_no_space_left_the_rest_is_the_token()
11+
{
12+
var message= "token";
13+
var token = CommandMessageTokenizer.GetToken(ref message);
14+
Assert.AreEqual("token", token);
15+
Assert.AreEqual("", message);
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using LocalNetAppChat.Domain.Shared;
2+
using NUnit.Framework;
3+
4+
namespace LocalNetAppChat.Domain.Tests.Shared;
5+
6+
[TestFixture]
7+
public class MessageForDisplayFormatterTests
8+
{
9+
private string GetFormattedMessage(string receiverPrivateMessage)
10+
{
11+
var message = new ReceivedMessage(0, new DateTime(2023, 2, 1, 1, 2, 3),
12+
receiverPrivateMessage,
13+
new LnacMessage("Id", "Name", "Text", Array.Empty<string>(), true, "Message")
14+
);
15+
16+
var result = MessageForDisplayFormatter.GetTextFor(message);
17+
18+
return result;
19+
}
20+
21+
[Test]
22+
public void Normal_message_format_is_correct()
23+
{
24+
var result = GetFormattedMessage(string.Empty);
25+
26+
Assert.AreEqual(" - [2023-02-01 01:02:03] Name: Text", result);
27+
}
28+
29+
[Test]
30+
public void Private_message_format_is_correct()
31+
{
32+
var result = GetFormattedMessage("Receiver");
33+
34+
Assert.AreEqual(" - [2023-02-01 01:02:03] *PRIVATEMSG* Name: Text", result);
35+
}
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using LocalNetAppChat.Domain.Shared;
2+
using NUnit.Framework;
3+
4+
namespace LocalNetAppChat.Domain.Tests.Shared;
5+
6+
[TestFixture]
7+
public class ResultTests
8+
{
9+
[Test]
10+
public void When_Result_is_successful_but_without_value_an_exception_is_thrown()
11+
{
12+
Assert.Throws<ArgumentNullException>(
13+
() => Result<string>.Success(null)
14+
);
15+
}
16+
17+
[Test]
18+
public void When_Result_is_not_Successful_the_error_is_not_empty()
19+
{
20+
Assert.Throws<ArgumentException>(
21+
() => Result<string>.Failure(null)
22+
);
23+
}
24+
}

Source/LocalNetAppChat/LocalNetAppChat.Domain/Clientside/ClientSideCommandLineParameters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
13
namespace LocalNetAppChat.Domain.Clientside;
24

5+
[ExcludeFromCodeCoverage]
36
public record ClientSideCommandLineParameters(
47
bool Message,
58
bool Listener,

Source/LocalNetAppChat/LocalNetAppChat.Domain/Clientside/MimeMapping.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
13
namespace LocalNetAppChat.Domain.Clientside;
24

5+
[ExcludeFromCodeCoverage]
36
public class MimeMapping
47
{
5-
private static Dictionary<string, string> ExtensionMap = new Dictionary<string, string>();
8+
private static Dictionary<string, string> ExtensionMap = new();
69

710
static MimeMapping()
811
{
@@ -207,7 +210,7 @@ public static string GetMimeMapping(string fileExtension)
207210
{
208211
if (ExtensionMap.ContainsKey(fileExtension))
209212
return ExtensionMap[fileExtension];
210-
else
211-
return ExtensionMap[".*"];
213+
214+
return ExtensionMap[".*"];
212215
}
213216
}

Source/LocalNetAppChat/LocalNetAppChat.Domain/Clientside/OperatingModes/ChatOperatingMode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
1111
return parameters.Chat;
1212
}
1313

14-
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
14+
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
1515
{
16-
output.WriteLine($"Connecting to server {lnacServer}...");
16+
output.WriteLine($"Connecting to server {lnacClient}...");
1717
while (true)
1818
{
19-
var receivedMessages = lnacServer.GetMessages();
19+
var receivedMessages = await lnacClient.GetMessages();
2020

2121
foreach (var receivedMessage in receivedMessages)
2222
{
@@ -26,7 +26,7 @@ public async Task Run(ClientSideCommandLineParameters parameters, IOutput output
2626
if (input.IsInputWaiting())
2727
{
2828
var message = input.GetInput();
29-
await lnacServer.SendMessage(message);
29+
await lnacClient.SendMessage(message);
3030
}
3131

3232
Thread.Sleep(1000);

Source/LocalNetAppChat/LocalNetAppChat.Domain/Clientside/OperatingModes/DeleteFileOperatingMode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
1111
return parameters.FileDelete;
1212
}
1313

14-
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
14+
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
1515
{
16-
await lnacServer.DeleteFile(parameters.File);
16+
await lnacClient.DeleteFile(parameters.File);
1717
}
1818
}

Source/LocalNetAppChat/LocalNetAppChat.Domain/Clientside/OperatingModes/DownloadFileOperatingMode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public bool IsResponsibleFor(ClientSideCommandLineParameters parameters)
1111
return parameters.FileDownload;
1212
}
1313

14-
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacServer lnacServer, IInput input)
14+
public async Task Run(ClientSideCommandLineParameters parameters, IOutput output, ILnacClient lnacClient, IInput input)
1515
{
16-
await lnacServer.DownloadFile(parameters.File, parameters.TargetPath);
16+
await lnacClient.DownloadFile(parameters.File, parameters.TargetPath);
1717
}
1818
}

0 commit comments

Comments
 (0)