mirror of
https://gitlab.com/fabinfra/fabaccess/fabaccess-api-cs.git
synced 2025-03-11 14:21:44 +01:00
Added: More Tests
This commit is contained in:
parent
f2480f22d0
commit
cb14936bdc
@ -408,19 +408,20 @@ namespace FabAccessAPI
|
||||
/// <exception cref="AuthenticationException"></exception>
|
||||
private async Task<Session> _Authenticate(ConnectionData connectionData)
|
||||
{
|
||||
IAuthentication? authentication = await _Bootstrap.CreateSession(SASLMechanism.ToString(connectionData.Mechanism)).ConfigureAwait(false);
|
||||
throw new NotImplementedException();
|
||||
// IAuthentication? authentication = await _Bootstrap.CreateSession(SASLMechanism.ToString(connectionData.Mechanism)).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
return await _SASLAuthenticate(authentication, SASLMechanism.ToString(connectionData.Mechanism), connectionData.Properties).ConfigureAwait(false);
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
{
|
||||
Log.Warn(exception, "API authenticating failed");
|
||||
AuthenticationException authenticationException = new AuthenticationException("Authentication failed", exception);
|
||||
// try
|
||||
// {
|
||||
// return await _SASLAuthenticate(authentication, SASLMechanism.ToString(connectionData.Mechanism), connectionData.Properties).ConfigureAwait(false);
|
||||
// }
|
||||
// catch (System.Exception exception)
|
||||
// {
|
||||
// Log.Warn(exception, "API authenticating failed");
|
||||
// AuthenticationException authenticationException = new AuthenticationException("Authentication failed", exception);
|
||||
|
||||
throw authenticationException;
|
||||
}
|
||||
// throw authenticationException;
|
||||
// }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -429,63 +430,63 @@ namespace FabAccessAPI
|
||||
/// <exception cref="BadMechanismException"></exception>
|
||||
/// <exception cref="InvalidCredentialsException"></exception>
|
||||
/// <exception cref="AuthenticationFailedException"></exception>
|
||||
private async Task<Session> _SASLAuthenticate(IAuthentication authentication, string mech, Dictionary<string, object> properties)
|
||||
{
|
||||
SaslMechanism? saslMechanism = SaslFactory.Create(mech);
|
||||
foreach (KeyValuePair<string, object> entry in properties)
|
||||
{
|
||||
saslMechanism.Properties.Add(entry.Key, entry.Value);
|
||||
}
|
||||
// private async Task<Session> _SASLAuthenticate(IAuthentication<TSuccessful> authentication, string mech, Dictionary<string, object> properties)
|
||||
// {
|
||||
// SaslMechanism? saslMechanism = SaslFactory.Create(mech);
|
||||
// foreach (KeyValuePair<string, object> entry in properties)
|
||||
// {
|
||||
// saslMechanism.Properties.Add(entry.Key, entry.Value);
|
||||
// }
|
||||
|
||||
byte[] data = new byte[0];
|
||||
// byte[] data = new byte[0];
|
||||
|
||||
if (saslMechanism.HasInitial)
|
||||
{
|
||||
data = saslMechanism.GetResponse(new byte[0]);
|
||||
}
|
||||
// if (saslMechanism.HasInitial)
|
||||
// {
|
||||
// data = saslMechanism.GetResponse(new byte[0]);
|
||||
// }
|
||||
|
||||
Response? response = await authentication.Step(data);
|
||||
while (!saslMechanism.IsCompleted)
|
||||
{
|
||||
if (response != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (response.Challenge != null)
|
||||
{
|
||||
byte[]? additional = saslMechanism.GetResponse(response.Challenge.ToArray());
|
||||
response = await authentication.Step(additional);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new AuthenticationFailedException();
|
||||
}
|
||||
}
|
||||
// Response? response = await authentication.Step(data);
|
||||
// while (!saslMechanism.IsCompleted)
|
||||
// {
|
||||
// if (response != null)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// if (response.Challenge != null)
|
||||
// {
|
||||
// byte[]? additional = saslMechanism.GetResponse(response.Challenge.ToArray());
|
||||
// response = await authentication.Step(additional);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new AuthenticationFailedException();
|
||||
// }
|
||||
// }
|
||||
|
||||
if (response.Successful != null)
|
||||
{
|
||||
return response.Successful.Session;
|
||||
}
|
||||
else if (response.Error != null)
|
||||
{
|
||||
switch (response.Error.Reason)
|
||||
{
|
||||
case Response.Reason.badMechanism:
|
||||
throw new BadMechanismException();
|
||||
case Response.Reason.invalidCredentials:
|
||||
throw new InvalidCredentialsException();
|
||||
case Response.Reason.aborted:
|
||||
case Response.Reason.failed:
|
||||
default:
|
||||
throw new AuthenticationFailedException();
|
||||
// TODO throw new AuthenticationFailedException(response.Error.AdditionalData.ToArray());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new AuthenticationFailedException();
|
||||
}
|
||||
}
|
||||
// if (response.Successful != null)
|
||||
// {
|
||||
// return response.Successful.Session;
|
||||
// }
|
||||
// else if (response.Error != null)
|
||||
// {
|
||||
// switch (response.Error.Reason)
|
||||
// {
|
||||
// case Response.Reason.badMechanism:
|
||||
// throw new BadMechanismException();
|
||||
// case Response.Reason.invalidCredentials:
|
||||
// throw new InvalidCredentialsException();
|
||||
// case Response.Reason.aborted:
|
||||
// case Response.Reason.failed:
|
||||
// default:
|
||||
// throw new AuthenticationFailedException();
|
||||
// // TODO throw new AuthenticationFailedException(response.Error.AdditionalData.ToArray());
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// throw new AuthenticationFailedException();
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Get ServerData from server with tcprpcconnection
|
||||
@ -498,7 +499,7 @@ namespace FabAccessAPI
|
||||
ServerData serverData = new ServerData()
|
||||
{
|
||||
APIVersion = await bootstrap.GetAPIVersion().ConfigureAwait(false),
|
||||
Mechanisms = new List<Mechanism>(await bootstrap.Mechanisms().ConfigureAwait(false)),
|
||||
AuthSupported = await bootstrap.Mechanisms().ConfigureAwait(false),
|
||||
ServerName = release.Item1,
|
||||
ServerRelease = release.Item2,
|
||||
SpaceName = info.Item1,
|
||||
|
25
FabAccessAPI/FabAccessAPI.sln
Normal file
25
FabAccessAPI/FabAccessAPI.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.002.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FabAccessAPI", "FabAccessAPI.csproj", "{B10CC69E-5B93-400F-BC2A-CC3C3F0A0D77}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B10CC69E-5B93-400F-BC2A-CC3C3F0A0D77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B10CC69E-5B93-400F-BC2A-CC3C3F0A0D77}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B10CC69E-5B93-400F-BC2A-CC3C3F0A0D77}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B10CC69E-5B93-400F-BC2A-CC3C3F0A0D77}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {8C44F6EC-75A3-4A27-AC42-646614906FB6}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
68
FabAccessAPI/OID.cs
Normal file
68
FabAccessAPI/OID.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using System.Text;
|
||||
|
||||
namespace FabAccessAPI;
|
||||
|
||||
public class OID
|
||||
{
|
||||
public static byte[] OidStringToByteArray(string oid)
|
||||
{
|
||||
string[] split = oid.Trim(' ','.').Split('.');
|
||||
List<int> retVal = new List<int>();
|
||||
|
||||
for (int a = 0, b = 0, i = 0; i < split.Length; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
a = int.Parse(split[0]);
|
||||
else if (i == 1)
|
||||
retVal.Add(40 * a + int.Parse(split[1]));
|
||||
else
|
||||
{
|
||||
b = int.Parse(split[i]);
|
||||
|
||||
if (b < 128)
|
||||
retVal.Add(b);
|
||||
else
|
||||
{
|
||||
retVal.Add(128+(b/128));
|
||||
retVal.Add(b%128);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] temp = new byte[retVal.Count];
|
||||
|
||||
for (int i = 0; i < retVal.Count; i++)
|
||||
temp[i] = (byte)retVal[i];
|
||||
|
||||
return temp;
|
||||
|
||||
}
|
||||
|
||||
public static string OidByteArrayToString(byte[] oid)
|
||||
{
|
||||
StringBuilder retVal = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < oid.Length; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
int b = oid[0] % 40;
|
||||
int a = (oid[0] - b) / 40;
|
||||
retVal.AppendFormat("{0}.{1}", a, b);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oid[i] < 128)
|
||||
retVal.AppendFormat(".{0}", oid[i]);
|
||||
else
|
||||
{
|
||||
retVal.AppendFormat(".{0}",
|
||||
((oid[i] - 128) * 128) + oid[i + 1]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return retVal.ToString();
|
||||
}
|
||||
}
|
@ -12,6 +12,6 @@ namespace FabAccessAPI
|
||||
public string SpaceName;
|
||||
|
||||
public string InstanceURL;
|
||||
public List<Mechanism> Mechanisms;
|
||||
public AuthSupported AuthSupported;
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 9d7c66b9dd5d5245f21cb14c5d09de99954951e7
|
||||
Subproject commit cae56b00842362bd4599187b171ddf8a1030e248
|
49
FabAccessAPI_Test/API_SpecTests/Configuration.cs
Normal file
49
FabAccessAPI_Test/API_SpecTests/Configuration.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using FabAccessAPI;
|
||||
using FabAccessAPI.Schema;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace FabAccessAPI_Test;
|
||||
|
||||
public class Configuration
|
||||
{
|
||||
[Test(Description="Check if user have Permission to discover resource by list or name")]
|
||||
[Parallelizable]
|
||||
[TestCase("sbarth", "solderstation", true, true)]
|
||||
public async Task ResourceDiscover(string username, string resourcename, bool disclose, bool read)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
List<Resource> resources = (List<Resource>)await api.Session.Resources.List().ConfigureAwait(false);
|
||||
|
||||
Assert.That(resources.Exists(x => x.Identifier == resourcename), Is.EqualTo(disclose));
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false)).Item1;
|
||||
Assert.That(resource != null, Is.EqualTo(read));
|
||||
}
|
||||
|
||||
[Test(Description="Check if user have Permission to access resource")]
|
||||
[Parallelizable]
|
||||
[TestCase("sbarth", "solderstation", true, true, true, true, true, true, true)]
|
||||
public async Task ResourcePermissions(string username, string resourcename, bool notify, bool interest, bool claim, bool lock_, bool audit)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
IReadOnlyList<Resource> resources = await api.Session.Resources.List().ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false)).Item1;
|
||||
Assert.That(resource, Is.Not.Null);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(!((Notifiable_Proxy)resource.Notify).IsNull, Is.EqualTo(notify));
|
||||
Assert.That(!((Interestable_Proxy)resource.Interest).IsNull, Is.EqualTo(interest));
|
||||
Assert.That(!((Claimable_Proxy)resource.Claim).IsNull, Is.EqualTo(claim));
|
||||
Assert.That(!((Lockable_Proxy)resource.Lock).IsNull, Is.EqualTo(lock_));
|
||||
Assert.That(!((Auditable_Proxy)resource.Audit).IsNull, Is.EqualTo(audit));
|
||||
});
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using FabAccessAPI;
|
||||
using System.Security.Cryptography;
|
||||
using Capnp.Rpc;
|
||||
using FabAccessAPI;
|
||||
using FabAccessAPI.Schema;
|
||||
using NLog.Time;
|
||||
using NUnit.Framework;
|
||||
@ -8,94 +10,129 @@ namespace FabAccessAPI_Test;
|
||||
[TestFixture]
|
||||
public class ResourceTest
|
||||
{
|
||||
[Test(Description="TestID: 00001")]
|
||||
[TestCase("UserA", "MachineA")]
|
||||
public async Task Claim(string username, string resourcename)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false);
|
||||
Fallible<IClaim, Claimable.IClaimError> result = await resource.Claim.Claim().ConfigureAwait(false);
|
||||
// [TestCase("UserA", "MachineAPowerable")]
|
||||
// public async Task Claim_Powerable_ON_OFF(string username, string resourcename)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// TODO: Claim Error State in Case of Success
|
||||
// Resource resource = (await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = result.Ok;
|
||||
// IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
// TODO: Change State
|
||||
// Map<IReadOnlyList<byte>, object> traits = await claim.Traits().ConfigureAwait(false);
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
// byte[] oid_trait_powerable = OID.OidStringToByteArray("1.3.6.1.4.1.61783.612.1.1");
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[TestCase("UserA", "fabaccess://test.fab-access.space/resources/MachineA")]
|
||||
public async Task GetByUrl(string username, string resourceURN)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = await api.Session.Resources.GetByUrl(resourceURL).ConfigureAwait(false);
|
||||
|
||||
Assert.That(resource, Is.Not.Null);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[TestCase("UserA", "urn:fabaccess:resource:MachineA")]
|
||||
public async Task GetByUrn(string username, string resourceURN)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData);
|
||||
|
||||
Resource resource = await api.Session.Resources.GetByUrn(resourceURN).ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[TestCase("UserA")]
|
||||
public async Task List(string username)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
IReadOnlyList<Resource> result = await api.Session.Resources.List().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[TestCase("UserA")]
|
||||
public async Task Claimed(string username)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
IReadOnlyList<IClaim> result = await api.Session.Resources.Claimed().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[TestCase("UserA", "MachineA")]
|
||||
public async Task ClaimedListCheck(string username, string resourcename)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
IReadOnlyList<IClaim> result1 = await api.Session.Resources.Claimed().ConfigureAwait(false);
|
||||
Assert.That(result1.Count, Is.Zero);
|
||||
|
||||
Resource machineA = await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false);
|
||||
IClaim claim = (await machineA.Claim.Claim().ConfigureAwait(false)).Ok;
|
||||
|
||||
IReadOnlyList<IClaim> result2 = await api.Session.Resources.Claimed().ConfigureAwait(false);
|
||||
Assert.That(result2.Count, Is.Not.Zero);
|
||||
// object pointer = traits.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
// ITraitPowerable traitPowerable = ((Proxy)pointer).Cast<ITraitPowerable>(false);
|
||||
|
||||
// StatePowerable statePowerable = await traitPowerable.GetState().ConfigureAwait(false);
|
||||
// if(statePowerable.which != StatePowerable.WHICH.Off)
|
||||
// {
|
||||
// Assert.Inconclusive("start state is not the one expected");
|
||||
// }
|
||||
|
||||
// traitPowerable = (await traitPowerable.TurnOn().ConfigureAwait(false)).Ok;
|
||||
|
||||
// Assert.That(await traitPowerable.GetState().ConfigureAwait(false), Is.EqualTo(StatePowerable.WHICH.On));
|
||||
|
||||
// traitPowerable = (await traitPowerable.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
// Assert.That(await traitPowerable.GetState().ConfigureAwait(false), Is.EqualTo(StatePowerable.WHICH.Off));
|
||||
// }
|
||||
|
||||
// [Test(Description="TestID: 00001")]
|
||||
// [TestCase("UserA", "MachineA")]
|
||||
// public async Task Claim(string username, string resourcename)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// Resource resource = await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false);
|
||||
// Fallible<IClaim, Claimable.IClaimError> result = await resource.Claim.Claim().ConfigureAwait(false);
|
||||
|
||||
// // TODO: Claim Error State in Case of Success
|
||||
|
||||
// IClaim claim = result.Ok;
|
||||
|
||||
// // TODO: Change State
|
||||
|
||||
// await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
// await api.Disconnect().ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// [TestCase("UserA", "fabaccess://test.fab-access.space/resources/MachineA")]
|
||||
// public async Task GetByUrl(string username, string resourceURN)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// Resource resource = await api.Session.Resources.GetByUrl(resourceURL).ConfigureAwait(false);
|
||||
|
||||
// Assert.That(resource, Is.Not.Null);
|
||||
|
||||
// await api.Disconnect().ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// [TestCase("UserA", "urn:fabaccess:resource:MachineA")]
|
||||
// public async Task GetByUrn(string username, string resourceURN)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData);
|
||||
|
||||
// Resource resource = await api.Session.Resources.GetByUrn(resourceURN).ConfigureAwait(false);
|
||||
|
||||
// await api.Disconnect().ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// [TestCase("UserA")]
|
||||
// public async Task List(string username)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// IReadOnlyList<Resource> result = await api.Session.Resources.List().ConfigureAwait(false);
|
||||
|
||||
// await api.Disconnect().ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// [TestCase("UserA")]
|
||||
// public async Task Claimed(string username)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// IReadOnlyList<IClaim> result = await api.Session.Resources.Claimed().ConfigureAwait(false);
|
||||
|
||||
// await api.Disconnect().ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// [TestCase("UserA", "MachineA")]
|
||||
// public async Task ClaimedListCheck(string username, string resourcename)
|
||||
// {
|
||||
// API api = new API();
|
||||
// ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
// await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// IReadOnlyList<IClaim> result1 = await api.Session.Resources.Claimed().ConfigureAwait(false);
|
||||
// Assert.That(result1.Count, Is.Zero);
|
||||
|
||||
// Resource machineA = await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false);
|
||||
// IClaim claim = (await machineA.Claim.Claim().ConfigureAwait(false)).Ok;
|
||||
|
||||
// IReadOnlyList<IClaim> result2 = await api.Session.Resources.Claimed().ConfigureAwait(false);
|
||||
// Assert.That(result2.Count, Is.Not.Zero);
|
||||
|
||||
// await api.Disconnect().ConfigureAwait(false);
|
||||
// }
|
||||
}
|
||||
|
319
FabAccessAPI_Test/API_SpecTests/UseCases.cs
Normal file
319
FabAccessAPI_Test/API_SpecTests/UseCases.cs
Normal file
@ -0,0 +1,319 @@
|
||||
using Capnp.Rpc;
|
||||
using FabAccessAPI;
|
||||
using FabAccessAPI.Schema;
|
||||
using NUnit.Framework;
|
||||
using static FabAccessAPI.Schema.Claim;
|
||||
|
||||
namespace FabAccessAPI_Test;
|
||||
|
||||
public class UseCases
|
||||
{
|
||||
[SetUp]
|
||||
public async Task SetUp()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("admin");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
// TODO: Vendor Reset DB
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Niklas Becker wants to use the solderstation, claims the resource, turns the power on and returns the resource"
|
||||
)]
|
||||
public async Task UseCase001()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("nbecker");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("solderstation").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits = await claim.Traits().ConfigureAwait(false);
|
||||
|
||||
byte[] oid_trait_powerable = OID.OidStringToByteArray("1.3.6.1.4.1.61783.612.1.1");
|
||||
|
||||
object pointer = traits.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable = ((Proxy)pointer).Cast<ITraitPowerable>(false);
|
||||
traitPowerable = (await traitPowerable.TurnOn().ConfigureAwait(false)).Ok;
|
||||
traitPowerable = (await traitPowerable.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Niklas Becker wants to enter the space through the frontdoor, claims the resource and unlocks it temporarily"
|
||||
)]
|
||||
public async Task UseCase002()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("nbecker");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("frontdoor").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits = await claim.Traits().ConfigureAwait(false);
|
||||
|
||||
byte[] oid_trait_doorable = OID.OidStringToByteArray("1.3.6.1.4.1.61783.612.1.2");
|
||||
|
||||
object pointer = traits.Entries.First(x => x.Key == oid_trait_doorable).Value;
|
||||
|
||||
ITraitDoorable traitdoorable = ((Proxy)pointer).Cast<ITraitDoorable>(false);
|
||||
traitdoorable = (await traitdoorable.UnlockTemp(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Niklas Becker wants to reserve a the solderstation for 1 hour in 5 seconds"
|
||||
)]
|
||||
public async Task UseCase003()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("nbecker");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("solderstation").ConfigureAwait(false)).Item1;
|
||||
|
||||
DateTimeOffset now = DateTimeOffset.UtcNow;
|
||||
|
||||
When when = new When()
|
||||
{
|
||||
Start = new Timestamp()
|
||||
{
|
||||
Seconds = now.AddSeconds(5).ToUnixTimeSeconds(),
|
||||
Nanoseconds = 0
|
||||
},
|
||||
End = new Timestamp()
|
||||
{
|
||||
Seconds = now.AddSeconds(5).AddHours(1).ToUnixTimeSeconds(),
|
||||
Nanoseconds = 0
|
||||
}
|
||||
};
|
||||
|
||||
IInterest interest = (await resource.Interest.Reserve(when).ConfigureAwait(false)).Ok;
|
||||
|
||||
await Task.Delay(5*1000 + 1000);
|
||||
|
||||
IClaim claim = await interest.Upgrade().ConfigureAwait(false);
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Niklas Becker wants to use the solderstation but Phillipp Blau allready using it. So Niklas Becker joins the queue and gets the claim after Phillipp Blau disowns the claim."
|
||||
)]
|
||||
public async Task UseCase004()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("pblau");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("solderstation").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
API api2 = new API();
|
||||
ConnectionData connectionData2 = TestEnv.CreateConnetionData("nbecker");
|
||||
await api2.Connect(connectionData2).ConfigureAwait(false);
|
||||
|
||||
Resource resource2 = (await api2.Session.Resources.GetByName("solderstation").ConfigureAwait(false)).Item1;
|
||||
|
||||
IInterest interest = (await resource.Interest.Queue().ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
|
||||
IClaim claim2 = await interest.Upgrade().ConfigureAwait(false);
|
||||
|
||||
await claim2.Disown().ConfigureAwait(false);
|
||||
|
||||
await api2.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Julia Schneider needs an instruction from Maik Pfeiffer for the 3D-Printer FDM"
|
||||
)]
|
||||
public async Task UseCase005()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("mpfeifer");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("3dprinterfdm").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
MakeLendableOk makeLendableOk = (await claim.MakeLendable().ConfigureAwait(false)).Ok;
|
||||
|
||||
API api2 = new API();
|
||||
ConnectionData connectionData2 = TestEnv.CreateConnetionData("jschneider");
|
||||
await api2.Connect(connectionData2).ConfigureAwait(false);
|
||||
|
||||
IClaim claim2 = (await api2.Session.Resources.AcceptToken(makeLendableOk.Token).ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim2.Disown().ConfigureAwait(false);
|
||||
|
||||
await api2.Disconnect().ConfigureAwait(false);
|
||||
|
||||
IReadOnlyList<RestoredResource> restoredResources = await api.Session.Resources.Restore().ConfigureAwait(false);
|
||||
IClaim claim3 = restoredResources[0].Claim;
|
||||
await claim3.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Julia Schneider uses the WeldingMachine and wants to transfer it to Leonie Fischer"
|
||||
)]
|
||||
public async Task UseCase006()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("jschneider");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("weldingmachine").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
IReadOnlyList<byte> token = (await claim.MakeTransferable().ConfigureAwait(false)).Ok;
|
||||
|
||||
API api2 = new API();
|
||||
ConnectionData connectionData2 = TestEnv.CreateConnetionData("lfischer");
|
||||
await api2.Connect(connectionData2).ConfigureAwait(false);
|
||||
|
||||
IClaim claim2 = (await api2.Session.Resources.AcceptToken(token).ConfigureAwait(false)).Ok;
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
|
||||
await claim2.Disown().ConfigureAwait(false);
|
||||
|
||||
await api2.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Felix Wagner wants to use the circularsaw which depends on the centralsuction"
|
||||
)]
|
||||
public async Task UseCase007()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("fwagner");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("circularsaw").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
IReadOnlyList<IClaim> dependencies = await claim.GetDependencies().ConfigureAwait(false);
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits2 = await dependencies[0].Traits().ConfigureAwait(false);
|
||||
|
||||
byte[] oid_trait_powerable = OID.OidStringToByteArray("1.3.6.1.4.1.61783.612.1.1");
|
||||
|
||||
object pointer2 = traits2.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable2 = ((Proxy)pointer2).Cast<ITraitPowerable>(false);
|
||||
traitPowerable2 = (await traitPowerable2.TurnOn().ConfigureAwait(false)).Ok;
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits = await dependencies[0].Traits().ConfigureAwait(false);
|
||||
object pointer = traits2.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable = ((Proxy)pointer).Cast<ITraitPowerable>(false);
|
||||
traitPowerable = (await traitPowerable.TurnOn().ConfigureAwait(false)).Ok;
|
||||
traitPowerable = (await traitPowerable.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
traitPowerable2 = (await traitPowerable2.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Felix Wagner wants to use the circularsaw and Thomas Naumann wants to use the bandsaw which depends on the centralsuction. Felix Wagner use the circularsaw first, than Thomas Naumann uses the bandsaw and Felix Wagner disowns th circularsaw first."
|
||||
)]
|
||||
public async Task UseCase008()
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData("fwagner");
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName("circularsaw").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
IReadOnlyList<IClaim> dependencies = await claim.GetDependencies().ConfigureAwait(false);
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits2 = await dependencies[0].Traits().ConfigureAwait(false);
|
||||
|
||||
byte[] oid_trait_powerable = OID.OidStringToByteArray("1.3.6.1.4.1.61783.612.1.1");
|
||||
|
||||
object pointer2 = traits2.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable2 = ((Proxy)pointer2).Cast<ITraitPowerable>(false);
|
||||
traitPowerable2 = (await traitPowerable2.TurnOn().ConfigureAwait(false)).Ok;
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits = await dependencies[0].Traits().ConfigureAwait(false);
|
||||
object pointer = traits2.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable = ((Proxy)pointer).Cast<ITraitPowerable>(false);
|
||||
traitPowerable = (await traitPowerable.TurnOn().ConfigureAwait(false)).Ok;
|
||||
|
||||
API api2 = new API();
|
||||
ConnectionData connectionData2 = TestEnv.CreateConnetionData("fwagner");
|
||||
await api2.Connect(connectionData2).ConfigureAwait(false);
|
||||
|
||||
Resource resource2 = (await api2.Session.Resources.GetByName("circularsaw").ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim2 = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits3 = await claim2.Traits().ConfigureAwait(false);
|
||||
|
||||
object pointer3 = traits2.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable3 = ((Proxy)pointer3).Cast<ITraitPowerable>(false);
|
||||
traitPowerable3 = (await traitPowerable3.TurnOn().ConfigureAwait(false)).Ok;
|
||||
|
||||
traitPowerable = (await traitPowerable.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
|
||||
await api.Disconnect().ConfigureAwait(false);
|
||||
|
||||
traitPowerable3 = (await traitPowerable3.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
IReadOnlyList<IClaim> dependencies2 = await claim.GetDependencies().ConfigureAwait(false);
|
||||
Map<IReadOnlyList<byte>, object> traits4 = await dependencies2[0].Traits().ConfigureAwait(false);
|
||||
object pointer4 = traits4.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable4 = ((Proxy)pointer4).Cast<ITraitPowerable>(false);
|
||||
traitPowerable4 = (await traitPowerable4.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
await claim2.Disown().ConfigureAwait(false);
|
||||
|
||||
await api2.Disconnect().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test(
|
||||
Description = "Felix Wagner wants to use the circularsaw and Thomas Naumann wants to use the bandsaw which depends on the centralsuction. Felix Wagner use the circularsaw first, than Thomas Naumann uses the bandsaw and Felix Wagner disowns th circularsaw first."
|
||||
)]
|
||||
public async Task UseCase008()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
45
FabAccessAPI_Test/ResourcenRaum/ResourcenRaum.cs
Normal file
45
FabAccessAPI_Test/ResourcenRaum/ResourcenRaum.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Capnp.Rpc;
|
||||
using FabAccessAPI;
|
||||
using FabAccessAPI.Schema;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace FabAccessAPI_Test;
|
||||
|
||||
public class ResourcenRaum
|
||||
{
|
||||
[TestCase("nbecker", "solderingstation-1")]
|
||||
public async Task ClaimPowerableResource(string username, string resourcename)
|
||||
{
|
||||
API api = new API();
|
||||
ConnectionData connectionData = TestEnv.CreateConnetionData(username);
|
||||
await api.Connect(connectionData).ConfigureAwait(false);
|
||||
|
||||
Resource resource = (await api.Session.Resources.GetByName(resourcename).ConfigureAwait(false)).Item1;
|
||||
|
||||
IClaim claim = (await resource.Claim.Claim(null).ConfigureAwait(false)).Ok;
|
||||
|
||||
Map<IReadOnlyList<byte>, object> traits = await claim.Traits().ConfigureAwait(false);
|
||||
|
||||
byte[] oid_trait_powerable = OID.OidStringToByteArray("1.3.6.1.4.1.61783.612.1.1");
|
||||
|
||||
object pointer = traits.Entries.First(x => x.Key == oid_trait_powerable).Value;
|
||||
|
||||
ITraitPowerable traitPowerable = ((Proxy)pointer).Cast<ITraitPowerable>(false);
|
||||
|
||||
StatePowerable statePowerable = await traitPowerable.GetState().ConfigureAwait(false);
|
||||
if(statePowerable.which != StatePowerable.WHICH.Off)
|
||||
{
|
||||
Assert.Inconclusive("start state is not the one expected");
|
||||
}
|
||||
|
||||
traitPowerable = (await traitPowerable.TurnOn().ConfigureAwait(false)).Ok;
|
||||
|
||||
Assert.That(await traitPowerable.GetState().ConfigureAwait(false), Is.EqualTo(StatePowerable.WHICH.On));
|
||||
|
||||
traitPowerable = (await traitPowerable.TurnOff().ConfigureAwait(false)).Ok;
|
||||
|
||||
Assert.That(await traitPowerable.GetState().ConfigureAwait(false), Is.EqualTo(StatePowerable.WHICH.Off));
|
||||
|
||||
await claim.Disown().ConfigureAwait(false);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user