2022-05-10 13:35:23 +02:00
|
|
|
|
|
2022-05-12 23:08:37 +02:00
|
|
|
|
using Capnp.Rpc;
|
2022-05-10 13:35:23 +02:00
|
|
|
|
using FabAccessAPI;
|
|
|
|
|
using FabAccessAPI.Exceptions;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-05-12 23:08:37 +02:00
|
|
|
|
using System.Threading;
|
2022-05-11 15:02:17 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2022-05-10 13:35:23 +02:00
|
|
|
|
|
|
|
|
|
namespace FabAccessAPI_Test
|
|
|
|
|
{
|
|
|
|
|
public class API_Test
|
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task Connect_HostUnreachable()
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
|
|
|
|
API api = new API();
|
|
|
|
|
|
|
|
|
|
ConnectionData connectionData = new ConnectionData()
|
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
Host = new UriBuilder(TestEnv.SCHEMA, "UnkownHost" + TestEnv.TESTSERVER, TestEnv.TESTSERVER_PORT).Uri,
|
2022-05-10 13:35:23 +02:00
|
|
|
|
Mechanism = Mechanism.PLAIN,
|
2022-05-12 23:08:37 +02:00
|
|
|
|
Username = "UnkownUser",
|
2022-05-10 13:35:23 +02:00
|
|
|
|
Properties = new Dictionary<string, object>()
|
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
{ "Username", "UnkownUser" }
|
2022-05-10 13:35:23 +02:00
|
|
|
|
},
|
|
|
|
|
SecretProperties = new Dictionary<string, object>()
|
|
|
|
|
{
|
|
|
|
|
{ "Password", TestEnv.PASSWORD }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-12 23:08:37 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await api.Connect(connectionData);
|
|
|
|
|
}
|
|
|
|
|
catch (ConnectingFailedException)
|
|
|
|
|
{
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
}
|
|
|
|
|
Assert.Fail();
|
2022-05-10 13:35:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2022-05-12 23:08:37 +02:00
|
|
|
|
public async Task Connect_InvalidCredentials()
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
|
|
|
|
API api = new API();
|
|
|
|
|
|
2022-05-12 23:08:37 +02:00
|
|
|
|
ConnectionData connectionData = TestEnv.CreateConnetionData("UnkownUser");
|
2022-05-10 13:35:23 +02:00
|
|
|
|
|
2022-05-11 15:02:17 +02:00
|
|
|
|
try
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
2022-05-11 15:02:17 +02:00
|
|
|
|
await api.Connect(connectionData);
|
|
|
|
|
}
|
2022-05-12 23:08:37 +02:00
|
|
|
|
catch(InvalidCredentialsException)
|
2022-05-11 15:02:17 +02:00
|
|
|
|
{
|
|
|
|
|
Assert.Pass();
|
|
|
|
|
}
|
|
|
|
|
Assert.Fail();
|
2022-05-10 13:35:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 23:08:37 +02:00
|
|
|
|
[TestCase("Admin1")]
|
|
|
|
|
public async Task ConnectDisconnect(string username)
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
|
|
|
|
API api = new API();
|
|
|
|
|
|
2022-05-12 23:08:37 +02:00
|
|
|
|
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
|
|
|
|
|
|
|
|
|
bool event_Connected = false;
|
|
|
|
|
bool event_Disconnected = false;
|
|
|
|
|
api.ConnectionStatusChanged += (sender, eventArgs) =>
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
if (eventArgs == ConnectionStatusChange.Connected)
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
event_Connected = true;
|
|
|
|
|
}
|
|
|
|
|
if(eventArgs == ConnectionStatusChange.Disconnected)
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
event_Disconnected = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await api.Connect(connectionData);
|
|
|
|
|
|
|
|
|
|
bool HasSesion = api.Session != null;
|
|
|
|
|
bool HasConnectionData = api.ConnectionData != null;
|
|
|
|
|
bool HasConnectionInfo = api.ConnectionInfo != null;
|
|
|
|
|
bool IsConnected = api.IsConnected;
|
|
|
|
|
|
|
|
|
|
await api.Disconnect();
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.IsTrue(event_Connected, "event_Connected");
|
|
|
|
|
Assert.IsTrue(event_Disconnected, "event_Disconnected");
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(HasSesion, "HasSesion");
|
|
|
|
|
Assert.IsTrue(HasConnectionData, "HasConnectionData");
|
|
|
|
|
Assert.IsTrue(HasConnectionInfo, "HasConnectionInfo");
|
|
|
|
|
Assert.IsTrue(IsConnected, "IsConnected");
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(api.IsConnected, "api.IsConnected");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase("Admin1")]
|
|
|
|
|
public async Task TestConnectíon(string username)
|
|
|
|
|
{
|
|
|
|
|
API api = new API();
|
|
|
|
|
|
|
|
|
|
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
|
|
|
|
|
|
|
|
|
ConnectionInfo connectionInfo = await api.TestConnection(connectionData);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(connectionInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase("Admin1"), Explicit]
|
|
|
|
|
public async Task Reconnect(string username)
|
|
|
|
|
{
|
|
|
|
|
API api = new API();
|
|
|
|
|
|
|
|
|
|
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
|
|
|
|
|
|
|
|
|
bool event_Connected = false;
|
|
|
|
|
bool event_ConnectionLoss = false;
|
|
|
|
|
bool event_Reconnect = false;
|
|
|
|
|
bool event_Disconnected = false;
|
|
|
|
|
|
|
|
|
|
api.ConnectionStatusChanged += (sender, eventArgs) =>
|
|
|
|
|
{
|
|
|
|
|
if (eventArgs == ConnectionStatusChange.Connected)
|
|
|
|
|
{
|
|
|
|
|
event_Connected = true;
|
|
|
|
|
}
|
|
|
|
|
if (eventArgs == ConnectionStatusChange.ConnectionLoss)
|
|
|
|
|
{
|
|
|
|
|
event_ConnectionLoss = true;
|
|
|
|
|
}
|
|
|
|
|
if (eventArgs == ConnectionStatusChange.Reconnected)
|
|
|
|
|
{
|
|
|
|
|
event_Reconnect = true;
|
|
|
|
|
}
|
|
|
|
|
if (eventArgs == ConnectionStatusChange.Disconnected)
|
|
|
|
|
{
|
|
|
|
|
event_Disconnected = true;
|
2022-05-10 13:35:23 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-12 23:08:37 +02:00
|
|
|
|
TcpRpcClient tcpRpcClient = new TcpRpcClient();
|
|
|
|
|
await api.Connect(connectionData, tcpRpcClient);
|
|
|
|
|
|
2022-05-11 15:02:17 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
// Stop here and cut internet connection
|
|
|
|
|
await api.Session.MachineSystem.Info.GetMachineList().ConfigureAwait(false);
|
2022-05-11 15:02:17 +02:00
|
|
|
|
}
|
2022-05-12 23:08:37 +02:00
|
|
|
|
catch
|
2022-05-10 13:35:23 +02:00
|
|
|
|
{
|
2022-05-12 23:08:37 +02:00
|
|
|
|
|
2022-05-11 15:02:17 +02:00
|
|
|
|
}
|
2022-05-12 23:08:37 +02:00
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
|
|
|
|
|
// Stop here and connect with internet again
|
|
|
|
|
await api.Reconnect();
|
|
|
|
|
await api.Disconnect();
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.IsTrue(event_Connected, "event_Connected");
|
|
|
|
|
Assert.IsTrue(event_ConnectionLoss, "event_ConnectionLoss");
|
|
|
|
|
Assert.IsTrue(event_Reconnect, "event_Reconnect");
|
|
|
|
|
Assert.IsTrue(event_Disconnected, "event_Disconnected");
|
|
|
|
|
});
|
2022-05-10 13:35:23 +02:00
|
|
|
|
}
|
2022-05-12 23:08:37 +02:00
|
|
|
|
|
|
|
|
|
|
2022-05-10 13:35:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|