From 7516dc061ccbbd8ef638b85a4b06c70f4cbb1d73 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Tue, 10 May 2022 13:35:23 +0200 Subject: [PATCH] Started better API Class --- FabAccessAPI/API.cs | 65 ++++++++++++++ FabAccessAPI/ConnectionData.cs | 15 ++++ FabAccessAPI/ConnectionInfo.cs | 13 +++ FabAccessAPI/ConnectionStatusChange.cs | 23 +++++ .../Exceptions/APIIncompatibleException.cs | 22 +++++ .../Exceptions/ConnectingFailedException.cs | 22 +++++ .../Exceptions/ReconnectingFailedException.cs | 22 +++++ FabAccessAPI/IAPI.cs | 56 ++++++++++++ FabAccessAPI/Mechanism.cs | 7 ++ FabAccessAPI_Test/API_Test.cs | 88 +++++++++++++++++++ FabAccessAPI_Test/TestEnv.cs | 10 +++ 11 files changed, 343 insertions(+) create mode 100644 FabAccessAPI/API.cs create mode 100644 FabAccessAPI/ConnectionData.cs create mode 100644 FabAccessAPI/ConnectionInfo.cs create mode 100644 FabAccessAPI/ConnectionStatusChange.cs create mode 100644 FabAccessAPI/Exceptions/APIIncompatibleException.cs create mode 100644 FabAccessAPI/Exceptions/ConnectingFailedException.cs create mode 100644 FabAccessAPI/Exceptions/ReconnectingFailedException.cs create mode 100644 FabAccessAPI/IAPI.cs create mode 100644 FabAccessAPI/Mechanism.cs create mode 100644 FabAccessAPI_Test/API_Test.cs create mode 100644 FabAccessAPI_Test/TestEnv.cs diff --git a/FabAccessAPI/API.cs b/FabAccessAPI/API.cs new file mode 100644 index 0000000..e66e960 --- /dev/null +++ b/FabAccessAPI/API.cs @@ -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 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 + } +} diff --git a/FabAccessAPI/ConnectionData.cs b/FabAccessAPI/ConnectionData.cs new file mode 100644 index 0000000..84bb26f --- /dev/null +++ b/FabAccessAPI/ConnectionData.cs @@ -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 Properties; + public Dictionary SecretProperties; + public DateTime LastTime; + } +} diff --git a/FabAccessAPI/ConnectionInfo.cs b/FabAccessAPI/ConnectionInfo.cs new file mode 100644 index 0000000..e0870a3 --- /dev/null +++ b/FabAccessAPI/ConnectionInfo.cs @@ -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 Mechanisms; + } +} diff --git a/FabAccessAPI/ConnectionStatusChange.cs b/FabAccessAPI/ConnectionStatusChange.cs new file mode 100644 index 0000000..d6fd683 --- /dev/null +++ b/FabAccessAPI/ConnectionStatusChange.cs @@ -0,0 +1,23 @@ +namespace FabAccessAPI +{ + public enum ConnectionStatusChange + { + /// + /// Client has established connection to server. + /// + Connected, + /// + /// Client has closed connection to server. + /// + Disconnected, + /// + /// Connection was lost and Client has reestablished connection to server. + /// + Reconnected, + /// + /// Connection was lost and can be reestablished with reconnect + /// Connection should be closed if reconnecting fails. + /// + ConnectionLoss + } +} diff --git a/FabAccessAPI/Exceptions/APIIncompatibleException.cs b/FabAccessAPI/Exceptions/APIIncompatibleException.cs new file mode 100644 index 0000000..24f91ff --- /dev/null +++ b/FabAccessAPI/Exceptions/APIIncompatibleException.cs @@ -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) + { + + } + } +} diff --git a/FabAccessAPI/Exceptions/ConnectingFailedException.cs b/FabAccessAPI/Exceptions/ConnectingFailedException.cs new file mode 100644 index 0000000..9f96c18 --- /dev/null +++ b/FabAccessAPI/Exceptions/ConnectingFailedException.cs @@ -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) + { + + } + } +} diff --git a/FabAccessAPI/Exceptions/ReconnectingFailedException.cs b/FabAccessAPI/Exceptions/ReconnectingFailedException.cs new file mode 100644 index 0000000..4d4b810 --- /dev/null +++ b/FabAccessAPI/Exceptions/ReconnectingFailedException.cs @@ -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) + { + + } + } +} diff --git a/FabAccessAPI/IAPI.cs b/FabAccessAPI/IAPI.cs new file mode 100644 index 0000000..884f8bc --- /dev/null +++ b/FabAccessAPI/IAPI.cs @@ -0,0 +1,56 @@ +using FabAccessAPI.Schema; +using System; + +namespace FabAccessAPI +{ + public interface IAPI + { + /// + /// Data to establish connection. + /// + /// Without SecretProperties + ConnectionData ConnectionData { get; } + + /// + /// Information about the established connection. + /// + ConnectionInfo ConnectionInfo { get; } + + /// + /// Is API connected? + /// + bool IsConnected { get; } + + /// + /// Get session when connection is established + /// + Session Session { get; } + + /// + /// Event on changes in connection state. + /// + event EventHandler ConnectionStatusChanged; + + /// + /// Connect to BFFH Server + /// + /// + void Connect(ConnectionData connectionData); + + /// + /// Disconnect from BFFH Server + /// + void Disconnect(); + + /// + /// Reconnect after connection loss with the last ConnectionData + /// + void Reconnect(); + + /// + /// Connect to Server and get ConnectionInfo. + /// The Connection is not maintained. + /// + ConnectionInfo TestConnection(ConnectionData connectionData); + } +} diff --git a/FabAccessAPI/Mechanism.cs b/FabAccessAPI/Mechanism.cs new file mode 100644 index 0000000..0d196b3 --- /dev/null +++ b/FabAccessAPI/Mechanism.cs @@ -0,0 +1,7 @@ +namespace FabAccessAPI +{ + public enum Mechanism + { + PLAIN + } +} diff --git a/FabAccessAPI_Test/API_Test.cs b/FabAccessAPI_Test/API_Test.cs new file mode 100644 index 0000000..dd3f52a --- /dev/null +++ b/FabAccessAPI_Test/API_Test.cs @@ -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() + { + { "Username", username } + }, + SecretProperties = new Dictionary() + { + { "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() + { + { "Username", "UnknownUser" } + }, + SecretProperties = new Dictionary() + { + { "Password", TestEnv.PASSWORD } + } + }; + + Assert.Throws(() => + { + 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() + { + { "Username", "UnknownUser" } + }, + SecretProperties = new Dictionary() + { + { "Password", TestEnv.PASSWORD } + } + }; + + Assert.Throws(() => + { + api.Connect(connectionData); + }); + } + } +} diff --git a/FabAccessAPI_Test/TestEnv.cs b/FabAccessAPI_Test/TestEnv.cs new file mode 100644 index 0000000..adf2f83 --- /dev/null +++ b/FabAccessAPI_Test/TestEnv.cs @@ -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"; + } +}