using System.Collections.Generic; using System.Threading.Tasks; using NUnit.Framework; using FabAccessAPI; using Capnp; using Capnp.Rpc; using log4net.Config; using Microsoft.Extensions.Logging; using static FabAccessAPI.Schema.Machine.WriteInterface; namespace FabAccessAPI_Test { public class Tests { #region Log private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion private static ILoggerFactory _loggerFactory; private Connection _connection; [OneTimeSetUp] public void InitialSetup() { XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config")); _loggerFactory = LoggerFactory.Create(builder => { builder .AddFilter("Microsoft", LogLevel.Warning) .AddFilter("System", LogLevel.Warning); }); _loggerFactory.AddLog4Net(); Logging.LoggerFactory = _loggerFactory; } [SetUp] public void Setup() { TcpRpcClient rpcClient = new TcpRpcClient(); rpcClient.Connect("[::1]", 59661); _connection = new Connection(rpcClient); } [TearDown] public void Teardown() { _connection.RpcClient?.Dispose(); _connection = null; } [Test] public void Connect() { Assert.AreEqual(ConnectionState.Active, _connection.RpcClient.State); } [Test] public async Task Authenticate() { await _connection.Auth("PLAIN", new Dictionary{{"Username", "Testuser"}, {"Password", "secret"}}); } [Test] public async Task GetMInfo() { await _connection.Auth("PLAIN", new Dictionary{{"Username", "Testuser"}, {"Password", "secret"}}); Machines machines = await _connection.AccessMachines(); Machine testmachine = await machines.GetMachine("Testmachine"); Assert.NotNull(testmachine); FabAccessAPI.Schema.Machine.MInfo minfo = await testmachine.GetMInfo(); Assert.NotNull(minfo); _Log.Info($"Name: {minfo.Name}, Description: {minfo.Description}, State: {minfo.State}"); } [Test] public async Task ListMachines() { await _connection.Auth("PLAIN", new Dictionary { { "Username", "Testuser" }, { "Password", "secret" } }); Machines machines = await _connection.AccessMachines(); IReadOnlyList machineList = await machines.ListMachines(); Assert.NotNull(machineList); Assert.AreNotEqual(0, machineList.Count); } [Test] public async Task UseMachine() { await _connection.Auth("PLAIN", new Dictionary { { "Username", "Testuser" }, { "Password", "secret" } }); Machines machines = await _connection.AccessMachines(); Machine testmachine = await machines.GetMachine("Testmachine"); Assert.NotNull(testmachine); await testmachine.Use(); FabAccessAPI.Schema.Machine.MInfo minfo = await testmachine.GetMInfo(); Assert.NotNull(minfo); Assert.AreEqual(FabAccessAPI.Schema.State.inUse, minfo.State); //await giveBack.Ret(); //minfo = await testmachine.GetMInfo(); //Assert.NotNull(minfo); //Assert.AreEqual(FabAccessAPI.Schema.State.free, minfo.State); } } }