using System.Text.Json; using dopt.DeltaBarth; using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext; namespace dopt.DeltaBarth.Tests { using System.Text.Encodings.Web; using Microsoft.Extensions.Configuration; internal class Config { public string apiUrl; public string username; public string password; public string database; public string mandant; public Config() { var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("CREDENTIALS.json", optional: false, reloadOnChange: true) .Build(); var v = config["delta-barth-server:api:base_url"]; Assert.IsNotNull(v); apiUrl = v; v = config["delta-barth-server:api:user"]; Assert.IsNotNull(v); username = v; v = config["delta-barth-server:api:pwd"]; Assert.IsNotNull(v); password = v; v = config["delta-barth-server:api:db"]; Assert.IsNotNull(v); database = v; v = config["delta-barth-server:api:mandant"]; Assert.IsNotNull(v); mandant = v; } } public class TPlugin : DeltaBarth.Plugin { private const string absPath = @"A:\Arbeitsaufgaben\Delta-Barth\cs-wrapper\dopt.DeltaBarth"; public TPlugin() : base(absPath) { } public new DataObjects.Credentials GetCredentials() { return base.GetCredentials(); } public new string GetBaseApiUrl() { return base.GetBaseApiUrl(); } public new string GetDataPath() { return base.GetDataPath(); } public new void Setup(string datenPfad, string basisApiUrl) { base.Setup(datenPfad, basisApiUrl); } } [TestClass] public sealed class PluginTest { private const string baseDataPath = @"A:\Arbeitsaufgaben\Delta-Barth\cs-wrapper\dopt.DeltaBarth\test_data_path"; internal Config config = new Config(); static void PrettyPrint(object toSerialise) { string prettyJson = JsonSerializer.Serialize(toSerialise, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, WriteIndented = true }); Console.WriteLine($"Parsed struct is: {prettyJson}"); } [TestMethod] public void ConstructInitialiseFinalise_Test() { var test = new TPlugin(); test.Dispose(); } [TestMethod] public void Set_and_Obtain_Credentials_Test() { var test = new TPlugin(); string user = "user", password = "password", database = "DB1", mandant = "mandant1"; test.SetzeNutzerdaten(user, password, database, mandant); var creds = test.GetCredentials(); PrettyPrint(creds); Assert.AreEqual(user, creds.username); Assert.AreEqual(password, creds.password); Assert.AreEqual(database, creds.database); Assert.AreEqual(mandant, creds.mandant); test.Dispose(); } [TestMethod] public void SetupSession_Test() { var test = new TPlugin(); string apiUrlSet = "http://10.2.22.21:8080/api/"; test.Setup(baseDataPath, apiUrlSet); // data path var dataPathGet = test.GetDataPath(); Console.WriteLine($"Result for data path from Python session was: {dataPathGet}"); bool pathElementsContained = dataPathGet.Contains("test_data_path"); Assert.IsTrue(pathElementsContained); // API URL var apiUrlGet = test.GetBaseApiUrl(); Console.WriteLine($"Result for API URL from Python session was: {apiUrlGet}"); Assert.AreEqual(apiUrlSet, apiUrlGet); test.Dispose(); } [TestMethod] public void Startup_Test() { var test = new TPlugin(); string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1"; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var apiUrlGet = test.GetBaseApiUrl(); var creds = test.GetCredentials(); Console.WriteLine($"Result from Python session was: API-URL={apiUrlGet}, creds={creds}"); PrettyPrint(creds); Assert.AreEqual(apiUrlSet, apiUrlGet); Assert.AreEqual(user, creds.username); Assert.AreEqual(password, creds.password); Assert.AreEqual(database, creds.database); Assert.AreEqual(mandant, creds.mandant); test.Dispose(); } [TestMethod] public void UmsatzprognoseDummy_WithoutParams_Test() { var test = new TPlugin(); string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1"; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var res = test.UmsatzprognoseDummy(null, null); Assert.AreEqual((int)StatusCodes.Erfolg, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] public void UmsatzprognoseDummy_WithCompanyIdWithoutDate_Test() { var test = new TPlugin(); string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1"; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var comp_id = 1000; var res = test.UmsatzprognoseDummy(comp_id, null); Assert.AreEqual((int)StatusCodes.Erfolg, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] public void UmsatzprognoseDummy_WithoutCompanyIdWithDate_Test() { var test = new TPlugin(); string apiUrlSet = "http://10.2.22.21:8080/api/", user = "user", password = "password", database = "DB1", mandant = "mandant1"; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var date = new DateTime(2023, 1, 1, 12, 45, 30); var res = test.UmsatzprognoseDummy(null, date); Assert.AreEqual((int)StatusCodes.Erfolg, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] public void Umsatzprognose_NoConnectionNeeded_Test() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var date = new DateTime(2030, 1, 1, 12, 45, 30); var res = test.Umsatzprognose(null, date); PrettyPrint(res); test.Dispose(); } [TestMethod] [TestCategory("ActiveAPI")] public void Umsatzprognose_WithCompanyIdWithoutDate_Test() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var comp_id = 1024; var res = test.Umsatzprognose(comp_id, null); Assert.AreEqual(4, res.status.code); Assert.AreEqual((int)StatusCodes.DatensatzZuWenigeMonatsdatenpunkte, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] public void Umsatzprognose_WithCompanyIdWithoutDate_Test_FailConnection() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var comp_id = 1024; var res = test.Umsatzprognose(comp_id, null); Assert.AreEqual(1, res.status.code); Assert.AreEqual((int)StatusCodes.VerbindungTimeout, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] [TestCategory("ActiveAPI")] public void Umsatzprognose_WithoutCompanyIdWithInvalidDate_Test() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var date = new DateTime(2030, 1, 1, 12, 45, 30); var res = test.Umsatzprognose(null, date); Assert.AreEqual(3, res.status.code); Assert.AreEqual((int)StatusCodes.DatensatzZuWenigeDatenpunkte, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] public void Umsatzprognose_WithoutCompanyIdWithInvalidDate_Test_FailConnection() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var date = new DateTime(2030, 1, 1, 12, 45, 30); var res = test.Umsatzprognose(null, date); Assert.AreEqual(1, res.status.code); Assert.AreEqual((int)StatusCodes.VerbindungTimeout, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] [TestCategory("ActiveAPI")] public void Umsatzprognose_WithoutCompanyIdWithValidDate_Test() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var date = new DateTime(2015, 1, 1, 12, 45, 30); var res = test.Umsatzprognose(null, date); Assert.AreEqual(4, res.status.code); Assert.AreEqual((int)StatusCodes.DatensatzZuWenigeMonatsdatenpunkte, res.status.code); PrettyPrint(res); test.Dispose(); } [TestMethod] public void Umsatzprognose_WithoutCompanyIdWithValidDate_Test_FailConnection() { var test = new TPlugin(); string apiUrlSet = config.apiUrl, user = config.username, password = config.password, database = config.database, mandant = config.mandant; test.Startup(baseDataPath, apiUrlSet, user, password, database, mandant); var date = new DateTime(2015, 1, 1, 12, 45, 30); var res = test.Umsatzprognose(null, date); Assert.AreEqual(1, res.status.code); Assert.AreEqual((int)StatusCodes.VerbindungTimeout, res.status.code); PrettyPrint(res); test.Dispose(); } } }