mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-12 23:01:52 +01:00
Started better API Class
This commit is contained in:
parent
fc47a7d2f8
commit
7516dc061c
65
FabAccessAPI/API.cs
Normal file
65
FabAccessAPI/API.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using FabAccessAPI.Schema;
|
||||
using System;
|
||||
|
||||
namespace FabAccessAPI
|
||||
{
|
||||
public class API : IAPI
|
||||
{
|
||||
#region Private Members
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
public API()
|
||||
{
|
||||
|
||||
}
|
||||
#endregion
|
||||
#region Events
|
||||
public event EventHandler<ConnectionStatusChange> ConnectionStatusChanged;
|
||||
#endregion
|
||||
|
||||
#region Members
|
||||
public ConnectionData ConnectionData { get; private set; }
|
||||
|
||||
public ConnectionInfo ConnectionInfo { get; private set; }
|
||||
|
||||
public bool IsConnected
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public Session Session
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
public void Connect(ConnectionData connectionData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Reconnect()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ConnectionInfo TestConnection(ConnectionData connectionData)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
15
FabAccessAPI/ConnectionData.cs
Normal file
15
FabAccessAPI/ConnectionData.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FabAccessAPI
|
||||
{
|
||||
public class ConnectionData
|
||||
{
|
||||
public Uri Host;
|
||||
public Mechanism Mechanism;
|
||||
public string Username;
|
||||
public Dictionary<string, object> Properties;
|
||||
public Dictionary<string, object> SecretProperties;
|
||||
public DateTime LastTime;
|
||||
}
|
||||
}
|
13
FabAccessAPI/ConnectionInfo.cs
Normal file
13
FabAccessAPI/ConnectionInfo.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FabAccessAPI
|
||||
{
|
||||
public class ConnectionInfo
|
||||
{
|
||||
public Version APIVersion;
|
||||
public string ServerName;
|
||||
public string ServerRelease;
|
||||
public List<string> Mechanisms;
|
||||
}
|
||||
}
|
23
FabAccessAPI/ConnectionStatusChange.cs
Normal file
23
FabAccessAPI/ConnectionStatusChange.cs
Normal file
@ -0,0 +1,23 @@
|
||||
namespace FabAccessAPI
|
||||
{
|
||||
public enum ConnectionStatusChange
|
||||
{
|
||||
/// <summary>
|
||||
/// Client has established connection to server.
|
||||
/// </summary>
|
||||
Connected,
|
||||
/// <summary>
|
||||
/// Client has closed connection to server.
|
||||
/// </summary>
|
||||
Disconnected,
|
||||
/// <summary>
|
||||
/// Connection was lost and Client has reestablished connection to server.
|
||||
/// </summary>
|
||||
Reconnected,
|
||||
/// <summary>
|
||||
/// Connection was lost and can be reestablished with reconnect
|
||||
/// Connection should be closed if reconnecting fails.
|
||||
/// </summary>
|
||||
ConnectionLoss
|
||||
}
|
||||
}
|
22
FabAccessAPI/Exceptions/APIIncompatibleException.cs
Normal file
22
FabAccessAPI/Exceptions/APIIncompatibleException.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace FabAccessAPI.Exceptions
|
||||
{
|
||||
public class APIIncompatibleException : Exception
|
||||
{
|
||||
public APIIncompatibleException()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public APIIncompatibleException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public APIIncompatibleException(string message, Exception inner) : base(message, inner)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
22
FabAccessAPI/Exceptions/ConnectingFailedException.cs
Normal file
22
FabAccessAPI/Exceptions/ConnectingFailedException.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace FabAccessAPI.Exceptions
|
||||
{
|
||||
public class ConnectingFailedException : Exception
|
||||
{
|
||||
public ConnectingFailedException()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ConnectingFailedException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ConnectingFailedException(string message, Exception inner) : base(message, inner)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
22
FabAccessAPI/Exceptions/ReconnectingFailedException.cs
Normal file
22
FabAccessAPI/Exceptions/ReconnectingFailedException.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace FabAccessAPI.Exceptions
|
||||
{
|
||||
public class ReconnectingFailedException : Exception
|
||||
{
|
||||
public ReconnectingFailedException()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ReconnectingFailedException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ReconnectingFailedException(string message, Exception inner) : base(message, inner)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
56
FabAccessAPI/IAPI.cs
Normal file
56
FabAccessAPI/IAPI.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using FabAccessAPI.Schema;
|
||||
using System;
|
||||
|
||||
namespace FabAccessAPI
|
||||
{
|
||||
public interface IAPI
|
||||
{
|
||||
/// <summary>
|
||||
/// Data to establish connection.
|
||||
/// </summary>
|
||||
/// Without SecretProperties
|
||||
ConnectionData ConnectionData { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Information about the established connection.
|
||||
/// </summary>
|
||||
ConnectionInfo ConnectionInfo { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Is API connected?
|
||||
/// </summary>
|
||||
bool IsConnected { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get session when connection is established
|
||||
/// </summary>
|
||||
Session Session { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Event on changes in connection state.
|
||||
/// </summary>
|
||||
event EventHandler<ConnectionStatusChange> ConnectionStatusChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Connect to BFFH Server
|
||||
/// </summary>
|
||||
/// <param name="connectionData"></param>
|
||||
void Connect(ConnectionData connectionData);
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect from BFFH Server
|
||||
/// </summary>
|
||||
void Disconnect();
|
||||
|
||||
/// <summary>
|
||||
/// Reconnect after connection loss with the last ConnectionData
|
||||
/// </summary>
|
||||
void Reconnect();
|
||||
|
||||
/// <summary>
|
||||
/// Connect to Server and get ConnectionInfo.
|
||||
/// The Connection is not maintained.
|
||||
/// </summary>
|
||||
ConnectionInfo TestConnection(ConnectionData connectionData);
|
||||
}
|
||||
}
|
7
FabAccessAPI/Mechanism.cs
Normal file
7
FabAccessAPI/Mechanism.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace FabAccessAPI
|
||||
{
|
||||
public enum Mechanism
|
||||
{
|
||||
PLAIN
|
||||
}
|
||||
}
|
88
FabAccessAPI_Test/API_Test.cs
Normal file
88
FabAccessAPI_Test/API_Test.cs
Normal file
@ -0,0 +1,88 @@
|
||||
|
||||
using FabAccessAPI;
|
||||
using FabAccessAPI.Exceptions;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace FabAccessAPI_Test
|
||||
{
|
||||
public class API_Test
|
||||
{
|
||||
[TestCase("Admin1")]
|
||||
public void ConnectDisconnect(string username)
|
||||
{
|
||||
API api = new API();
|
||||
|
||||
ConnectionData connectionData = new ConnectionData()
|
||||
{
|
||||
Host = new UriBuilder(TestEnv.SCHEMA, TestEnv.TESTSERVER, TestEnv.TESTSERVER_PORT).Uri,
|
||||
Mechanism = Mechanism.PLAIN,
|
||||
Username = username,
|
||||
Properties = new Dictionary<string, object>()
|
||||
{
|
||||
{ "Username", username }
|
||||
},
|
||||
SecretProperties = new Dictionary<string, object>()
|
||||
{
|
||||
{ "Password", TestEnv.PASSWORD }
|
||||
}
|
||||
};
|
||||
|
||||
api.Connect(connectionData);
|
||||
api.Disconnect();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Connect_HostUnreachable()
|
||||
{
|
||||
API api = new API();
|
||||
|
||||
ConnectionData connectionData = new ConnectionData()
|
||||
{
|
||||
Host = new UriBuilder(TestEnv.SCHEMA, "NotReachable." + TestEnv.TESTSERVER, TestEnv.TESTSERVER_PORT).Uri,
|
||||
Mechanism = Mechanism.PLAIN,
|
||||
Username = "UnknownUser",
|
||||
Properties = new Dictionary<string, object>()
|
||||
{
|
||||
{ "Username", "UnknownUser" }
|
||||
},
|
||||
SecretProperties = new Dictionary<string, object>()
|
||||
{
|
||||
{ "Password", TestEnv.PASSWORD }
|
||||
}
|
||||
};
|
||||
|
||||
Assert.Throws<ConnectingFailedException>(() =>
|
||||
{
|
||||
api.Connect(connectionData);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Connect_InvalidCredentials()
|
||||
{
|
||||
API api = new API();
|
||||
|
||||
ConnectionData connectionData = new ConnectionData()
|
||||
{
|
||||
Host = new UriBuilder(TestEnv.SCHEMA, TestEnv.TESTSERVER, TestEnv.TESTSERVER_PORT).Uri,
|
||||
Mechanism = Mechanism.PLAIN,
|
||||
Username = "UnknownUser",
|
||||
Properties = new Dictionary<string, object>()
|
||||
{
|
||||
{ "Username", "UnknownUser" }
|
||||
},
|
||||
SecretProperties = new Dictionary<string, object>()
|
||||
{
|
||||
{ "Password", TestEnv.PASSWORD }
|
||||
}
|
||||
};
|
||||
|
||||
Assert.Throws<InvalidCredentialsException>(() =>
|
||||
{
|
||||
api.Connect(connectionData);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
10
FabAccessAPI_Test/TestEnv.cs
Normal file
10
FabAccessAPI_Test/TestEnv.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace FabAccessAPI_Test
|
||||
{
|
||||
public static class TestEnv
|
||||
{
|
||||
public const string SCHEMA = "fabaccess";
|
||||
public const string TESTSERVER = "bffh.lab.bln.kjknet.de";
|
||||
public const int TESTSERVER_PORT = 59666;
|
||||
public const string PASSWORD = "secret";
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user