Files
Mapo-IOB-WIN/EgwProxy.SqlDb.Test/Program.cs
T
2023-03-13 18:27:18 +01:00

363 lines
18 KiB
C#

using EgwProxy.SqlDb.Controllers;
using EgwProxy.SqlDb.DbModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using static System.Net.Mime.MediaTypeNames;
namespace EgwProxy.SqlDb.Test
{
internal class Program
{
#region Protected Methods
/// <summary>
/// legge conf in formato stringa
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected static string ReadSetting(string key)
{
string answ = "";
try
{
answ = $"{ConfigurationManager.AppSettings[key]}" ?? "";
}
catch (Exception exc)
{
Console.Write("Eccezione in ReadSettings");
Console.Write(exc.Message);
}
return answ;
}
#endregion Protected Methods
#region Private Fields
/// <summary>
/// Helper separatore dash
/// </summary>
private const string separator = "------------------------";
#endregion Private Fields
#region Private Methods
/// <summary>
/// Programma principale
/// </summary>
/// <param name="args"></param>
private static void Main(string[] args)
{
Console.WriteLine(separator);
Console.WriteLine("Test DB Client");
Console.WriteLine(separator);
Console.WriteLine();
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string BaseDirectory = System.IO.Path.GetDirectoryName(exePath);
string testFile = Path.Combine(BaseDirectory, ReadSetting("testFile"));
string connStr = "";
DbController dbClient = new DbController(connStr);
if (!string.IsNullOrEmpty(testFile))
{
Console.WriteLine(separator);
Console.WriteLine($"Mode json ({testFile})");
Console.WriteLine(separator);
Console.WriteLine();
if (File.Exists(testFile))
{
var rawData = File.ReadAllText(testFile);
if (!string.IsNullOrEmpty(rawData))
{
TestSetup testConf = new TestSetup();
try
{
testConf = JsonConvert.DeserializeObject<TestSetup>(rawData);
}
catch
{ }
// setup server
connStr = $"data source={testConf.server};initial catalog={testConf.dbName};persist security info=True;user id={testConf.user};password={testConf.password};MultipleActiveResultSets=True;App={testConf.app}";
dbClient = new DbController(connStr);
serverTest(dbClient);
// eseguo per ogni step
foreach (var item in testConf.steps)
{
Console.WriteLine($"------ Step {item.id} | {item.description} ------");
StringBuilder sb = new StringBuilder();
sb.AppendLine(separator);
switch (item.action)
{
case stepType.getSyncState:
var currTab = dbClient.SyncStateGetAll();
if (currTab != null && currTab.Count > 0)
{
foreach (var syncRec in currTab)
{
sb.AppendLine($"{syncRec.TableName} | {syncRec.LastIdx} | {syncRec.Note} | {syncRec.LastUpdate}");
}
}
else
{
sb.AppendLine("Error: Table empty/missing!");
}
sb.AppendLine(separator);
break;
case stepType.resetPodl:
sb.AppendLine("Inizio reset PODL");
var dbRes = dbClient.ResetPODLMes();
if (dbRes.Count == 0)
{
sb.AppendLine("Pulizia eseguita!");
}
else
{
sb.AppendLine("Problemi con svuotamento tab PODL");
}
sb.AppendLine(separator);
break;
case stepType.setNewPodl:
sb.AppendLine($"Inizio inserimento {item.paramList.Count} rec");
// creo una lista di PODL da processare...
List<MesPODLReqModel> CurrPodlReq = new List<MesPODLReqModel>();
foreach (var podl in item.paramList)
{
int idxPodl = 0;
int.TryParse(podl, out idxPodl);
MesPODLReqModel newRec = new MesPODLReqModel()
{
Attivabile = true,
CodArticolo = "ART000000",
IdxMacchina = "SIMUL_01",
IdxODL = 0,
IdxPromessa = idxPodl,
NumPezzi = 1
};
CurrPodlReq.Add(newRec);
sb.AppendLine($"Aggiunto PODL{podl:0000000}");
}
// chiamo procedura insert PODL
bool fatto = dbClient.PodlWriteReq(CurrPodlReq);
sb.AppendLine($"Records Aggiunti!");
sb.AppendLine(separator);
break;
case stepType.dbMigrateAndSync:
try
{
var esitoMigrate = dbClient.forceMigrate();
sb.Append($"Migration: {esitoMigrate}");
}
catch (Exception exc)
{
sb.AppendLine($"eccezione in migrate:{Environment.NewLine}{exc}");
}
break;
#if false
case stepType.createDir:
if (item.paramList != null && item.paramList.Count > 0)
{
string dir2check = item.paramList[0];
var preTest = dbClient.dirExists(dir2check);
if (preTest)
{
esitoStep = "Error: Folder already exists!";
}
else
{
var dirCreate = dbClient.createDir(dir2check);
esitoStep = dirCreate ? $"Directory {dir2check} created!" : $"Error: {dir2check} NOT created!";
}
}
else
{
esitoStep = "Error: missing parameter!";
}
break;
case stepType.delDir:
if (item.paramList != null && item.paramList.Count > 0)
{
string dir2check = item.paramList[0];
var preTest = dbClient.dirExists(dir2check);
if (preTest)
{
var dirDelete = dbClient.deleteDir(dir2check);
esitoStep = dirDelete ? $"Directory {dir2check} deleted!" : $"Error: {dir2check} NOT deleted!";
}
else
{
esitoStep = "Error: Folder doesn't exists, delete not possible!";
}
}
else
{
esitoStep = "Error: missing parameter!";
}
break;
case stepType.delFile:
if (item.paramList != null && item.paramList.Count > 0)
{
string file2check = item.paramList[0];
var preTest = dbClient.fileExists(file2check);
if (preTest)
{
var dirDelete = dbClient.deleteFile(file2check);
esitoStep = dirDelete ? $"File {file2check} deleted!" : $"Error: {file2check} NOT deleted!";
}
else
{
esitoStep = "Error: File doesn't exists, delete not possible!";
}
}
else
{
esitoStep = "Error: missing parameter!";
}
break;
case stepType.downloadDir:
if (item.paramList != null && item.paramList.Count > 1)
{
string dir2check = item.paramList[0];
string localDir = Path.Combine(BaseDirectory, item.paramList[1]);
var preTest = dbClient.dirExists(dir2check);
if (preTest)
{
var dirDelete = dbClient.getDir(localDir, dir2check);
esitoStep = dirDelete ? $"Directory {dir2check} downloaded!" : $"Error: {dir2check} NOT downloaded!";
}
else
{
esitoStep = "Error: Folder doesn't exists, download not possible!";
}
}
else
{
esitoStep = "Error: missing parameters!";
}
break;
case stepType.downloadFile:
if (item.paramList != null && item.paramList.Count > 1)
{
string dir2check = item.paramList[0];
string localDir = Path.Combine(BaseDirectory, item.paramList[1]);
var preTest = dbClient.dirExists(dir2check);
if (preTest)
{
var dirDelete = dbClient.getDir(localDir, dir2check);
esitoStep = dirDelete ? $"Directory {dir2check} downloaded!" : $"Error: {dir2check} NOT downloaded!";
}
else
{
esitoStep = "Error: Folder doesn't exists, download not possible!";
}
}
else
{
esitoStep = "Error: missing parameters!";
}
break;
case stepType.uploadDir:
if (item.paramList != null && item.paramList.Count > 1)
{
string remoteDir = item.paramList[0];
string localDir = Path.Combine(BaseDirectory, item.paramList[1]);
var dirUploaded = dbClient.sendDir(localDir, remoteDir);
esitoStep = dirUploaded ? $"Directory {remoteDir} uploaded!" : $"Error: {remoteDir} NOT uploaded!";
}
else
{
esitoStep = "Error: missing parameters!";
}
break;
case stepType.uploadFile:
if (item.paramList != null && item.paramList.Count > 1)
{
string remotePath = item.paramList[0];
string localPath = Path.Combine(BaseDirectory, item.paramList[1]);
var fileUploaded = dbClient.sendFile(localPath, remotePath);
esitoStep = fileUploaded ? $"File {remotePath} uploaded!" : $"Error: {remotePath} NOT uploaded!";
}
else
{
esitoStep = "Error: missing parameters!";
}
break;
case stepType.listContent:
if (item.paramList != null && item.paramList.Count > 0)
{
string remoteDir = item.paramList[0];
var preTest = dbClient.dirExists(remoteDir);
if (preTest)
{
var listResult = dbClient.listDir(remoteDir, false);
Console.WriteLine($"Content of dir {remoteDir}:");
foreach (var itemList in listResult)
{
Console.WriteLine(itemList);
}
}
else
{
esitoStep = "Error: Folder doesn't exists, list not possible!";
}
}
else
{
esitoStep = "Error: missing parameter!";
}
break;
#endif
default:
break;
}
string esitoStep = sb.ToString();
Console.WriteLine(esitoStep);
Console.WriteLine($"------ Done Step {item.id} ------");
Console.WriteLine();
Console.WriteLine("Press a key to continue...");
Console.ReadKey();
}
}
}
}
}
private static void serverTest(DbController dbClient)
{
Console.WriteLine(separator);
string testServer = dbClient.serverOk();
Console.WriteLine($"Test connessione: esito {testServer}");
Console.WriteLine(separator);
Console.WriteLine();
}
#endregion Private Methods
}
}